archive_util.py (5297B)
1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\carbon\common\stdlib\distutils\archive_util.py 2 __revision__ = '$Id: archive_util.py 82110 2010-06-20 13:38:51Z kristjan.jonsson $' 3 import os 4 from warnings import warn 5 import sys 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 try: 11 from pwd import getpwnam 12 except ImportError: 13 getpwnam = None 14 15 try: 16 from grp import getgrnam 17 except ImportError: 18 getgrnam = None 19 20 def _get_gid(name): 21 if getgrnam is None or name is None: 22 return 23 try: 24 result = getgrnam(name) 25 except KeyError: 26 result = None 27 28 if result is not None: 29 return result[2] 30 31 32 def _get_uid(name): 33 if getpwnam is None or name is None: 34 return 35 try: 36 result = getpwnam(name) 37 except KeyError: 38 result = None 39 40 if result is not None: 41 return result[2] 42 43 44 def make_tarball(base_name, base_dir, compress = 'gzip', verbose = 0, dry_run = 0, owner = None, group = None): 45 tar_compression = {'gzip': 'gz', 46 'bzip2': 'bz2', 47 None: '', 48 'compress': ''} 49 compress_ext = {'gzip': '.gz', 50 'bzip2': '.bz2', 51 'compress': '.Z'} 52 if compress is not None and compress not in compress_ext.keys(): 53 raise ValueError, "bad value for 'compress': must be None, 'gzip', 'bzip2' or 'compress'" 54 archive_name = base_name + '.tar' 55 if compress != 'compress': 56 archive_name += compress_ext.get(compress, '') 57 mkpath(os.path.dirname(archive_name), dry_run=dry_run) 58 import tarfile 59 log.info('Creating tar archive') 60 uid = _get_uid(owner) 61 gid = _get_gid(group) 62 63 def _set_uid_gid(tarinfo): 64 if gid is not None: 65 tarinfo.gid = gid 66 tarinfo.gname = group 67 if uid is not None: 68 tarinfo.uid = uid 69 tarinfo.uname = owner 70 return tarinfo 71 72 if not dry_run: 73 tar = tarfile.open(archive_name, 'w|%s' % tar_compression[compress]) 74 try: 75 tar.add(base_dir, filter=_set_uid_gid) 76 finally: 77 tar.close() 78 79 if compress == 'compress': 80 warn("'compress' will be deprecated.", PendingDeprecationWarning) 81 compressed_name = archive_name + compress_ext[compress] 82 if sys.platform == 'win32': 83 cmd = [compress, archive_name, compressed_name] 84 else: 85 cmd = [compress, '-f', archive_name] 86 spawn(cmd, dry_run=dry_run) 87 return compressed_name 88 return archive_name 89 90 91 def make_zipfile(base_name, base_dir, verbose = 0, dry_run = 0): 92 try: 93 import zipfile 94 except ImportError: 95 zipfile = None 96 97 zip_filename = base_name + '.zip' 98 mkpath(os.path.dirname(zip_filename), dry_run=dry_run) 99 if zipfile is None: 100 if verbose: 101 zipoptions = '-r' 102 else: 103 zipoptions = '-rq' 104 try: 105 spawn(['zip', 106 zipoptions, 107 zip_filename, 108 base_dir], dry_run=dry_run) 109 except DistutilsExecError: 110 raise DistutilsExecError, "unable to create zip file '%s': could neither import the 'zipfile' module nor find a standalone zip utility" % zip_filename 111 112 else: 113 log.info("creating '%s' and adding '%s' to it", zip_filename, base_dir) 114 if not dry_run: 115 zip = zipfile.ZipFile(zip_filename, 'w', compression=zipfile.ZIP_DEFLATED) 116 for dirpath, dirnames, filenames in os.walk(base_dir): 117 for name in filenames: 118 path = os.path.normpath(os.path.join(dirpath, name)) 119 if os.path.isfile(path): 120 zip.write(path, path) 121 log.info("adding '%s'" % path) 122 123 zip.close() 124 return zip_filename 125 126 127 ARCHIVE_FORMATS = {'gztar': (make_tarball, [('compress', 'gzip')], "gzip'ed tar-file"), 128 'bztar': (make_tarball, [('compress', 'bzip2')], "bzip2'ed tar-file"), 129 'ztar': (make_tarball, [('compress', 'compress')], 'compressed tar file'), 130 'tar': (make_tarball, [('compress', None)], 'uncompressed tar file'), 131 'zip': (make_zipfile, [], 'ZIP file')} 132 133 def check_archive_formats(formats): 134 for format in formats: 135 if format not in ARCHIVE_FORMATS: 136 return format 137 138 139 def make_archive(base_name, format, root_dir = None, base_dir = None, verbose = 0, dry_run = 0, owner = None, group = None): 140 save_cwd = os.getcwd() 141 if root_dir is not None: 142 log.debug("changing into '%s'", root_dir) 143 base_name = os.path.abspath(base_name) 144 if not dry_run: 145 os.chdir(root_dir) 146 if base_dir is None: 147 base_dir = os.curdir 148 kwargs = {'dry_run': dry_run} 149 try: 150 format_info = ARCHIVE_FORMATS[format] 151 except KeyError: 152 raise ValueError, "unknown archive format '%s'" % format 153 154 func = format_info[0] 155 for arg, val in format_info[1]: 156 kwargs[arg] = val 157 158 if format != 'zip': 159 kwargs['owner'] = owner 160 kwargs['group'] = group 161 try: 162 filename = func(base_name, base_dir, **kwargs) 163 finally: 164 if root_dir is not None: 165 log.debug("changing back to '%s'", save_cwd) 166 os.chdir(save_cwd) 167 168 return filename