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