|
| 1 | +import glob |
| 2 | + |
| 3 | +from os.path import join, exists |
| 4 | +from shutil import rmtree, copytree |
| 5 | +from subprocess import run |
| 6 | +from os import makedirs, remove |
| 7 | + |
| 8 | +from invoke import task |
| 9 | + |
| 10 | +from tasks.env import PROJ_ROOT, THIRD_PARTY_DIR |
| 11 | + |
| 12 | +FAASM_RUNTIME_ROOT = "/usr/local/faasm/runtime_root" |
| 13 | +CPYTHON_SRC = join(THIRD_PARTY_DIR, "cpython") |
| 14 | +CPYTHON_INSTALL_DIR = join(CPYTHON_SRC, "install", "wasm") |
| 15 | +CROSSENV_WASM_DIR = join(PROJ_ROOT, "cross_venv", "cross") |
| 16 | + |
| 17 | + |
| 18 | +def _glob_remove(glob_pattern, recursive=False, directory=False): |
| 19 | + print("Recursive remove: {}".format(glob_pattern)) |
| 20 | + for filename in glob.iglob(glob_pattern, recursive=recursive): |
| 21 | + print("Removing {}".format(filename)) |
| 22 | + if directory: |
| 23 | + rmtree(filename) |
| 24 | + else: |
| 25 | + remove(filename) |
| 26 | + |
| 27 | + |
| 28 | +def _clear_pyc_files(dir_path): |
| 29 | + pyc_glob = "{}/**/*.pyc".format(dir_path) |
| 30 | + _glob_remove(pyc_glob, recursive=True) |
| 31 | + |
| 32 | + pycache_glob = "{}/**/__pycache__".format(dir_path) |
| 33 | + _glob_remove(pycache_glob, recursive=True, directory=True) |
| 34 | + |
| 35 | + |
| 36 | +@task(default=True) |
| 37 | +def copy(ctx): |
| 38 | + """ |
| 39 | + Copies the CPython archive and all installed modules from the cpython build |
| 40 | + into the Faasm runtime root |
| 41 | + """ |
| 42 | + |
| 43 | + include_root = join(FAASM_RUNTIME_ROOT, "include") |
| 44 | + lib_root = join(FAASM_RUNTIME_ROOT, "lib") |
| 45 | + |
| 46 | + if not exists(lib_root): |
| 47 | + print("Creating {}".format(lib_root)) |
| 48 | + makedirs(lib_root) |
| 49 | + |
| 50 | + if not exists(include_root): |
| 51 | + print("Creating {}".format(include_root)) |
| 52 | + makedirs(include_root) |
| 53 | + |
| 54 | + include_src_dir = join(CPYTHON_INSTALL_DIR, "include", "python3.8") |
| 55 | + lib_src_dir = join(CPYTHON_INSTALL_DIR, "lib", "python3.8") |
| 56 | + site_packages_src_dir = join( |
| 57 | + CROSSENV_WASM_DIR, "lib", "python3.8", "site-packages" |
| 58 | + ) |
| 59 | + |
| 60 | + include_dest_dir = join(include_root, "python3.8") |
| 61 | + lib_dest_dir = join(lib_root, "python3.8") |
| 62 | + site_packages_dest_dir = join(lib_dest_dir, "site-packages") |
| 63 | + |
| 64 | + # Clear out pyc files |
| 65 | + print("Clearing out pyc files") |
| 66 | + _clear_pyc_files(lib_src_dir) |
| 67 | + _clear_pyc_files(site_packages_src_dir) |
| 68 | + |
| 69 | + # Remove dirs to be replaced by those we copy in |
| 70 | + if exists(include_dest_dir): |
| 71 | + print("Removing {}".format(include_dest_dir)) |
| 72 | + rmtree(include_dest_dir) |
| 73 | + |
| 74 | + if exists(lib_dest_dir): |
| 75 | + print("Removing {}".format(lib_dest_dir)) |
| 76 | + rmtree(lib_dest_dir) |
| 77 | + |
| 78 | + # Copy CPython includes |
| 79 | + print("Copying {} to {}".format(include_src_dir, include_dest_dir)) |
| 80 | + copytree(include_src_dir, include_dest_dir) |
| 81 | + |
| 82 | + # Copy CPython libs |
| 83 | + print("Copying {} to {}".format(lib_src_dir, lib_dest_dir)) |
| 84 | + copytree(lib_src_dir, lib_dest_dir) |
| 85 | + |
| 86 | + # Copy cross-compiled modules |
| 87 | + if not exists(site_packages_dest_dir): |
| 88 | + makedirs(site_packages_dest_dir) |
| 89 | + |
| 90 | + print( |
| 91 | + "Copying {} to {}".format( |
| 92 | + site_packages_src_dir, site_packages_dest_dir |
| 93 | + ) |
| 94 | + ) |
| 95 | + run( |
| 96 | + "cp -r {}/* {}/".format(site_packages_src_dir, site_packages_dest_dir), |
| 97 | + shell=True, |
| 98 | + check=True, |
| 99 | + ) |
0 commit comments