| 1 | # Copyright: 2007 Markus Ullmann <jokey@gentoo.org> |
|---|
| 2 | # License: GPL2 |
|---|
| 3 | |
|---|
| 4 | """check for some bad coding styles like insinto's, old variables etc""" |
|---|
| 5 | |
|---|
| 6 | from pkgcore_checks import base |
|---|
| 7 | from snakeoil.demandload import demandload |
|---|
| 8 | demandload(globals(), "re") |
|---|
| 9 | |
|---|
| 10 | class BadInsIntoDir(base.Result): |
|---|
| 11 | |
|---|
| 12 | """ebuild uses insinto where compact commands exist""" |
|---|
| 13 | |
|---|
| 14 | threshold = base.versioned_feed |
|---|
| 15 | |
|---|
| 16 | __slots__ = ("category", "package", "version", "line", "insintodir") |
|---|
| 17 | |
|---|
| 18 | def __init__(self, pkg, insintodir, line): |
|---|
| 19 | base.Result.__init__(self) |
|---|
| 20 | self._store_cpv(pkg) |
|---|
| 21 | self.line = line |
|---|
| 22 | self.insintodir = insintodir |
|---|
| 23 | |
|---|
| 24 | @property |
|---|
| 25 | def short_desc(self): |
|---|
| 26 | return "ebuild uses insinto %s on line %s" % (self.insintodir, |
|---|
| 27 | self.line) |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | class BadInsIntoCheck(base.Template): |
|---|
| 31 | |
|---|
| 32 | """checking ebuild for bad insinto usage""" |
|---|
| 33 | |
|---|
| 34 | feed_type = base.ebuild_feed |
|---|
| 35 | _bad_insinto = None |
|---|
| 36 | _bad_etc = ("conf", "env", "init", "pam") |
|---|
| 37 | _bad_cron = ("hourly", "daily", "weekly", "d") |
|---|
| 38 | _bad_paths = ("/usr/share/applications",) |
|---|
| 39 | |
|---|
| 40 | known_results = (BadInsIntoDir,) |
|---|
| 41 | |
|---|
| 42 | def __init__(self, *args, **kwds): |
|---|
| 43 | base.Template.__init__(self, *args, **kwds) |
|---|
| 44 | if self._bad_insinto is None: |
|---|
| 45 | self._load_class_regex() |
|---|
| 46 | |
|---|
| 47 | @classmethod |
|---|
| 48 | def _load_class_regex(cls): |
|---|
| 49 | patterns = [] |
|---|
| 50 | if cls._bad_etc: |
|---|
| 51 | patterns.append("etc/(?:%s).d" % "|".join(cls._bad_etc)) |
|---|
| 52 | if cls._bad_cron: |
|---|
| 53 | patterns.append("etc/cron.(?:%s)" % "|".join(cls._bad_cron)) |
|---|
| 54 | if cls._bad_paths: |
|---|
| 55 | patterns.extend(x.strip("/") for x in cls._bad_paths) |
|---|
| 56 | s = "|".join(patterns) |
|---|
| 57 | s = s.replace("/", "/+") |
|---|
| 58 | cls._bad_insinto = re.compile("insinto[ \t]+(/+(?:%s))(?:$|[/ \t])" % s) |
|---|
| 59 | |
|---|
| 60 | def feed(self, entry, reporter): |
|---|
| 61 | pkg, lines = entry |
|---|
| 62 | |
|---|
| 63 | badf = self._bad_insinto.search |
|---|
| 64 | for lineno, line in enumerate(lines): |
|---|
| 65 | if not line: |
|---|
| 66 | continue |
|---|
| 67 | matches = badf(line) |
|---|
| 68 | if matches is not None: |
|---|
| 69 | reporter.add_report(BadInsIntoDir(pkg, |
|---|
| 70 | matches.groups()[0], lineno + 1)) |
|---|