zipapp pack (#35253)
Used to ship python UI in agnos without an openpilot clone * add a main method to target * pack script * validate inputs * refactors * copy into temp, dont keep this * cleanup * help messages * rename to pack.py * pack.py * updates for device * moar * don't use cereal * just log normally * use importlib.resources * revert * Revert "don't use cereal" This reverts commitpull/35255/head7208524d42
. * fix cereal? * cleanup * Revert "cleanup" This reverts commit921edfe502
. * cython hotfix * Reapply "cleanup" This reverts commit9b54552f78
. * more cleanup * any script? * slightly clearer * rm print * nothing python should use SVGs --------- Co-authored-by: Trey Moen <trey@moen.ai>
parent
09fde3c3ad
commit
8097a92515
5 changed files with 79 additions and 16 deletions
@ -1,9 +1,11 @@ |
|||||||
import os |
import os |
||||||
import capnp |
import capnp |
||||||
|
from importlib.resources import as_file, files |
||||||
|
|
||||||
CEREAL_PATH = os.path.dirname(os.path.abspath(__file__)) |
|
||||||
capnp.remove_import_hook() |
capnp.remove_import_hook() |
||||||
|
|
||||||
log = capnp.load(os.path.join(CEREAL_PATH, "log.capnp")) |
with as_file(files("cereal")) as fspath: |
||||||
car = capnp.load(os.path.join(CEREAL_PATH, "car.capnp")) |
CEREAL_PATH = fspath.as_posix() |
||||||
custom = capnp.load(os.path.join(CEREAL_PATH, "custom.capnp")) |
log = capnp.load(os.path.join(CEREAL_PATH, "log.capnp")) |
||||||
|
car = capnp.load(os.path.join(CEREAL_PATH, "car.capnp")) |
||||||
|
custom = capnp.load(os.path.join(CEREAL_PATH, "custom.capnp")) |
||||||
|
@ -0,0 +1,50 @@ |
|||||||
|
#!/usr/bin/env python3 |
||||||
|
|
||||||
|
import importlib |
||||||
|
import shutil |
||||||
|
import sys |
||||||
|
import tempfile |
||||||
|
import zipapp |
||||||
|
from argparse import ArgumentParser |
||||||
|
from pathlib import Path |
||||||
|
|
||||||
|
from openpilot.common.basedir import BASEDIR |
||||||
|
|
||||||
|
|
||||||
|
DIRS = ['cereal', 'openpilot'] |
||||||
|
EXTS = ['.png', '.py', '.ttf', '.capnp'] |
||||||
|
INTERPRETER = '/usr/bin/env python3' |
||||||
|
|
||||||
|
|
||||||
|
def copy(src, dest): |
||||||
|
if any(src.endswith(ext) for ext in EXTS): |
||||||
|
shutil.copy2(src, dest, follow_symlinks=True) |
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__': |
||||||
|
parser = ArgumentParser(prog='pack.py', description="package script into a portable executable", epilog='comma.ai') |
||||||
|
parser.add_argument('-e', '--entrypoint', help="function to call in module, default is 'main'", default='main') |
||||||
|
parser.add_argument('-o', '--output', help='output file') |
||||||
|
parser.add_argument('module', help="the module to target, e.g. 'openpilot.system.ui.spinner'") |
||||||
|
args = parser.parse_args() |
||||||
|
|
||||||
|
if not args.output: |
||||||
|
args.output = args.module |
||||||
|
|
||||||
|
try: |
||||||
|
mod = importlib.import_module(args.module) |
||||||
|
except ModuleNotFoundError: |
||||||
|
print(f'{args.module} not found, typo?') |
||||||
|
sys.exit(1) |
||||||
|
|
||||||
|
if not hasattr(mod, args.entrypoint): |
||||||
|
print(f'{args.module} does not have a {args.entrypoint}() function, typo?') |
||||||
|
sys.exit(1) |
||||||
|
|
||||||
|
with tempfile.TemporaryDirectory() as tmp: |
||||||
|
for directory in DIRS: |
||||||
|
shutil.copytree(BASEDIR + '/' + directory, tmp + '/' + directory, symlinks=False, dirs_exist_ok=True, copy_function=copy) |
||||||
|
entry = f'{args.module}:{args.entrypoint}' |
||||||
|
zipapp.create_archive(tmp, target=args.output, interpreter=INTERPRETER, main=entry) |
||||||
|
|
||||||
|
print(f'created executable {Path(args.output).resolve()}') |
Loading…
Reference in new issue