UpstreamSource.pack: support prefix mangling
authorMarkus Lehtonen <markus.lehtonen@linux.intel.com>
Thu, 14 Jun 2012 16:30:26 +0000 (19:30 +0300)
committerMarkus Lehtonen <markus.lehtonen@linux.intel.com>
Tue, 3 Mar 2015 08:07:45 +0000 (10:07 +0200)
Add support for changing the prefix directory inside the tarball that is
generated. Also, fixes a bug that caused a "prefix-less" tarball to get
one, if unpacked and then repacked.

Also, adds this support to repack_source() in common/import_orig.

Signed-off-by: Markus Lehtonen <markus.lehtonen@linux.intel.com>
gbp/command_wrappers.py
gbp/pkg/__init__.py
gbp/scripts/common/import_orig.py
tests/06_test_upstream_source.py

index de67a1f225eb824d3253e51a5b16aa205c004d4e..11891422ff03b663f53530e566730d50b526e89d 100644 (file)
@@ -183,7 +183,8 @@ class UnpackTarArchive(Command):
 
 class PackTarArchive(Command):
     """Wrap tar to pack a compressed tar archive"""
-    def __init__(self, archive, dir, dest, filters=[], compression=None):
+    def __init__(self, archive, dir, dest, filters=[], compression=None,
+                 transform=None):
         self.archive = archive
         self.dir = dir
         exclude = [("--exclude=%s" % _filter) for _filter in filters]
@@ -191,8 +192,14 @@ class PackTarArchive(Command):
         if not compression:
             compression = '-a'
 
-        Command.__init__(self, 'tar', exclude +
-                         ['-C', dir, compression, '-cf', archive, dest])
+        args = exclude + ['-C', dir, compression, '-cf', archive ]
+
+        if transform != None:
+            args.append('--transform=%s' % transform)
+
+        args.append(dest)
+
+        Command.__init__(self, 'tar', args)
         self.run_error = 'Couldn\'t repack "%s"' % self.archive
 
 
index bbc0dc3ff63ce906c4cddc506703fa7fe3bcd4b4..3f125e1b1bd52b001624295f7ceeaf0bfeb0cecf 100644 (file)
@@ -338,7 +338,8 @@ class UpstreamSource(object):
         if len(unpacked) == 1 and os.path.isdir(unpacked[0]):
             return unpacked[0]
         else:
-            return dir
+            # We can determine "no prefix" from this
+            return os.path.join(dir, ".")
 
     def _unpack_tar(self, dir, filters):
         """
@@ -352,7 +353,7 @@ class UpstreamSource(object):
             # unpackArchive already printed an error message
             raise GbpError
 
-    def pack(self, newarchive, filters=[]):
+    def pack(self, newarchive, filters=[], newprefix=None):
         """
         Recreate a new archive from the current one
 
@@ -360,6 +361,8 @@ class UpstreamSource(object):
         @type newarchive: string
         @param filters: tar filters to apply
         @type filters: array of strings
+        @param newprefix: new prefix, None implies that prefix is not mangled
+        @type newprefix: string or None
         @return: the new upstream source
         @rtype: UpstreamSource
         """
@@ -372,12 +375,21 @@ class UpstreamSource(object):
         if type(filters) != type([]):
             raise GbpError("Filters must be a list")
 
+        run_dir = os.path.dirname(self.unpacked.rstrip('/'))
+        pack_this = os.path.basename(self.unpacked.rstrip('/'))
+        transform = None
+        if newprefix is not None:
+            newprefix = newprefix.strip('/.')
+            if newprefix:
+                transform = 's!%s!%s!' % (pack_this, newprefix)
+            else:
+                transform = 's!%s!%s!' % (pack_this, '.')
         try:
-            unpacked = self.unpacked.rstrip('/')
             repackArchive = gbpc.PackTarArchive(newarchive,
-                                os.path.dirname(unpacked),
-                                os.path.basename(unpacked),
-                                filters)
+                                                run_dir,
+                                                pack_this,
+                                                filters,
+                                                transform=transform)
             repackArchive()
         except gbpc.CommandExecFailed:
             # repackArchive already printed an error
index c89d7341b432f40c7a790b3e10542bf136e9b9d5..61bed3e62d2cc4f5c745f7f4cf810f7a6d303d02 100644 (file)
@@ -111,9 +111,9 @@ def ask_package_version(default, ver_validator_func, err_msg):
         gbp.log.warn("\nNot a valid upstream version: '%s'.\n%s" % (version, err_msg))
 
 
-def repack_source(source, new_name, unpack_dir, filters):
+def repack_source(source, new_name, unpack_dir, filters, new_prefix=None):
     """Repack the source tree"""
-    repacked = source.pack(new_name, filters)
+    repacked = source.pack(new_name, filters, new_prefix)
     if source.is_orig(): # the tarball was filtered on unpack
         repacked.unpacked = source.unpacked
     else: # otherwise unpack the generated tarball get a filtered tree
index 34787bf43afdb33f1af2af38a9f375a90c6c3b2f..ac210a032ab51289a5321c6df06509af43795b80 100644 (file)
@@ -72,6 +72,15 @@ class TestTar(unittest.TestCase):
         self._check_tar(repacked, ["gbp/errors.py"],
                                   ["gbp/__init__.py"])
 
+    def test_pack_mangle_prefix(self):
+        """Check if mangling prefix works"""
+        source = UpstreamSource(os.path.abspath("gbp/"))
+        target = self.tmpdir.join("gbp_0.1.tar.bz2")
+        repacked = source.pack(target, newprefix="foobar")
+        self._check_tar(repacked, ["foobar/errors.py", "foobar/__init__.py"])
+        repacked2 = source.pack(target, newprefix="")
+        self._check_tar(repacked2, ["./errors.py", "./__init__.py"])
+
 
 class TestZip(unittest.TestCase):
     """Test if unpacking zip archives works"""