]> rtime.felk.cvut.cz Git - coffee/buildroot.git/blob - support/scripts/checkpackagelib_hash.py
check-package: check *.hash files
[coffee/buildroot.git] / support / scripts / checkpackagelib_hash.py
1 # See support/scripts/check-package.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
6 import re
7
8 from checkpackagebase import _CheckFunction
9 # Notice: ignore 'imported but unused' from pyflakes for check functions.
10 from checkpackagelib import ConsecutiveEmptyLines
11 from checkpackagelib import EmptyLastLine
12 from checkpackagelib import NewlineAtEof
13 from checkpackagelib import TrailingSpace
14
15
16 def _empty_line_or_comment(text):
17     return text.strip() == "" or text.startswith("#")
18
19
20 class HashFilename(_CheckFunction):
21     def check_line(self, lineno, text):
22         if _empty_line_or_comment(text):
23             return
24
25         fields = text.split()
26         if len(fields) < 3:
27             return
28
29         if '/' in fields[2]:
30             return ["{}:{}: use filename without directory component"
31                     " ({}#adding-packages-hash)"
32                     .format(self.filename, lineno, self.url_to_manual),
33                     text]
34
35
36 class HashNumberOfFields(_CheckFunction):
37     def check_line(self, lineno, text):
38         if _empty_line_or_comment(text):
39             return
40
41         fields = text.split()
42         if len(fields) != 3:
43             return ["{}:{}: expected three fields ({}#adding-packages-hash)"
44                     .format(self.filename, lineno, self.url_to_manual),
45                     text]
46
47
48 class HashType(_CheckFunction):
49     len_of_hash = {"md5": 32, "sha1": 40, "sha224": 56, "sha256": 64,
50                    "sha384": 96, "sha512": 128}
51
52     def check_line(self, lineno, text):
53         if _empty_line_or_comment(text):
54             return
55
56         fields = text.split()
57         if len(fields) < 2:
58             return
59
60         htype, hexa = fields[:2]
61         if htype == "none":
62             return
63         if htype not in self.len_of_hash.keys():
64             return ["{}:{}: unexpected type of hash ({}#adding-packages-hash)"
65                     .format(self.filename, lineno, self.url_to_manual),
66                     text]
67         if not re.match("^[0-9A-Fa-f]{%s}$" % self.len_of_hash[htype], hexa):
68             return ["{}:{}: hash size does not match type "
69                     "({}#adding-packages-hash)"
70                     .format(self.filename, lineno, self.url_to_manual),
71                     text,
72                     "expected {} hex digits".format(self.len_of_hash[htype])]