lib_hash.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 == "none":
  33. return
  34. if htype not in self.len_of_hash.keys():
  35. return ["{}:{}: unexpected type of hash ({}#adding-packages-hash)"
  36. .format(self.filename, lineno, self.url_to_manual),
  37. text]
  38. if not re.match("^[0-9A-Fa-f]{%s}$" % self.len_of_hash[htype], hexa):
  39. return ["{}:{}: hash size does not match type "
  40. "({}#adding-packages-hash)"
  41. .format(self.filename, lineno, self.url_to_manual),
  42. text,
  43. "expected {} hex digits".format(self.len_of_hash[htype])]