|
5 | 5 | import hashlib |
6 | 6 | import sys |
7 | 7 | import zipfile |
| 8 | +import tarfile |
8 | 9 |
|
9 | 10 | def formatSize(amt): |
10 | 11 | """Format a size as a string in MB""" |
@@ -50,8 +51,23 @@ def md5sum(targetfile): |
50 | 51 | chunk = f.read(1024) |
51 | 52 | return digest.hexdigest() |
52 | 53 |
|
| 54 | +def unpackWithMode(unpacker, packedfile, parent_path, mode): |
| 55 | + with unpacker.open(packedfile, mode) as icuzip: |
| 56 | + print ' Extracting source file: %s' % packedfile |
| 57 | + icuzip.extractall(parent_path) |
| 58 | + |
53 | 59 | def unpack(packedfile, parent_path): |
54 | 60 | """Unpack packedfile into parent_path. Assumes .zip.""" |
55 | | - with zipfile.ZipFile(packedfile, 'r') as icuzip: |
56 | | - print ' Extracting source zip: %s' % packedfile |
57 | | - icuzip.extractall(parent_path) |
| 61 | + packedsuffix = packedfile.lower().split('.')[-1] # .zip, .tgz etc |
| 62 | + if zipfile.is_zipfile(packedfile): |
| 63 | + unpackWithMode(zipfile.ZipFile, packedfile, parent_path, 'r') |
| 64 | + elif tarfile.is_tarfile(packedfile): |
| 65 | + # if (packedsuffix == 'tgz') or (packedsuffix == 'gz'): |
| 66 | + # unpackWithMode(tarfile.TarFile, packedfile, parent_path, 'r:gz') |
| 67 | + # elif (packedsuffix == 'bz2') or (packedsuffix == 'tb2'): |
| 68 | + # unpackWithMode(tarfile.TarFile, packedfile, parent_path, 'r:bz2') |
| 69 | + # else: |
| 70 | + unpackWithMode(tarfile.TarFile, packedfile, parent_path, 'r') |
| 71 | + #raise Exception('Error: Don\'t know how to unpack tarfile %s with extension %s' % (packedfile, packedsuffix)) |
| 72 | + else: |
| 73 | + raise Exception('Error: Don\'t know how to unpack %s with extension %s' % (packedfile, packedsuffix)) |
0 commit comments