| 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 |
import argparse |
| 35 |
import hashlib |
| 36 |
import os |
| 37 |
import sys |
| 38 |
|
| 39 |
from os.path import basename |
| 40 |
|
| 41 |
from fontTools.ttLib import TTFont |
| 42 |
|
| 43 |
|
| 44 |
def write_checksum(filepaths, stdout_write=False, use_ttx=False, include_tables=None, exclude_tables=None, do_not_cleanup=False): |
| 45 |
checksum_dict = {} |
| 46 |
for path in filepaths: |
| 47 |
if not os.path.exists(path): |
| 48 |
sys.stderr.write("[checksum.py] ERROR: " + path + " is not a valid file path" + os.linesep) |
| 49 |
sys.exit(1) |
| 50 |
|
| 51 |
if use_ttx: |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
temp_ttx_path = path + ".ttx" |
| 60 |
|
| 61 |
tt = TTFont(path) |
| 62 |
|
| 63 |
|
| 64 |
tt.saveXML(temp_ttx_path, newlinestr="\n", skipTables=exclude_tables, tables=include_tables) |
| 65 |
checksum_path = temp_ttx_path |
| 66 |
else: |
| 67 |
if include_tables is not None: |
| 68 |
sys.stderr.write("[checksum.py] -i and --include are not supported for font binary filepaths. \ |
| 69 |
Use these flags for checksums with the --ttx flag.") |
| 70 |
sys.exit(1) |
| 71 |
if exclude_tables is not None: |
| 72 |
sys.stderr.write("[checksum.py] -e and --exclude are not supported for font binary filepaths. \ |
| 73 |
Use these flags for checksums with the --ttx flag.") |
| 74 |
sys.exit(1) |
| 75 |
checksum_path = path |
| 76 |
|
| 77 |
file_contents = _read_binary(checksum_path) |
| 78 |
|
| 79 |
|
| 80 |
checksum_dict[basename(checksum_path)] = hashlib.sha1(file_contents).hexdigest() |
| 81 |
|
| 82 |
|
| 83 |
if use_ttx and do_not_cleanup is False: |
| 84 |
os.remove(temp_ttx_path) |
| 85 |
|
| 86 |
|
| 87 |
checksum_out_data = "" |
| 88 |
for key in checksum_dict.keys(): |
| 89 |
checksum_out_data += checksum_dict[key] + " " + key + "\n" |
| 90 |
|
| 91 |
|
| 92 |
if stdout_write: |
| 93 |
sys.stdout.write(checksum_out_data) |
| 94 |
else: |
| 95 |
checksum_report_filepath = "checksum.txt" |
| 96 |
with open(checksum_report_filepath, "w") as file: |
| 97 |
file.write(checksum_out_data) |
| 98 |
|
| 99 |
|
| 100 |
def check_checksum(filepaths): |
| 101 |
check_failed = False |
| 102 |
for path in filepaths: |
| 103 |
if not os.path.exists(path): |
| 104 |
sys.stderr.write("[checksum.py] ERROR: " + path + " is not a valid filepath" + os.linesep) |
| 105 |
sys.exit(1) |
| 106 |
|
| 107 |
with open(path, mode='r') as file: |
| 108 |
for line in file.readlines(): |
| 109 |
cleaned_line = line.rstrip() |
| 110 |
line_list = cleaned_line.split(" ") |
| 111 |
|
| 112 |
line_list = list(filter(None, line_list)) |
| 113 |
if len(line_list) == 2: |
| 114 |
expected_sha1 = line_list[0] |
| 115 |
test_path = line_list[1] |
| 116 |
else: |
| 117 |
sys.stderr.write("[checksum.py] ERROR: failed to parse checksum file values" + os.linesep) |
| 118 |
sys.exit(1) |
| 119 |
|
| 120 |
if not os.path.exists(test_path): |
| 121 |
print(test_path + ": Filepath is not valid, ignored") |
| 122 |
else: |
| 123 |
file_contents = _read_binary(test_path) |
| 124 |
observed_sha1 = hashlib.sha1(file_contents).hexdigest() |
| 125 |
if observed_sha1 == expected_sha1: |
| 126 |
print(test_path + ": OK") |
| 127 |
else: |
| 128 |
print("-" * 80) |
| 129 |
print(test_path + ": === FAIL ===") |
| 130 |
print("Expected vs. Observed:") |
| 131 |
print(expected_sha1) |
| 132 |
print(observed_sha1) |
| 133 |
print("-" * 80) |
| 134 |
check_failed = True |
| 135 |
|
| 136 |
|
| 137 |
if check_failed is True: |
| 138 |
sys.exit(1) |
| 139 |
|
| 140 |
|
| 141 |
def _read_binary(filepath): |
| 142 |
with open(filepath, mode='rb') as file: |
| 143 |
return file.read() |
| 144 |
|
| 145 |
|
| 146 |
if __name__ == '__main__': |
| 147 |
parser = argparse.ArgumentParser(prog="checksum.py") |
| 148 |
parser.add_argument("-t", "--ttx", help="Calculate from ttx file", action="store_true") |
| 149 |
parser.add_argument("-s", "--stdout", help="Write output to stdout stream", action="store_true") |
| 150 |
parser.add_argument("-n", "--noclean", help="Do not discard *.ttx files used to calculate SHA1 hashes", action="store_true") |
| 151 |
parser.add_argument("-c", "--check", help="Verify checksum values vs. files", action="store_true") |
| 152 |
parser.add_argument("filepaths", nargs="+", help="One or more file paths to font binary files") |
| 153 |
|
| 154 |
parser.add_argument("-i", "--include", action="append", help="Included OpenType tables for ttx data dump") |
| 155 |
parser.add_argument("-e", "--exclude", action="append", help="Excluded OpenType tables for ttx data dump") |
| 156 |
|
| 157 |
args = parser.parse_args(sys.argv[1:]) |
| 158 |
|
| 159 |
if args.check is True: |
| 160 |
check_checksum(args.filepaths) |
| 161 |
else: |
| 162 |
write_checksum(args.filepaths, stdout_write=args.stdout, use_ttx=args.ttx, do_not_cleanup=args.noclean, include_tables=args.include, exclude_tables=args.exclude) |