2 # Copyright 2007 Gentoo Foundation
3 # Distributed under the terms of the GNU General Public License v2
6 """This module contains functions used in Repoman to ascertain the quality
7 and correctness of an ebuild."""
12 import repoman.errors as errors
14 class LineCheck(object):
15 """Run a check on a line of an ebuild."""
16 """A regular expression to determine whether to ignore the line"""
22 def check(self, num, line):
23 """Run the check on line and return error if there is one"""
24 if self.re.match(line):
30 class EbuildHeader(LineCheck):
31 """Ensure ebuilds have proper headers
32 Copyright header errors
37 modification_year - Year the ebuild was last modified
40 repoman_check_name = 'ebuild.badheader'
42 gentoo_copyright = r'^# Copyright ((1999|200\d)-)?%s Gentoo Foundation$'
43 # Why a regex here, use a string match
44 # gentoo_license = re.compile(r'^# Distributed under the terms of the GNU General Public License v2$')
45 gentoo_license = r'# Distributed under the terms of the GNU General Public License v2'
46 cvs_header = re.compile(r'^#\s*\$Header.*\$$')
49 self.modification_year = str(time.gmtime(pkg.mtime)[0])
50 self.gentoo_copyright_re = re.compile(
51 self.gentoo_copyright % self.modification_year)
53 def check(self, num, line):
57 if not self.gentoo_copyright_re.match(line):
58 return errors.COPYRIGHT_ERROR
59 elif num == 1 and line.strip() != self.gentoo_license:
60 return errors.LICENSE_ERROR
62 if not self.cvs_header.match(line):
63 return errors.CVS_HEADER_ERROR
66 class EbuildWhitespace(LineCheck):
67 """Ensure ebuilds have proper whitespacing"""
69 repoman_check_name = 'ebuild.minorsyn'
71 ignore_line = re.compile(r'(^$)|(^(\t)*#)')
72 leading_spaces = re.compile(r'^[\S\t]')
73 trailing_whitespace = re.compile(r'.*([\S]$)')
75 def check(self, num, line):
76 if not self.leading_spaces.match(line):
77 return errors.LEADING_SPACES_ERROR
78 if not self.trailing_whitespace.match(line):
79 return errors.TRAILING_WHITESPACE_ERROR
82 class EbuildQuote(LineCheck):
83 """Ensure ebuilds have valid quoting around things like D,FILESDIR, etc..."""
85 repoman_check_name = 'ebuild.minorsyn'
86 _message_commands = ["die", "echo", "eerror",
87 "einfo", "elog", "eqawarn", "ewarn"]
88 _message_re = re.compile(r'\s(' + "|".join(_message_commands) + \
90 _ignored_commands = ["local", "export"] + _message_commands
91 ignore_line = re.compile(r'(^$)|(^\s*#.*)|(^\s*\w+=.*)' + \
92 r'|(^\s*(' + "|".join(_ignored_commands) + r')\s+)')
93 var_names = ["D", "DISTDIR", "FILESDIR", "S", "T", "ROOT", "WORKDIR"]
95 # variables for games.eclass
96 var_names += ["Ddir", "dir", "GAMES_PREFIX_OPT", "GAMES_DATADIR",
97 "GAMES_DATADIR_BASE", "GAMES_SYSCONFDIR", "GAMES_STATEDIR",
98 "GAMES_LOGDIR", "GAMES_BINDIR"]
100 var_names = "(%s)" % "|".join(var_names)
101 var_reference = re.compile(r'\$(\{'+var_names+'\}|' + \
103 missing_quotes = re.compile(r'(\s|^)[^"\'\s]*\$\{?' + var_names + \
104 r'\}?[^"\'\s]*(\s|$)')
105 cond_begin = re.compile(r'(^|\s+)\[\[($|\\$|\s+)')
106 cond_end = re.compile(r'(^|\s+)\]\]($|\\$|\s+)')
108 def check(self, num, line):
109 if self.var_reference.search(line) is None:
111 # There can be multiple matches / violations on a single line. We
112 # have to make sure none of the matches are violators. Once we've
113 # found one violator, any remaining matches on the same line can
116 while pos <= len(line) - 1:
117 missing_quotes = self.missing_quotes.search(line, pos)
118 if not missing_quotes:
120 # If the last character of the previous match is a whitespace
121 # character, that character may be needed for the next
122 # missing_quotes match, so search overlaps by 1 character.
123 group = missing_quotes.group()
124 pos = missing_quotes.end() - 1
126 # Filter out some false positives that can
127 # get through the missing_quotes regex.
128 if self.var_reference.search(group) is None:
131 # Filter matches that appear to be an
132 # argument to a message command.
133 # For example: false || ewarn "foo $WORKDIR/bar baz"
134 message_match = self._message_re.search(line)
135 if message_match is not None and \
136 message_match.start() < pos and \
137 message_match.end() > pos:
140 # This is an attempt to avoid false positives without getting
141 # too complex, while possibly allowing some (hopefully
142 # unlikely) violations to slip through. We just assume
143 # everything is correct if the there is a ' [[ ' or a ' ]] '
144 # anywhere in the whole line (possibly continued over one
146 if self.cond_begin.search(line) is not None:
148 if self.cond_end.search(line) is not None:
151 # Any remaining matches on the same line can be ignored.
152 return errors.MISSING_QUOTES_ERROR
155 class EbuildAssignment(LineCheck):
156 """Ensure ebuilds don't assign to readonly variables."""
158 repoman_check_name = 'variable.readonly'
160 readonly_assignment = re.compile(r'^\s*(export\s+)?(A|CATEGORY|P|PV|PN|PR|PVR|PF|D|WORKDIR|FILESDIR|FEATURES|USE)=')
161 line_continuation = re.compile(r'([^#]*\S)(\s+|\t)\\$')
162 ignore_line = re.compile(r'(^$)|(^(\t)*#)')
165 self.previous_line = None
167 def check(self, num, line):
168 match = self.readonly_assignment.match(line)
170 if match and (not self.previous_line or not self.line_continuation.match(self.previous_line)):
171 e = errors.READONLY_ASSIGNMENT_ERROR
172 self.previous_line = line
176 class EbuildNestedDie(LineCheck):
177 """Check ebuild for nested die statements (die statements in subshells"""
179 repoman_check_name = 'ebuild.nesteddie'
180 nesteddie_re = re.compile(r'^[^#]*\([^)]*\bdie\b')
182 def check(self, num, line):
183 if self.nesteddie_re.match(line):
184 return errors.NESTED_DIE_ERROR
187 class EbuildUselessDodoc(LineCheck):
188 """Check ebuild for useless files in dodoc arguments."""
189 repoman_check_name = 'ebuild.minorsyn'
190 uselessdodoc_re = re.compile(
191 r'^\s*dodoc(\s+|\s+.*\s+)(ABOUT-NLS|COPYING|LICENSE)($|\s)')
193 def check(self, num, line):
194 match = self.uselessdodoc_re.match(line)
196 return "Useless dodoc '%s'" % (match.group(2), ) + " on line: %d"
199 class EbuildUselessCdS(LineCheck):
200 """Check for redundant cd ${S} statements"""
201 repoman_check_name = 'ebuild.minorsyn'
202 method_re = re.compile(r'^\s*src_(compile|install|test)\s*\(\)')
203 cds_re = re.compile(r'^\s*cd\s+("\$(\{S\}|S)"|\$(\{S\}|S))\s')
206 self.check_next_line = False
208 def check(self, num, line):
209 if self.check_next_line:
210 self.check_next_line = False
211 if self.cds_re.match(line):
212 return errors.REDUNDANT_CD_S_ERROR
213 elif self.method_re.match(line):
214 self.check_next_line = True
216 class EbuildPatches(LineCheck):
217 """Ensure ebuilds use bash arrays for PATCHES to ensure white space safety"""
218 repoman_check_name = 'ebuild.patches'
219 re = re.compile(r'^\s*PATCHES=[^\(]')
220 error = errors.PATCHES_ERROR
222 class EbuildQuotedA(LineCheck):
223 """Ensure ebuilds have no quoting around ${A}"""
225 repoman_check_name = 'ebuild.minorsyn'
226 a_quoted = re.compile(r'.*\"\$(\{A\}|A)\"')
228 def check(self, num, line):
229 match = self.a_quoted.match(line)
231 return "Quoted \"${A}\" on line: %d"
233 class InheritAutotools(LineCheck):
235 Make sure appropriate functions are called in
236 ebuilds that inherit autotools.eclass.
239 repoman_check_name = 'inherit.autotools'
240 ignore_line = re.compile(r'(^|\s*)#')
241 _inherit_autotools_re = re.compile(r'^\s*inherit\s(.*\s)?autotools(\s|$)')
243 "eaclocal", "eautoconf", "eautoheader",
244 "eautomake", "eautoreconf", "_elibtoolize")
245 _autotools_func_re = re.compile(r'(^|\s)(' + \
246 "|".join(_autotools_funcs) + ')(\s|$)')
248 # git - An EGIT_BOOTSTRAP variable may be used to call one of
249 # the autotools functions.
250 # subversion - An ESVN_BOOTSTRAP variable may be used to call one of
251 # the autotools functions.
252 _exempt_eclasses = frozenset(["git", "subversion"])
255 self._inherit_autotools = None
256 self._autotools_func_call = None
257 self._disabled = self._exempt_eclasses.intersection(pkg.inherited)
259 def check(self, num, line):
262 if self._inherit_autotools is None:
263 self._inherit_autotools = self._inherit_autotools_re.match(line)
264 if self._inherit_autotools is not None and \
265 self._autotools_func_call is None:
266 self._autotools_func_call = self._autotools_func_re.search(line)
269 if self._inherit_autotools and self._autotools_func_call is None:
270 yield 'no eauto* function called'
272 class IUseUndefined(LineCheck):
274 Make sure the ebuild defines IUSE (style guideline
275 says to define IUSE even when empty).
278 repoman_check_name = 'IUSE.undefined'
279 _iuse_def_re = re.compile(r'^IUSE=.*')
282 self._iuse_def = None
284 def check(self, num, line):
285 if self._iuse_def is None:
286 self._iuse_def = self._iuse_def_re.match(line)
289 if self._iuse_def is None:
290 yield 'IUSE is not defined'
292 class EMakeParallelDisabled(LineCheck):
293 """Check for emake -j1 calls which disable parallelization."""
294 repoman_check_name = 'upstream.workaround'
295 re = re.compile(r'^\s*emake\s+-j\s*1\s')
296 error = errors.EMAKE_PARALLEL_DISABLED
298 class DeprecatedBindnowFlags(LineCheck):
299 """Check for calls to the deprecated bindnow-flags function."""
300 repoman_check_name = 'ebuild.minorsyn'
301 re = re.compile(r'.*\$\(bindnow-flags\)')
302 error = errors.DEPRECATED_BINDNOW_FLAGS
304 _constant_checks = tuple((c() for c in (
305 EbuildHeader, EbuildWhitespace, EbuildQuote,
306 EbuildAssignment, EbuildUselessDodoc,
307 EbuildUselessCdS, EbuildNestedDie,
308 EbuildPatches, EbuildQuotedA,
309 IUseUndefined, InheritAutotools,
310 EMakeParallelDisabled, DeprecatedBindnowFlags)))
312 def run_checks(contents, pkg):
313 checks = _constant_checks
317 for num, line in enumerate(contents):
319 ignore = lc.ignore_line
320 if not ignore or not ignore.match(line):
321 e = lc.check(num, line)
323 yield lc.repoman_check_name, e % (num + 1)
328 yield lc.repoman_check_name, e