diff options
author | Guido Günther <agx@sigxcpu.org> | 2011-07-24 22:57:01 +0200 |
---|---|---|
committer | Guido Günther <agx@sigxcpu.org> | 2011-07-25 17:15:42 +0200 |
commit | 8d0143a7c1eb8f852ac2c95024a3cb438deb5c7f (patch) | |
tree | 9a0b334c5837c1ae8ec514c4df32abc0941b180c /gbp | |
parent | 00e1d97f2e940c6d7085d428541c4ba7467fc3e3 (diff) |
git-import-orig: support filters for all input formats
Closes: #628645
Diffstat (limited to 'gbp')
-rw-r--r-- | gbp/command_wrappers.py | 11 | ||||
-rw-r--r-- | gbp/deb.py | 138 |
2 files changed, 111 insertions, 38 deletions
diff --git a/gbp/command_wrappers.py b/gbp/command_wrappers.py index 2f77a7b4..49fc966d 100644 --- a/gbp/command_wrappers.py +++ b/gbp/command_wrappers.py @@ -129,7 +129,7 @@ class PristineTar(Command): class UnpackTarArchive(Command): - """Wrap tar to Unpack a gzipped tar archive""" + """Wrap tar to unpack a compressed tar archive""" def __init__(self, archive, dir, filters=[]): self.archive = archive self.dir = dir @@ -144,18 +144,19 @@ class UnpackTarArchive(Command): self.run_error = 'Couldn\'t unpack "%s"' % self.archive -class RepackTarArchive(Command): - """Wrap tar to Repack a gzipped tar archive""" - def __init__(self, archive, dir, dest): +class PackTarArchive(Command): + """Wrap tar to pack a compressed tar archive""" + def __init__(self, archive, dir, dest, filters=[]): self.archive = archive self.dir = dir + exclude = [("--exclude=%s" % filter) for filter in filters] if archive.lower().endswith(".bz2"): compress = "--bzip2" else: compress = "--gzip" - Command.__init__(self, 'tar', ['-C', dir, compress, '-cf', archive, dest]) + Command.__init__(self, 'tar', exclude + ['-C', dir, compress, '-cf', archive, dest]) self.run_error = 'Couldn\'t repack "%s"' % self.archive @@ -166,6 +166,111 @@ class DscFile(object): return "<%s object %s>" % (self.__class__.__name__, self.dscfile) +class UpstreamSource(object): + """ + Upstream source. Can be either an unpacked dir, a tarball or another type + or archive + + @cvar is_dir: are the upstream sources an unpacked dir + @type is_dir: boolean + @cvar _orig: are the upstream sources already suitable as an upstream + tarball + @type _irog: boolen + @cvar _path: path to the upstream sources + @type _path: string + @cvar _unpacked: path to the unpacked source tree + @type _unpacked: string + """ + def __init__(self, name, unpacked=None): + self.is_dir = False + self._orig = False + self._path = name + self.unpacked = unpacked + + self.is_dir = [False, True][os.path.isdir(name)] + self._check_orig() + if self.is_dir: + self.unpacked = self._path + + def _check_orig(self): + """Check if archive can be used as orig tarball""" + if self.is_dir: + self._orig = False + return + + parts = self._path.split('.') + try: + if parts[-2] == 'tar': + if (parts[-1] in compressor_opts or + parts[-1] in compressor_aliases): + self._orig = True + except IndexError: + self._orig = False + + @property + def is_orig(self): + return self._orig + + @property + def path(self): + return self._path + + def unpack(self, dir, filters=[]): + """ + Unpack packed upstream sources into a given directory + and determine the toplevel of the source tree. + """ + if self.is_dir: + raise GbpError, "Cannot unpack directory %s" % self.path + + if type(filters) != type([]): + raise GbpError, "Filters must be a list" + + self._unpack_archive(dir, filters) + self.unpacked = tar_toplevel(dir) + + def _unpack_archive(self, dir, filters): + """ + Unpack packed upstream sources into a given directory. + """ + ext = os.path.splitext(self.path)[1] + if ext in [ ".zip", ".xpi" ]: + try: + gbpc.UnpackZipArchive(self.path, dir)() + except gbpc.CommandExecFailed: + raise GbpError, "Unpacking of %s failed" % self.path + else: + unpack_orig(self.path, dir, filters) + + def pack(self, newarchive, filters=[]): + """ + recreate a new archive from the current one + + @param newarchive: the name of the new archive + @type newarchive: string + @param filters: tar filters to apply + @type filters: array of strings + @return: the new upstream source + @rtype: UpstreamSource + """ + if not self.unpacked: + raise GbpError, "Need an unpacked source tree to repack" + + if type(filters) != type([]): + raise GbpError, "Filters must be a list" + + try: + repackArchive = gbpc.PackTarArchive(newarchive, + os.path.dirname(self.unpacked), + os.path.basename(self.unpacked), + filters) + repackArchive() + except gbpc.CommandExecFailed: + # repackArchive already printed an error + raise GbpError + return UpstreamSource(newarchive) + + def parse_dsc(dscfile): """parse dsc by creating a DscFile object""" try: @@ -412,39 +517,6 @@ def unpack_orig(archive, tmpdir, filters): return unpackArchive.dir -def unpack_upstream_source(archive, tmpdir, filters): - """ - Unpack upstream sources into tmpdir - - @return: Return true if the importet archive is suitable as an upstream - tarball - @rtype: boolean - """ - ext = os.path.splitext(archive)[1] - if ext in [ ".zip", ".xpi" ]: - try: - gbpc.UnpackZipArchive(archive, tmpdir)() - except gbpc.CommandExecFailed: - raise GbpError, "Unpacking of %s failed" % archive - return False - else: - unpack_orig(archive, tmpdir, filters) - return True - - -def repack_orig(archive, tmpdir, dest): - """ - recreate a new .orig.tar.gz from tmpdir (useful when using filter option) - """ - try: - repackArchive = gbpc.RepackTarArchive(archive, tmpdir, dest) - repackArchive() - except gbpc.CommandExecFailed: - # repackArchive already printed an error - raise GbpError - return repackArchive.dir - - def tar_toplevel(dir): """tar archives can contain a leading directory not""" unpacked = glob.glob('%s/*' % dir) |