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