def zip_pack(filepath, options):
import zipfile<EOL>compression_format = zipfile.ZIP_DEFLATED<EOL>cumulative_size = <NUM_LIT:0> <EOL>cumulative_size += os.path.getsize(filepath)<EOL>dest = options.pyz<EOL>z = zipfile.ZipFile(dest, "<STR_LIT:w>", compression_format)<EOL>source = open(filepath).read()<EOL>primary_tokens = token_utils.listified_tokenizer(source)<EOL>shebang = analyze.get_shebang(primary_tokens)<EOL>if not shebang:<EOL><INDENT>shebang = "<STR_LIT>"<EOL><DEDENT>if py3:<EOL><INDENT>if shebang.rstrip().endswith('<STR_LIT>'): <EOL><INDENT>shebang = shebang.rstrip()<EOL>shebang += '<STR_LIT>' <EOL><DEDENT><DEDENT>if not options.nominify: <EOL><INDENT>source = minification.minify(primary_tokens, options)<EOL><DEDENT>temp = tempfile.NamedTemporaryFile(mode='<STR_LIT:w>')<EOL>temp.write(source)<EOL>temp.flush()<EOL>path = os.path.split(filepath)[<NUM_LIT:0>]<EOL>if not path:<EOL><INDENT>path = os.getcwd()<EOL><DEDENT>main_py = path + '<STR_LIT>'<EOL>if os.path.exists(main_py):<EOL><INDENT>z.write(main_py, '<STR_LIT>')<EOL>z.write(temp.name, os.path.split(filepath)[<NUM_LIT:1>])<EOL><DEDENT>else:<EOL><INDENT>z.write(filepath, '<STR_LIT>')<EOL><DEDENT>temp.close()<EOL>local_modules = analyze.enumerate_local_modules(primary_tokens, path)<EOL>name_generator = None <EOL>if options.obfuscate or options.obf_classesor options.obf_functions or options.obf_variablesor options.obf_builtins or options.obf_import_methods:<EOL><INDENT>identifier_length = int(options.replacement_length)<EOL>if options.use_nonlatin:<EOL><INDENT>if sys.version_info[<NUM_LIT:0>] == <NUM_LIT:3>:<EOL><INDENT>name_generator = obfuscate.obfuscation_machine(<EOL>use_unicode=True, identifier_length=identifier_length<EOL>)<EOL><DEDENT>else:<EOL><INDENT>print(<EOL>"<STR_LIT>")<EOL>sys.exit(<NUM_LIT:2>)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>name_generator = obfuscate.obfuscation_machine(<EOL>identifier_length=identifier_length)<EOL><DEDENT>table =[{}]<EOL><DEDENT>included_modules = []<EOL>for module in local_modules:<EOL><INDENT>module = module.replace('<STR_LIT:.>', '<STR_LIT:/>')<EOL>module = "<STR_LIT>" % module<EOL>cumulative_size += os.path.getsize(module)<EOL>included_modules.append(module)<EOL>source = open(os.path.join(path, module)).read()<EOL>tokens = token_utils.listified_tokenizer(source)<EOL>maybe_more_modules = analyze.enumerate_local_modules(tokens, path)<EOL>for mod in maybe_more_modules:<EOL><INDENT>if mod not in local_modules:<EOL><INDENT>local_modules.append(mod) <EOL><DEDENT><DEDENT>if not options.nominify:<EOL><INDENT>source = minification.minify(tokens, options)<EOL><DEDENT>tokens = token_utils.listified_tokenizer(source)<EOL>if name_generator:<EOL><INDENT>obfuscate.obfuscate(<EOL>module,<EOL>tokens,<EOL>options,<EOL>name_generator=name_generator,<EOL>table=table<EOL>)<EOL><DEDENT>result = token_utils.untokenize(tokens)<EOL>result += (<EOL>"<STR_LIT>"<EOL>"<STR_LIT>")<EOL>temp = tempfile.NamedTemporaryFile(mode='<STR_LIT:w>')<EOL>temp.write(source)<EOL>temp.flush()<EOL>z.write(temp.name, module)<EOL>temp.close()<EOL><DEDENT>z.close()<EOL>prepend(shebang, dest)<EOL>os.chmod(dest, <NUM_LIT>) <EOL>pyz_filesize = os.path.getsize(dest)<EOL>percent_saved = round(float(pyz_filesize) / float(cumulative_size) * <NUM_LIT:100>, <NUM_LIT:2>)<EOL>print('<STR_LIT>' % (filepath, dest))<EOL>print('<STR_LIT>'<EOL>'<STR_LIT>')<EOL>for module in included_modules:<EOL><INDENT>print('<STR_LIT>' % module)<EOL><DEDENT>print('<STR_LIT>' % percent_saved)<EOL>
Creates a zip archive containing the script at *filepath* along with all
imported modules that are local to *filepath* as a self-extracting python
script.  A shebang will be appended to the beginning of the resulting
zip archive which will allow it to

If being run inside Python 3 and the `lzma` module is available the
resulting 'pyz' file will use ZIP_LZMA compression to maximize compression.

*options* is expected to be the the same options parsed from pyminifier.py
on the command line.

.. note::

    * The file resulting from this method cannot be imported as a module into another python program (command line execution only).
    * Any required local (implied path) modules will be automatically included (well, it does its best).
    * The result will be saved as a .pyz file (which is an extension I invented for this format).