lib_hash.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. # See utils/checkpackagelib/readme.txt before editing this file.
  2. # The validity of the hashes itself is checked when building, so below check
  3. # functions don't need to check for things already checked by running
  4. # "make package-dirclean package-source".
  5. import re
  6. from checkpackagelib.base import _CheckFunction
  7. from checkpackagelib.lib import ConsecutiveEmptyLines # noqa: F401
  8. from checkpackagelib.lib import EmptyLastLine # noqa: F401
  9. from checkpackagelib.lib import NewlineAtEof # noqa: F401
  10. from checkpackagelib.lib import TrailingSpace # noqa: F401
  11. def _empty_line_or_comment(text):
  12. return text.strip() == "" or text.startswith("#")
  13. class HashNumberOfFields(_CheckFunction):
  14. def check_line(self, lineno, text):
  15. if _empty_line_or_comment(text):
  16. return
  17. fields = text.split()
  18. if len(fields) != 3:
  19. return ["{}:{}: expected three fields ({}#adding-packages-hash)"
  20. .format(self.filename, lineno, self.url_to_manual),
  21. text]
  22. class HashType(_CheckFunction):
  23. len_of_hash = {"md5": 32, "sha1": 40, "sha224": 56, "sha256": 64,
  24. "sha384": 96, "sha512": 128}
  25. def check_line(self, lineno, text):
  26. if _empty_line_or_comment(text):
  27. return
  28. fields = text.split()
  29. if len(fields) < 2:
  30. return
  31. htype, hexa = fields[:2]
  32. if htype not in self.len_of_hash.keys():
  33. return ["{}:{}: unexpected type of hash ({}#adding-packages-hash)"
  34. .format(self.filename, lineno, self.url_to_manual),
  35. text]
  36. if not re.match("^[0-9A-Fa-f]{%s}$" % self.len_of_hash[htype], hexa):
  37. return ["{}:{}: hash size does not match type "
  38. "({}#adding-packages-hash)"
  39. .format(self.filename, lineno, self.url_to_manual),
  40. text,
  41. "expected {} hex digits".format(self.len_of_hash[htype])]
  42. class HashSpaces(_CheckFunction):
  43. def check_line(self, lineno, text):
  44. if _empty_line_or_comment(text):
  45. return
  46. fields = text.split()
  47. if len(fields) != 3:
  48. # Handled by HashNumberOfFields
  49. return
  50. if not re.match(re.escape("{} {} {}".format(*fields)), text):
  51. return ["{}:{}: separation does not match expectation "
  52. "({}#adding-packages-hash)"
  53. .format(self.filename, lineno, self.url_to_manual), text]