annotate Lib/distutils/archive_util.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents b36cb4602e21
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
1 """distutils.archive_util
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
2
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
3 Utility functions for creating archive files (tarballs, zip files,
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
4 that sort of thing)."""
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
5
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
6 __revision__ = "$Id$"
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
7
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
8 import os
54391
11a54dabfb1c Fixed #6048: Distutils uses the tarfile module instead of the tar command now
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54164
diff changeset
9 from warnings import warn
11a54dabfb1c Fixed #6048: Distutils uses the tarfile module instead of the tar command now
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54164
diff changeset
10 import sys
11a54dabfb1c Fixed #6048: Distutils uses the tarfile module instead of the tar command now
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54164
diff changeset
11
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
12 from distutils.errors import DistutilsExecError
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
13 from distutils.spawn import spawn
14114
3a3adfddc29d Ensure destination directory exists before trying to create a tarball
Greg Ward <gward@python.net>
parents: 12997
diff changeset
14 from distutils.dir_util import mkpath
23668
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 21267
diff changeset
15 from distutils import log
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
16
56335
c2c90d1f3c33 #6516 added owner/group support for tarfiles in Distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54391
diff changeset
17 try:
c2c90d1f3c33 #6516 added owner/group support for tarfiles in Distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54391
diff changeset
18 from pwd import getpwnam
56350
5384c8ee4a26 now uses the right exception type
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 56339
diff changeset
19 except ImportError:
56335
c2c90d1f3c33 #6516 added owner/group support for tarfiles in Distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54391
diff changeset
20 getpwnam = None
c2c90d1f3c33 #6516 added owner/group support for tarfiles in Distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54391
diff changeset
21
c2c90d1f3c33 #6516 added owner/group support for tarfiles in Distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54391
diff changeset
22 try:
c2c90d1f3c33 #6516 added owner/group support for tarfiles in Distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54391
diff changeset
23 from grp import getgrnam
56350
5384c8ee4a26 now uses the right exception type
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 56339
diff changeset
24 except ImportError:
56335
c2c90d1f3c33 #6516 added owner/group support for tarfiles in Distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54391
diff changeset
25 getgrnam = None
c2c90d1f3c33 #6516 added owner/group support for tarfiles in Distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54391
diff changeset
26
c2c90d1f3c33 #6516 added owner/group support for tarfiles in Distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54391
diff changeset
27 def _get_gid(name):
c2c90d1f3c33 #6516 added owner/group support for tarfiles in Distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54391
diff changeset
28 """Returns a gid, given a group name."""
c2c90d1f3c33 #6516 added owner/group support for tarfiles in Distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54391
diff changeset
29 if getgrnam is None or name is None:
c2c90d1f3c33 #6516 added owner/group support for tarfiles in Distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54391
diff changeset
30 return None
c2c90d1f3c33 #6516 added owner/group support for tarfiles in Distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54391
diff changeset
31 try:
c2c90d1f3c33 #6516 added owner/group support for tarfiles in Distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54391
diff changeset
32 result = getgrnam(name)
c2c90d1f3c33 #6516 added owner/group support for tarfiles in Distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54391
diff changeset
33 except KeyError:
c2c90d1f3c33 #6516 added owner/group support for tarfiles in Distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54391
diff changeset
34 result = None
c2c90d1f3c33 #6516 added owner/group support for tarfiles in Distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54391
diff changeset
35 if result is not None:
c2c90d1f3c33 #6516 added owner/group support for tarfiles in Distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54391
diff changeset
36 return result[2]
c2c90d1f3c33 #6516 added owner/group support for tarfiles in Distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54391
diff changeset
37 return None
c2c90d1f3c33 #6516 added owner/group support for tarfiles in Distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54391
diff changeset
38
c2c90d1f3c33 #6516 added owner/group support for tarfiles in Distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54391
diff changeset
39 def _get_uid(name):
c2c90d1f3c33 #6516 added owner/group support for tarfiles in Distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54391
diff changeset
40 """Returns an uid, given a user name."""
c2c90d1f3c33 #6516 added owner/group support for tarfiles in Distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54391
diff changeset
41 if getpwnam is None or name is None:
c2c90d1f3c33 #6516 added owner/group support for tarfiles in Distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54391
diff changeset
42 return None
c2c90d1f3c33 #6516 added owner/group support for tarfiles in Distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54391
diff changeset
43 try:
c2c90d1f3c33 #6516 added owner/group support for tarfiles in Distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54391
diff changeset
44 result = getpwnam(name)
c2c90d1f3c33 #6516 added owner/group support for tarfiles in Distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54391
diff changeset
45 except KeyError:
c2c90d1f3c33 #6516 added owner/group support for tarfiles in Distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54391
diff changeset
46 result = None
c2c90d1f3c33 #6516 added owner/group support for tarfiles in Distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54391
diff changeset
47 if result is not None:
c2c90d1f3c33 #6516 added owner/group support for tarfiles in Distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54391
diff changeset
48 return result[2]
c2c90d1f3c33 #6516 added owner/group support for tarfiles in Distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54391
diff changeset
49 return None
c2c90d1f3c33 #6516 added owner/group support for tarfiles in Distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54391
diff changeset
50
c2c90d1f3c33 #6516 added owner/group support for tarfiles in Distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54391
diff changeset
51 def make_tarball(base_name, base_dir, compress="gzip", verbose=0, dry_run=0,
c2c90d1f3c33 #6516 added owner/group support for tarfiles in Distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54391
diff changeset
52 owner=None, group=None):
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
53 """Create a (possibly compressed) tar file from all the files under
54164
178f70fc478d pep8-fied distutils.archive_util + added minimum test coverage
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
54 'base_dir'.
178f70fc478d pep8-fied distutils.archive_util + added minimum test coverage
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
55
178f70fc478d pep8-fied distutils.archive_util + added minimum test coverage
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
56 'compress' must be "gzip" (the default), "compress", "bzip2", or None.
56335
c2c90d1f3c33 #6516 added owner/group support for tarfiles in Distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54391
diff changeset
57 (compress will be deprecated in Python 3.2)
c2c90d1f3c33 #6516 added owner/group support for tarfiles in Distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54391
diff changeset
58
c2c90d1f3c33 #6516 added owner/group support for tarfiles in Distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54391
diff changeset
59 'owner' and 'group' can be used to define an owner and a group for the
c2c90d1f3c33 #6516 added owner/group support for tarfiles in Distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54391
diff changeset
60 archive that is being built. If not provided, the current owner and group
c2c90d1f3c33 #6516 added owner/group support for tarfiles in Distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54391
diff changeset
61 will be used.
c2c90d1f3c33 #6516 added owner/group support for tarfiles in Distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54391
diff changeset
62
54164
178f70fc478d pep8-fied distutils.archive_util + added minimum test coverage
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
63 The output tar file will be named 'base_dir' + ".tar", possibly plus
178f70fc478d pep8-fied distutils.archive_util + added minimum test coverage
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
64 the appropriate compression extension (".gz", ".bz2" or ".Z").
56335
c2c90d1f3c33 #6516 added owner/group support for tarfiles in Distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54391
diff changeset
65
54164
178f70fc478d pep8-fied distutils.archive_util + added minimum test coverage
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
66 Returns the output filename.
14864
56fda4e9ece9 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 14114
diff changeset
67 """
54391
11a54dabfb1c Fixed #6048: Distutils uses the tarfile module instead of the tar command now
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54164
diff changeset
68 tar_compression = {'gzip': 'gz', 'bzip2': 'bz2', None: '', 'compress': ''}
11a54dabfb1c Fixed #6048: Distutils uses the tarfile module instead of the tar command now
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54164
diff changeset
69 compress_ext = {'gzip': '.gz', 'bzip2': '.bz2', 'compress': '.Z'}
21267
7a7f557b4d54 Whitespace normalization.
Fred Drake <fdrake@acm.org>
parents: 17515
diff changeset
70
12483
19e3e8f8c574 Harry Henry Gebel:
Greg Ward <gward@python.net>
parents: 12449
diff changeset
71 # flags for compression program, each element of list will be an argument
19e3e8f8c574 Harry Henry Gebel:
Greg Ward <gward@python.net>
parents: 12449
diff changeset
72 if compress is not None and compress not in compress_ext.keys():
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
73 raise ValueError, \
54391
11a54dabfb1c Fixed #6048: Distutils uses the tarfile module instead of the tar command now
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54164
diff changeset
74 ("bad value for 'compress': must be None, 'gzip', 'bzip2' "
11a54dabfb1c Fixed #6048: Distutils uses the tarfile module instead of the tar command now
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54164
diff changeset
75 "or 'compress'")
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
76
54391
11a54dabfb1c Fixed #6048: Distutils uses the tarfile module instead of the tar command now
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54164
diff changeset
77 archive_name = base_name + '.tar'
11a54dabfb1c Fixed #6048: Distutils uses the tarfile module instead of the tar command now
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54164
diff changeset
78 if compress != 'compress':
11a54dabfb1c Fixed #6048: Distutils uses the tarfile module instead of the tar command now
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54164
diff changeset
79 archive_name += compress_ext.get(compress, '')
11a54dabfb1c Fixed #6048: Distutils uses the tarfile module instead of the tar command now
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54164
diff changeset
80
23668
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 21267
diff changeset
81 mkpath(os.path.dirname(archive_name), dry_run=dry_run)
54391
11a54dabfb1c Fixed #6048: Distutils uses the tarfile module instead of the tar command now
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54164
diff changeset
82
11a54dabfb1c Fixed #6048: Distutils uses the tarfile module instead of the tar command now
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54164
diff changeset
83 # creating the tarball
11a54dabfb1c Fixed #6048: Distutils uses the tarfile module instead of the tar command now
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54164
diff changeset
84 import tarfile # late import so Python build itself doesn't break
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
85
54391
11a54dabfb1c Fixed #6048: Distutils uses the tarfile module instead of the tar command now
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54164
diff changeset
86 log.info('Creating tar archive')
56335
c2c90d1f3c33 #6516 added owner/group support for tarfiles in Distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54391
diff changeset
87
c2c90d1f3c33 #6516 added owner/group support for tarfiles in Distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54391
diff changeset
88 uid = _get_uid(owner)
c2c90d1f3c33 #6516 added owner/group support for tarfiles in Distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54391
diff changeset
89 gid = _get_gid(group)
c2c90d1f3c33 #6516 added owner/group support for tarfiles in Distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54391
diff changeset
90
c2c90d1f3c33 #6516 added owner/group support for tarfiles in Distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54391
diff changeset
91 def _set_uid_gid(tarinfo):
c2c90d1f3c33 #6516 added owner/group support for tarfiles in Distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54391
diff changeset
92 if gid is not None:
c2c90d1f3c33 #6516 added owner/group support for tarfiles in Distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54391
diff changeset
93 tarinfo.gid = gid
c2c90d1f3c33 #6516 added owner/group support for tarfiles in Distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54391
diff changeset
94 tarinfo.gname = group
c2c90d1f3c33 #6516 added owner/group support for tarfiles in Distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54391
diff changeset
95 if uid is not None:
c2c90d1f3c33 #6516 added owner/group support for tarfiles in Distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54391
diff changeset
96 tarinfo.uid = uid
c2c90d1f3c33 #6516 added owner/group support for tarfiles in Distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54391
diff changeset
97 tarinfo.uname = owner
c2c90d1f3c33 #6516 added owner/group support for tarfiles in Distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54391
diff changeset
98 return tarinfo
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
99
54391
11a54dabfb1c Fixed #6048: Distutils uses the tarfile module instead of the tar command now
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54164
diff changeset
100 if not dry_run:
11a54dabfb1c Fixed #6048: Distutils uses the tarfile module instead of the tar command now
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54164
diff changeset
101 tar = tarfile.open(archive_name, 'w|%s' % tar_compression[compress])
11a54dabfb1c Fixed #6048: Distutils uses the tarfile module instead of the tar command now
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54164
diff changeset
102 try:
56335
c2c90d1f3c33 #6516 added owner/group support for tarfiles in Distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54391
diff changeset
103 tar.add(base_dir, filter=_set_uid_gid)
54391
11a54dabfb1c Fixed #6048: Distutils uses the tarfile module instead of the tar command now
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54164
diff changeset
104 finally:
11a54dabfb1c Fixed #6048: Distutils uses the tarfile module instead of the tar command now
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54164
diff changeset
105 tar.close()
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
106
54391
11a54dabfb1c Fixed #6048: Distutils uses the tarfile module instead of the tar command now
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54164
diff changeset
107 # compression using `compress`
11a54dabfb1c Fixed #6048: Distutils uses the tarfile module instead of the tar command now
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54164
diff changeset
108 if compress == 'compress':
11a54dabfb1c Fixed #6048: Distutils uses the tarfile module instead of the tar command now
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54164
diff changeset
109 warn("'compress' will be deprecated.", PendingDeprecationWarning)
11a54dabfb1c Fixed #6048: Distutils uses the tarfile module instead of the tar command now
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54164
diff changeset
110 # the option varies depending on the platform
11a54dabfb1c Fixed #6048: Distutils uses the tarfile module instead of the tar command now
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54164
diff changeset
111 compressed_name = archive_name + compress_ext[compress]
11a54dabfb1c Fixed #6048: Distutils uses the tarfile module instead of the tar command now
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54164
diff changeset
112 if sys.platform == 'win32':
11a54dabfb1c Fixed #6048: Distutils uses the tarfile module instead of the tar command now
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54164
diff changeset
113 cmd = [compress, archive_name, compressed_name]
11a54dabfb1c Fixed #6048: Distutils uses the tarfile module instead of the tar command now
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54164
diff changeset
114 else:
11a54dabfb1c Fixed #6048: Distutils uses the tarfile module instead of the tar command now
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54164
diff changeset
115 cmd = [compress, '-f', archive_name]
11a54dabfb1c Fixed #6048: Distutils uses the tarfile module instead of the tar command now
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54164
diff changeset
116 spawn(cmd, dry_run=dry_run)
11a54dabfb1c Fixed #6048: Distutils uses the tarfile module instead of the tar command now
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54164
diff changeset
117 return compressed_name
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
118
54391
11a54dabfb1c Fixed #6048: Distutils uses the tarfile module instead of the tar command now
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54164
diff changeset
119 return archive_name
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
120
54164
178f70fc478d pep8-fied distutils.archive_util + added minimum test coverage
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
121 def make_zipfile(base_name, base_dir, verbose=0, dry_run=0):
178f70fc478d pep8-fied distutils.archive_util + added minimum test coverage
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
122 """Create a zip file from all the files under 'base_dir'.
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
123
66934
5b44059247db Merged revisions 87277 via svnmerge from
Éric Araujo <merwok@netwok.org>
parents: 56708
diff changeset
124 The output zip file will be named 'base_name' + ".zip". Uses either the
54164
178f70fc478d pep8-fied distutils.archive_util + added minimum test coverage
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
125 "zipfile" Python module (if available) or the InfoZIP "zip" utility
178f70fc478d pep8-fied distutils.archive_util + added minimum test coverage
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
126 (if installed and found on the default search path). If neither tool is
178f70fc478d pep8-fied distutils.archive_util + added minimum test coverage
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
127 available, raises DistutilsExecError. Returns the name of the output zip
178f70fc478d pep8-fied distutils.archive_util + added minimum test coverage
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
128 file.
14864
56fda4e9ece9 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 14114
diff changeset
129 """
26317
a4dba058f70e Bug #639118 from Ollie Oldham: archiver should use zipfile before zip
Andrew M. Kuchling <amk@amk.ca>
parents: 26285
diff changeset
130 try:
a4dba058f70e Bug #639118 from Ollie Oldham: archiver should use zipfile before zip
Andrew M. Kuchling <amk@amk.ca>
parents: 26285
diff changeset
131 import zipfile
a4dba058f70e Bug #639118 from Ollie Oldham: archiver should use zipfile before zip
Andrew M. Kuchling <amk@amk.ca>
parents: 26285
diff changeset
132 except ImportError:
a4dba058f70e Bug #639118 from Ollie Oldham: archiver should use zipfile before zip
Andrew M. Kuchling <amk@amk.ca>
parents: 26285
diff changeset
133 zipfile = None
32736
c3a096bfbd6b Whitespace normalization, via reindent.py.
Tim Peters <tim.peters@gmail.com>
parents: 26317
diff changeset
134
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
135 zip_filename = base_name + ".zip"
23668
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 21267
diff changeset
136 mkpath(os.path.dirname(zip_filename), dry_run=dry_run)
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
137
26317
a4dba058f70e Bug #639118 from Ollie Oldham: archiver should use zipfile before zip
Andrew M. Kuchling <amk@amk.ca>
parents: 26285
diff changeset
138 # If zipfile module is not available, try spawning an external
a4dba058f70e Bug #639118 from Ollie Oldham: archiver should use zipfile before zip
Andrew M. Kuchling <amk@amk.ca>
parents: 26285
diff changeset
139 # 'zip' command.
a4dba058f70e Bug #639118 from Ollie Oldham: archiver should use zipfile before zip
Andrew M. Kuchling <amk@amk.ca>
parents: 26285
diff changeset
140 if zipfile is None:
a4dba058f70e Bug #639118 from Ollie Oldham: archiver should use zipfile before zip
Andrew M. Kuchling <amk@amk.ca>
parents: 26285
diff changeset
141 if verbose:
a4dba058f70e Bug #639118 from Ollie Oldham: archiver should use zipfile before zip
Andrew M. Kuchling <amk@amk.ca>
parents: 26285
diff changeset
142 zipoptions = "-r"
a4dba058f70e Bug #639118 from Ollie Oldham: archiver should use zipfile before zip
Andrew M. Kuchling <amk@amk.ca>
parents: 26285
diff changeset
143 else:
a4dba058f70e Bug #639118 from Ollie Oldham: archiver should use zipfile before zip
Andrew M. Kuchling <amk@amk.ca>
parents: 26285
diff changeset
144 zipoptions = "-rq"
32736
c3a096bfbd6b Whitespace normalization, via reindent.py.
Tim Peters <tim.peters@gmail.com>
parents: 26317
diff changeset
145
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
146 try:
26317
a4dba058f70e Bug #639118 from Ollie Oldham: archiver should use zipfile before zip
Andrew M. Kuchling <amk@amk.ca>
parents: 26285
diff changeset
147 spawn(["zip", zipoptions, zip_filename, base_dir],
a4dba058f70e Bug #639118 from Ollie Oldham: archiver should use zipfile before zip
Andrew M. Kuchling <amk@amk.ca>
parents: 26285
diff changeset
148 dry_run=dry_run)
a4dba058f70e Bug #639118 from Ollie Oldham: archiver should use zipfile before zip
Andrew M. Kuchling <amk@amk.ca>
parents: 26285
diff changeset
149 except DistutilsExecError:
a4dba058f70e Bug #639118 from Ollie Oldham: archiver should use zipfile before zip
Andrew M. Kuchling <amk@amk.ca>
parents: 26285
diff changeset
150 # XXX really should distinguish between "couldn't find
a4dba058f70e Bug #639118 from Ollie Oldham: archiver should use zipfile before zip
Andrew M. Kuchling <amk@amk.ca>
parents: 26285
diff changeset
151 # external 'zip' command" and "zip failed".
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
152 raise DistutilsExecError, \
26317
a4dba058f70e Bug #639118 from Ollie Oldham: archiver should use zipfile before zip
Andrew M. Kuchling <amk@amk.ca>
parents: 26285
diff changeset
153 ("unable to create zip file '%s': "
a4dba058f70e Bug #639118 from Ollie Oldham: archiver should use zipfile before zip
Andrew M. Kuchling <amk@amk.ca>
parents: 26285
diff changeset
154 "could neither import the 'zipfile' module nor "
a4dba058f70e Bug #639118 from Ollie Oldham: archiver should use zipfile before zip
Andrew M. Kuchling <amk@amk.ca>
parents: 26285
diff changeset
155 "find a standalone zip utility") % zip_filename
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
156
26317
a4dba058f70e Bug #639118 from Ollie Oldham: archiver should use zipfile before zip
Andrew M. Kuchling <amk@amk.ca>
parents: 26285
diff changeset
157 else:
a4dba058f70e Bug #639118 from Ollie Oldham: archiver should use zipfile before zip
Andrew M. Kuchling <amk@amk.ca>
parents: 26285
diff changeset
158 log.info("creating '%s' and adding '%s' to it",
23668
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 21267
diff changeset
159 zip_filename, base_dir)
26317
a4dba058f70e Bug #639118 from Ollie Oldham: archiver should use zipfile before zip
Andrew M. Kuchling <amk@amk.ca>
parents: 26285
diff changeset
160
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
161 if not dry_run:
54164
178f70fc478d pep8-fied distutils.archive_util + added minimum test coverage
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
162 zip = zipfile.ZipFile(zip_filename, "w",
178f70fc478d pep8-fied distutils.archive_util + added minimum test coverage
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
163 compression=zipfile.ZIP_DEFLATED)
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
164
46767
dc573ee0d904 Replace instances of os.path.walk with os.walk
Benjamin Peterson <benjamin@python.org>
parents: 45413
diff changeset
165 for dirpath, dirnames, filenames in os.walk(base_dir):
dc573ee0d904 Replace instances of os.path.walk with os.walk
Benjamin Peterson <benjamin@python.org>
parents: 45413
diff changeset
166 for name in filenames:
dc573ee0d904 Replace instances of os.path.walk with os.walk
Benjamin Peterson <benjamin@python.org>
parents: 45413
diff changeset
167 path = os.path.normpath(os.path.join(dirpath, name))
dc573ee0d904 Replace instances of os.path.walk with os.walk
Benjamin Peterson <benjamin@python.org>
parents: 45413
diff changeset
168 if os.path.isfile(path):
54164
178f70fc478d pep8-fied distutils.archive_util + added minimum test coverage
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
169 zip.write(path, path)
46767
dc573ee0d904 Replace instances of os.path.walk with os.walk
Benjamin Peterson <benjamin@python.org>
parents: 45413
diff changeset
170 log.info("adding '%s'" % path)
54164
178f70fc478d pep8-fied distutils.archive_util + added minimum test coverage
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
171 zip.close()
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
172
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
173 return zip_filename
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
174
12449
19f53d8bd298 Extracted the "what-do-I-do-for-this-format" logic from code in
Greg Ward <gward@python.net>
parents: 12226
diff changeset
175 ARCHIVE_FORMATS = {
12997
27f3a18fd296 Stylistic/formatting changes to Rene Liebscher's '--help-xxx' patch.
Greg Ward <gward@python.net>
parents: 12869
diff changeset
176 'gztar': (make_tarball, [('compress', 'gzip')], "gzip'ed tar-file"),
27f3a18fd296 Stylistic/formatting changes to Rene Liebscher's '--help-xxx' patch.
Greg Ward <gward@python.net>
parents: 12869
diff changeset
177 'bztar': (make_tarball, [('compress', 'bzip2')], "bzip2'ed tar-file"),
27f3a18fd296 Stylistic/formatting changes to Rene Liebscher's '--help-xxx' patch.
Greg Ward <gward@python.net>
parents: 12869
diff changeset
178 'ztar': (make_tarball, [('compress', 'compress')], "compressed tar file"),
27f3a18fd296 Stylistic/formatting changes to Rene Liebscher's '--help-xxx' patch.
Greg Ward <gward@python.net>
parents: 12869
diff changeset
179 'tar': (make_tarball, [('compress', None)], "uncompressed tar file"),
14114
3a3adfddc29d Ensure destination directory exists before trying to create a tarball
Greg Ward <gward@python.net>
parents: 12997
diff changeset
180 'zip': (make_zipfile, [],"ZIP file")
12449
19f53d8bd298 Extracted the "what-do-I-do-for-this-format" logic from code in
Greg Ward <gward@python.net>
parents: 12226
diff changeset
181 }
19f53d8bd298 Extracted the "what-do-I-do-for-this-format" logic from code in
Greg Ward <gward@python.net>
parents: 12226
diff changeset
182
54164
178f70fc478d pep8-fied distutils.archive_util + added minimum test coverage
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
183 def check_archive_formats(formats):
178f70fc478d pep8-fied distutils.archive_util + added minimum test coverage
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
184 """Returns the first format from the 'format' list that is unknown.
178f70fc478d pep8-fied distutils.archive_util + added minimum test coverage
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
185
178f70fc478d pep8-fied distutils.archive_util + added minimum test coverage
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
186 If all formats are known, returns None
178f70fc478d pep8-fied distutils.archive_util + added minimum test coverage
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
187 """
12449
19f53d8bd298 Extracted the "what-do-I-do-for-this-format" logic from code in
Greg Ward <gward@python.net>
parents: 12226
diff changeset
188 for format in formats:
45413
8a187f562959 Removed uses of dict.has_key() from distutils, and uses of
Guido van Rossum <guido@python.org>
parents: 33965
diff changeset
189 if format not in ARCHIVE_FORMATS:
12449
19f53d8bd298 Extracted the "what-do-I-do-for-this-format" logic from code in
Greg Ward <gward@python.net>
parents: 12226
diff changeset
190 return format
54164
178f70fc478d pep8-fied distutils.archive_util + added minimum test coverage
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
191 return None
12449
19f53d8bd298 Extracted the "what-do-I-do-for-this-format" logic from code in
Greg Ward <gward@python.net>
parents: 12226
diff changeset
192
54164
178f70fc478d pep8-fied distutils.archive_util + added minimum test coverage
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
193 def make_archive(base_name, format, root_dir=None, base_dir=None, verbose=0,
56335
c2c90d1f3c33 #6516 added owner/group support for tarfiles in Distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54391
diff changeset
194 dry_run=0, owner=None, group=None):
54164
178f70fc478d pep8-fied distutils.archive_util + added minimum test coverage
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
195 """Create an archive file (eg. zip or tar).
178f70fc478d pep8-fied distutils.archive_util + added minimum test coverage
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
196
178f70fc478d pep8-fied distutils.archive_util + added minimum test coverage
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
197 'base_name' is the name of the file to create, minus any format-specific
178f70fc478d pep8-fied distutils.archive_util + added minimum test coverage
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
198 extension; 'format' is the archive format: one of "zip", "tar", "ztar",
178f70fc478d pep8-fied distutils.archive_util + added minimum test coverage
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
199 or "gztar".
178f70fc478d pep8-fied distutils.archive_util + added minimum test coverage
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
200
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
201 'root_dir' is a directory that will be the root directory of the
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
202 archive; ie. we typically chdir into 'root_dir' before creating the
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
203 archive. 'base_dir' is the directory where we start archiving from;
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
204 ie. 'base_dir' will be the common prefix of all files and
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
205 directories in the archive. 'root_dir' and 'base_dir' both default
12809
258eed4570ce Ensure that 'make_archive()' returns the name of the new archive file.
Greg Ward <gward@python.net>
parents: 12803
diff changeset
206 to the current directory. Returns the name of the archive file.
56335
c2c90d1f3c33 #6516 added owner/group support for tarfiles in Distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54391
diff changeset
207
c2c90d1f3c33 #6516 added owner/group support for tarfiles in Distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54391
diff changeset
208 'owner' and 'group' are used when creating a tar archive. By default,
c2c90d1f3c33 #6516 added owner/group support for tarfiles in Distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54391
diff changeset
209 uses the current owner and group.
12809
258eed4570ce Ensure that 'make_archive()' returns the name of the new archive file.
Greg Ward <gward@python.net>
parents: 12803
diff changeset
210 """
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
211 save_cwd = os.getcwd()
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
212 if root_dir is not None:
23668
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 21267
diff changeset
213 log.debug("changing into '%s'", root_dir)
14864
56fda4e9ece9 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 14114
diff changeset
214 base_name = os.path.abspath(base_name)
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
215 if not dry_run:
14864
56fda4e9ece9 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 14114
diff changeset
216 os.chdir(root_dir)
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
217
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
218 if base_dir is None:
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
219 base_dir = os.curdir
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
220
54164
178f70fc478d pep8-fied distutils.archive_util + added minimum test coverage
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
221 kwargs = {'dry_run': dry_run}
21267
7a7f557b4d54 Whitespace normalization.
Fred Drake <fdrake@acm.org>
parents: 17515
diff changeset
222
12449
19f53d8bd298 Extracted the "what-do-I-do-for-this-format" logic from code in
Greg Ward <gward@python.net>
parents: 12226
diff changeset
223 try:
19f53d8bd298 Extracted the "what-do-I-do-for-this-format" logic from code in
Greg Ward <gward@python.net>
parents: 12226
diff changeset
224 format_info = ARCHIVE_FORMATS[format]
19f53d8bd298 Extracted the "what-do-I-do-for-this-format" logic from code in
Greg Ward <gward@python.net>
parents: 12226
diff changeset
225 except KeyError:
19f53d8bd298 Extracted the "what-do-I-do-for-this-format" logic from code in
Greg Ward <gward@python.net>
parents: 12226
diff changeset
226 raise ValueError, "unknown archive format '%s'" % format
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
227
12449
19f53d8bd298 Extracted the "what-do-I-do-for-this-format" logic from code in
Greg Ward <gward@python.net>
parents: 12226
diff changeset
228 func = format_info[0]
54164
178f70fc478d pep8-fied distutils.archive_util + added minimum test coverage
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
229 for arg, val in format_info[1]:
12449
19f53d8bd298 Extracted the "what-do-I-do-for-this-format" logic from code in
Greg Ward <gward@python.net>
parents: 12226
diff changeset
230 kwargs[arg] = val
56335
c2c90d1f3c33 #6516 added owner/group support for tarfiles in Distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54391
diff changeset
231
c2c90d1f3c33 #6516 added owner/group support for tarfiles in Distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54391
diff changeset
232 if format != 'zip':
c2c90d1f3c33 #6516 added owner/group support for tarfiles in Distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54391
diff changeset
233 kwargs['owner'] = owner
c2c90d1f3c33 #6516 added owner/group support for tarfiles in Distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 54391
diff changeset
234 kwargs['group'] = group
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
235
63395
f29c319ff567 Merged revisions 75659 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 46767
diff changeset
236 try:
f29c319ff567 Merged revisions 75659 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 46767
diff changeset
237 filename = func(base_name, base_dir, **kwargs)
f29c319ff567 Merged revisions 75659 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 46767
diff changeset
238 finally:
f29c319ff567 Merged revisions 75659 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 46767
diff changeset
239 if root_dir is not None:
f29c319ff567 Merged revisions 75659 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 46767
diff changeset
240 log.debug("changing back to '%s'", save_cwd)
f29c319ff567 Merged revisions 75659 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 46767
diff changeset
241 os.chdir(save_cwd)
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
242
12809
258eed4570ce Ensure that 'make_archive()' returns the name of the new archive file.
Greg Ward <gward@python.net>
parents: 12803
diff changeset
243 return filename