| 24 | return filename |
| 25 | |
| 26 | def extract_package(filename, output_dir): |
| 27 | print('Extracting package:', filename) |
| 28 | # Detect the package format and open the package |
| 29 | if re.search(r'(\.tar(\..+)?|tgz)$', filename): |
| 30 | package = tarfile.open(filename) |
| 31 | members = package.getnames() |
| 32 | elif filename.endswith('.zip'): |
| 33 | package = ZipFile(filename) |
| 34 | members = package.namelist() |
| 35 | else: |
| 36 | raise Exception('unsupported package format') |
| 37 | # Avoid nesting two top-level directories with the same name |
| 38 | if os.path.commonpath(members) == os.path.basename(output_dir): |
| 39 | output_dir = os.path.dirname(output_dir) |
| 40 | # Create the output directory if it doesn't exist |
| 41 | if not os.path.isdir(output_dir): |
| 42 | os.makedirs(output_dir) |
| 43 | # Extract the package |
| 44 | package.extractall(output_dir) |
| 45 | package.close() |
| 46 | |
| 47 | def create_package(filename, input_dir): |
| 48 | print('Creating package:', filename) |