From f746e5dae34242017049c2c2096b5dfb57841071 Mon Sep 17 00:00:00 2001 From: Valentin Niess Date: Thu, 22 May 2025 09:32:51 +0200 Subject: [PATCH] Get images to the cache --- python_appimage/__main__.py | 11 +++++++- python_appimage/commands/build/manylinux.py | 6 ++-- python_appimage/commands/cache/get.py | 18 ++++++++++++ python_appimage/manylinux/__init__.py | 31 +++++++++++++-------- python_appimage/manylinux/extract.py | 8 +++--- 5 files changed, 54 insertions(+), 20 deletions(-) create mode 100644 python_appimage/commands/cache/get.py diff --git a/python_appimage/__main__.py b/python_appimage/__main__.py index 409b0ae..eb9cb76 100644 --- a/python_appimage/__main__.py +++ b/python_appimage/__main__.py @@ -51,6 +51,13 @@ def main(): cache_clean_parser.add_argument('-a', '--all', action='store_true', help='remove all image(s) data') + cache_get_parser = cache_subparsers.add_parser('get', + description='Download image(s) to the cache') + cache_get_parser.add_argument('tags', nargs='+', + help='manylinux image tag(s) (e.g. 2014_x86_64)') + cache_get_parser.add_argument('-e', '--extract', action='store_true', + help='extract compressed image data') + cache_list_parser = cache_subparsers.add_parser('list', description='List cached image(s)') @@ -66,11 +73,13 @@ def main(): build_local_parser.add_argument('-p', '--python', help='python executable') build_manylinux_parser = build_subparsers.add_parser('manylinux', - description='Bundle a manylinux Python installation using docker') + description='Bundle a manylinux Python installation') build_manylinux_parser.add_argument('tag', help='manylinux image tag (e.g. 2010_x86_64)') build_manylinux_parser.add_argument('abi', help='python ABI (e.g. cp37-cp37m)') + build_manylinux_parser.add_argument('-c', '--clean', + help='compress the image after extraction', action='store_true') build_app_parser = build_subparsers.add_parser('app', description='Build a Python application using a base AppImage') diff --git a/python_appimage/commands/build/manylinux.py b/python_appimage/commands/build/manylinux.py index 7442b2d..4102a2e 100644 --- a/python_appimage/commands/build/manylinux.py +++ b/python_appimage/commands/build/manylinux.py @@ -13,14 +13,14 @@ __all__ = ['execute'] def _unpack_args(args): '''Unpack command line arguments ''' - return args.tag, args.abi + return args.tag, args.abi, args.clean -def execute(tag, abi): +def execute(tag, abi, clean): '''Build a Python AppImage using a Manylinux image ''' - image = ensure_image(tag) + image = ensure_image(tag, clean=clean) pwd = os.getcwd() with TemporaryDirectory() as tmpdir: diff --git a/python_appimage/commands/cache/get.py b/python_appimage/commands/cache/get.py new file mode 100644 index 0000000..1dfe048 --- /dev/null +++ b/python_appimage/commands/cache/get.py @@ -0,0 +1,18 @@ +from ...manylinux import ensure_image + + +__all__ = ['execute'] + + +def _unpack_args(args): + '''Unpack command line arguments + ''' + return (args.tags, args.extract) + + +def execute(images, extract): + '''Download image(s) to the cache + ''' + + for image in images: + ensure_image(image, extract=extract) diff --git a/python_appimage/manylinux/__init__.py b/python_appimage/manylinux/__init__.py index 09e32ad..f80b859 100644 --- a/python_appimage/manylinux/__init__.py +++ b/python_appimage/manylinux/__init__.py @@ -9,8 +9,8 @@ __all__ = ['Arch', 'Downloader', 'ensure_image', 'ImageExtractor', 'LinuxTag', 'PythonExtractor', 'PythonImpl', 'PythonVersion'] -def ensure_image(tag): - '''Extract a manylinux image to the cache''' +def ensure_image(tag, *, clean=False, extract=True): + '''Download a manylinux image to the cache''' try: tag, image_tag = tag.rsplit(':', 1) @@ -24,14 +24,21 @@ def ensure_image(tag): downloader = Downloader(tag=tag, arch=arch) downloader.download(tag=image_tag) - image_extractor = ImageExtractor( - prefix = downloader.default_destination(), - tag = image_tag - ) - image_extractor.extract() + if extract: + image_extractor = ImageExtractor( + prefix = downloader.default_destination(), + tag = image_tag + ) + image_extractor.extract(clean=clean) - return SimpleNamespace( - arch = arch, - tag = tag, - path = image_extractor.default_destination(), - ) + return SimpleNamespace( + arch = arch, + tag = tag, + path = image_extractor.default_destination(), + ) + else: + return SimpleNamespace( + arch = arch, + tag = tag, + path = downloader.default_destination(), + ) diff --git a/python_appimage/manylinux/extract.py b/python_appimage/manylinux/extract.py index 8ce301d..7c22fcb 100644 --- a/python_appimage/manylinux/extract.py +++ b/python_appimage/manylinux/extract.py @@ -335,16 +335,16 @@ class ImageExtractor: return self.prefix / f'extracted/{self.tag}' - def extract(self, destination: Optional[Path]=None, *, cleanup=False): + def extract(self, destination: Optional[Path]=None, *, clean=False): '''Extract Manylinux image.''' if destination is None: destination = self.default_destination() - if cleanup: - def cleanup(destination): + if clean: + def clean(destination): shutil.rmtree(destination, ignore_errors=True) - atexit.register(cleanup, destination) + atexit.register(clean, destination) log('EXTRACT', f'{self.prefix.name}:{self.tag}')