home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 October / maximum-cd-2011-10.iso / DiscContents / digsby_setup.exe / lib / distutils / archive_util.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2011-06-22  |  3.7 KB  |  137 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.6)
  3.  
  4. __revision__ = '$Id: archive_util.py 62904 2008-05-08 22:09:54Z benjamin.peterson $'
  5. import os
  6. from distutils.errors import DistutilsExecError
  7. from distutils.spawn import spawn
  8. from distutils.dir_util import mkpath
  9. from distutils import log
  10.  
  11. def make_tarball(base_name, base_dir, compress = 'gzip', verbose = 0, dry_run = 0):
  12.     compress_ext = {
  13.         'gzip': '.gz',
  14.         'bzip2': '.bz2',
  15.         'compress': '.Z' }
  16.     compress_flags = {
  17.         'gzip': [
  18.             '-f9'],
  19.         'compress': [
  20.             '-f'],
  21.         'bzip2': [
  22.             '-f9'] }
  23.     if compress is not None and compress not in compress_ext.keys():
  24.         raise ValueError, "bad value for 'compress': must be None, 'gzip', or 'compress'"
  25.     compress not in compress_ext.keys()
  26.     archive_name = base_name + '.tar'
  27.     mkpath(os.path.dirname(archive_name), dry_run = dry_run)
  28.     cmd = [
  29.         'tar',
  30.         '-cf',
  31.         archive_name,
  32.         base_dir]
  33.     spawn(cmd, dry_run = dry_run)
  34.     if compress:
  35.         spawn([
  36.             compress] + compress_flags[compress] + [
  37.             archive_name], dry_run = dry_run)
  38.         return archive_name + compress_ext[compress]
  39.     return archive_name
  40.  
  41.  
  42. def make_zipfile(base_name, base_dir, verbose = 0, dry_run = 0):
  43.     
  44.     try:
  45.         import zipfile
  46.     except ImportError:
  47.         zipfile = None
  48.  
  49.     zip_filename = base_name + '.zip'
  50.     mkpath(os.path.dirname(zip_filename), dry_run = dry_run)
  51.     if zipfile is None:
  52.         if verbose:
  53.             zipoptions = '-r'
  54.         else:
  55.             zipoptions = '-rq'
  56.         
  57.         try:
  58.             spawn([
  59.                 'zip',
  60.                 zipoptions,
  61.                 zip_filename,
  62.                 base_dir], dry_run = dry_run)
  63.         except DistutilsExecError:
  64.             raise DistutilsExecError, "unable to create zip file '%s': could neither import the 'zipfile' module nor find a standalone zip utility" % zip_filename
  65.         except:
  66.             None<EXCEPTION MATCH>DistutilsExecError
  67.         
  68.  
  69.     None<EXCEPTION MATCH>DistutilsExecError
  70.     log.info("creating '%s' and adding '%s' to it", zip_filename, base_dir)
  71.     if not dry_run:
  72.         z = zipfile.ZipFile(zip_filename, 'w', compression = zipfile.ZIP_DEFLATED)
  73.         for dirpath, dirnames, filenames in os.walk(base_dir):
  74.             for name in filenames:
  75.                 path = os.path.normpath(os.path.join(dirpath, name))
  76.                 if os.path.isfile(path):
  77.                     z.write(path, path)
  78.                     log.info("adding '%s'" % path)
  79.                     continue
  80.             
  81.         
  82.         z.close()
  83.     
  84.     return zip_filename
  85.  
  86. ARCHIVE_FORMATS = {
  87.     'gztar': (make_tarball, [
  88.         ('compress', 'gzip')], "gzip'ed tar-file"),
  89.     'bztar': (make_tarball, [
  90.         ('compress', 'bzip2')], "bzip2'ed tar-file"),
  91.     'ztar': (make_tarball, [
  92.         ('compress', 'compress')], 'compressed tar file'),
  93.     'tar': (make_tarball, [
  94.         ('compress', None)], 'uncompressed tar file'),
  95.     'zip': (make_zipfile, [], 'ZIP file') }
  96.  
  97. def check_archive_formats(formats):
  98.     for format in formats:
  99.         if format not in ARCHIVE_FORMATS:
  100.             return format
  101.     else:
  102.         return None
  103.     return format not in ARCHIVE_FORMATS
  104.  
  105.  
  106. def make_archive(base_name, format, root_dir = None, base_dir = None, verbose = 0, dry_run = 0):
  107.     save_cwd = os.getcwd()
  108.     if root_dir is not None:
  109.         log.debug("changing into '%s'", root_dir)
  110.         base_name = os.path.abspath(base_name)
  111.         if not dry_run:
  112.             os.chdir(root_dir)
  113.         
  114.     
  115.     if base_dir is None:
  116.         base_dir = os.curdir
  117.     
  118.     kwargs = {
  119.         'dry_run': dry_run }
  120.     
  121.     try:
  122.         format_info = ARCHIVE_FORMATS[format]
  123.     except KeyError:
  124.         raise ValueError, "unknown archive format '%s'" % format
  125.  
  126.     func = format_info[0]
  127.     for arg, val in format_info[1]:
  128.         kwargs[arg] = val
  129.     
  130.     filename = apply(func, (base_name, base_dir), kwargs)
  131.     if root_dir is not None:
  132.         log.debug("changing back to '%s'", save_cwd)
  133.         os.chdir(save_cwd)
  134.     
  135.     return filename
  136.  
  137.