Back to index

python3.2  3.2.2
Functions | Variables
distutils.archive_util Namespace Reference

Functions

def make_tarball
def make_zipfile
def check_archive_formats
def make_archive

Variables

 zipfile = None
dictionary ARCHIVE_FORMATS

Detailed Description

distutils.archive_util

Utility functions for creating archive files (tarballs, zip files,
that sort of thing).

Function Documentation

Returns the first format from the 'format' list that is unknown.

If all formats are known, returns None

Definition at line 132 of file archive_util.py.

00132 
00133 def check_archive_formats(formats):
00134     """Returns the first format from the 'format' list that is unknown.
00135 
00136     If all formats are known, returns None
00137     """
00138     for format in formats:
00139         if format not in ARCHIVE_FORMATS:
00140             return format
00141     return None

Here is the call graph for this function:

Here is the caller graph for this function:

def distutils.archive_util.make_archive (   base_name,
  format,
  root_dir = None,
  base_dir = None,
  verbose = 0,
  dry_run = 0 
)
Create an archive file (eg. zip or tar).

'base_name' is the name of the file to create, minus any format-specific
extension; 'format' is the archive format: one of "zip", "tar", "ztar",
or "gztar".

'root_dir' is a directory that will be the root directory of the
archive; ie. we typically chdir into 'root_dir' before creating the
archive.  'base_dir' is the directory where we start archiving from;
ie. 'base_dir' will be the common prefix of all files and
directories in the archive.  'root_dir' and 'base_dir' both default
to the current directory.  Returns the name of the archive file.

Definition at line 143 of file archive_util.py.

00143 
00144                  dry_run=0):
00145     """Create an archive file (eg. zip or tar).
00146 
00147     'base_name' is the name of the file to create, minus any format-specific
00148     extension; 'format' is the archive format: one of "zip", "tar", "ztar",
00149     or "gztar".
00150 
00151     'root_dir' is a directory that will be the root directory of the
00152     archive; ie. we typically chdir into 'root_dir' before creating the
00153     archive.  'base_dir' is the directory where we start archiving from;
00154     ie. 'base_dir' will be the common prefix of all files and
00155     directories in the archive.  'root_dir' and 'base_dir' both default
00156     to the current directory.  Returns the name of the archive file.
00157     """
00158     save_cwd = os.getcwd()
00159     if root_dir is not None:
00160         log.debug("changing into '%s'", root_dir)
00161         base_name = os.path.abspath(base_name)
00162         if not dry_run:
00163             os.chdir(root_dir)
00164 
00165     if base_dir is None:
00166         base_dir = os.curdir
00167 
00168     kwargs = {'dry_run': dry_run}
00169 
00170     try:
00171         format_info = ARCHIVE_FORMATS[format]
00172     except KeyError:
00173         raise ValueError("unknown archive format '%s'" % format)
00174 
00175     func = format_info[0]
00176     for arg, val in format_info[1]:
00177         kwargs[arg] = val
00178     try:
00179         filename = func(base_name, base_dir, **kwargs)
00180     finally:
00181         if root_dir is not None:
00182             log.debug("changing back to '%s'", save_cwd)
00183             os.chdir(save_cwd)
00184 
00185     return filename

Here is the caller graph for this function:

def distutils.archive_util.make_tarball (   base_name,
  base_dir,
  compress = "gzip",
  verbose = 0,
  dry_run = 0 
)
Create a (possibly compressed) tar file from all the files under
'base_dir'.

'compress' must be "gzip" (the default), "compress", "bzip2", or None.
Both "tar" and the compression utility named by 'compress' must be on
the default program search path, so this is probably Unix-specific.
The output tar file will be named 'base_dir' +  ".tar", possibly plus
the appropriate compression extension (".gz", ".bz2" or ".Z").
Returns the output filename.

Definition at line 21 of file archive_util.py.

00021 
00022 def make_tarball(base_name, base_dir, compress="gzip", verbose=0, dry_run=0):
00023     """Create a (possibly compressed) tar file from all the files under
00024     'base_dir'.
00025 
00026     'compress' must be "gzip" (the default), "compress", "bzip2", or None.
00027     Both "tar" and the compression utility named by 'compress' must be on
00028     the default program search path, so this is probably Unix-specific.
00029     The output tar file will be named 'base_dir' +  ".tar", possibly plus
00030     the appropriate compression extension (".gz", ".bz2" or ".Z").
00031     Returns the output filename.
00032     """
00033     tar_compression = {'gzip': 'gz', 'bzip2': 'bz2', None: '', 'compress': ''}
00034     compress_ext = {'gzip': '.gz', 'bzip2': '.bz2', 'compress': '.Z'}
00035 
00036     # flags for compression program, each element of list will be an argument
00037     if compress is not None and compress not in compress_ext.keys():
00038         raise ValueError(
00039               "bad value for 'compress': must be None, 'gzip', 'bzip2' "
00040               "or 'compress'")
00041 
00042     archive_name = base_name + '.tar'
00043     if compress != 'compress':
00044         archive_name += compress_ext.get(compress, '')
00045 
00046     mkpath(os.path.dirname(archive_name), dry_run=dry_run)
00047 
00048     # creating the tarball
00049     import tarfile  # late import so Python build itself doesn't break
00050 
00051     log.info('Creating tar archive')
00052     if not dry_run:
00053         tar = tarfile.open(archive_name, 'w|%s' % tar_compression[compress])
00054         try:
00055             tar.add(base_dir)
00056         finally:
00057             tar.close()
00058 
00059     # compression using `compress`
00060     if compress == 'compress':
00061         warn("'compress' will be deprecated.", PendingDeprecationWarning)
00062         # the option varies depending on the platform
00063         compressed_name = archive_name + compress_ext[compress]
00064         if sys.platform == 'win32':
00065             cmd = [compress, archive_name, compressed_name]
00066         else:
00067             cmd = [compress, '-f', archive_name]
00068         spawn(cmd, dry_run=dry_run)
00069         return compressed_name
00070 
00071     return archive_name

Here is the call graph for this function:

Here is the caller graph for this function:

def distutils.archive_util.make_zipfile (   base_name,
  base_dir,
  verbose = 0,
  dry_run = 0 
)
Create a zip file from all the files under 'base_dir'.

The output zip file will be named 'base_name' + ".zip".  Uses either the
"zipfile" Python module (if available) or the InfoZIP "zip" utility
(if installed and found on the default search path).  If neither tool is
available, raises DistutilsExecError.  Returns the name of the output zip
file.

Definition at line 72 of file archive_util.py.

00072 
00073 def make_zipfile(base_name, base_dir, verbose=0, dry_run=0):
00074     """Create a zip file from all the files under 'base_dir'.
00075 
00076     The output zip file will be named 'base_name' + ".zip".  Uses either the
00077     "zipfile" Python module (if available) or the InfoZIP "zip" utility
00078     (if installed and found on the default search path).  If neither tool is
00079     available, raises DistutilsExecError.  Returns the name of the output zip
00080     file.
00081     """
00082     zip_filename = base_name + ".zip"
00083     mkpath(os.path.dirname(zip_filename), dry_run=dry_run)
00084 
00085     # If zipfile module is not available, try spawning an external
00086     # 'zip' command.
00087     if zipfile is None:
00088         if verbose:
00089             zipoptions = "-r"
00090         else:
00091             zipoptions = "-rq"
00092 
00093         try:
00094             spawn(["zip", zipoptions, zip_filename, base_dir],
00095                   dry_run=dry_run)
00096         except DistutilsExecError:
00097             # XXX really should distinguish between "couldn't find
00098             # external 'zip' command" and "zip failed".
00099             raise DistutilsExecError(("unable to create zip file '%s': "
00100                    "could neither import the 'zipfile' module nor "
00101                    "find a standalone zip utility") % zip_filename)
00102 
00103     else:
00104         log.info("creating '%s' and adding '%s' to it",
00105                  zip_filename, base_dir)
00106 
00107         if not dry_run:
00108             try:
00109                 zip = zipfile.ZipFile(zip_filename, "w",
00110                                       compression=zipfile.ZIP_DEFLATED)
00111             except RuntimeError:
00112                 zip = zipfile.ZipFile(zip_filename, "w",
00113                                       compression=zipfile.ZIP_STORED)
00114 
00115             for dirpath, dirnames, filenames in os.walk(base_dir):
00116                 for name in filenames:
00117                     path = os.path.normpath(os.path.join(dirpath, name))
00118                     if os.path.isfile(path):
00119                         zip.write(path, path)
00120                         log.info("adding '%s'" % path)
00121             zip.close()
00122 
00123     return zip_filename

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

Initial value:
00001 {
00002     'gztar': (make_tarball, [('compress', 'gzip')], "gzip'ed tar-file"),
00003     'bztar': (make_tarball, [('compress', 'bzip2')], "bzip2'ed tar-file"),
00004     'ztar':  (make_tarball, [('compress', 'compress')], "compressed tar file"),
00005     'tar':   (make_tarball, [('compress', None)], "uncompressed tar file"),
00006     'zip':   (make_zipfile, [],"ZIP file")
00007     }

Definition at line 124 of file archive_util.py.

Definition at line 13 of file archive_util.py.