1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
"""
"""
#*****************************************************************************
# Copyright (C) 2016 Volker Braun <vbraun.name@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
# http://www.gnu.org/licenses/
#*****************************************************************************
from __future__ import print_function
import os
from sage_bootstrap.uncompress.tar_file import SageTarFile
from sage_bootstrap.uncompress.zip_file import SageZipFile
ARCHIVE_TYPES = [SageTarFile, SageZipFile]
def open_archive(filename):
"""
Automatically detect archive type
"""
for cls in ARCHIVE_TYPES:
if cls.can_read(filename):
break
else:
raise ValueError
# For now ZipFile and TarFile both have default open modes that are
# acceptable
return cls(filename)
def unpack_archive(archive, dirname=None):
"""
Unpack archive
"""
top_level = None
if dirname:
top_levels = set()
for member in archive.names:
# Zip and tar files all use forward slashes as separators
# internally
top_levels.add(member.split('/', 1)[0])
if len(top_levels) == 1:
top_level = top_levels.pop()
else:
os.makedirs(dirname)
prev_cwd = os.getcwd()
if dirname and not top_level:
# We want to extract content into dirname, but there is not
# a single top-level directory for the tarball, so we cd into
# the extraction target first
os.chdir(dirname)
try:
archive.extractall(members=archive.names)
if dirname and top_level:
os.rename(top_level, dirname)
finally:
os.chdir(prev_cwd)
|