1import os
2import re
3
4from checkpackagelib.base import _CheckFunction
5from checkpackagelib.lib import ConsecutiveEmptyLines  # noqa: F401
6from checkpackagelib.lib import EmptyLastLine          # noqa: F401
7from checkpackagelib.lib import NewlineAtEof           # noqa: F401
8from checkpackagelib.lib import TrailingSpace          # noqa: F401
9import checkpackagelib.tool
10from checkpackagelib.tool import Shellcheck            # noqa: F401
11
12
13class Indent(_CheckFunction):
14    INDENTED_WITH_SPACES = re.compile(r"^[\t]* ")
15
16    def check_line(self, lineno, text):
17        if self.INDENTED_WITH_SPACES.search(text.rstrip()):
18            return ["{}:{}: should be indented with tabs ({}#adding-packages-start-script)"
19                    .format(self.filename, lineno, self.url_to_manual),
20                    text]
21
22
23class NotExecutable(checkpackagelib.tool.NotExecutable):
24    def ignore(self):
25        return 'etc/init.d/' in self.filename
26
27    def hint(self):
28        return ", just make sure you use '$(INSTALL) -D -m 0755' in the .mk file"
29
30
31class Variables(_CheckFunction):
32    DAEMON_VAR = re.compile(r"^DAEMON=[\"']{0,1}([^\"']*)[\"']{0,1}")
33    PIDFILE_PATTERN = re.compile(r"/var/run/(\$DAEMON|\$\{DAEMON\}).pid")
34    PIDFILE_VAR = re.compile(r"^PIDFILE=[\"']{0,1}([^\"']*)[\"']{0,1}")
35
36    def before(self):
37        self.name = None
38
39    def check_line(self, lineno, text):
40        name_found = self.DAEMON_VAR.search(text.rstrip())
41        if name_found:
42            if self.name:
43                return ["{}:{}: DAEMON variable redefined ({}#adding-packages-start-script)"
44                        .format(self.filename, lineno, self.url_to_manual),
45                        text]
46            self.name = name_found.group(1)
47            if '/' in self.name:
48                self.name = os.path.basename(self.name)  # to be used in after() to check the expected filename
49                return ["{}:{}: Do not include path in DAEMON ({}#adding-packages-start-script)"
50                        .format(self.filename, lineno, self.url_to_manual),
51                        text,
52                        'DAEMON="{}"'.format(self.name)]
53            return
54
55        pidfile_found = self.PIDFILE_VAR.search(text.rstrip())
56        if pidfile_found:
57            pidfile = pidfile_found.group(1)
58            if not self.PIDFILE_PATTERN.match(pidfile):
59                return ["{}:{}: Incorrect PIDFILE value  ({}#adding-packages-start-script)"
60                        .format(self.filename, lineno, self.url_to_manual),
61                        text,
62                        'PIDFILE="/var/run/$DAEMON.pid"']
63
64    def after(self):
65        if self.name is None:
66            return ["{}:0: DAEMON variable not defined ({}#adding-packages-start-script)"
67                    .format(self.filename, self.url_to_manual)]
68        expected_filename = re.compile(r"S\d\d{}$".format(self.name))
69        if not expected_filename.match(os.path.basename(self.filename)):
70            return ["{}:0: filename should be S<number><number><daemon name> ({}#adding-packages-start-script)"
71                    .format(self.filename, self.url_to_manual),
72                    "expecting S<number><number>{}".format(self.name)]
73