2
1

lib_hash.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. from checkpackagelib.tool import NotExecutable # noqa: F401
  12. def _empty_line_or_comment(text):
  13. return text.strip() == "" or text.startswith("#")
  14. class HashNumberOfFields(_CheckFunction):
  15. def check_line(self, lineno, text):
  16. if _empty_line_or_comment(text):
  17. return
  18. fields = text.split()
  19. if len(fields) != 3:
  20. return ["{}:{}: expected three fields ({}#adding-packages-hash)"
  21. .format(self.filename, lineno, self.url_to_manual),
  22. text]
  23. class HashType(_CheckFunction):
  24. len_of_hash = {"md5": 32, "sha1": 40, "sha224": 56, "sha256": 64,
  25. "sha384": 96, "sha512": 128}
  26. def check_line(self, lineno, text):
  27. if _empty_line_or_comment(text):
  28. return
  29. fields = text.split()
  30. if len(fields) < 2:
  31. return
  32. htype, hexa = fields[:2]
  33. if htype not in self.len_of_hash.keys():
  34. return ["{}:{}: unexpected type of hash ({}#adding-packages-hash)"
  35. .format(self.filename, lineno, self.url_to_manual),
  36. text]
  37. if not re.match("^[0-9A-Fa-f]{%s}$" % self.len_of_hash[htype], hexa):
  38. return ["{}:{}: hash size does not match type "
  39. "({}#adding-packages-hash)"
  40. .format(self.filename, lineno, self.url_to_manual),
  41. text,
  42. "expected {} hex digits".format(self.len_of_hash[htype])]
  43. class HashSpaces(_CheckFunction):
  44. def check_line(self, lineno, text):
  45. if _empty_line_or_comment(text):
  46. return
  47. fields = text.split()
  48. if len(fields) != 3:
  49. # Handled by HashNumberOfFields
  50. return
  51. if not re.match(re.escape("{} {} {}".format(*fields)), text):
  52. return ["{}:{}: separation does not match expectation "
  53. "({}#adding-packages-hash)"
  54. .format(self.filename, lineno, self.url_to_manual), text]