Create an application archive from SOURCE. The SOURCE can be the name of a directory, or a filename or a file-like object referring to an existing archive. The content of SOURCE is packed into an application archive in TARGET, which can be a filename or a file-like object. If SOUR
(source, target=None, interpreter=None, main=None,
filter=None, compressed=False)
| 74 | |
| 75 | |
| 76 | def create_archive(source, target=None, interpreter=None, main=None, |
| 77 | filter=None, compressed=False): |
| 78 | """Create an application archive from SOURCE. |
| 79 | |
| 80 | The SOURCE can be the name of a directory, or a filename or a file-like |
| 81 | object referring to an existing archive. |
| 82 | |
| 83 | The content of SOURCE is packed into an application archive in TARGET, |
| 84 | which can be a filename or a file-like object. If SOURCE is a directory, |
| 85 | TARGET can be omitted and will default to the name of SOURCE with .pyz |
| 86 | appended. |
| 87 | |
| 88 | The created application archive will have a shebang line specifying |
| 89 | that it should run with INTERPRETER (there will be no shebang line if |
| 90 | INTERPRETER is None), and a __main__.py which runs MAIN (if MAIN is |
| 91 | not specified, an existing __main__.py will be used). It is an error |
| 92 | to specify MAIN for anything other than a directory source with no |
| 93 | __main__.py, and it is an error to omit MAIN if the directory has no |
| 94 | __main__.py. |
| 95 | """ |
| 96 | # Are we copying an existing archive? |
| 97 | source_is_file = False |
| 98 | if hasattr(source, 'read') and hasattr(source, 'readline'): |
| 99 | source_is_file = True |
| 100 | else: |
| 101 | source = pathlib.Path(source) |
| 102 | if source.is_file(): |
| 103 | source_is_file = True |
| 104 | |
| 105 | if source_is_file: |
| 106 | _copy_archive(source, target, interpreter) |
| 107 | return |
| 108 | |
| 109 | # We are creating a new archive from a directory. |
| 110 | if not source.exists(): |
| 111 | raise ZipAppError("Source does not exist") |
| 112 | has_main = (source / '__main__.py').is_file() |
| 113 | if main and has_main: |
| 114 | raise ZipAppError( |
| 115 | "Cannot specify entry point if the source has __main__.py") |
| 116 | if not (main or has_main): |
| 117 | raise ZipAppError("Archive has no entry point") |
| 118 | |
| 119 | main_py = None |
| 120 | if main: |
| 121 | # Check that main has the right format. |
| 122 | mod, sep, fn = main.partition(':') |
| 123 | mod_ok = all(part.isidentifier() for part in mod.split('.')) |
| 124 | fn_ok = all(part.isidentifier() for part in fn.split('.')) |
| 125 | if not (sep == ':' and mod_ok and fn_ok): |
| 126 | raise ZipAppError("Invalid entry point: " + main) |
| 127 | main_py = MAIN_TEMPLATE.format(module=mod, fn=fn) |
| 128 | |
| 129 | if target is None: |
| 130 | target = source.with_suffix('.pyz') |
| 131 | elif not hasattr(target, 'write'): |
| 132 | target = pathlib.Path(target) |
| 133 |
no test coverage detected
searching dependent graphs…