1010import fnmatch
1111import collections
1212import errno
13- import tarfile
13+
14+ try :
15+ import zlib
16+ del zlib
17+ _ZLIB_SUPPORTED = True
18+ except ImportError :
19+ _ZLIB_SUPPORTED = False
1420
1521try :
1622 import bz2
@@ -602,23 +608,22 @@ def _make_tarball(base_name, base_dir, compress="gzip", verbose=0, dry_run=0,
602608
603609 Returns the output filename.
604610 """
605- tar_compression = {'gzip' : 'gz' , None : '' }
606- compress_ext = {'gzip' : '.gz' }
607-
608- if _BZ2_SUPPORTED :
609- tar_compression ['bzip2' ] = 'bz2'
610- compress_ext ['bzip2' ] = '.bz2'
611-
612- if _LZMA_SUPPORTED :
613- tar_compression ['xz' ] = 'xz'
614- compress_ext ['xz' ] = '.xz'
615-
616- # flags for compression program, each element of list will be an argument
617- if compress is not None and compress not in compress_ext :
611+ if compress is None :
612+ tar_compression = ''
613+ elif _ZLIB_SUPPORTED and compress == 'gzip' :
614+ tar_compression = 'gz'
615+ elif _BZ2_SUPPORTED and compress == 'bzip2' :
616+ tar_compression = 'bz2'
617+ elif _LZMA_SUPPORTED and compress == 'xz' :
618+ tar_compression = 'xz'
619+ else :
618620 raise ValueError ("bad value for 'compress', or compression format not "
619621 "supported : {0}" .format (compress ))
620622
621- archive_name = base_name + '.tar' + compress_ext .get (compress , '' )
623+ import tarfile # late import for breaking circular dependency
624+
625+ compress_ext = '.' + tar_compression if compress else ''
626+ archive_name = base_name + '.tar' + compress_ext
622627 archive_dir = os .path .dirname (archive_name )
623628
624629 if archive_dir and not os .path .exists (archive_dir ):
@@ -644,7 +649,7 @@ def _set_uid_gid(tarinfo):
644649 return tarinfo
645650
646651 if not dry_run :
647- tar = tarfile .open (archive_name , 'w|%s' % tar_compression [ compress ] )
652+ tar = tarfile .open (archive_name , 'w|%s' % tar_compression )
648653 try :
649654 tar .add (base_dir , filter = _set_uid_gid )
650655 finally :
@@ -655,13 +660,10 @@ def _set_uid_gid(tarinfo):
655660def _make_zipfile (base_name , base_dir , verbose = 0 , dry_run = 0 , logger = None ):
656661 """Create a zip file from all the files under 'base_dir'.
657662
658- The output zip file will be named 'base_name' + ".zip". Uses either the
659- "zipfile" Python module (if available) or the InfoZIP "zip" utility
660- (if installed and found on the default search path). If neither tool is
661- available, raises ExecError. Returns the name of the output zip
662- file.
663+ The output zip file will be named 'base_name' + ".zip". Returns the
664+ name of the output zip file.
663665 """
664- import zipfile
666+ import zipfile # late import for breaking circular dependency
665667
666668 zip_filename = base_name + ".zip"
667669 archive_dir = os .path .dirname (base_name )
@@ -700,10 +702,13 @@ def _make_zipfile(base_name, base_dir, verbose=0, dry_run=0, logger=None):
700702 return zip_filename
701703
702704_ARCHIVE_FORMATS = {
703- 'gztar' : (_make_tarball , [('compress' , 'gzip' )], "gzip'ed tar-file" ),
704705 'tar' : (_make_tarball , [('compress' , None )], "uncompressed tar file" ),
705- 'zip' : (_make_zipfile , [], "ZIP file" )
706- }
706+ }
707+
708+ if _ZLIB_SUPPORTED :
709+ _ARCHIVE_FORMATS ['gztar' ] = (_make_tarball , [('compress' , 'gzip' )],
710+ "gzip'ed tar-file" )
711+ _ARCHIVE_FORMATS ['zip' ] = (_make_zipfile , [], "ZIP file" )
707712
708713if _BZ2_SUPPORTED :
709714 _ARCHIVE_FORMATS ['bztar' ] = (_make_tarball , [('compress' , 'bzip2' )],
@@ -752,8 +757,8 @@ def make_archive(base_name, format, root_dir=None, base_dir=None, verbose=0,
752757 """Create an archive file (eg. zip or tar).
753758
754759 'base_name' is the name of the file to create, minus any format-specific
755- extension; 'format' is the archive format: one of "zip", "tar", "bztar"
756- or "gztar" .
760+ extension; 'format' is the archive format: one of "zip", "tar", "gztar",
761+ "bztar", or "xztar". Or any other registered format .
757762
758763 'root_dir' is a directory that will be the root directory of the
759764 archive; ie. we typically chdir into 'root_dir' before creating the
@@ -866,10 +871,7 @@ def _ensure_directory(path):
866871def _unpack_zipfile (filename , extract_dir ):
867872 """Unpack zip `filename` to `extract_dir`
868873 """
869- try :
870- import zipfile
871- except ImportError :
872- raise ReadError ('zlib not supported, cannot unpack this archive.' )
874+ import zipfile # late import for breaking circular dependency
873875
874876 if not zipfile .is_zipfile (filename ):
875877 raise ReadError ("%s is not a zip file" % filename )
@@ -903,6 +905,7 @@ def _unpack_zipfile(filename, extract_dir):
903905def _unpack_tarfile (filename , extract_dir ):
904906 """Unpack tar/tar.gz/tar.bz2/tar.xz `filename` to `extract_dir`
905907 """
908+ import tarfile # late import for breaking circular dependency
906909 try :
907910 tarobj = tarfile .open (filename )
908911 except tarfile .TarError :
@@ -914,10 +917,13 @@ def _unpack_tarfile(filename, extract_dir):
914917 tarobj .close ()
915918
916919_UNPACK_FORMATS = {
917- 'gztar' : (['.tar.gz' , '.tgz' ], _unpack_tarfile , [], "gzip'ed tar-file" ),
918920 'tar' : (['.tar' ], _unpack_tarfile , [], "uncompressed tar file" ),
919- 'zip' : (['.zip' ], _unpack_zipfile , [], "ZIP file" )
920- }
921+ 'zip' : (['.zip' ], _unpack_zipfile , [], "ZIP file" ),
922+ }
923+
924+ if _ZLIB_SUPPORTED :
925+ _UNPACK_FORMATS ['gztar' ] = (['.tar.gz' , '.tgz' ], _unpack_tarfile , [],
926+ "gzip'ed tar-file" )
921927
922928if _BZ2_SUPPORTED :
923929 _UNPACK_FORMATS ['bztar' ] = (['.tar.bz2' , '.tbz2' ], _unpack_tarfile , [],
@@ -942,10 +948,10 @@ def unpack_archive(filename, extract_dir=None, format=None):
942948 `extract_dir` is the name of the target directory, where the archive
943949 is unpacked. If not provided, the current working directory is used.
944950
945- `format` is the archive format: one of "zip", "tar", or "gztar". Or any
946- other registered format. If not provided, unpack_archive will use the
947- filename extension and see if an unpacker was registered for that
948- extension.
951+ `format` is the archive format: one of "zip", "tar", "gztar", "bztar",
952+ or "xztar". Or any other registered format. If not provided,
953+ unpack_archive will use the filename extension and see if an unpacker
954+ was registered for that extension.
949955
950956 In case none is found, a ValueError is raised.
951957 """
0 commit comments