| 1 | n/a | """distutils.archive_util |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | Utility functions for creating archive files (tarballs, zip files, |
|---|
| 4 | n/a | that sort of thing).""" |
|---|
| 5 | n/a | |
|---|
| 6 | n/a | import os |
|---|
| 7 | n/a | from warnings import warn |
|---|
| 8 | n/a | import sys |
|---|
| 9 | n/a | |
|---|
| 10 | n/a | try: |
|---|
| 11 | n/a | import zipfile |
|---|
| 12 | n/a | except ImportError: |
|---|
| 13 | n/a | zipfile = None |
|---|
| 14 | n/a | |
|---|
| 15 | n/a | |
|---|
| 16 | n/a | from distutils.errors import DistutilsExecError |
|---|
| 17 | n/a | from distutils.spawn import spawn |
|---|
| 18 | n/a | from distutils.dir_util import mkpath |
|---|
| 19 | n/a | from distutils import log |
|---|
| 20 | n/a | |
|---|
| 21 | n/a | try: |
|---|
| 22 | n/a | from pwd import getpwnam |
|---|
| 23 | n/a | except ImportError: |
|---|
| 24 | n/a | getpwnam = None |
|---|
| 25 | n/a | |
|---|
| 26 | n/a | try: |
|---|
| 27 | n/a | from grp import getgrnam |
|---|
| 28 | n/a | except ImportError: |
|---|
| 29 | n/a | getgrnam = None |
|---|
| 30 | n/a | |
|---|
| 31 | n/a | def _get_gid(name): |
|---|
| 32 | n/a | """Returns a gid, given a group name.""" |
|---|
| 33 | n/a | if getgrnam is None or name is None: |
|---|
| 34 | n/a | return None |
|---|
| 35 | n/a | try: |
|---|
| 36 | n/a | result = getgrnam(name) |
|---|
| 37 | n/a | except KeyError: |
|---|
| 38 | n/a | result = None |
|---|
| 39 | n/a | if result is not None: |
|---|
| 40 | n/a | return result[2] |
|---|
| 41 | n/a | return None |
|---|
| 42 | n/a | |
|---|
| 43 | n/a | def _get_uid(name): |
|---|
| 44 | n/a | """Returns an uid, given a user name.""" |
|---|
| 45 | n/a | if getpwnam is None or name is None: |
|---|
| 46 | n/a | return None |
|---|
| 47 | n/a | try: |
|---|
| 48 | n/a | result = getpwnam(name) |
|---|
| 49 | n/a | except KeyError: |
|---|
| 50 | n/a | result = None |
|---|
| 51 | n/a | if result is not None: |
|---|
| 52 | n/a | return result[2] |
|---|
| 53 | n/a | return None |
|---|
| 54 | n/a | |
|---|
| 55 | n/a | def make_tarball(base_name, base_dir, compress="gzip", verbose=0, dry_run=0, |
|---|
| 56 | n/a | owner=None, group=None): |
|---|
| 57 | n/a | """Create a (possibly compressed) tar file from all the files under |
|---|
| 58 | n/a | 'base_dir'. |
|---|
| 59 | n/a | |
|---|
| 60 | n/a | 'compress' must be "gzip" (the default), "bzip2", "xz", "compress", or |
|---|
| 61 | n/a | None. ("compress" will be deprecated in Python 3.2) |
|---|
| 62 | n/a | |
|---|
| 63 | n/a | 'owner' and 'group' can be used to define an owner and a group for the |
|---|
| 64 | n/a | archive that is being built. If not provided, the current owner and group |
|---|
| 65 | n/a | will be used. |
|---|
| 66 | n/a | |
|---|
| 67 | n/a | The output tar file will be named 'base_dir' + ".tar", possibly plus |
|---|
| 68 | n/a | the appropriate compression extension (".gz", ".bz2", ".xz" or ".Z"). |
|---|
| 69 | n/a | |
|---|
| 70 | n/a | Returns the output filename. |
|---|
| 71 | n/a | """ |
|---|
| 72 | n/a | tar_compression = {'gzip': 'gz', 'bzip2': 'bz2', 'xz': 'xz', None: '', |
|---|
| 73 | n/a | 'compress': ''} |
|---|
| 74 | n/a | compress_ext = {'gzip': '.gz', 'bzip2': '.bz2', 'xz': '.xz', |
|---|
| 75 | n/a | 'compress': '.Z'} |
|---|
| 76 | n/a | |
|---|
| 77 | n/a | # flags for compression program, each element of list will be an argument |
|---|
| 78 | n/a | if compress is not None and compress not in compress_ext.keys(): |
|---|
| 79 | n/a | raise ValueError( |
|---|
| 80 | n/a | "bad value for 'compress': must be None, 'gzip', 'bzip2', " |
|---|
| 81 | n/a | "'xz' or 'compress'") |
|---|
| 82 | n/a | |
|---|
| 83 | n/a | archive_name = base_name + '.tar' |
|---|
| 84 | n/a | if compress != 'compress': |
|---|
| 85 | n/a | archive_name += compress_ext.get(compress, '') |
|---|
| 86 | n/a | |
|---|
| 87 | n/a | mkpath(os.path.dirname(archive_name), dry_run=dry_run) |
|---|
| 88 | n/a | |
|---|
| 89 | n/a | # creating the tarball |
|---|
| 90 | n/a | import tarfile # late import so Python build itself doesn't break |
|---|
| 91 | n/a | |
|---|
| 92 | n/a | log.info('Creating tar archive') |
|---|
| 93 | n/a | |
|---|
| 94 | n/a | uid = _get_uid(owner) |
|---|
| 95 | n/a | gid = _get_gid(group) |
|---|
| 96 | n/a | |
|---|
| 97 | n/a | def _set_uid_gid(tarinfo): |
|---|
| 98 | n/a | if gid is not None: |
|---|
| 99 | n/a | tarinfo.gid = gid |
|---|
| 100 | n/a | tarinfo.gname = group |
|---|
| 101 | n/a | if uid is not None: |
|---|
| 102 | n/a | tarinfo.uid = uid |
|---|
| 103 | n/a | tarinfo.uname = owner |
|---|
| 104 | n/a | return tarinfo |
|---|
| 105 | n/a | |
|---|
| 106 | n/a | if not dry_run: |
|---|
| 107 | n/a | tar = tarfile.open(archive_name, 'w|%s' % tar_compression[compress]) |
|---|
| 108 | n/a | try: |
|---|
| 109 | n/a | tar.add(base_dir, filter=_set_uid_gid) |
|---|
| 110 | n/a | finally: |
|---|
| 111 | n/a | tar.close() |
|---|
| 112 | n/a | |
|---|
| 113 | n/a | # compression using `compress` |
|---|
| 114 | n/a | if compress == 'compress': |
|---|
| 115 | n/a | warn("'compress' will be deprecated.", PendingDeprecationWarning) |
|---|
| 116 | n/a | # the option varies depending on the platform |
|---|
| 117 | n/a | compressed_name = archive_name + compress_ext[compress] |
|---|
| 118 | n/a | if sys.platform == 'win32': |
|---|
| 119 | n/a | cmd = [compress, archive_name, compressed_name] |
|---|
| 120 | n/a | else: |
|---|
| 121 | n/a | cmd = [compress, '-f', archive_name] |
|---|
| 122 | n/a | spawn(cmd, dry_run=dry_run) |
|---|
| 123 | n/a | return compressed_name |
|---|
| 124 | n/a | |
|---|
| 125 | n/a | return archive_name |
|---|
| 126 | n/a | |
|---|
| 127 | n/a | def make_zipfile(base_name, base_dir, verbose=0, dry_run=0): |
|---|
| 128 | n/a | """Create a zip file from all the files under 'base_dir'. |
|---|
| 129 | n/a | |
|---|
| 130 | n/a | The output zip file will be named 'base_name' + ".zip". Uses either the |
|---|
| 131 | n/a | "zipfile" Python module (if available) or the InfoZIP "zip" utility |
|---|
| 132 | n/a | (if installed and found on the default search path). If neither tool is |
|---|
| 133 | n/a | available, raises DistutilsExecError. Returns the name of the output zip |
|---|
| 134 | n/a | file. |
|---|
| 135 | n/a | """ |
|---|
| 136 | n/a | zip_filename = base_name + ".zip" |
|---|
| 137 | n/a | mkpath(os.path.dirname(zip_filename), dry_run=dry_run) |
|---|
| 138 | n/a | |
|---|
| 139 | n/a | # If zipfile module is not available, try spawning an external |
|---|
| 140 | n/a | # 'zip' command. |
|---|
| 141 | n/a | if zipfile is None: |
|---|
| 142 | n/a | if verbose: |
|---|
| 143 | n/a | zipoptions = "-r" |
|---|
| 144 | n/a | else: |
|---|
| 145 | n/a | zipoptions = "-rq" |
|---|
| 146 | n/a | |
|---|
| 147 | n/a | try: |
|---|
| 148 | n/a | spawn(["zip", zipoptions, zip_filename, base_dir], |
|---|
| 149 | n/a | dry_run=dry_run) |
|---|
| 150 | n/a | except DistutilsExecError: |
|---|
| 151 | n/a | # XXX really should distinguish between "couldn't find |
|---|
| 152 | n/a | # external 'zip' command" and "zip failed". |
|---|
| 153 | n/a | raise DistutilsExecError(("unable to create zip file '%s': " |
|---|
| 154 | n/a | "could neither import the 'zipfile' module nor " |
|---|
| 155 | n/a | "find a standalone zip utility") % zip_filename) |
|---|
| 156 | n/a | |
|---|
| 157 | n/a | else: |
|---|
| 158 | n/a | log.info("creating '%s' and adding '%s' to it", |
|---|
| 159 | n/a | zip_filename, base_dir) |
|---|
| 160 | n/a | |
|---|
| 161 | n/a | if not dry_run: |
|---|
| 162 | n/a | try: |
|---|
| 163 | n/a | zip = zipfile.ZipFile(zip_filename, "w", |
|---|
| 164 | n/a | compression=zipfile.ZIP_DEFLATED) |
|---|
| 165 | n/a | except RuntimeError: |
|---|
| 166 | n/a | zip = zipfile.ZipFile(zip_filename, "w", |
|---|
| 167 | n/a | compression=zipfile.ZIP_STORED) |
|---|
| 168 | n/a | |
|---|
| 169 | n/a | for dirpath, dirnames, filenames in os.walk(base_dir): |
|---|
| 170 | n/a | for name in filenames: |
|---|
| 171 | n/a | path = os.path.normpath(os.path.join(dirpath, name)) |
|---|
| 172 | n/a | if os.path.isfile(path): |
|---|
| 173 | n/a | zip.write(path, path) |
|---|
| 174 | n/a | log.info("adding '%s'", path) |
|---|
| 175 | n/a | zip.close() |
|---|
| 176 | n/a | |
|---|
| 177 | n/a | return zip_filename |
|---|
| 178 | n/a | |
|---|
| 179 | n/a | ARCHIVE_FORMATS = { |
|---|
| 180 | n/a | 'gztar': (make_tarball, [('compress', 'gzip')], "gzip'ed tar-file"), |
|---|
| 181 | n/a | 'bztar': (make_tarball, [('compress', 'bzip2')], "bzip2'ed tar-file"), |
|---|
| 182 | n/a | 'xztar': (make_tarball, [('compress', 'xz')], "xz'ed tar-file"), |
|---|
| 183 | n/a | 'ztar': (make_tarball, [('compress', 'compress')], "compressed tar file"), |
|---|
| 184 | n/a | 'tar': (make_tarball, [('compress', None)], "uncompressed tar file"), |
|---|
| 185 | n/a | 'zip': (make_zipfile, [],"ZIP file") |
|---|
| 186 | n/a | } |
|---|
| 187 | n/a | |
|---|
| 188 | n/a | def check_archive_formats(formats): |
|---|
| 189 | n/a | """Returns the first format from the 'format' list that is unknown. |
|---|
| 190 | n/a | |
|---|
| 191 | n/a | If all formats are known, returns None |
|---|
| 192 | n/a | """ |
|---|
| 193 | n/a | for format in formats: |
|---|
| 194 | n/a | if format not in ARCHIVE_FORMATS: |
|---|
| 195 | n/a | return format |
|---|
| 196 | n/a | return None |
|---|
| 197 | n/a | |
|---|
| 198 | n/a | def make_archive(base_name, format, root_dir=None, base_dir=None, verbose=0, |
|---|
| 199 | n/a | dry_run=0, owner=None, group=None): |
|---|
| 200 | n/a | """Create an archive file (eg. zip or tar). |
|---|
| 201 | n/a | |
|---|
| 202 | n/a | 'base_name' is the name of the file to create, minus any format-specific |
|---|
| 203 | n/a | extension; 'format' is the archive format: one of "zip", "tar", "gztar", |
|---|
| 204 | n/a | "bztar", "xztar", or "ztar". |
|---|
| 205 | n/a | |
|---|
| 206 | n/a | 'root_dir' is a directory that will be the root directory of the |
|---|
| 207 | n/a | archive; ie. we typically chdir into 'root_dir' before creating the |
|---|
| 208 | n/a | archive. 'base_dir' is the directory where we start archiving from; |
|---|
| 209 | n/a | ie. 'base_dir' will be the common prefix of all files and |
|---|
| 210 | n/a | directories in the archive. 'root_dir' and 'base_dir' both default |
|---|
| 211 | n/a | to the current directory. Returns the name of the archive file. |
|---|
| 212 | n/a | |
|---|
| 213 | n/a | 'owner' and 'group' are used when creating a tar archive. By default, |
|---|
| 214 | n/a | uses the current owner and group. |
|---|
| 215 | n/a | """ |
|---|
| 216 | n/a | save_cwd = os.getcwd() |
|---|
| 217 | n/a | if root_dir is not None: |
|---|
| 218 | n/a | log.debug("changing into '%s'", root_dir) |
|---|
| 219 | n/a | base_name = os.path.abspath(base_name) |
|---|
| 220 | n/a | if not dry_run: |
|---|
| 221 | n/a | os.chdir(root_dir) |
|---|
| 222 | n/a | |
|---|
| 223 | n/a | if base_dir is None: |
|---|
| 224 | n/a | base_dir = os.curdir |
|---|
| 225 | n/a | |
|---|
| 226 | n/a | kwargs = {'dry_run': dry_run} |
|---|
| 227 | n/a | |
|---|
| 228 | n/a | try: |
|---|
| 229 | n/a | format_info = ARCHIVE_FORMATS[format] |
|---|
| 230 | n/a | except KeyError: |
|---|
| 231 | n/a | raise ValueError("unknown archive format '%s'" % format) |
|---|
| 232 | n/a | |
|---|
| 233 | n/a | func = format_info[0] |
|---|
| 234 | n/a | for arg, val in format_info[1]: |
|---|
| 235 | n/a | kwargs[arg] = val |
|---|
| 236 | n/a | |
|---|
| 237 | n/a | if format != 'zip': |
|---|
| 238 | n/a | kwargs['owner'] = owner |
|---|
| 239 | n/a | kwargs['group'] = group |
|---|
| 240 | n/a | |
|---|
| 241 | n/a | try: |
|---|
| 242 | n/a | filename = func(base_name, base_dir, **kwargs) |
|---|
| 243 | n/a | finally: |
|---|
| 244 | n/a | if root_dir is not None: |
|---|
| 245 | n/a | log.debug("changing back to '%s'", save_cwd) |
|---|
| 246 | n/a | os.chdir(save_cwd) |
|---|
| 247 | n/a | |
|---|
| 248 | n/a | return filename |
|---|