1 |
commit: 3618bc93cf1cfcb9f7f2d5f11852a1b0edbed871 |
2 |
Author: Brian Dolbec <dolsen <AT> gentoo <DOT> org> |
3 |
AuthorDate: Sat Jul 15 01:04:31 2017 +0000 |
4 |
Commit: Zac Medico <zmedico <AT> gentoo <DOT> org> |
5 |
CommitDate: Fri Mar 30 03:51:18 2018 +0000 |
6 |
URL: https://gitweb.gentoo.org/proj/portage.git/commit/?id=3618bc93 |
7 |
|
8 |
repoman: New linechecks module, quotes |
9 |
|
10 |
.../repoman/modules/linechecks/quotes/__init__.py | 27 +++++++ |
11 |
.../repoman/modules/linechecks/quotes/quoteda.py | 16 ++++ |
12 |
.../repoman/modules/linechecks/quotes/quotes.py | 86 ++++++++++++++++++++++ |
13 |
3 files changed, 129 insertions(+) |
14 |
|
15 |
diff --git a/repoman/pym/repoman/modules/linechecks/quotes/__init__.py b/repoman/pym/repoman/modules/linechecks/quotes/__init__.py |
16 |
new file mode 100644 |
17 |
index 000000000..6043ab20c |
18 |
--- /dev/null |
19 |
+++ b/repoman/pym/repoman/modules/linechecks/quotes/__init__.py |
20 |
@@ -0,0 +1,27 @@ |
21 |
+# Copyright 2015-2016 Gentoo Foundation |
22 |
+# Distributed under the terms of the GNU General Public License v2 |
23 |
+ |
24 |
+doc = """Nested plug-in module for repoman LineChecks. |
25 |
+Performs nested subshell checks on ebuilds.""" |
26 |
+__doc__ = doc[:] |
27 |
+ |
28 |
+ |
29 |
+module_spec = { |
30 |
+ 'name': 'do', |
31 |
+ 'description': doc, |
32 |
+ 'provides':{ |
33 |
+ 'quote-check': { |
34 |
+ 'name': "quote", |
35 |
+ 'sourcefile': "quotes", |
36 |
+ 'class': "EbuildQuote", |
37 |
+ 'description': doc, |
38 |
+ }, |
39 |
+ 'quoteda-check': { |
40 |
+ 'name': "quoteda", |
41 |
+ 'sourcefile': "quoteda", |
42 |
+ 'class': "EbuildQuotedA", |
43 |
+ 'description': doc, |
44 |
+ }, |
45 |
+ } |
46 |
+} |
47 |
+ |
48 |
|
49 |
diff --git a/repoman/pym/repoman/modules/linechecks/quotes/quoteda.py b/repoman/pym/repoman/modules/linechecks/quotes/quoteda.py |
50 |
new file mode 100644 |
51 |
index 000000000..7fd9ba797 |
52 |
--- /dev/null |
53 |
+++ b/repoman/pym/repoman/modules/linechecks/quotes/quoteda.py |
54 |
@@ -0,0 +1,16 @@ |
55 |
+ |
56 |
+import re |
57 |
+ |
58 |
+from repoman.modules.linechecks.base import LineCheck |
59 |
+ |
60 |
+ |
61 |
+class EbuildQuotedA(LineCheck): |
62 |
+ """Ensure ebuilds have no quoting around ${A}""" |
63 |
+ |
64 |
+ repoman_check_name = 'ebuild.minorsyn' |
65 |
+ a_quoted = re.compile(r'.*\"\$(\{A\}|A)\"') |
66 |
+ |
67 |
+ def check(self, num, line): |
68 |
+ match = self.a_quoted.match(line) |
69 |
+ if match: |
70 |
+ return "Quoted \"${A}\" on line: %d" |
71 |
|
72 |
diff --git a/repoman/pym/repoman/modules/linechecks/quotes/quotes.py b/repoman/pym/repoman/modules/linechecks/quotes/quotes.py |
73 |
new file mode 100644 |
74 |
index 000000000..68c594e23 |
75 |
--- /dev/null |
76 |
+++ b/repoman/pym/repoman/modules/linechecks/quotes/quotes.py |
77 |
@@ -0,0 +1,86 @@ |
78 |
+ |
79 |
+import re |
80 |
+ |
81 |
+from repoman.modules.linechecks.base import LineCheck |
82 |
+ |
83 |
+ |
84 |
+class EbuildQuote(LineCheck): |
85 |
+ """Ensure ebuilds have valid quoting around things like D,FILESDIR, etc...""" |
86 |
+ |
87 |
+ repoman_check_name = 'ebuild.minorsyn' |
88 |
+ _message_commands = [ |
89 |
+ "die", "echo", "eerror", "einfo", "elog", "eqawarn", "ewarn"] |
90 |
+ _message_re = re.compile( |
91 |
+ r'\s(' + "|".join(_message_commands) + r')\s+"[^"]*"\s*$') |
92 |
+ _ignored_commands = ["local", "export"] + _message_commands |
93 |
+ ignore_line = re.compile( |
94 |
+ r'(^$)|(^\s*#.*)|(^\s*\w+=.*)' + |
95 |
+ r'|(^\s*(' + "|".join(_ignored_commands) + r')\s+)') |
96 |
+ ignore_comment = False |
97 |
+ var_names = ["D", "DISTDIR", "FILESDIR", "S", "T", "ROOT", "WORKDIR"] |
98 |
+ |
99 |
+ # EAPI=3/Prefix vars |
100 |
+ var_names += ["ED", "EPREFIX", "EROOT"] |
101 |
+ |
102 |
+ # variables for games.eclass |
103 |
+ var_names += [ |
104 |
+ "Ddir", "GAMES_PREFIX_OPT", "GAMES_DATADIR", |
105 |
+ "GAMES_DATADIR_BASE", "GAMES_SYSCONFDIR", "GAMES_STATEDIR", |
106 |
+ "GAMES_LOGDIR", "GAMES_BINDIR"] |
107 |
+ |
108 |
+ # variables for multibuild.eclass |
109 |
+ var_names += ["BUILD_DIR"] |
110 |
+ |
111 |
+ var_names = "(%s)" % "|".join(var_names) |
112 |
+ var_reference = re.compile( |
113 |
+ r'\$(\{%s\}|%s\W)' % (var_names, var_names)) |
114 |
+ missing_quotes = re.compile( |
115 |
+ r'(\s|^)[^"\'\s]*\$\{?%s\}?[^"\'\s]*(\s|$)' % var_names) |
116 |
+ cond_begin = re.compile(r'(^|\s+)\[\[($|\\$|\s+)') |
117 |
+ cond_end = re.compile(r'(^|\s+)\]\]($|\\$|\s+)') |
118 |
+ |
119 |
+ def check(self, num, line): |
120 |
+ if self.var_reference.search(line) is None: |
121 |
+ return |
122 |
+ # There can be multiple matches / violations on a single line. We |
123 |
+ # have to make sure none of the matches are violators. Once we've |
124 |
+ # found one violator, any remaining matches on the same line can |
125 |
+ # be ignored. |
126 |
+ pos = 0 |
127 |
+ while pos <= len(line) - 1: |
128 |
+ missing_quotes = self.missing_quotes.search(line, pos) |
129 |
+ if not missing_quotes: |
130 |
+ break |
131 |
+ # If the last character of the previous match is a whitespace |
132 |
+ # character, that character may be needed for the next |
133 |
+ # missing_quotes match, so search overlaps by 1 character. |
134 |
+ group = missing_quotes.group() |
135 |
+ pos = missing_quotes.end() - 1 |
136 |
+ |
137 |
+ # Filter out some false positives that can |
138 |
+ # get through the missing_quotes regex. |
139 |
+ if self.var_reference.search(group) is None: |
140 |
+ continue |
141 |
+ |
142 |
+ # Filter matches that appear to be an |
143 |
+ # argument to a message command. |
144 |
+ # For example: false || ewarn "foo $WORKDIR/bar baz" |
145 |
+ message_match = self._message_re.search(line) |
146 |
+ if message_match is not None and \ |
147 |
+ message_match.start() < pos and \ |
148 |
+ message_match.end() > pos: |
149 |
+ break |
150 |
+ |
151 |
+ # This is an attempt to avoid false positives without getting |
152 |
+ # too complex, while possibly allowing some (hopefully |
153 |
+ # unlikely) violations to slip through. We just assume |
154 |
+ # everything is correct if the there is a ' [[ ' or a ' ]] ' |
155 |
+ # anywhere in the whole line (possibly continued over one |
156 |
+ # line). |
157 |
+ if self.cond_begin.search(line) is not None: |
158 |
+ continue |
159 |
+ if self.cond_end.search(line) is not None: |
160 |
+ continue |
161 |
+ |
162 |
+ # Any remaining matches on the same line can be ignored. |
163 |
+ return self.errors['MISSING_QUOTES_ERROR'] |