2
1

lib_hash.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # See support/scripts/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 base import _CheckFunction
  7. # Notice: ignore 'imported but unused' from pyflakes for check functions.
  8. from lib import ConsecutiveEmptyLines
  9. from lib import EmptyLastLine
  10. from lib import NewlineAtEof
  11. from lib import TrailingSpace
  12. def _empty_line_or_comment(text):
  13. return text.strip() == "" or text.startswith("#")
  14. class HashFilename(_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
  21. if '/' in fields[2]:
  22. return ["{}:{}: use filename without directory component"
  23. " ({}#adding-packages-hash)"
  24. .format(self.filename, lineno, self.url_to_manual),
  25. text]
  26. class HashNumberOfFields(_CheckFunction):
  27. def check_line(self, lineno, text):
  28. if _empty_line_or_comment(text):
  29. return
  30. fields = text.split()
  31. if len(fields) != 3:
  32. return ["{}:{}: expected three fields ({}#adding-packages-hash)"
  33. .format(self.filename, lineno, self.url_to_manual),
  34. text]
  35. class HashType(_CheckFunction):
  36. len_of_hash = {"md5": 32, "sha1": 40, "sha224": 56, "sha256": 64,
  37. "sha384": 96, "sha512": 128}
  38. def check_line(self, lineno, text):
  39. if _empty_line_or_comment(text):
  40. return
  41. fields = text.split()
  42. if len(fields) < 2:
  43. return
  44. htype, hexa = fields[:2]
  45. if htype == "none":
  46. return
  47. if htype not in self.len_of_hash.keys():
  48. return ["{}:{}: unexpected type of hash ({}#adding-packages-hash)"
  49. .format(self.filename, lineno, self.url_to_manual),
  50. text]
  51. if not re.match("^[0-9A-Fa-f]{%s}$" % self.len_of_hash[htype], hexa):
  52. return ["{}:{}: hash size does not match type "
  53. "({}#adding-packages-hash)"
  54. .format(self.filename, lineno, self.url_to_manual),
  55. text,
  56. "expected {} hex digits".format(self.len_of_hash[htype])]