|
| 1 | +import compileall |
| 2 | +import os |
| 3 | +import shutil |
| 4 | +import sys |
| 5 | +import zipfile |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | + |
| 9 | +PYTHON_VERSION = "3.8.10" |
| 10 | + |
| 11 | + |
| 12 | +DO_NOT_ADD = [ |
| 13 | + "__pycache__", |
| 14 | + "_distutils_hack", |
| 15 | + "pip", |
| 16 | + "pkg_resources", |
| 17 | + "setuptools", |
| 18 | + "wheel", |
| 19 | + "_virtualenv.py", |
| 20 | +] |
| 21 | + |
| 22 | +BLACKLIST = ["numpy"] # Special case - can't be loaded from ZIP |
| 23 | + |
| 24 | +DO_NOT_ADD.extend(BLACKLIST) |
| 25 | + |
| 26 | + |
| 27 | +ENV_PATH = Path(".venv") |
| 28 | +LIB_PATH = Path(ENV_PATH, "Lib", "site-packages") |
| 29 | + |
| 30 | +OUT_PATH = Path("Lib", "site-packages") |
| 31 | +ZIP_PATH = Path("Lib", "site-packages.zip") |
| 32 | +REL_PATH = Path("Data", "Python", OUT_PATH) |
| 33 | + |
| 34 | + |
| 35 | +def pack_filter(path): |
| 36 | + fn = os.path.basename(path) |
| 37 | + return not (fn in DO_NOT_ADD or ".dist-info" in fn) |
| 38 | + |
| 39 | + |
| 40 | +copy_filter = shutil.ignore_patterns("__pycache__") |
| 41 | + |
| 42 | + |
| 43 | +def main(): |
| 44 | + |
| 45 | + # Check if the Python version is correct |
| 46 | + py_version = ".".join(str(s) for s in sys.version_info[:3]) |
| 47 | + if py_version != PYTHON_VERSION: |
| 48 | + raise RuntimeError( |
| 49 | + f"This script must be run in Python {PYTHON_VERSION} " |
| 50 | + f"(you're running {py_version})" |
| 51 | + ) |
| 52 | + |
| 53 | + # Check the script is being run from the right subdirectory |
| 54 | + cwd = Path.cwd() |
| 55 | + if not cwd.parts[-3:] == ("datafiles", "Data", "Python"): |
| 56 | + raise RuntimeWarning( |
| 57 | + "This script must be run from the datafiles/Data/Python " |
| 58 | + "subfolder in the Open Note Block Studio root directory." |
| 59 | + ) |
| 60 | + |
| 61 | + if not os.path.exists(ENV_PATH): |
| 62 | + raise FileNotFoundError( |
| 63 | + "The .venv directory was not found. Have you ran" |
| 64 | + "poetry install before running this script?" |
| 65 | + ) |
| 66 | + |
| 67 | + # Delete previous build directory |
| 68 | + if Path.exists(OUT_PATH): |
| 69 | + print("Removing output directory") |
| 70 | + shutil.rmtree(OUT_PATH) |
| 71 | + |
| 72 | + # Create output directory |
| 73 | + os.makedirs(OUT_PATH) |
| 74 | + |
| 75 | + # Package dependencies |
| 76 | + package_count = 0 |
| 77 | + with zipfile.PyZipFile(ZIP_PATH, mode="w") as zip_file: |
| 78 | + for path in os.listdir(LIB_PATH): |
| 79 | + lib_name = os.path.basename(path) |
| 80 | + lib_path = Path(LIB_PATH, lib_name) |
| 81 | + |
| 82 | + # Pre-compile all modules without absolute paths |
| 83 | + path_prefix = REL_PATH |
| 84 | + if os.path.isdir(lib_path): |
| 85 | + compileall.compile_dir( |
| 86 | + lib_path, ddir=path_prefix, force=True, quiet=2, legacy=True |
| 87 | + ) |
| 88 | + else: |
| 89 | + compileall.compile_file( |
| 90 | + str(lib_path), ddir=path_prefix, force=True, quiet=2, legacy=True |
| 91 | + ) |
| 92 | + |
| 93 | + # Write to ZIP |
| 94 | + try: |
| 95 | + zip_file.writepy(lib_path, filterfunc=pack_filter) |
| 96 | + except RuntimeError: # only directories or .py files accepted |
| 97 | + continue |
| 98 | + else: |
| 99 | + if pack_filter(lib_path): |
| 100 | + print(f"Packaging {lib_name}") |
| 101 | + package_count += 1 |
| 102 | + |
| 103 | + # Handle special cases |
| 104 | + for lib_name in BLACKLIST: |
| 105 | + print(f"Packaging {lib_name}") |
| 106 | + package_count += 1 |
| 107 | + inpath = Path(LIB_PATH, lib_name) |
| 108 | + outpath = Path(OUT_PATH, lib_name) |
| 109 | + shutil.copytree(inpath, outpath, ignore=copy_filter) |
| 110 | + compileall.compile_dir( |
| 111 | + outpath, ddir=REL_PATH, force=True, quiet=True, legacy=True |
| 112 | + ) |
| 113 | + # Remove corresponding .py files |
| 114 | + for root, _, files in os.walk(outpath): |
| 115 | + for filename in files: |
| 116 | + if filename.endswith(".py"): |
| 117 | + os.remove(os.path.join(root, filename)) |
| 118 | + |
| 119 | + # Delete virtual environment |
| 120 | + print("Removing virtual environment") |
| 121 | + shutil.rmtree(ENV_PATH) |
| 122 | + |
| 123 | + print(f"Done! {package_count} packages were added.") |
| 124 | + |
| 125 | + |
| 126 | +if __name__ == "__main__": |
| 127 | + main() |
0 commit comments