| 1 |
#!/usr/bin/pypy3 |
| 2 |
|
| 3 |
import argparse |
| 4 |
import itertools |
| 5 |
import os |
| 6 |
import re |
| 7 |
import py_compile |
| 8 |
import subprocess |
| 9 |
import sys |
| 10 |
|
| 11 |
|
| 12 |
def abort(message): |
| 13 |
print(message, file=sys.stderr) |
| 14 |
sys.exit(1) |
| 15 |
|
| 16 |
|
| 17 |
def package_modules(package): |
| 18 |
"""Iterate through all python modules in an installed Debian package""" |
| 19 |
env = os.environ.copy() |
| 20 |
env['LC_ALL'] = 'C.UTF-8' |
| 21 |
p = subprocess.Popen(('dpkg', '-L', package), stdout=subprocess.PIPE, |
| 22 |
stderr=subprocess.PIPE, env=env) |
| 23 |
files, stderr = p.communicate() |
| 24 |
if p.returncode != 0: |
| 25 |
abort('Unable to list files in %s. Is it installed?' % package) |
| 26 |
|
| 27 |
for fn in files.decode('utf8').splitlines(): |
| 28 |
if fn.endswith('.py'): |
| 29 |
if fn.startswith('/usr/share/doc/'): |
| 30 |
continue |
| 31 |
yield fn |
| 32 |
|
| 33 |
|
| 34 |
def find_modules(root): |
| 35 |
"""Iterate through all python modules in directory tree root""" |
| 36 |
if os.path.isfile(root): |
| 37 |
yield root |
| 38 |
return |
| 39 |
|
| 40 |
for dirpath, dirnames, filenames in os.walk(root): |
| 41 |
for fn in filenames: |
| 42 |
if fn.endswith('.py'): |
| 43 |
yield os.path.join(dirpath, fn) |
| 44 |
|
| 45 |
|
| 46 |
# BCEP support, from py3compile |
| 47 |
def get_exclude_patterns_from_dir(name='/usr/share/python3/bcep/'): |
| 48 |
"""Return patterns for files that shouldn't be bytecompiled.""" |
| 49 |
if not os.path.isdir(name): |
| 50 |
return |
| 51 |
|
| 52 |
for fn in os.listdir(name): |
| 53 |
if fn.startswith('.'): |
| 54 |
continue |
| 55 |
with open(os.path.join(name, fn), 'r', encoding='utf-8') as lines: |
| 56 |
for line in lines: |
| 57 |
if line.startswith('#'): |
| 58 |
continue |
| 59 |
line = line.rstrip('\n') |
| 60 |
|
| 61 |
type_, vrange, dname = line.split('|', 2) |
| 62 |
if not within_vrange(vrange): |
| 63 |
continue |
| 64 |
|
| 65 |
try: |
| 66 |
dname, pattern = dname.split('|', 1) |
| 67 |
except ValueError: |
| 68 |
pattern = '.*' |
| 69 |
|
| 70 |
if type_ != 'file' and not dname.endswith('/'): |
| 71 |
dname += '/' |
| 72 |
|
| 73 |
if pattern: |
| 74 |
try: |
| 75 |
pattern = re.compile(pattern) |
| 76 |
except re.error: |
| 77 |
print('skipping invalid pattern in file {}, line: {}' |
| 78 |
.format(fn, line), file=sys.stderr) |
| 79 |
continue |
| 80 |
|
| 81 |
yield type_, dname, pattern |
| 82 |
|
| 83 |
|
| 84 |
def within_vrange(vrange): |
| 85 |
"""Is our interpreter's cpython compatibility level within vrange?""" |
| 86 |
min_, sep, max_ = vrange.partition('-') |
| 87 |
if min_: |
| 88 |
min_ = tuple(map(int, min_.split('.'))) |
| 89 |
if max_: |
| 90 |
max_ = tuple(map(int, max_.split('.'))) |
| 91 |
version = sys.version_info[:2] |
| 92 |
|
| 93 |
if not min_ and not max_: |
| 94 |
return True |
| 95 |
|
| 96 |
if not sep: |
| 97 |
return version == min_ |
| 98 |
|
| 99 |
if max_ and version >= max_: |
| 100 |
return False |
| 101 |
|
| 102 |
if min_ and min_ > version: |
| 103 |
return False |
| 104 |
|
| 105 |
return True |
| 106 |
|
| 107 |
|
| 108 |
def main(): |
| 109 |
parser = argparse.ArgumentParser( |
| 110 |
description='Byte-compile Python source files in a package, for PyPy') |
| 111 |
parser.add_argument('-p', '--package', metavar='PACKAGE', |
| 112 |
action='append', default=[], |
| 113 |
help='Debian package to byte-compile ' |
| 114 |
'(may be specified multiple times)') |
| 115 |
parser.add_argument('directory', nargs='*', |
| 116 |
help='Directory tree (or file) to byte-compile') |
| 117 |
parser.add_argument('-X', '--exclude', metavar='REGEXPR', |
| 118 |
action='append', default=[], type=re.compile, |
| 119 |
help='Exclude items that match given REGEXPR ' |
| 120 |
'(may be specified multiple times)') |
| 121 |
parser.add_argument('-v', '--verbose', action='store_true', |
| 122 |
help='Be more verbose') |
| 123 |
parser.add_argument('-q', '--quiet', action='store_true', |
| 124 |
help='Be quiet') |
| 125 |
parser.add_argument('-V', metavar='VRANGE', dest='vrange', |
| 126 |
help='Only byte-compile modules if the cPython ' |
| 127 |
'compatibility version falls within the specified ' |
| 128 |
'range.') |
| 129 |
parser.add_argument('-O', action='store_true', dest='pyo', |
| 130 |
help=argparse.SUPPRESS) |
| 131 |
parser.add_argument('--bcep-path', default='/usr/share/python3/bcep/', |
| 132 |
help=argparse.SUPPRESS) |
| 133 |
args = parser.parse_args() |
| 134 |
|
| 135 |
if not (args.package or args.directory): |
| 136 |
parser.error('Either a package or a directory must be specified') |
| 137 |
if args.quiet and args.verbose: |
| 138 |
parser.error('--quiet and --verbose cannot both be specified') |
| 139 |
|
| 140 |
if args.vrange and not within_vrange(args.vrange): |
| 141 |
return |
| 142 |
|
| 143 |
if args.pyo: |
| 144 |
print('-O is ignored in pypy3compile', file=sys.stderr) |
| 145 |
|
| 146 |
modules_p = set(itertools.chain(*( |
| 147 |
package_modules(package) for package in args.package))) |
| 148 |
modules_d = set(itertools.chain(*( |
| 149 |
find_modules(dir_) for dir_ in args.directory))) |
| 150 |
|
| 151 |
if args.package and args.directory: |
| 152 |
modules = modules_d & modules_p |
| 153 |
else: |
| 154 |
modules = modules_d | modules_p |
| 155 |
|
| 156 |
modules = filter(lambda module: not any(pattern.match(module) |
| 157 |
for pattern in args.exclude), |
| 158 |
modules) |
| 159 |
|
| 160 |
bcep_excludes = list(get_exclude_patterns_from_dir(args.bcep_path)) |
| 161 |
|
| 162 |
for module in modules: |
| 163 |
skip = False |
| 164 |
for pattern in args.exclude: |
| 165 |
if pattern.match(module): |
| 166 |
skip = True |
| 167 |
for type_, dname, pattern in bcep_excludes: |
| 168 |
if not module.startswith(dname): |
| 169 |
continue |
| 170 |
if type_ == 'dir': |
| 171 |
skip = True |
| 172 |
if type_ == 'file' and module == dname: |
| 173 |
skip = True |
| 174 |
if type_ == 're': |
| 175 |
relative_path = module[len(dname):] |
| 176 |
if pattern.match(module) or pattern.match(relative_path): |
| 177 |
skip = True |
| 178 |
if skip: |
| 179 |
continue |
| 180 |
if args.verbose: |
| 181 |
print('Byte-compiling %s' % module) |
| 182 |
try: |
| 183 |
py_compile.compile(module, doraise=True) |
| 184 |
except py_compile.PyCompileError as e: |
| 185 |
if not args.quiet: |
| 186 |
print('Failed to byte-compile %s: %s' % (module, e.msg), |
| 187 |
file=sys.stderr) |
| 188 |
|
| 189 |
|
| 190 |
if __name__ == '__main__': |
| 191 |
main() |
| 192 |
|
| 193 |
# vim: ft=python |