| 1 | # Copyright: 2006 Markus Ullmann <jokey@gentoo.org> |
|---|
| 2 | # Copyright: 2006 Marien Zwart <marienz@gentoo.org> |
|---|
| 3 | # License: GPL2 |
|---|
| 4 | |
|---|
| 5 | import os |
|---|
| 6 | from pkgcore_checks import base |
|---|
| 7 | |
|---|
| 8 | class base_whitespace(base.Result): |
|---|
| 9 | |
|---|
| 10 | threshold = base.versioned_feed |
|---|
| 11 | |
|---|
| 12 | __slots__ = () |
|---|
| 13 | |
|---|
| 14 | @property |
|---|
| 15 | def lines_str(self): |
|---|
| 16 | if len(self.lines) == 1: |
|---|
| 17 | return "line %i" % self.lines[0] |
|---|
| 18 | return "lines %s" % ', '.join(str(x) for x in self.lines) |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | class WhitespaceFound(base_whitespace): |
|---|
| 22 | |
|---|
| 23 | """leading or trailing whitespaces are found""" |
|---|
| 24 | |
|---|
| 25 | __slots__ = ("category", "package", "version", "lines", "leadtrail") |
|---|
| 26 | |
|---|
| 27 | def __init__(self, pkg, leadtrail, lines): |
|---|
| 28 | base.Result.__init__(self) |
|---|
| 29 | self._store_cpv(pkg) |
|---|
| 30 | self.lines = lines |
|---|
| 31 | self.leadtrail = leadtrail |
|---|
| 32 | |
|---|
| 33 | @property |
|---|
| 34 | def short_desc(self): |
|---|
| 35 | return "ebuild has %s whitespace on %s" % (self.leadtrail, |
|---|
| 36 | self.lines_str) |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | class WrongIndentFound(base_whitespace): |
|---|
| 40 | |
|---|
| 41 | """leading or trailing whitespaces are found""" |
|---|
| 42 | |
|---|
| 43 | __slots__ = ("category", "package", "version", "lines") |
|---|
| 44 | |
|---|
| 45 | def __init__(self, pkg, lines): |
|---|
| 46 | base.Result.__init__(self) |
|---|
| 47 | self._store_cpv(pkg) |
|---|
| 48 | self.lines = lines |
|---|
| 49 | |
|---|
| 50 | @property |
|---|
| 51 | def short_desc(self): |
|---|
| 52 | return "ebuild has whitespace in indentation on %s" % self.lines_str |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | class DoubleEmptyLine(base_whitespace): |
|---|
| 56 | |
|---|
| 57 | """unneeded blank lines are found""" |
|---|
| 58 | |
|---|
| 59 | __slots__ = ("category", "package", "version", "lines") |
|---|
| 60 | |
|---|
| 61 | def __init__(self, pkg, lines): |
|---|
| 62 | base.Result.__init__(self) |
|---|
| 63 | self._store_cpv(pkg) |
|---|
| 64 | self.lines = lines |
|---|
| 65 | |
|---|
| 66 | @property |
|---|
| 67 | def short_desc(self): |
|---|
| 68 | return "ebuild has unneeded empty %s" % self.lines_str |
|---|
| 69 | |
|---|
| 70 | |
|---|
| 71 | class TrailingEmptyLine(base.Result): |
|---|
| 72 | |
|---|
| 73 | """unneeded blank lines are found""" |
|---|
| 74 | |
|---|
| 75 | __slots__ = ("category", "package", "version") |
|---|
| 76 | |
|---|
| 77 | threshold = base.versioned_feed |
|---|
| 78 | |
|---|
| 79 | def __init__(self, pkg): |
|---|
| 80 | base.Result.__init__(self) |
|---|
| 81 | self._store_cpv(pkg) |
|---|
| 82 | |
|---|
| 83 | short_desc = "ebuild has trailing blank line(s)" |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | class NoFinalNewline(base.Result): |
|---|
| 87 | |
|---|
| 88 | """Ebuild's last line does not have a final newline.""" |
|---|
| 89 | |
|---|
| 90 | __slots__ = ("category", "package", "version") |
|---|
| 91 | |
|---|
| 92 | threshold = base.versioned_feed |
|---|
| 93 | |
|---|
| 94 | def __init__(self, pkg): |
|---|
| 95 | base.Result.__init__(self) |
|---|
| 96 | self._store_cpv(pkg) |
|---|
| 97 | |
|---|
| 98 | short_desc = "ebuild lacks an ending newline" |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | class WhitespaceCheck(base.Template): |
|---|
| 102 | |
|---|
| 103 | """checking ebuild for (useless) whitespaces""" |
|---|
| 104 | |
|---|
| 105 | feed_type = base.ebuild_feed |
|---|
| 106 | known_results = (WhitespaceFound, WrongIndentFound, DoubleEmptyLine, |
|---|
| 107 | TrailingEmptyLine, NoFinalNewline) |
|---|
| 108 | |
|---|
| 109 | def feed(self, entry, reporter): |
|---|
| 110 | pkg, lines = entry |
|---|
| 111 | lastlineempty = False |
|---|
| 112 | trailing = [] |
|---|
| 113 | leading = [] |
|---|
| 114 | indent = [] |
|---|
| 115 | double_empty = [] |
|---|
| 116 | |
|---|
| 117 | for lineno, line in enumerate(lines): |
|---|
| 118 | if line != '\n': |
|---|
| 119 | lastlineempty = False |
|---|
| 120 | if line[-2:-1] == ' ' or line[-2:-1] == '\t': |
|---|
| 121 | trailing.append(lineno + 1) |
|---|
| 122 | elif line[0] == ' ': |
|---|
| 123 | leading.append(lineno + 1) |
|---|
| 124 | if line.find("\t ") >= 0: |
|---|
| 125 | indent.append(lineno + 1) |
|---|
| 126 | elif lastlineempty: |
|---|
| 127 | double_empty.append(lineno + 1) |
|---|
| 128 | else: |
|---|
| 129 | lastlineempty = True |
|---|
| 130 | if trailing: |
|---|
| 131 | reporter.add_report( |
|---|
| 132 | WhitespaceFound(pkg, "trailing", trailing)) |
|---|
| 133 | if leading: |
|---|
| 134 | reporter.add_report( |
|---|
| 135 | WhitespaceFound(pkg, "leading", leading)) |
|---|
| 136 | if indent: |
|---|
| 137 | reporter.add_report(WrongIndentFound(pkg, indent)) |
|---|
| 138 | if double_empty: |
|---|
| 139 | reporter.add_report(DoubleEmptyLine(pkg, double_empty)) |
|---|
| 140 | if lastlineempty: |
|---|
| 141 | reporter.add_report(TrailingEmptyLine(pkg)) |
|---|
| 142 | |
|---|
| 143 | # Dealing with empty ebuilds is just paranoia |
|---|
| 144 | if lines and not lines[-1].endswith('\n'): |
|---|
| 145 | reporter.add_report(NoFinalNewline(pkg)) |
|---|