Browse Source

ref: fix typing in bin (#50632)

best viewed with
[?w=1](https://github.com/getsentry/sentry/pull/50632/files?w=1)
<!-- Describe your PR here. -->
anthony sottile 1 year ago
parent
commit
abc2e340b6
3 changed files with 82 additions and 77 deletions
  1. 79 73
      bin/react-to-product-owners-yml-changes.py
  2. 3 2
      bin/typed_code.py
  3. 0 2
      pyproject.toml

+ 79 - 73
bin/react-to-product-owners-yml-changes.py

@@ -1,4 +1,5 @@
 #!/usr/bin/env python3
+from __future__ import annotations
 
 import os
 import sys
@@ -6,92 +7,97 @@ from os.path import dirname, join, realpath
 
 import yaml
 
-# Run from project root.
-os.chdir(realpath(join(dirname(sys.argv[0]), "..")))
-
 PREFIX = "Product Area: "
 LABELS_YAML = ".github/labels.yml"
 
-product_owners = yaml.safe_load(open(sys.argv[1]))
-labels = open(LABELS_YAML)
-areas = [
-    # For `sentry` repo we don't want SDK or Docs areas, that stuff should
-    # be transferred to other repos.
-    x
-    for x in product_owners["by_area"]
-    if not x.startswith("SDK") and not x == "Docs"
-]
-
-fastforward = False
-head = []
-area_lines = ["- name: 'Product Area: Unknown'\n", "  color: '8D5494'\n"]
-tail = []
-current = head
-
-# Best to look the other way, Buck. This is just waaaay easier than trying to
-# use ruamel.yaml to preserve comments and other formatting. What this does is
-# loop through line by line, collecting everything before the Product Area
-# labels in `head` and everything after in `tail`. It "fast-forwards" past the
-# existing Product Area labels. Then we generate output for the new Product
-# Area labels, then emit the three parts (head, area_lines, tail) in order.
-
-for line in labels:
-    if line == "\n":
-        fastforward = False
-    elif fastforward:
-        continue
-    elif line.startswith("- name: 'Product Area: "):
-        fastforward = True
-        current = tail
-        continue
-    current.append(line)
-
-for area in areas:
-    if "'" in area:
-        area_lines.append(f'- name: "Product Area: {area}"\n')
-    else:
-        area_lines.append(f"- name: 'Product Area: {area}'\n")
-    area_lines.append("  color: '8D5494'\n")
-
-area_lines += ["- name: 'Product Area: Other'\n", "  color: '8D5494'\n"]
-
-with open(".github/labels.yml", "w+") as fp:
-    fp.writelines(head)
-    fp.writelines(area_lines)
-    fp.writelines(tail)
-
-
-# Now for issue templates. Same game. Hang in there.
-
-BEGINNING = (
-    "        # begin product areas - autogenerated by bin/react-to-product-owners-yml-changes.py\n"
-)
-END = "        # end product areas\n"
-
-for template in ("bug", "feature"):
-    filepath = f".github/ISSUE_TEMPLATE/{template}.yml"
-    head = []
-    area_lines = [BEGINNING, "        - 'Unknown'\n"]
-    tail = []
+
+def main():
+    # Run from project root.
+    os.chdir(realpath(join(dirname(sys.argv[0]), "..")))
+
+    product_owners = yaml.safe_load(open(sys.argv[1]))
+    labels = open(LABELS_YAML)
+    areas = [
+        # For `sentry` repo we don't want SDK or Docs areas, that stuff should
+        # be transferred to other repos.
+        x
+        for x in product_owners["by_area"]
+        if not x.startswith("SDK") and not x == "Docs"
+    ]
+
+    fastforward = False
+    head: list[str] = []
+    area_lines = ["- name: 'Product Area: Unknown'\n", "  color: '8D5494'\n"]
+    tail: list[str] = []
     current = head
-    for line in open(filepath):
-        if line == END:
+
+    # Best to look the other way, Buck. This is just waaaay easier than trying to
+    # use ruamel.yaml to preserve comments and other formatting. What this does is
+    # loop through line by line, collecting everything before the Product Area
+    # labels in `head` and everything after in `tail`. It "fast-forwards" past the
+    # existing Product Area labels. Then we generate output for the new Product
+    # Area labels, then emit the three parts (head, area_lines, tail) in order.
+
+    for line in labels:
+        if line == "\n":
             fastforward = False
-        elif line == BEGINNING:
+        elif fastforward:
+            continue
+        elif line.startswith("- name: 'Product Area: "):
             fastforward = True
             current = tail
-        elif not fastforward:
-            current.append(line)
+            continue
+        current.append(line)
 
     for area in areas:
         if "'" in area:
-            area_lines.append(f'        - "{area}"\n')
+            area_lines.append(f'- name: "Product Area: {area}"\n')
         else:
-            area_lines.append(f"        - '{area}'\n")
+            area_lines.append(f"- name: 'Product Area: {area}'\n")
+        area_lines.append("  color: '8D5494'\n")
 
-    area_lines += ["        - 'Other'\n", END]
+    area_lines += ["- name: 'Product Area: Other'\n", "  color: '8D5494'\n"]
 
-    with open(filepath, "w+") as fp:
+    with open(".github/labels.yml", "w+") as fp:
         fp.writelines(head)
         fp.writelines(area_lines)
         fp.writelines(tail)
+
+    # Now for issue templates. Same game. Hang in there.
+
+    BEGINNING = "        # begin product areas - autogenerated by bin/react-to-product-owners-yml-changes.py\n"
+    END = "        # end product areas\n"
+
+    for template in ("bug", "feature"):
+        filepath = f".github/ISSUE_TEMPLATE/{template}.yml"
+        head = []
+        area_lines = [BEGINNING, "        - 'Unknown'\n"]
+        tail = []
+        current = head
+        for line in open(filepath):
+            if line == END:
+                fastforward = False
+            elif line == BEGINNING:
+                fastforward = True
+                current = tail
+            elif not fastforward:
+                current.append(line)
+
+        for area in areas:
+            if "'" in area:
+                area_lines.append(f'        - "{area}"\n')
+            else:
+                area_lines.append(f"        - '{area}'\n")
+
+        area_lines += ["        - 'Other'\n", END]
+
+        with open(filepath, "w+") as fp:
+            fp.writelines(head)
+            fp.writelines(area_lines)
+            fp.writelines(tail)
+
+    return 0
+
+
+if __name__ == "__main__":
+    raise SystemExit(main())

+ 3 - 2
bin/typed_code.py

@@ -1,4 +1,5 @@
 #!/usr/bin/env python
+from __future__ import annotations
 
 import argparse
 import configparser
@@ -185,9 +186,9 @@ def analyze_files(
     total = total_lines(files, cache, status)
     files_by_codeowner = split_files_by_codeowner(files, codeowners)
 
-    count_by_team = defaultdict(int)
+    count_by_team: defaultdict[str, int] = defaultdict(int)
     for team in teams:
-        subset_of_files = files_by_codeowner.get(team, [])
+        subset_of_files: set[str] = files_by_codeowner.get(team, set())
         logger.debug(f"{team} {len(subset_of_files)}")
         for file in subset_of_files:
             value = analyze_file(file, cache)

+ 0 - 2
pyproject.toml

@@ -144,8 +144,6 @@ ignore_missing_imports = true
 # - python3 -m tools.mypy_helpers.find_easiest_modules
 [[tool.mypy.overrides]]
 module = [
-    "bin.react-to-product-owners-yml-changes",
-    "bin.typed_code",
     "fixtures.integrations.jira.stub_client",
     "fixtures.integrations.mock_service",
     "fixtures.integrations.stub_service",