[elbe-devel] [PATCH 11/14] elbepack: split fetch_source into utility function

Thomas Weißschuh thomas.weissschuh at linutronix.de
Wed Feb 28 11:39:04 CET 2024


This makes it usable from fetch_initvm_pkgs.py.

Signed-off-by: Thomas Weißschuh <thomas.weissschuh at linutronix.de>
---
 elbepack/aptpkgutils.py | 57 +++++++++++++++++++++++++++++++++++++
 elbepack/rpcaptcache.py | 62 ++---------------------------------------
 2 files changed, 59 insertions(+), 60 deletions(-)

diff --git a/elbepack/aptpkgutils.py b/elbepack/aptpkgutils.py
index 4db70af3f91b..a0992de3e2e2 100644
--- a/elbepack/aptpkgutils.py
+++ b/elbepack/aptpkgutils.py
@@ -146,6 +146,63 @@ def fetch_binary(version, destdir='', progress=None):
     return os.path.abspath(destfile)
 
 
+def fetch_source(name, version, destdir, progress=None):
+
+    allow_untrusted = apt_pkg.config.find_b('APT::Get::AllowUnauthenticated', False)
+
+    rec = apt_pkg.SourceRecords()
+    acq = apt_pkg.Acquire(progress or apt.progress.text.AcquireProgress())
+
+    # poorman's iterator
+    while True:
+        next_p = rec.lookup(name)
+        # End of the list?
+        if not next_p:
+            raise ValueError(
+                f'No source found for {name}_{version}')
+        if version == rec.version:
+            break
+
+    # We don't allow untrusted package and the package is not
+    # marks as trusted
+    if not (allow_untrusted or rec.index.is_trusted):
+        raise FetchError(
+            f"Can't fetch source {name}_{version}; "
+            f'Source {rec.index.describe} is not trusted')
+
+    # Copy from src to dst all files of the source package
+    dsc = None
+    files = []
+    for _file in rec.files:
+        src = os.path.basename(_file.path)
+        dst = os.path.join(destdir, src)
+
+        if 'dsc' == _file.type:
+            dsc = dst
+
+        if not (allow_untrusted or _file.hashes.usable):
+            raise FetchError(
+                f"Can't fetch file {dst}. No trusted hash found.")
+
+        # acq is accumlating the AcquireFile, the files list only
+        # exists to prevent Python from GC the object .. I guess.
+        # Anyway, if we don't keep the list, We will get an empty
+        # directory
+        files.append(apt_pkg.AcquireFile(acq, rec.index.archive_uri(_file.path),
+                                         _file.hashes, _file.size, src, destfile=dst))
+    acq.run()
+
+    if dsc is None:
+        raise ValueError(f'No source found for {name}_{version}')
+
+    for item in acq.items:
+        if item.STAT_DONE != item.status:
+            raise FetchError(
+                f"Can't fetch item {item.destfile}: {item.error_text}")
+
+    return os.path.abspath(dsc)
+
+
 class PackageBase:
 
     def __init__(self, name,
diff --git a/elbepack/rpcaptcache.py b/elbepack/rpcaptcache.py
index b71fc7a39a9c..66519669838e 100644
--- a/elbepack/rpcaptcache.py
+++ b/elbepack/rpcaptcache.py
@@ -10,18 +10,14 @@ from multiprocessing.managers import BaseManager
 from multiprocessing.util import Finalize
 
 from apt import Cache
-from apt.package import FetchError
 
 from apt_pkg import (
-    Acquire,
-    AcquireFile,
-    SourceRecords,
     TagFile,
     config,
     version_compare,
 )
 
-from elbepack.aptpkgutils import APTPackage, fetch_binary, getalldeps
+from elbepack.aptpkgutils import APTPackage, fetch_binary, fetch_source, getalldeps
 from elbepack.aptprogress import (
     ElbeAcquireProgress,
     ElbeInstallProgress,
@@ -367,61 +363,7 @@ class RPCAPTCache(InChRootObject):
         return self.rfs.fname(rel_filename)
 
     def download_source(self, src_name, src_version, dest_dir):
-
-        allow_untrusted = config.find_b('APT::Get::AllowUnauthenticated',
-                                        False)
-
-        rec = SourceRecords()
-        acq = Acquire(ElbeAcquireProgress())
-
-        # poorman's iterator
-        while True:
-            next_p = rec.lookup(src_name)
-            # End of the list?
-            if not next_p:
-                raise ValueError(
-                    f'No source found for {src_name}_{src_version}')
-            if src_version == rec.version:
-                break
-
-        # We don't allow untrusted package and the package is not
-        # marks as trusted
-        if not (allow_untrusted or rec.index.is_trusted):
-            raise FetchError(
-                f"Can't fetch source {src_name}_{src_version}; "
-                f'Source {rec.index.describe} is not trusted')
-
-        # Copy from src to dst all files of the source package
-        dsc = None
-        files = []
-        for _file in rec.files:
-            src = os.path.basename(_file.path)
-            dst = os.path.join(dest_dir, src)
-
-            if 'dsc' == _file.type:
-                dsc = dst
-
-            if not (allow_untrusted or _file.hashes.usable):
-                raise FetchError(
-                    f"Can't fetch file {dst}. No trusted hash found.")
-
-            # acq is accumlating the AcquireFile, the files list only
-            # exists to prevent Python from GC the object .. I guess.
-            # Anyway, if we don't keep the list, We will get an empty
-            # directory
-            files.append(AcquireFile(acq, rec.index.archive_uri(_file.path),
-                                     _file.hashes, _file.size, src, destfile=dst))
-        acq.run()
-
-        if dsc is None:
-            raise ValueError(f'No source found for {src_name}_{src_version}')
-
-        for item in acq.items:
-            if item.STAT_DONE != item.status:
-                raise FetchError(
-                    f"Can't fetch item {item.destfile}: {item.error_text}")
-
-        return self.rfs.fname(os.path.abspath(dsc))
+        return self.rfs.fname(fetch_source(src_name, src_version, dest_dir, ElbeAcquireProgress()))
 
 
 def get_rpcaptcache(rfs, arch,
-- 
2.44.0



More information about the elbe-devel mailing list