whatchanged.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import subprocess
  2. import re
  3. import os
  4. from collections import defaultdict
  5. from enum import Enum
  6. class CheckType(Enum):
  7. NEW_FAMILY = 1
  8. MODIFIED_FAMILY = 2
  9. MODIFIED_FAMILY_METADATA = 3
  10. DESIGNER = 4
  11. class CheckToken(Enum):
  12. NEW_FONT = 1
  13. DESIGNER = 2
  14. FONT_FAMILY = 3
  15. MODIFIED_FONTS = 4
  16. MODIFIED_METADATA = 5
  17. def _parse_git_diff(diff_info: str) -> list[tuple[str, str]]:
  18. """
  19. '''
  20. A ofl/mavenpro/MavenPro[wght].ttf
  21. M ofl/mavenpro/METADATA.pb
  22. ''' -> [
  23. ("A", "ofl/mavenpro/MavenPro[wght].ttf"),
  24. ("M", "ofl/mavenpro/METADATA.pb")
  25. ]
  26. """
  27. parsed = re.findall(r"([A|M|D])(\t)(.*)", diff_info)
  28. return [(s, p) for s, _, p in parsed]
  29. def directory_check_types(branch="origin/main"):
  30. git_diff_text = subprocess.run(
  31. ["git", "diff", branch, "--name-status"], capture_output=True
  32. ).stdout.decode("utf-8")
  33. git_diff = _parse_git_diff(git_diff_text)
  34. # Tokenize each directory git diff has reported
  35. directories_to_check = defaultdict(set)
  36. for state, path in git_diff:
  37. dirpath = (
  38. os.path.dirname(path)
  39. if path not in ("to_sandbox.txt", "to_production.txt")
  40. else path
  41. )
  42. # skip article directories. These should be checked on github
  43. if os.path.basename(dirpath) == "article":
  44. continue
  45. dir_tokens = directories_to_check[dirpath]
  46. if path.startswith("catalog"):
  47. dir_tokens.add(CheckToken.DESIGNER)
  48. if path.startswith(("ofl", "ufl", "apache")):
  49. dir_tokens.add(CheckToken.FONT_FAMILY)
  50. if path.endswith((".ttf", ".otf")) and state == "A":
  51. dir_tokens.add(CheckToken.NEW_FONT)
  52. if path.endswith((".ttf", ".otf")) and (state == "M" or state == "D"):
  53. dir_tokens.add(CheckToken.MODIFIED_FONTS)
  54. if path.endswith((".txt", ".pb", ".html")) and state == "M":
  55. dir_tokens.add(CheckToken.MODIFIED_METADATA)
  56. # Set each directory's check type
  57. results = []
  58. for path, tokens in directories_to_check.items():
  59. if CheckToken.FONT_FAMILY in tokens:
  60. if CheckToken.MODIFIED_FONTS in tokens:
  61. results.append((path, CheckType.MODIFIED_FAMILY))
  62. elif CheckToken.MODIFIED_METADATA in tokens:
  63. results.append((path, CheckType.MODIFIED_FAMILY_METADATA))
  64. else:
  65. results.append((path, CheckType.NEW_FAMILY))
  66. if CheckToken.DESIGNER in tokens:
  67. results.append((path, CheckType.DESIGNER))
  68. return results