Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../proc/self/root/lib64/python3....
File: compileall.py
"""Module/script to byte-compile all .py files to .pyc files.
[0] Fix | Delete
[1] Fix | Delete
When called as a script with arguments, this compiles the directories
[2] Fix | Delete
given as arguments recursively; the -l option prevents it from
[3] Fix | Delete
recursing into directories.
[4] Fix | Delete
[5] Fix | Delete
Without arguments, if compiles all modules on sys.path, without
[6] Fix | Delete
recursing into subdirectories. (Even though it should do so for
[7] Fix | Delete
packages -- for now, you'll have to deal with packages separately.)
[8] Fix | Delete
[9] Fix | Delete
See module py_compile for details of the actual byte-compilation.
[10] Fix | Delete
"""
[11] Fix | Delete
import os
[12] Fix | Delete
import sys
[13] Fix | Delete
import importlib.util
[14] Fix | Delete
import py_compile
[15] Fix | Delete
import struct
[16] Fix | Delete
[17] Fix | Delete
from functools import partial
[18] Fix | Delete
[19] Fix | Delete
__all__ = ["compile_dir","compile_file","compile_path"]
[20] Fix | Delete
[21] Fix | Delete
def _walk_dir(dir, ddir=None, maxlevels=10, quiet=0):
[22] Fix | Delete
if quiet < 2 and isinstance(dir, os.PathLike):
[23] Fix | Delete
dir = os.fspath(dir)
[24] Fix | Delete
if not quiet:
[25] Fix | Delete
print('Listing {!r}...'.format(dir))
[26] Fix | Delete
try:
[27] Fix | Delete
names = os.listdir(dir)
[28] Fix | Delete
except OSError:
[29] Fix | Delete
if quiet < 2:
[30] Fix | Delete
print("Can't list {!r}".format(dir))
[31] Fix | Delete
names = []
[32] Fix | Delete
names.sort()
[33] Fix | Delete
for name in names:
[34] Fix | Delete
if name == '__pycache__':
[35] Fix | Delete
continue
[36] Fix | Delete
fullname = os.path.join(dir, name)
[37] Fix | Delete
if ddir is not None:
[38] Fix | Delete
dfile = os.path.join(ddir, name)
[39] Fix | Delete
else:
[40] Fix | Delete
dfile = None
[41] Fix | Delete
if not os.path.isdir(fullname):
[42] Fix | Delete
yield fullname
[43] Fix | Delete
elif (maxlevels > 0 and name != os.curdir and name != os.pardir and
[44] Fix | Delete
os.path.isdir(fullname) and not os.path.islink(fullname)):
[45] Fix | Delete
yield from _walk_dir(fullname, ddir=dfile,
[46] Fix | Delete
maxlevels=maxlevels - 1, quiet=quiet)
[47] Fix | Delete
[48] Fix | Delete
def compile_dir(dir, maxlevels=10, ddir=None, force=False, rx=None,
[49] Fix | Delete
quiet=0, legacy=False, optimize=-1, workers=1):
[50] Fix | Delete
"""Byte-compile all modules in the given directory tree.
[51] Fix | Delete
[52] Fix | Delete
Arguments (only dir is required):
[53] Fix | Delete
[54] Fix | Delete
dir: the directory to byte-compile
[55] Fix | Delete
maxlevels: maximum recursion level (default 10)
[56] Fix | Delete
ddir: the directory that will be prepended to the path to the
[57] Fix | Delete
file as it is compiled into each byte-code file.
[58] Fix | Delete
force: if True, force compilation, even if timestamps are up-to-date
[59] Fix | Delete
quiet: full output with False or 0, errors only with 1,
[60] Fix | Delete
no output with 2
[61] Fix | Delete
legacy: if True, produce legacy pyc paths instead of PEP 3147 paths
[62] Fix | Delete
optimize: optimization level or -1 for level of the interpreter
[63] Fix | Delete
workers: maximum number of parallel workers
[64] Fix | Delete
"""
[65] Fix | Delete
ProcessPoolExecutor = None
[66] Fix | Delete
if workers is not None:
[67] Fix | Delete
if workers < 0:
[68] Fix | Delete
raise ValueError('workers must be greater or equal to 0')
[69] Fix | Delete
elif workers != 1:
[70] Fix | Delete
try:
[71] Fix | Delete
# Only import when needed, as low resource platforms may
[72] Fix | Delete
# fail to import it
[73] Fix | Delete
from concurrent.futures import ProcessPoolExecutor
[74] Fix | Delete
except ImportError:
[75] Fix | Delete
workers = 1
[76] Fix | Delete
files = _walk_dir(dir, quiet=quiet, maxlevels=maxlevels,
[77] Fix | Delete
ddir=ddir)
[78] Fix | Delete
success = True
[79] Fix | Delete
if workers is not None and workers != 1 and ProcessPoolExecutor is not None:
[80] Fix | Delete
workers = workers or None
[81] Fix | Delete
with ProcessPoolExecutor(max_workers=workers) as executor:
[82] Fix | Delete
results = executor.map(partial(compile_file,
[83] Fix | Delete
ddir=ddir, force=force,
[84] Fix | Delete
rx=rx, quiet=quiet,
[85] Fix | Delete
legacy=legacy,
[86] Fix | Delete
optimize=optimize),
[87] Fix | Delete
files)
[88] Fix | Delete
success = min(results, default=True)
[89] Fix | Delete
else:
[90] Fix | Delete
for file in files:
[91] Fix | Delete
if not compile_file(file, ddir, force, rx, quiet,
[92] Fix | Delete
legacy, optimize):
[93] Fix | Delete
success = False
[94] Fix | Delete
return success
[95] Fix | Delete
[96] Fix | Delete
def compile_file(fullname, ddir=None, force=False, rx=None, quiet=0,
[97] Fix | Delete
legacy=False, optimize=-1):
[98] Fix | Delete
"""Byte-compile one file.
[99] Fix | Delete
[100] Fix | Delete
Arguments (only fullname is required):
[101] Fix | Delete
[102] Fix | Delete
fullname: the file to byte-compile
[103] Fix | Delete
ddir: if given, the directory name compiled in to the
[104] Fix | Delete
byte-code file.
[105] Fix | Delete
force: if True, force compilation, even if timestamps are up-to-date
[106] Fix | Delete
quiet: full output with False or 0, errors only with 1,
[107] Fix | Delete
no output with 2
[108] Fix | Delete
legacy: if True, produce legacy pyc paths instead of PEP 3147 paths
[109] Fix | Delete
optimize: optimization level or -1 for level of the interpreter
[110] Fix | Delete
"""
[111] Fix | Delete
success = True
[112] Fix | Delete
if quiet < 2 and isinstance(fullname, os.PathLike):
[113] Fix | Delete
fullname = os.fspath(fullname)
[114] Fix | Delete
name = os.path.basename(fullname)
[115] Fix | Delete
if ddir is not None:
[116] Fix | Delete
dfile = os.path.join(ddir, name)
[117] Fix | Delete
else:
[118] Fix | Delete
dfile = None
[119] Fix | Delete
if rx is not None:
[120] Fix | Delete
mo = rx.search(fullname)
[121] Fix | Delete
if mo:
[122] Fix | Delete
return success
[123] Fix | Delete
if os.path.isfile(fullname):
[124] Fix | Delete
if legacy:
[125] Fix | Delete
cfile = fullname + 'c'
[126] Fix | Delete
else:
[127] Fix | Delete
if optimize >= 0:
[128] Fix | Delete
opt = optimize if optimize >= 1 else ''
[129] Fix | Delete
cfile = importlib.util.cache_from_source(
[130] Fix | Delete
fullname, optimization=opt)
[131] Fix | Delete
else:
[132] Fix | Delete
cfile = importlib.util.cache_from_source(fullname)
[133] Fix | Delete
cache_dir = os.path.dirname(cfile)
[134] Fix | Delete
head, tail = name[:-3], name[-3:]
[135] Fix | Delete
if tail == '.py':
[136] Fix | Delete
if not force:
[137] Fix | Delete
try:
[138] Fix | Delete
mtime = int(os.stat(fullname).st_mtime)
[139] Fix | Delete
expect = struct.pack('<4sl', importlib.util.MAGIC_NUMBER,
[140] Fix | Delete
mtime)
[141] Fix | Delete
with open(cfile, 'rb') as chandle:
[142] Fix | Delete
actual = chandle.read(8)
[143] Fix | Delete
if expect == actual:
[144] Fix | Delete
return success
[145] Fix | Delete
except OSError:
[146] Fix | Delete
pass
[147] Fix | Delete
if not quiet:
[148] Fix | Delete
print('Compiling {!r}...'.format(fullname))
[149] Fix | Delete
try:
[150] Fix | Delete
ok = py_compile.compile(fullname, cfile, dfile, True,
[151] Fix | Delete
optimize=optimize)
[152] Fix | Delete
except py_compile.PyCompileError as err:
[153] Fix | Delete
success = False
[154] Fix | Delete
if quiet >= 2:
[155] Fix | Delete
return success
[156] Fix | Delete
elif quiet:
[157] Fix | Delete
print('*** Error compiling {!r}...'.format(fullname))
[158] Fix | Delete
else:
[159] Fix | Delete
print('*** ', end='')
[160] Fix | Delete
# escape non-printable characters in msg
[161] Fix | Delete
msg = err.msg.encode(sys.stdout.encoding,
[162] Fix | Delete
errors='backslashreplace')
[163] Fix | Delete
msg = msg.decode(sys.stdout.encoding)
[164] Fix | Delete
print(msg)
[165] Fix | Delete
except (SyntaxError, UnicodeError, OSError) as e:
[166] Fix | Delete
success = False
[167] Fix | Delete
if quiet >= 2:
[168] Fix | Delete
return success
[169] Fix | Delete
elif quiet:
[170] Fix | Delete
print('*** Error compiling {!r}...'.format(fullname))
[171] Fix | Delete
else:
[172] Fix | Delete
print('*** ', end='')
[173] Fix | Delete
print(e.__class__.__name__ + ':', e)
[174] Fix | Delete
else:
[175] Fix | Delete
if ok == 0:
[176] Fix | Delete
success = False
[177] Fix | Delete
return success
[178] Fix | Delete
[179] Fix | Delete
def compile_path(skip_curdir=1, maxlevels=0, force=False, quiet=0,
[180] Fix | Delete
legacy=False, optimize=-1):
[181] Fix | Delete
"""Byte-compile all module on sys.path.
[182] Fix | Delete
[183] Fix | Delete
Arguments (all optional):
[184] Fix | Delete
[185] Fix | Delete
skip_curdir: if true, skip current directory (default True)
[186] Fix | Delete
maxlevels: max recursion level (default 0)
[187] Fix | Delete
force: as for compile_dir() (default False)
[188] Fix | Delete
quiet: as for compile_dir() (default 0)
[189] Fix | Delete
legacy: as for compile_dir() (default False)
[190] Fix | Delete
optimize: as for compile_dir() (default -1)
[191] Fix | Delete
"""
[192] Fix | Delete
success = True
[193] Fix | Delete
for dir in sys.path:
[194] Fix | Delete
if (not dir or dir == os.curdir) and skip_curdir:
[195] Fix | Delete
if quiet < 2:
[196] Fix | Delete
print('Skipping current directory')
[197] Fix | Delete
else:
[198] Fix | Delete
success = success and compile_dir(dir, maxlevels, None,
[199] Fix | Delete
force, quiet=quiet,
[200] Fix | Delete
legacy=legacy, optimize=optimize)
[201] Fix | Delete
return success
[202] Fix | Delete
[203] Fix | Delete
[204] Fix | Delete
def main():
[205] Fix | Delete
"""Script main program."""
[206] Fix | Delete
import argparse
[207] Fix | Delete
[208] Fix | Delete
parser = argparse.ArgumentParser(
[209] Fix | Delete
description='Utilities to support installing Python libraries.')
[210] Fix | Delete
parser.add_argument('-l', action='store_const', const=0,
[211] Fix | Delete
default=10, dest='maxlevels',
[212] Fix | Delete
help="don't recurse into subdirectories")
[213] Fix | Delete
parser.add_argument('-r', type=int, dest='recursion',
[214] Fix | Delete
help=('control the maximum recursion level. '
[215] Fix | Delete
'if `-l` and `-r` options are specified, '
[216] Fix | Delete
'then `-r` takes precedence.'))
[217] Fix | Delete
parser.add_argument('-f', action='store_true', dest='force',
[218] Fix | Delete
help='force rebuild even if timestamps are up to date')
[219] Fix | Delete
parser.add_argument('-q', action='count', dest='quiet', default=0,
[220] Fix | Delete
help='output only error messages; -qq will suppress '
[221] Fix | Delete
'the error messages as well.')
[222] Fix | Delete
parser.add_argument('-b', action='store_true', dest='legacy',
[223] Fix | Delete
help='use legacy (pre-PEP3147) compiled file locations')
[224] Fix | Delete
parser.add_argument('-d', metavar='DESTDIR', dest='ddir', default=None,
[225] Fix | Delete
help=('directory to prepend to file paths for use in '
[226] Fix | Delete
'compile-time tracebacks and in runtime '
[227] Fix | Delete
'tracebacks in cases where the source file is '
[228] Fix | Delete
'unavailable'))
[229] Fix | Delete
parser.add_argument('-x', metavar='REGEXP', dest='rx', default=None,
[230] Fix | Delete
help=('skip files matching the regular expression; '
[231] Fix | Delete
'the regexp is searched for in the full path '
[232] Fix | Delete
'of each file considered for compilation'))
[233] Fix | Delete
parser.add_argument('-i', metavar='FILE', dest='flist',
[234] Fix | Delete
help=('add all the files and directories listed in '
[235] Fix | Delete
'FILE to the list considered for compilation; '
[236] Fix | Delete
'if "-", names are read from stdin'))
[237] Fix | Delete
parser.add_argument('compile_dest', metavar='FILE|DIR', nargs='*',
[238] Fix | Delete
help=('zero or more file and directory names '
[239] Fix | Delete
'to compile; if no arguments given, defaults '
[240] Fix | Delete
'to the equivalent of -l sys.path'))
[241] Fix | Delete
parser.add_argument('-j', '--workers', default=1,
[242] Fix | Delete
type=int, help='Run compileall concurrently')
[243] Fix | Delete
[244] Fix | Delete
args = parser.parse_args()
[245] Fix | Delete
compile_dests = args.compile_dest
[246] Fix | Delete
[247] Fix | Delete
if args.rx:
[248] Fix | Delete
import re
[249] Fix | Delete
args.rx = re.compile(args.rx)
[250] Fix | Delete
[251] Fix | Delete
[252] Fix | Delete
if args.recursion is not None:
[253] Fix | Delete
maxlevels = args.recursion
[254] Fix | Delete
else:
[255] Fix | Delete
maxlevels = args.maxlevels
[256] Fix | Delete
[257] Fix | Delete
# if flist is provided then load it
[258] Fix | Delete
if args.flist:
[259] Fix | Delete
try:
[260] Fix | Delete
with (sys.stdin if args.flist=='-' else open(args.flist)) as f:
[261] Fix | Delete
for line in f:
[262] Fix | Delete
compile_dests.append(line.strip())
[263] Fix | Delete
except OSError:
[264] Fix | Delete
if args.quiet < 2:
[265] Fix | Delete
print("Error reading file list {}".format(args.flist))
[266] Fix | Delete
return False
[267] Fix | Delete
[268] Fix | Delete
if args.workers is not None:
[269] Fix | Delete
args.workers = args.workers or None
[270] Fix | Delete
[271] Fix | Delete
success = True
[272] Fix | Delete
try:
[273] Fix | Delete
if compile_dests:
[274] Fix | Delete
for dest in compile_dests:
[275] Fix | Delete
if os.path.isfile(dest):
[276] Fix | Delete
if not compile_file(dest, args.ddir, args.force, args.rx,
[277] Fix | Delete
args.quiet, args.legacy):
[278] Fix | Delete
success = False
[279] Fix | Delete
else:
[280] Fix | Delete
if not compile_dir(dest, maxlevels, args.ddir,
[281] Fix | Delete
args.force, args.rx, args.quiet,
[282] Fix | Delete
args.legacy, workers=args.workers):
[283] Fix | Delete
success = False
[284] Fix | Delete
return success
[285] Fix | Delete
else:
[286] Fix | Delete
return compile_path(legacy=args.legacy, force=args.force,
[287] Fix | Delete
quiet=args.quiet)
[288] Fix | Delete
except KeyboardInterrupt:
[289] Fix | Delete
if args.quiet < 2:
[290] Fix | Delete
print("\n[interrupted]")
[291] Fix | Delete
return False
[292] Fix | Delete
return True
[293] Fix | Delete
[294] Fix | Delete
[295] Fix | Delete
if __name__ == '__main__':
[296] Fix | Delete
exit_status = int(not main())
[297] Fix | Delete
sys.exit(exit_status)
[298] Fix | Delete
[299] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function