Add custom zipper script to zip python modules for torch.deploy (#67006)

Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/67006

Test Plan: nervouslaugh_

Reviewed By: shunting314

Differential Revision: D31822429

fbshipit-source-id: c2efeab1446fbeb70b98d4ee766fbc670cf091b0
This commit is contained in:
Tugsbayasgalan (Tugsuu) Manlaibaatar 2021-11-06 11:47:22 -07:00 committed by Facebook GitHub Bot
parent ae501a9727
commit 9cacf2b718

40
torch/utils/_zip.py Normal file
View file

@ -0,0 +1,40 @@
import argparse
from pathlib import Path
from zipfile import PyZipFile
# Exclude some standard library modules to:
# 1. Slim down the final zipped file size
# 2. Remove functionality we don't want to support.
DENY_LIST = [
# Interface to unix databases
"dbm",
# ncurses bindings (terminal interfaces)
"curses",
# Tcl/Tk GUI
"tkinter",
"tkinter",
# Tests for the standard library
"test",
"tests",
"idle_test",
"__phello__.foo.py",
# importlib frozen modules. These are already baked into CPython.
"_bootstrap.py",
"_bootstrap_external.py",
]
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Zip py source")
parser.add_argument("paths", nargs="*", help="Paths to zip.")
parser.add_argument("--install_dir", help="Root directory for all output files")
parser.add_argument("--zip_name", help="Output zip name")
args = parser.parse_args()
zip_file_name = args.install_dir + '/' + args.zip_name
zf = PyZipFile(zip_file_name, mode='w')
for p in args.paths:
path = Path(p)
if path.name in DENY_LIST:
continue
zf.write(p)