[elbe-devel] [PATCH 3/3] Unpack the archives depending on volume attribute

Akash Satamkar akash at linutronix.de
Wed Sep 4 16:03:20 CEST 2019


The volume attribute can be used to specify the volume number
in which the archive is added.

When volume attribute is not specified, the archive is added
to each volume.

Signed-off-by: Akash Satamkar <akash at linutronix.de>
---
 elbepack/archivedir.py  | 46 ++++++++++++++++++++++++++++-------------
 elbepack/cdroms.py      | 13 ++++++++++++
 elbepack/repomanager.py |  7 +++++--
 3 files changed, 50 insertions(+), 16 deletions(-)

diff --git a/elbepack/archivedir.py b/elbepack/archivedir.py
index 10155228..c6fc3a47 100644
--- a/elbepack/archivedir.py
+++ b/elbepack/archivedir.py
@@ -6,6 +6,7 @@
 import os
 import re
 import sys
+import glob
 
 # The urlparse module is renamed to urllib.parse in Python 3.
 try:
@@ -17,7 +18,7 @@ from base64 import standard_b64encode
 from bz2 import compress as bz2compress
 from subprocess import CalledProcessError
 
-from elbepack.treeutils import etree
+from elbepack.treeutils import etree, elem
 from elbepack.shellhelper import system
 
 class ArchivedirError(Exception):
@@ -90,32 +91,49 @@ def get_and_append_method(url):
         'file': get_and_append_local,
     }.get(urlparse(url).scheme, get_and_append_unknown)
 
-def _combinearchivedir(xml):
+def _combinearchivedir(xml, xpath):
     elbexml = etree(None)
     elbexml.et = xml
-
     archive = '.combinedarchive.tar'
-    for archivedir in xml.iterfind("archivedir"):
+    for archivedir in xml.iterfind(xpath):
         try:
             archiveurl = urljoin(archivedir.base, archivedir.text)
             keep = elbexml.check_boolean(archivedir, "keep-attributes")
+            parent = archivedir.getparent()
+            volume_attr = archivedir.get('volume')
+            if volume_attr:
+                arch = parent.get("archive[@volume='%s']" % volume_attr)
+                if not arch:
+                    arch = elem(parent).append("archive")
+                    arch.et.set("volume", volume_attr)
+            else:
+                arch = elem(parent).ensure_child("archive")
+                if arch.et.get('volume'):
+                    arch = elem(parent).append("archive")
+
             get_and_append = get_and_append_method(archiveurl)
-            get_and_append(archiveurl, archive, keep)
-            archivedir.getparent().remove(archivedir)
+            if volume_attr:
+                get_and_append(archiveurl, volume_attr + archive, keep)
+                arch.set_text(enbase(volume_attr + archive, True))
+            else:
+                get_and_append(archiveurl, archive, keep)
+                arch.set_text(enbase(archive, True))
+
+            parent.remove(archivedir)
         except (CalledProcessError, OSError):
             msg = "Failure while processing \"" + archivedir.text + "\":\n"
             msg += str(sys.exc_info()[1])
             raise ArchivedirError(msg)
 
-    arch = elbexml.ensure_child("archive")
-    arch.set_text(enbase(archive, True))
-
-    os.remove(archive)
-
-    return xml
+    if os.path.exists(archive):
+        os.remove(archive)
+    for rem_archive in glob.glob("*" + archive):
+        os.remove(rem_archive)
 
 def combinearchivedir(xml):
-    if xml.find("archivedir") is None:
+    if xml.find("//archivedir") is None:
         return xml
 
-    return _combinearchivedir(xml)
+    _combinearchivedir(xml, "archivedir")
+    _combinearchivedir(xml, "src-cdrom/archivedir")
+    return xml
diff --git a/elbepack/cdroms.py b/elbepack/cdroms.py
index 3ba67f7b..0f5fa394 100644
--- a/elbepack/cdroms.py
+++ b/elbepack/cdroms.py
@@ -18,6 +18,8 @@ from elbepack.aptpkgutils import XMLPackage
 from elbepack.filesystem import Filesystem, hostfs
 from elbepack.shellhelper import CommandError
 from elbepack.isooptions import get_iso_options
+from elbepack.treeutils import etree, elem
+from elbepack.shellhelper import system
 
 CDROM_SIZE = 640 * 1000 * 1000
 
@@ -105,6 +107,17 @@ def mk_source_cdrom(
 
     if xml is not None:
         options = get_iso_options(log, xml)
+
+        if xml.has("src-cdrom/archive") and xml.text("src-cdrom/archive"):
+            for i, arch in enumerate(xml.node('src-cdrom').all('archive')):
+                volume_attr = arch.et.get('volume')
+                if volume_attr:
+                    volume_list = [int(v) for v in volume_attr.split(",")]
+                else:
+                    volume_list = repo.volume_indexes()
+                for volume_number in volume_list:
+                    with xml.archive_tmpfile("src-cdrom/archive[%i]" % (i+1)) as fp:
+                        log.do('tar xvfj "%s" -h -C "%s"' % (fp.name, repo.get_volume_fs(volume_number).path))
     else:
         options = ""
 
diff --git a/elbepack/repomanager.py b/elbepack/repomanager.py
index 18fa6926..b0f2ad82 100644
--- a/elbepack/repomanager.py
+++ b/elbepack/repomanager.py
@@ -99,7 +99,8 @@ class RepoBase(object):
 
     def get_volume_fs(self, volume):
         if self.maxsize:
-            volname = os.path.join(self.vol_path, "vol%02d" % volume)
+            offset = volume if volume >= 0 else self.volume_count + 1 + volume
+            volname = os.path.join(self.vol_path, "vol%02d" % offset)
             return Filesystem(volname)
 
         return Filesystem(self.vol_path)
@@ -313,7 +314,7 @@ class RepoBase(object):
                         (options, fname, new_path))
             files.append(fname)
         else:
-            for i in range(self.volume_count + 1):
+            for i in self.volume_indexes():
                 volfs = self.get_volume_fs(i)
                 newname = fname + ("%02d" % i)
                 self.log.do(
@@ -323,6 +324,8 @@ class RepoBase(object):
 
         return files
 
+    def volume_indexes(self):
+        return range(self.volume_count + 1)
 
 class UpdateRepo(RepoBase):
     def __init__(self, xml, path, log):
-- 
2.20.1




More information about the elbe-devel mailing list