ftxvalidator.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import argparse
  2. import glob
  3. import os
  4. import plistlib
  5. import sys
  6. import subprocess
  7. from collections import defaultdict
  8. from whatchanged import directory_check_types, CheckType
  9. severity_mapping = {
  10. "kATSFontTestSeverityMinorError": "WARN",
  11. "kATSFontTestSeverityFatalError": "FAIL",
  12. "kATSFontTestSeverityInformation": "INFO",
  13. }
  14. default_levels = ["WARN", "FAIL"]
  15. def combine_severity(s1, s2):
  16. if s1 == "FAIL" or s2 == "FAIL":
  17. return "FAIL"
  18. if s1 == "WARN" or s2 == "WARN":
  19. return "WARN"
  20. return None
  21. def parse_ftxvalidator_report(contents):
  22. tree = plistlib.loads(contents)
  23. worst_severity = None
  24. for fonts in tree["kATSFontTestFontsTestedKey"]:
  25. font = fonts["kATSFontTestFontPostScriptNameKey"]
  26. results = fonts["kATSFontTestArrayKey"]
  27. for result in results:
  28. if not result["kATSFontTestResultKey"]:
  29. continue
  30. messages = result["kATSFontTestMessagesKey"]
  31. this_messages = defaultdict(list)
  32. for message in messages:
  33. messagetext = message["kATSFontTestMessageTextKey"]
  34. severity = severity_mapping.get(message["kATSFontTestResultKey"])
  35. if severity not in default_levels:
  36. continue
  37. this_messages[severity].append(messagetext)
  38. if not this_messages:
  39. continue
  40. print(result["kATSFontTestIdentifierKey"])
  41. for severity, messages in this_messages.items():
  42. for message in messages:
  43. print(f" {severity}: {message}")
  44. worst_severity = combine_severity(worst_severity, severity)
  45. print()
  46. return worst_severity
  47. def main():
  48. parser = argparse.ArgumentParser()
  49. parser.add_argument(
  50. "--branch", default="origin/main", help="branch to compare current head against"
  51. )
  52. args = parser.parse_args()
  53. worst_severity = None
  54. for directory, check_type in directory_check_types(args.branch):
  55. if check_type not in [CheckType.NEW_FAMILY, CheckType.MODIFIED_FAMILY]:
  56. continue
  57. fonts = glob.glob(os.path.join(directory, "*.ttf"))
  58. for font in fonts:
  59. print(f"Validating {font}")
  60. result = subprocess.run(
  61. ["/Library/Apple/usr/bin/ftxvalidator", "-r", font],
  62. capture_output=True,
  63. text=False,
  64. check=True,
  65. )
  66. worst_severity = combine_severity(
  67. worst_severity, parse_ftxvalidator_report(result.stdout)
  68. )
  69. if worst_severity == "FAIL":
  70. sys.exit(1)
  71. if __name__ == "__main__":
  72. main()