diff options
author | Helmut Grohne <helmut@subdivi.de> | 2014-05-11 15:57:36 +0200 |
---|---|---|
committer | Helmut Grohne <helmut@subdivi.de> | 2014-05-11 15:57:36 +0200 |
commit | 0097cdc7ffa881427f72ac35428de4214a26d834 (patch) | |
tree | 9e4b22e54b413326c9f1b7a071a4c2f20c162e18 | |
parent | e2d7f5e1e1ef06c28ca432bc070595b87d78ac85 (diff) | |
download | debian-dedup-0097cdc7ffa881427f72ac35428de4214a26d834.tar.gz |
importpkg: reduce copy&paste
-rwxr-xr-x | importpkg.py | 48 |
1 files changed, 15 insertions, 33 deletions
diff --git a/importpkg.py b/importpkg.py index 247965f..06d9da4 100755 --- a/importpkg.py +++ b/importpkg.py @@ -44,6 +44,15 @@ def gifhash(): hashobj.name = "gif_sha512" return hashobj +def decompress_tar(filelike, extension): + if extension in (".lzma", ".xz"): + filelike = DecompressedStream(filelike, lzma.LZMADecompressor()) + extension = "" + if extension not in ("", ".gz", ".bz2"): + raise ValueError("unknown compression format with extension %r" % + extension) + return tarfile.open(fileobj=filelike, mode="r|" + extension[1:]) + def process_package(filelike, hash_functions): af = ArReader(filelike) af.read_magic() @@ -53,39 +62,11 @@ def process_package(filelike, hash_functions): name = af.read_entry() except EOFError: raise ValueError("data.tar not found") - if name == "control.tar.gz": - new_state = "control" - tf = tarfile.open(fileobj=af, mode="r|gz") - elif name == "control.tar.xz": - new_state = "control" - zf = DecompressedStream(af, lzma.LZMADecompressor()) - tf = tarfile.open(fileobj=zf, mode="r|") - elif name == "control.tar": - new_state = "control" - tf = tarfile.open(fileobj=af, mode="r|") - elif name == "data.tar.gz": - new_state = "data" - tf = tarfile.open(fileobj=af, mode="r|gz") - elif name == "data.tar.bz2": - new_state = "data" - tf = tarfile.open(fileobj=af, mode="r|bz2") - elif name == "data.tar.lzma": - new_state = "data" - zf = DecompressedStream(af, lzma.LZMADecompressor()) - tf = tarfile.open(fileobj=zf, mode="r|") - elif name == "data.tar.xz": - new_state = "data" - zf = DecompressedStream(af, lzma.LZMADecompressor()) - tf = tarfile.open(fileobj=zf, mode="r|") - elif name == "data.tar": - new_state = "data" - tf = tarfile.open(fileobj=af, mode="r|") - else: - continue - if new_state == "control": + if name.startswith("control.tar"): if state != "start": raise ValueError("unexpected control.tar") - state = new_state + state = "control" + tf = decompress_tar(af, name[11:]) for elem in tf: if elem.name != "./control": continue @@ -95,10 +76,11 @@ def process_package(filelike, hash_functions): yield process_control(tf.extractfile(elem).read()) break continue - elif new_state == "data": + elif name.startswith("data.tar"): if state != "control_file": raise ValueError("missing control file") - state = new_state + state = "data" + tf = decompress_tar(af, name[8:]) for name, size, hashes in get_tar_hashes(tf, hash_functions): try: name = name.decode("utf8") |