| 1 |
#!/usr/bin/env python |
| 2 |
""" |
| 3 |
Checker for file headers |
| 4 |
~~~~~~~~~~~~~~~~~~~~~~~~ |
| 5 |
|
| 6 |
Make sure each Python file has a correct file header |
| 7 |
including copyright and license information. |
| 8 |
|
| 9 |
:copyright: Copyright 2006-present by the Pygments team, see AUTHORS. |
| 10 |
:license: BSD, see LICENSE for details. |
| 11 |
""" |
| 12 |
|
| 13 |
import io |
| 14 |
import os |
| 15 |
import re |
| 16 |
import sys |
| 17 |
import getopt |
| 18 |
from os.path import join, splitext, abspath |
| 19 |
|
| 20 |
|
| 21 |
checkers = {} |
| 22 |
|
| 23 |
|
| 24 |
def checker(*suffixes, **kwds): |
| 25 |
only_pkg = kwds.pop('only_pkg', False) |
| 26 |
|
| 27 |
def deco(func): |
| 28 |
for suffix in suffixes: |
| 29 |
checkers.setdefault(suffix, []).append(func) |
| 30 |
func.only_pkg = only_pkg |
| 31 |
return func |
| 32 |
return deco |
| 33 |
|
| 34 |
|
| 35 |
name_mail_re = r'[\w ]+(<.*?>)?' |
| 36 |
copyright_re = re.compile(r'^ :copyright: Copyright 2006-present by ' |
| 37 |
r'the Pygments team, see AUTHORS\.$') |
| 38 |
copyright_2_re = re.compile(rf'^ {name_mail_re}(, {name_mail_re})*[,.]$') |
| 39 |
is_const_re = re.compile(r'if.*?==\s+(None|False|True)\b') |
| 40 |
|
| 41 |
misspellings = ["developement", "adress", "verificate", # ALLOW-MISSPELLING |
| 42 |
"informations", "unlexer"] # ALLOW-MISSPELLING |
| 43 |
|
| 44 |
|
| 45 |
@checker('.py') |
| 46 |
def check_syntax(fn, lines): |
| 47 |
if not lines: |
| 48 |
yield 0, "empty file" |
| 49 |
return |
| 50 |
if '#!/' in lines[0]: |
| 51 |
lines = lines[1:] |
| 52 |
try: |
| 53 |
compile('\n'.join(lines), fn, "exec") |
| 54 |
except SyntaxError as err: |
| 55 |
yield 0, f"not compilable: {err}" |
| 56 |
|
| 57 |
|
| 58 |
@checker('.py') |
| 59 |
def check_style_and_encoding(fn, lines): |
| 60 |
for lno, line in enumerate(lines): |
| 61 |
if is_const_re.search(line): |
| 62 |
yield lno+1, 'using == None/True/False' |
| 63 |
|
| 64 |
|
| 65 |
@checker('.py', only_pkg=True) |
| 66 |
def check_fileheader(fn, lines): |
| 67 |
# line number correction |
| 68 |
c = 1 |
| 69 |
if lines[0:1] == ['#!/usr/bin/env python']: |
| 70 |
lines = lines[1:] |
| 71 |
c = 2 |
| 72 |
|
| 73 |
llist = [] |
| 74 |
docopen = False |
| 75 |
for lno, line in enumerate(lines): |
| 76 |
llist.append(line) |
| 77 |
if lno == 0: |
| 78 |
if line != '"""' and line != 'r"""': |
| 79 |
yield 2, f'missing docstring begin ("""), found {line!r}' |
| 80 |
else: |
| 81 |
docopen = True |
| 82 |
elif docopen: |
| 83 |
if line == '"""': |
| 84 |
# end of docstring |
| 85 |
if lno <= 3: |
| 86 |
yield lno+c, "missing module name in docstring" |
| 87 |
break |
| 88 |
|
| 89 |
if line != "" and line[:4] != ' ' and docopen: |
| 90 |
yield lno+c, "missing correct docstring indentation" |
| 91 |
|
| 92 |
if lno == 1: |
| 93 |
# if not in package, don't check the module name |
| 94 |
modname = fn[:-3].replace('/', '.').replace('.__init__', '') |
| 95 |
while modname: |
| 96 |
if line.lower()[4:] == modname: |
| 97 |
break |
| 98 |
modname = '.'.join(modname.split('.')[1:]) |
| 99 |
else: |
| 100 |
yield 3, "wrong module name in docstring heading" |
| 101 |
modnamelen = len(line.strip()) |
| 102 |
elif lno == 2: |
| 103 |
if line.strip() != modnamelen * "~": |
| 104 |
yield 4, "wrong module name underline, should be ~~~...~" |
| 105 |
|
| 106 |
else: |
| 107 |
yield 0, "missing end and/or start of docstring..." |
| 108 |
|
| 109 |
# check for copyright and license fields |
| 110 |
license = llist[-2:-1] |
| 111 |
if license != [" :license: BSD, see LICENSE for details."]: |
| 112 |
yield 0, "no correct license info" |
| 113 |
|
| 114 |
ci = -3 |
| 115 |
copyright = llist[ci:ci+1] |
| 116 |
while copyright and copyright_2_re.match(copyright[0]): |
| 117 |
ci -= 1 |
| 118 |
copyright = llist[ci:ci+1] |
| 119 |
if not copyright or not copyright_re.match(copyright[0]): |
| 120 |
yield 0, "no correct copyright info" |
| 121 |
|
| 122 |
|
| 123 |
def main(argv): |
| 124 |
try: |
| 125 |
gopts, args = getopt.getopt(argv[1:], "vi:") |
| 126 |
except getopt.GetoptError: |
| 127 |
print(f"Usage: {argv[0]} [-v] [-i ignorepath]* [path]") |
| 128 |
return 2 |
| 129 |
opts = {} |
| 130 |
for opt, val in gopts: |
| 131 |
if opt == '-i': |
| 132 |
val = abspath(val) |
| 133 |
opts.setdefault(opt, []).append(val) |
| 134 |
|
| 135 |
if len(args) == 0: |
| 136 |
path = '.' |
| 137 |
elif len(args) == 1: |
| 138 |
path = args[0] |
| 139 |
else: |
| 140 |
print(f"Usage: {argv[0]} [-v] [-i ignorepath]* [path]") |
| 141 |
return 2 |
| 142 |
|
| 143 |
verbose = '-v' in opts |
| 144 |
|
| 145 |
num = 0 |
| 146 |
out = io.StringIO() |
| 147 |
|
| 148 |
for root, dirs, files in os.walk(path): |
| 149 |
for excl in ['.tox', '.git', '.venv', 'examplefiles']: |
| 150 |
if excl in dirs: |
| 151 |
dirs.remove(excl) |
| 152 |
if '-i' in opts and abspath(root) in opts['-i']: |
| 153 |
del dirs[:] |
| 154 |
continue |
| 155 |
# XXX: awkward: for the Makefile call: don't check non-package |
| 156 |
# files for file headers |
| 157 |
in_pygments_pkg = root.startswith('./pygments') |
| 158 |
for fn in files: |
| 159 |
|
| 160 |
fn = join(root, fn) |
| 161 |
if fn[:2] == './': |
| 162 |
fn = fn[2:] |
| 163 |
|
| 164 |
if '-i' in opts and abspath(fn) in opts['-i']: |
| 165 |
continue |
| 166 |
|
| 167 |
ext = splitext(fn)[1] |
| 168 |
checkerlist = checkers.get(ext, None) |
| 169 |
if not checkerlist: |
| 170 |
continue |
| 171 |
|
| 172 |
if verbose: |
| 173 |
print(f"Checking {fn}...") |
| 174 |
|
| 175 |
try: |
| 176 |
with open(fn, 'rb') as f: |
| 177 |
lines = f.read().decode('utf-8').splitlines() |
| 178 |
except OSError as err: |
| 179 |
print(f"{fn}: cannot open: {err}") |
| 180 |
num += 1 |
| 181 |
continue |
| 182 |
except UnicodeDecodeError as err: |
| 183 |
print(f"{fn}: error decoding: {err}") |
| 184 |
num += 1 |
| 185 |
continue |
| 186 |
|
| 187 |
for checker in checkerlist: |
| 188 |
if not in_pygments_pkg and checker.only_pkg: |
| 189 |
continue |
| 190 |
for lno, msg in checker(fn, lines): |
| 191 |
print('%s:%d: %s' % (fn, lno, msg), file=out) |
| 192 |
num += 1 |
| 193 |
if verbose: |
| 194 |
print() |
| 195 |
if num == 0: |
| 196 |
print("No errors found.") |
| 197 |
else: |
| 198 |
print(out.getvalue().rstrip('\n')) |
| 199 |
print("%d error%s found." % (num, num > 1 and "s" or "")) |
| 200 |
return int(num > 0) |
| 201 |
|
| 202 |
|
| 203 |
if __name__ == '__main__': |
| 204 |
sys.exit(main(sys.argv)) |