xref: /OK3568_Linux_fs/buildroot/utils/checkpackagelib/lib.py (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1# See utils/checkpackagelib/readme.txt before editing this file.
2
3from checkpackagelib.base import _CheckFunction
4
5
6class ConsecutiveEmptyLines(_CheckFunction):
7    def before(self):
8        self.lastline = "non empty"
9
10    def check_line(self, lineno, text):
11        if text.strip() == "" == self.lastline.strip():
12            return ["{}:{}: consecutive empty lines"
13                    .format(self.filename, lineno)]
14        self.lastline = text
15
16
17class EmptyLastLine(_CheckFunction):
18    def before(self):
19        self.lastlineno = 0
20        self.lastline = "non empty"
21
22    def check_line(self, lineno, text):
23        self.lastlineno = lineno
24        self.lastline = text
25
26    def after(self):
27        if self.lastline.strip() == "":
28            return ["{}:{}: empty line at end of file"
29                    .format(self.filename, self.lastlineno)]
30
31
32class NewlineAtEof(_CheckFunction):
33    def before(self):
34        self.lastlineno = 0
35        self.lastline = "\n"
36
37    def check_line(self, lineno, text):
38        self.lastlineno = lineno
39        self.lastline = text
40
41    def after(self):
42        if self.lastline == self.lastline.rstrip("\r\n"):
43            return ["{}:{}: missing newline at end of file"
44                    .format(self.filename, self.lastlineno),
45                    self.lastline]
46
47
48class TrailingSpace(_CheckFunction):
49    def check_line(self, lineno, text):
50        line = text.rstrip("\r\n")
51        if line != line.rstrip():
52            return ["{}:{}: line contains trailing whitespace"
53                    .format(self.filename, lineno),
54                    text]
55
56
57class Utf8Characters(_CheckFunction):
58    def is_ascii(self, s):
59        try:
60            return all(ord(c) < 128 for c in s)
61        except TypeError:
62            return False
63
64    def check_line(self, lineno, text):
65        if not self.is_ascii(text):
66            return ["{}:{}: line contains UTF-8 characters"
67                    .format(self.filename, lineno),
68                    text]
69