]> git.ipfire.org Git - thirdparty/google/fonts.git/commitdiff
Move git parsing logic to shared file
authorSimon Cozens <simon@simon-cozens.org>
Wed, 7 Aug 2024 08:29:06 +0000 (09:29 +0100)
committerSimon Cozens <simon@simon-cozens.org>
Wed, 7 Aug 2024 08:29:06 +0000 (09:29 +0100)
.ci/run.py
.ci/whatchanged.py [new file with mode: 0644]

index 978659334d8ee105c1fe32b0b0c1cb9bc3d8ba7b..3acc3939d72897ec8268ce57e2667e9c39fec6b9 100644 (file)
@@ -1,90 +1,10 @@
 #!/usr/bin/python3
 import subprocess
-import re
 import os
 import argparse
-from collections import defaultdict
-from enum import Enum
 from glob import glob
 
-
-class CheckType(Enum):
-    NEW_FAMILY = 1
-    MODIFIED_FAMILY = 2
-    MODIFIED_FAMILY_METADATA = 3
-    DESIGNER = 4
-
-
-class CheckToken(Enum):
-    NEW_FONT = 1
-    DESIGNER = 2
-    FONT_FAMILY = 3
-    MODIFIED_FONTS = 4
-    MODIFIED_METADATA = 5
-
-
-def _parse_git_diff(diff_info: str) -> list[tuple[str, str]]:
-    """
-    '''
-    A    ofl/mavenpro/MavenPro[wght].ttf
-    M    ofl/mavenpro/METADATA.pb
-    ''' -> [
-        ("A", "ofl/mavenpro/MavenPro[wght].ttf"),
-        ("M", "ofl/mavenpro/METADATA.pb")
-    ]
-
-    """
-    parsed = re.findall(r"([A|M|D])(\t)(.*)", diff_info)
-    return [(s, p) for s, _, p in parsed]
-
-
-def directory_check_types(branch="origin/main"):
-    git_diff_text = subprocess.run(
-        ["git", "diff", branch, "--name-status"], capture_output=True
-    ).stdout.decode("utf-8")
-    git_diff = _parse_git_diff(git_diff_text)
-
-    # Tokenize each directory git diff has reported
-    directories_to_check = defaultdict(set)
-    for state, path in git_diff:
-        dirpath = (
-            os.path.dirname(path)
-            if path not in ("to_sandbox.txt", "to_production.txt")
-            else path
-        )
-        # skip article directories. These should be checked on github
-        if os.path.basename(dirpath) == "article":
-            continue
-
-        dir_tokens = directories_to_check[dirpath]
-        if path.startswith("catalog"):
-            dir_tokens.add(CheckToken.DESIGNER)
-
-        if path.startswith(("ofl", "ufl", "apache")):
-            dir_tokens.add(CheckToken.FONT_FAMILY)
-
-        if path.endswith((".ttf", ".otf")) and state == "A":
-            dir_tokens.add(CheckToken.NEW_FONT)
-
-        if path.endswith((".ttf", ".otf")) and (state == "M" or state == "D"):
-            dir_tokens.add(CheckToken.MODIFIED_FONTS)
-
-        if path.endswith((".txt", ".pb", ".html")) and state == "M":
-            dir_tokens.add(CheckToken.MODIFIED_METADATA)
-
-    # Set each directory's check type
-    results = []
-    for path, tokens in directories_to_check.items():
-        if CheckToken.FONT_FAMILY in tokens:
-            if CheckToken.MODIFIED_FONTS in tokens:
-                results.append((path, CheckType.MODIFIED_FAMILY))
-            elif CheckToken.MODIFIED_METADATA in tokens:
-                results.append((path, CheckType.MODIFIED_FAMILY_METADATA))
-            else:
-                results.append((path, CheckType.NEW_FAMILY))
-        if CheckToken.DESIGNER in tokens:
-            results.append((path, CheckType.DESIGNER))
-    return results
+from whatchanged import directory_check_types, CheckType
 
 
 def main():
@@ -96,7 +16,9 @@ def main():
         "--render", action="store_true", help="Check rendering of families only"
     )
     parser.add_argument("--pr-number", help="PR to output fontbakery report to")
-    parser.add_argument("--pr-url-body", default="https://www.github.com/google/fonts/pull/%s")
+    parser.add_argument(
+        "--pr-url-body", default="https://www.github.com/google/fonts/pull/%s"
+    )
     args = parser.parse_args()
 
     profile_test_file = os.path.join(os.path.dirname(__file__), "test_profiles.py")
@@ -119,18 +41,24 @@ def main():
         elif args.render and check_type == CheckType.MODIFIED_FAMILY:
             print(f"Rendering modified family: {directory}")
             subprocess.run(qa_cmd_prefix + ["-gfb", "--render", "--imgs"])
-        
+
         # we only want args.render to do the above two conditions
-        elif args.render: 
+        elif args.render:
             continue
 
         elif check_type == CheckType.NEW_FAMILY:
             print(f"Checking new family: {directory}")
-            subprocess.run(qa_cmd_prefix + ["--fontbakery", "--interpolations"], check=True)
+            subprocess.run(
+                qa_cmd_prefix + ["--fontbakery", "--interpolations"], check=True
+            )
 
         elif check_type == CheckType.MODIFIED_FAMILY:
             print(f"Checking modified family: {directory}")
-            subprocess.run(qa_cmd_prefix + ["-gfb", "--fontbakery", "--diffenator", "--interpolations"], check=True)
+            subprocess.run(
+                qa_cmd_prefix
+                + ["-gfb", "--fontbakery", "--diffenator", "--interpolations"],
+                check=True,
+            )
 
         elif check_type == CheckType.MODIFIED_FAMILY_METADATA:
             print(f"Checking modified family metadata: {directory}")
diff --git a/.ci/whatchanged.py b/.ci/whatchanged.py
new file mode 100644 (file)
index 0000000..0103eb9
--- /dev/null
@@ -0,0 +1,84 @@
+import subprocess
+import re
+import os
+from collections import defaultdict
+from enum import Enum
+
+
+class CheckType(Enum):
+    NEW_FAMILY = 1
+    MODIFIED_FAMILY = 2
+    MODIFIED_FAMILY_METADATA = 3
+    DESIGNER = 4
+
+
+class CheckToken(Enum):
+    NEW_FONT = 1
+    DESIGNER = 2
+    FONT_FAMILY = 3
+    MODIFIED_FONTS = 4
+    MODIFIED_METADATA = 5
+
+
+def _parse_git_diff(diff_info: str) -> list[tuple[str, str]]:
+    """
+    '''
+    A    ofl/mavenpro/MavenPro[wght].ttf
+    M    ofl/mavenpro/METADATA.pb
+    ''' -> [
+        ("A", "ofl/mavenpro/MavenPro[wght].ttf"),
+        ("M", "ofl/mavenpro/METADATA.pb")
+    ]
+
+    """
+    parsed = re.findall(r"([A|M|D])(\t)(.*)", diff_info)
+    return [(s, p) for s, _, p in parsed]
+
+
+def directory_check_types(branch="origin/main"):
+    git_diff_text = subprocess.run(
+        ["git", "diff", branch, "--name-status"], capture_output=True
+    ).stdout.decode("utf-8")
+    git_diff = _parse_git_diff(git_diff_text)
+
+    # Tokenize each directory git diff has reported
+    directories_to_check = defaultdict(set)
+    for state, path in git_diff:
+        dirpath = (
+            os.path.dirname(path)
+            if path not in ("to_sandbox.txt", "to_production.txt")
+            else path
+        )
+        # skip article directories. These should be checked on github
+        if os.path.basename(dirpath) == "article":
+            continue
+
+        dir_tokens = directories_to_check[dirpath]
+        if path.startswith("catalog"):
+            dir_tokens.add(CheckToken.DESIGNER)
+
+        if path.startswith(("ofl", "ufl", "apache")):
+            dir_tokens.add(CheckToken.FONT_FAMILY)
+
+        if path.endswith((".ttf", ".otf")) and state == "A":
+            dir_tokens.add(CheckToken.NEW_FONT)
+
+        if path.endswith((".ttf", ".otf")) and (state == "M" or state == "D"):
+            dir_tokens.add(CheckToken.MODIFIED_FONTS)
+
+        if path.endswith((".txt", ".pb", ".html")) and state == "M":
+            dir_tokens.add(CheckToken.MODIFIED_METADATA)
+
+    # Set each directory's check type
+    results = []
+    for path, tokens in directories_to_check.items():
+        if CheckToken.FONT_FAMILY in tokens:
+            if CheckToken.MODIFIED_FONTS in tokens:
+                results.append((path, CheckType.MODIFIED_FAMILY))
+            elif CheckToken.MODIFIED_METADATA in tokens:
+                results.append((path, CheckType.MODIFIED_FAMILY_METADATA))
+            else:
+                results.append((path, CheckType.NEW_FAMILY))
+        if CheckToken.DESIGNER in tokens:
+            results.append((path, CheckType.DESIGNER))
+    return results