Browse Source

Get images to the cache

Valentin Niess 7 months ago
parent
commit
f746e5dae3

+ 10 - 1
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')

+ 3 - 3
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:

+ 18 - 0
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)

+ 20 - 13
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()
-
-    return SimpleNamespace(
-        arch = arch,
-        tag = tag,
-        path = image_extractor.default_destination(),
-    )
+    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(),
+        )
+    else:
+        return SimpleNamespace(
+            arch = arch,
+            tag = tag,
+            path = downloader.default_destination(),
+        )

+ 4 - 4
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}')