Debian Codegraph @

saltpylint/fileperms.py

raw symbols SCIP
1 """
2 PyLint File Permissions Check Plugin
3 ====================================
4
5 PyLint plugin which checks for specific file permissions
6 """
7
8 import glob
9 import os
10 import stat
11 import sys
12 from typing import ClassVar
13
14 from pylint.checkers import BaseChecker
15
16
17 class FilePermsChecker(BaseChecker):
18 """Check for files with undesirable permissions."""
19
20 name = "fileperms"
21 msgs: ClassVar = {
22 "E0599": (
23 "Module file has the wrong file permissions(expected %s): %s",
24 "file-perms",
25 ("Wrong file permissions"),
26 ),
27 }
28
29 priority = -1
30
31 options = (
32 (
33 "fileperms-default",
34 {
35 "default": "0644",
36 "type": "string",
37 "metavar": "ZERO_PADDED_PERM",
38 "help": "Desired file permissons. Default: 0644",
39 },
40 ),
41 (
42 "fileperms-ignore-paths",
43 {
44 "default": (),
45 "type": "csv",
46 "metavar": "<comma-separated-list>",
47 "help": "File paths to ignore file permission. Glob patterns allowed.",
48 },
49 ),
50 )
51
52 def process_module(self, node):
53 """Process a module."""
54 for listing in self.config.fileperms_ignore_paths:
55 if node.file.split(f"{os.getcwd()}/")[-1] in glob.glob(listing):
56 # File is ignored, no checking should be done
57 return
58
59 desired_perm = self.config.fileperms_default
60 if "-" in desired_perm:
61 desired_perm = desired_perm.split("-")
62 else:
63 desired_perm = [desired_perm]
64
65 if len(desired_perm) > 2: # noqa: PLR2004
66 msg = "Permission ranges should be like XXXX-YYYY"
67 raise RuntimeError(msg)
68
69 for idx, _perm in enumerate(desired_perm):
70 desired_perm[idx] = desired_perm[idx].strip('"').strip("'").lstrip("0").zfill(4)
71 if desired_perm[idx][0] != "0":
72 # Always include a leading zero
73 desired_perm[idx] = f"0{desired_perm[idx]}"
74 if desired_perm[idx][1] != "o":
75 desired_perm[idx] = "0o" + desired_perm[idx][1:]
76 if sys.platform.startswith("win"):
77 # Windows does not distinguish between user/group/other.
78 # They must all be the same. Also, Windows will automatically
79 # set the execution bit on files with a known extension
80 # (eg .exe, .bat, .com). So we cannot reliably test the
81 # execution bit on other files such as .py files.
82 user_perm_noexec = int(desired_perm[idx][-3])
83 if user_perm_noexec % 2 == 1:
84 user_perm_noexec -= 1
85 desired_perm[idx] = desired_perm[idx][:-3] + (str(user_perm_noexec) * 3)
86
87 module_perms = oct(stat.S_IMODE(os.stat(node.file).st_mode))
88
89 if len(desired_perm) == 1:
90 if module_perms != desired_perm[0]:
91 if sys.platform.startswith("win"):
92 # Check the variant with execution bit set due to the
93 # unreliability of checking the execution bit on Windows.
94 user_perm_noexec = int(desired_perm[0][-3])
95 desired_perm_exec = desired_perm[0][:-3] + (str(user_perm_noexec + 1) * 3)
96 if module_perms == desired_perm_exec:
97 return
98 self.add_message("E0599", line=1, args=(desired_perm[0], module_perms))
99 elif module_perms < desired_perm[0] or module_perms > desired_perm[1]:
100 if sys.platform.startswith("win"):
101 # Check the variant with execution bit set due to the
102 # unreliability of checking the execution bit on Windows.
103 user_perm_noexec0 = int(desired_perm[0][-3])
104 desired_perm_exec0 = desired_perm[0][:-3] + (str(user_perm_noexec0 + 1) * 3)
105 user_perm_noexec1 = int(desired_perm[1][-3])
106 desired_perm_exec1 = desired_perm[1][:-3] + (str(user_perm_noexec1 + 1) * 3)
107 if desired_perm_exec0 <= module_perms <= desired_perm_exec1:
108 return
109 desired_perm = ">= {} OR <= {}".format(*desired_perm)
110 self.add_message("E0599", line=1, args=(desired_perm, module_perms))
111
112
113 def register(linter):
114 """Required method to auto register this checker."""
115 linter.register_checker(FilePermsChecker(linter))