checksum.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. #------------------------------------------------------------------------------------------------------
  4. # checksum.py
  5. # A SHA1 hash checksum list generator for fonts and fontTools
  6. # XML dumps of font OpenType table data + checksum testing
  7. # script
  8. #
  9. # Copyright 2018 Christopher Simpkins
  10. # MIT License
  11. #
  12. # Dependencies: Python fontTools library
  13. #
  14. # Usage: checksum.py (options) [file path 1]...[file path n]
  15. #
  16. # Options:
  17. # -h, --help Help
  18. # -t, --ttx Calculate SHA1 hash values from ttx dump of XML (default = font binary)
  19. # -s, --stdout Stream to standard output stream (default = write to disk as 'checksum.txt')
  20. # -c, --check Test SHA1 checksum values against a valid checksum file
  21. #
  22. # Options, --ttx only:
  23. # -e, --exclude Excluded OpenType table (may be used more than once, mutually exclusive with -i)
  24. # -i, --include Included OpenType table (may be used more than once, mutually exclusive with -e)
  25. # -n, --noclean Do not discard .ttx files that are used to calculate SHA1 hashes
  26. #-------------------------------------------------------------------------------------------------------
  27. import argparse
  28. import hashlib
  29. import os
  30. import sys
  31. from os.path import basename
  32. from fontTools.ttLib import TTFont
  33. def write_checksum(filepaths, stdout_write=False, use_ttx=False, include_tables=None, exclude_tables=None, do_not_cleanup=False):
  34. checksum_dict = {}
  35. for path in filepaths:
  36. if not os.path.exists(path):
  37. sys.stderr.write("[checksum.py] ERROR: " + path + " is not a valid file path" + os.linesep)
  38. sys.exit(1)
  39. if use_ttx:
  40. # append a .ttx extension to existing extension to maintain data about the binary that
  41. # was used to generate the .ttx XML dump. This creates unique checksum path values for
  42. # paths that would otherwise not be unique with a file extension replacement with .ttx
  43. # An example is woff and woff2 web font files that share the same base file name:
  44. #
  45. # coolfont-regular.woff ==> coolfont-regular.ttx
  46. # coolfont-regular.woff2 ==> coolfont-regular.ttx (KAPOW! checksum data lost as this would overwrite dict value)
  47. temp_ttx_path = path + ".ttx"
  48. tt = TTFont(path)
  49. # important to keep the newlinestr value defined here as hash values will change across platforms
  50. # if platform specific newline values are assumed
  51. tt.saveXML(temp_ttx_path, newlinestr="\n", skipTables=exclude_tables, tables=include_tables)
  52. checksum_path = temp_ttx_path
  53. else:
  54. if include_tables is not None:
  55. sys.stderr.write("[checksum.py] -i and --include are not supported for font binary filepaths. \
  56. Use these flags for checksums with the --ttx flag.")
  57. sys.exit(1)
  58. if exclude_tables is not None:
  59. sys.stderr.write("[checksum.py] -e and --exclude are not supported for font binary filepaths. \
  60. Use these flags for checksums with the --ttx flag.")
  61. sys.exit(1)
  62. checksum_path = path
  63. file_contents = _read_binary(checksum_path)
  64. # store SHA1 hash data and associated file path basename in the checksum_dict dictionary
  65. checksum_dict[basename(checksum_path)] = hashlib.sha1(file_contents).hexdigest()
  66. # remove temp ttx files when present
  67. if use_ttx and do_not_cleanup is False:
  68. os.remove(temp_ttx_path)
  69. # generate the checksum list string for writes
  70. checksum_out_data = ""
  71. for key in checksum_dict.keys():
  72. checksum_out_data += checksum_dict[key] + " " + key + "\n"
  73. # write to stdout stream or file based upon user request (default = file write)
  74. if stdout_write:
  75. sys.stdout.write(checksum_out_data)
  76. else:
  77. checksum_report_filepath = "checksum.txt"
  78. with open(checksum_report_filepath, "w") as file:
  79. file.write(checksum_out_data)
  80. def check_checksum(filepaths):
  81. check_failed = False
  82. for path in filepaths:
  83. if not os.path.exists(path):
  84. sys.stderr.write("[checksum.py] ERROR: " + filepath + " is not a valid filepath" + os.linesep)
  85. sys.exit(1)
  86. with open(path, mode='r') as file:
  87. for line in file.readlines():
  88. cleaned_line = line.rstrip()
  89. line_list = cleaned_line.split(" ")
  90. # eliminate empty strings parsed from > 1 space characters
  91. line_list = list(filter(None, line_list))
  92. if len(line_list) == 2:
  93. expected_sha1 = line_list[0]
  94. test_path = line_list[1]
  95. else:
  96. sys.stderr.write("[checksum.py] ERROR: failed to parse checksum file values" + os.linesep)
  97. sys.exit(1)
  98. if not os.path.exists(test_path):
  99. print(test_path + ": Filepath is not valid, ignored")
  100. else:
  101. file_contents = _read_binary(test_path)
  102. observed_sha1 = hashlib.sha1(file_contents).hexdigest()
  103. if observed_sha1 == expected_sha1:
  104. print(test_path + ": OK")
  105. else:
  106. print("-" * 80)
  107. print(test_path + ": === FAIL ===")
  108. print("Expected vs. Observed:")
  109. print(expected_sha1)
  110. print(observed_sha1)
  111. print("-" * 80)
  112. check_failed = True
  113. # exit with status code 1 if any fails detected across all tests in the check
  114. if check_failed is True:
  115. sys.exit(1)
  116. def _read_binary(filepath):
  117. with open(filepath, mode='rb') as file:
  118. return file.read()
  119. if __name__ == '__main__':
  120. parser = argparse.ArgumentParser(prog="checksum.py")
  121. parser.add_argument("-t", "--ttx", help="Calculate from ttx file", action="store_true")
  122. parser.add_argument("-s", "--stdout", help="Write output to stdout stream", action="store_true")
  123. parser.add_argument("-n", "--noclean", help="Do not discard *.ttx files used to calculate SHA1 hashes", action="store_true")
  124. parser.add_argument("-c", "--check", help="Verify checksum values vs. files", action="store_true")
  125. parser.add_argument("filepaths", nargs="+", help="One or more file paths to font binary files")
  126. parser.add_argument("-i", "--include", action="append", help="Included OpenType tables for ttx data dump")
  127. parser.add_argument("-e", "--exclude", action="append", help="Excluded OpenType tables for ttx data dump")
  128. args = parser.parse_args(sys.argv[1:])
  129. if args.check is True:
  130. check_checksum(args.filepaths)
  131. else:
  132. 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)