Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../usr/lib64/python3....
File: zipapp.py
import contextlib
[0] Fix | Delete
import os
[1] Fix | Delete
import pathlib
[2] Fix | Delete
import shutil
[3] Fix | Delete
import stat
[4] Fix | Delete
import sys
[5] Fix | Delete
import zipfile
[6] Fix | Delete
[7] Fix | Delete
__all__ = ['ZipAppError', 'create_archive', 'get_interpreter']
[8] Fix | Delete
[9] Fix | Delete
[10] Fix | Delete
# The __main__.py used if the users specifies "-m module:fn".
[11] Fix | Delete
# Note that this will always be written as UTF-8 (module and
[12] Fix | Delete
# function names can be non-ASCII in Python 3).
[13] Fix | Delete
# We add a coding cookie even though UTF-8 is the default in Python 3
[14] Fix | Delete
# because the resulting archive may be intended to be run under Python 2.
[15] Fix | Delete
MAIN_TEMPLATE = """\
[16] Fix | Delete
# -*- coding: utf-8 -*-
[17] Fix | Delete
import {module}
[18] Fix | Delete
{module}.{fn}()
[19] Fix | Delete
"""
[20] Fix | Delete
[21] Fix | Delete
[22] Fix | Delete
# The Windows launcher defaults to UTF-8 when parsing shebang lines if the
[23] Fix | Delete
# file has no BOM. So use UTF-8 on Windows.
[24] Fix | Delete
# On Unix, use the filesystem encoding.
[25] Fix | Delete
if sys.platform.startswith('win'):
[26] Fix | Delete
shebang_encoding = 'utf-8'
[27] Fix | Delete
else:
[28] Fix | Delete
shebang_encoding = sys.getfilesystemencoding()
[29] Fix | Delete
[30] Fix | Delete
[31] Fix | Delete
class ZipAppError(ValueError):
[32] Fix | Delete
pass
[33] Fix | Delete
[34] Fix | Delete
[35] Fix | Delete
@contextlib.contextmanager
[36] Fix | Delete
def _maybe_open(archive, mode):
[37] Fix | Delete
if isinstance(archive, (str, os.PathLike)):
[38] Fix | Delete
with open(archive, mode) as f:
[39] Fix | Delete
yield f
[40] Fix | Delete
else:
[41] Fix | Delete
yield archive
[42] Fix | Delete
[43] Fix | Delete
[44] Fix | Delete
def _write_file_prefix(f, interpreter):
[45] Fix | Delete
"""Write a shebang line."""
[46] Fix | Delete
if interpreter:
[47] Fix | Delete
shebang = b'#!' + interpreter.encode(shebang_encoding) + b'\n'
[48] Fix | Delete
f.write(shebang)
[49] Fix | Delete
[50] Fix | Delete
[51] Fix | Delete
def _copy_archive(archive, new_archive, interpreter=None):
[52] Fix | Delete
"""Copy an application archive, modifying the shebang line."""
[53] Fix | Delete
with _maybe_open(archive, 'rb') as src:
[54] Fix | Delete
# Skip the shebang line from the source.
[55] Fix | Delete
# Read 2 bytes of the source and check if they are #!.
[56] Fix | Delete
first_2 = src.read(2)
[57] Fix | Delete
if first_2 == b'#!':
[58] Fix | Delete
# Discard the initial 2 bytes and the rest of the shebang line.
[59] Fix | Delete
first_2 = b''
[60] Fix | Delete
src.readline()
[61] Fix | Delete
[62] Fix | Delete
with _maybe_open(new_archive, 'wb') as dst:
[63] Fix | Delete
_write_file_prefix(dst, interpreter)
[64] Fix | Delete
# If there was no shebang, "first_2" contains the first 2 bytes
[65] Fix | Delete
# of the source file, so write them before copying the rest
[66] Fix | Delete
# of the file.
[67] Fix | Delete
dst.write(first_2)
[68] Fix | Delete
shutil.copyfileobj(src, dst)
[69] Fix | Delete
[70] Fix | Delete
if interpreter and isinstance(new_archive, str):
[71] Fix | Delete
os.chmod(new_archive, os.stat(new_archive).st_mode | stat.S_IEXEC)
[72] Fix | Delete
[73] Fix | Delete
[74] Fix | Delete
def create_archive(source, target=None, interpreter=None, main=None,
[75] Fix | Delete
filter=None, compressed=False):
[76] Fix | Delete
"""Create an application archive from SOURCE.
[77] Fix | Delete
[78] Fix | Delete
The SOURCE can be the name of a directory, or a filename or a file-like
[79] Fix | Delete
object referring to an existing archive.
[80] Fix | Delete
[81] Fix | Delete
The content of SOURCE is packed into an application archive in TARGET,
[82] Fix | Delete
which can be a filename or a file-like object. If SOURCE is a directory,
[83] Fix | Delete
TARGET can be omitted and will default to the name of SOURCE with .pyz
[84] Fix | Delete
appended.
[85] Fix | Delete
[86] Fix | Delete
The created application archive will have a shebang line specifying
[87] Fix | Delete
that it should run with INTERPRETER (there will be no shebang line if
[88] Fix | Delete
INTERPRETER is None), and a __main__.py which runs MAIN (if MAIN is
[89] Fix | Delete
not specified, an existing __main__.py will be used). It is an error
[90] Fix | Delete
to specify MAIN for anything other than a directory source with no
[91] Fix | Delete
__main__.py, and it is an error to omit MAIN if the directory has no
[92] Fix | Delete
__main__.py.
[93] Fix | Delete
"""
[94] Fix | Delete
# Are we copying an existing archive?
[95] Fix | Delete
source_is_file = False
[96] Fix | Delete
if hasattr(source, 'read') and hasattr(source, 'readline'):
[97] Fix | Delete
source_is_file = True
[98] Fix | Delete
else:
[99] Fix | Delete
source = pathlib.Path(source)
[100] Fix | Delete
if source.is_file():
[101] Fix | Delete
source_is_file = True
[102] Fix | Delete
[103] Fix | Delete
if source_is_file:
[104] Fix | Delete
_copy_archive(source, target, interpreter)
[105] Fix | Delete
return
[106] Fix | Delete
[107] Fix | Delete
# We are creating a new archive from a directory.
[108] Fix | Delete
if not source.exists():
[109] Fix | Delete
raise ZipAppError("Source does not exist")
[110] Fix | Delete
has_main = (source / '__main__.py').is_file()
[111] Fix | Delete
if main and has_main:
[112] Fix | Delete
raise ZipAppError(
[113] Fix | Delete
"Cannot specify entry point if the source has __main__.py")
[114] Fix | Delete
if not (main or has_main):
[115] Fix | Delete
raise ZipAppError("Archive has no entry point")
[116] Fix | Delete
[117] Fix | Delete
main_py = None
[118] Fix | Delete
if main:
[119] Fix | Delete
# Check that main has the right format.
[120] Fix | Delete
mod, sep, fn = main.partition(':')
[121] Fix | Delete
mod_ok = all(part.isidentifier() for part in mod.split('.'))
[122] Fix | Delete
fn_ok = all(part.isidentifier() for part in fn.split('.'))
[123] Fix | Delete
if not (sep == ':' and mod_ok and fn_ok):
[124] Fix | Delete
raise ZipAppError("Invalid entry point: " + main)
[125] Fix | Delete
main_py = MAIN_TEMPLATE.format(module=mod, fn=fn)
[126] Fix | Delete
[127] Fix | Delete
if target is None:
[128] Fix | Delete
target = source.with_suffix('.pyz')
[129] Fix | Delete
elif not hasattr(target, 'write'):
[130] Fix | Delete
target = pathlib.Path(target)
[131] Fix | Delete
[132] Fix | Delete
with _maybe_open(target, 'wb') as fd:
[133] Fix | Delete
_write_file_prefix(fd, interpreter)
[134] Fix | Delete
compression = (zipfile.ZIP_DEFLATED if compressed else
[135] Fix | Delete
zipfile.ZIP_STORED)
[136] Fix | Delete
with zipfile.ZipFile(fd, 'w', compression=compression) as z:
[137] Fix | Delete
for child in source.rglob('*'):
[138] Fix | Delete
arcname = child.relative_to(source)
[139] Fix | Delete
if filter is None or filter(arcname):
[140] Fix | Delete
z.write(child, arcname.as_posix())
[141] Fix | Delete
if main_py:
[142] Fix | Delete
z.writestr('__main__.py', main_py.encode('utf-8'))
[143] Fix | Delete
[144] Fix | Delete
if interpreter and not hasattr(target, 'write'):
[145] Fix | Delete
target.chmod(target.stat().st_mode | stat.S_IEXEC)
[146] Fix | Delete
[147] Fix | Delete
[148] Fix | Delete
def get_interpreter(archive):
[149] Fix | Delete
with _maybe_open(archive, 'rb') as f:
[150] Fix | Delete
if f.read(2) == b'#!':
[151] Fix | Delete
return f.readline().strip().decode(shebang_encoding)
[152] Fix | Delete
[153] Fix | Delete
[154] Fix | Delete
def main(args=None):
[155] Fix | Delete
"""Run the zipapp command line interface.
[156] Fix | Delete
[157] Fix | Delete
The ARGS parameter lets you specify the argument list directly.
[158] Fix | Delete
Omitting ARGS (or setting it to None) works as for argparse, using
[159] Fix | Delete
sys.argv[1:] as the argument list.
[160] Fix | Delete
"""
[161] Fix | Delete
import argparse
[162] Fix | Delete
[163] Fix | Delete
parser = argparse.ArgumentParser()
[164] Fix | Delete
parser.add_argument('--output', '-o', default=None,
[165] Fix | Delete
help="The name of the output archive. "
[166] Fix | Delete
"Required if SOURCE is an archive.")
[167] Fix | Delete
parser.add_argument('--python', '-p', default=None,
[168] Fix | Delete
help="The name of the Python interpreter to use "
[169] Fix | Delete
"(default: no shebang line).")
[170] Fix | Delete
parser.add_argument('--main', '-m', default=None,
[171] Fix | Delete
help="The main function of the application "
[172] Fix | Delete
"(default: use an existing __main__.py).")
[173] Fix | Delete
parser.add_argument('--compress', '-c', action='store_true',
[174] Fix | Delete
help="Compress files with the deflate method. "
[175] Fix | Delete
"Files are stored uncompressed by default.")
[176] Fix | Delete
parser.add_argument('--info', default=False, action='store_true',
[177] Fix | Delete
help="Display the interpreter from the archive.")
[178] Fix | Delete
parser.add_argument('source',
[179] Fix | Delete
help="Source directory (or existing archive).")
[180] Fix | Delete
[181] Fix | Delete
args = parser.parse_args(args)
[182] Fix | Delete
[183] Fix | Delete
# Handle `python -m zipapp archive.pyz --info`.
[184] Fix | Delete
if args.info:
[185] Fix | Delete
if not os.path.isfile(args.source):
[186] Fix | Delete
raise SystemExit("Can only get info for an archive file")
[187] Fix | Delete
interpreter = get_interpreter(args.source)
[188] Fix | Delete
print("Interpreter: {}".format(interpreter or "<none>"))
[189] Fix | Delete
sys.exit(0)
[190] Fix | Delete
[191] Fix | Delete
if os.path.isfile(args.source):
[192] Fix | Delete
if args.output is None or (os.path.exists(args.output) and
[193] Fix | Delete
os.path.samefile(args.source, args.output)):
[194] Fix | Delete
raise SystemExit("In-place editing of archives is not supported")
[195] Fix | Delete
if args.main:
[196] Fix | Delete
raise SystemExit("Cannot change the main function when copying")
[197] Fix | Delete
[198] Fix | Delete
create_archive(args.source, args.output,
[199] Fix | Delete
interpreter=args.python, main=args.main,
[200] Fix | Delete
compressed=args.compress)
[201] Fix | Delete
[202] Fix | Delete
[203] Fix | Delete
if __name__ == '__main__':
[204] Fix | Delete
main()
[205] Fix | Delete
[206] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function