From: Markus Lehtonen Date: Thu, 14 Jun 2012 16:30:26 +0000 (+0300) Subject: UpstreamSource.pack: support prefix mangling X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=116149e77d7e9e079609f57d2235b5c4cb86468c;p=tools%2Fgit-buildpackage.git UpstreamSource.pack: support prefix mangling 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 --- diff --git a/gbp/command_wrappers.py b/gbp/command_wrappers.py index de67a1f2..11891422 100644 --- a/gbp/command_wrappers.py +++ b/gbp/command_wrappers.py @@ -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 diff --git a/gbp/pkg/__init__.py b/gbp/pkg/__init__.py index bbc0dc3f..3f125e1b 100644 --- a/gbp/pkg/__init__.py +++ b/gbp/pkg/__init__.py @@ -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 diff --git a/gbp/scripts/common/import_orig.py b/gbp/scripts/common/import_orig.py index c89d7341..61bed3e6 100644 --- a/gbp/scripts/common/import_orig.py +++ b/gbp/scripts/common/import_orig.py @@ -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 diff --git a/tests/06_test_upstream_source.py b/tests/06_test_upstream_source.py index 34787bf4..ac210a03 100644 --- a/tests/06_test_upstream_source.py +++ b/tests/06_test_upstream_source.py @@ -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"""