annotate Lib/distutils/archive_util.py @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents 178f70fc478d
children 11a54dabfb1c
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
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
9 from distutils.errors import DistutilsExecError
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
10 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
11 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
12 from distutils import log
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
13
54164
178f70fc478d pep8-fied distutils.archive_util + added minimum test coverage
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
14 def make_tarball(base_name, base_dir, compress="gzip", verbose=0, dry_run=0):
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
15 """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
16 'base_dir'.
178f70fc478d pep8-fied distutils.archive_util + added minimum test coverage
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
17
178f70fc478d pep8-fied distutils.archive_util + added minimum test coverage
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
18 'compress' must be "gzip" (the default), "compress", "bzip2", or None.
178f70fc478d pep8-fied distutils.archive_util + added minimum test coverage
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
19 Both "tar" and the compression utility named by 'compress' must be on
178f70fc478d pep8-fied distutils.archive_util + added minimum test coverage
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
20 the default program search path, so this is probably Unix-specific.
178f70fc478d pep8-fied distutils.archive_util + added minimum test coverage
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
21 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
22 the appropriate compression extension (".gz", ".bz2" or ".Z").
178f70fc478d pep8-fied distutils.archive_util + added minimum test coverage
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
23 Returns the output filename.
14864
56fda4e9ece9 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 14114
diff changeset
24 """
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
25 # XXX GNU tar 1.13 has a nifty option to add a prefix directory.
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
26 # It's pretty new, though, so we certainly can't require it --
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
27 # but it would be nice to take advantage of it to skip the
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
28 # "create a tree of hardlinks" step! (Would also be nice to
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
29 # detect GNU tar to use its 'z' option and save a step.)
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
30
54164
178f70fc478d pep8-fied distutils.archive_util + added minimum test coverage
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
31 compress_ext = {'gzip': ".gz",
178f70fc478d pep8-fied distutils.archive_util + added minimum test coverage
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
32 'bzip2': '.bz2',
178f70fc478d pep8-fied distutils.archive_util + added minimum test coverage
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
33 'compress': ".Z" }
21267
7a7f557b4d54 Whitespace normalization.
Fred Drake <fdrake@acm.org>
parents: 17515
diff changeset
34
12483
19e3e8f8c574 Harry Henry Gebel:
Greg Ward <gward@python.net>
parents: 12449
diff changeset
35 # 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
36 compress_flags = {'gzip': ["-f9"],
19e3e8f8c574 Harry Henry Gebel:
Greg Ward <gward@python.net>
parents: 12449
diff changeset
37 'compress': ["-f"],
19e3e8f8c574 Harry Henry Gebel:
Greg Ward <gward@python.net>
parents: 12449
diff changeset
38 'bzip2': ['-f9']}
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
39
12483
19e3e8f8c574 Harry Henry Gebel:
Greg Ward <gward@python.net>
parents: 12449
diff changeset
40 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
41 raise ValueError, \
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
42 "bad value for 'compress': must be None, 'gzip', or 'compress'"
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
43
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
44 archive_name = base_name + ".tar"
23668
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 21267
diff changeset
45 mkpath(os.path.dirname(archive_name), dry_run=dry_run)
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
46 cmd = ["tar", "-cf", archive_name, base_dir]
23668
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 21267
diff changeset
47 spawn(cmd, dry_run=dry_run)
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
48
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
49 if compress:
14864
56fda4e9ece9 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 14114
diff changeset
50 spawn([compress] + compress_flags[compress] + [archive_name],
23668
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 21267
diff changeset
51 dry_run=dry_run)
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
52 return archive_name + compress_ext[compress]
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
53 else:
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
54 return archive_name
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
55
54164
178f70fc478d pep8-fied distutils.archive_util + added minimum test coverage
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
56 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
57 """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
58
54164
178f70fc478d pep8-fied distutils.archive_util + added minimum test coverage
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
59 The output zip file will be named 'base_dir' + ".zip". Uses either the
178f70fc478d pep8-fied distutils.archive_util + added minimum test coverage
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
60 "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
61 (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
62 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
63 file.
14864
56fda4e9ece9 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 14114
diff changeset
64 """
26317
a4dba058f70e Bug #639118 from Ollie Oldham: archiver should use zipfile before zip
Andrew M. Kuchling <amk@amk.ca>
parents: 26285
diff changeset
65 try:
a4dba058f70e Bug #639118 from Ollie Oldham: archiver should use zipfile before zip
Andrew M. Kuchling <amk@amk.ca>
parents: 26285
diff changeset
66 import zipfile
a4dba058f70e Bug #639118 from Ollie Oldham: archiver should use zipfile before zip
Andrew M. Kuchling <amk@amk.ca>
parents: 26285
diff changeset
67 except ImportError:
a4dba058f70e Bug #639118 from Ollie Oldham: archiver should use zipfile before zip
Andrew M. Kuchling <amk@amk.ca>
parents: 26285
diff changeset
68 zipfile = None
32736
c3a096bfbd6b Whitespace normalization, via reindent.py.
Tim Peters <tim.peters@gmail.com>
parents: 26317
diff changeset
69
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
70 zip_filename = base_name + ".zip"
23668
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 21267
diff changeset
71 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
72
26317
a4dba058f70e Bug #639118 from Ollie Oldham: archiver should use zipfile before zip
Andrew M. Kuchling <amk@amk.ca>
parents: 26285
diff changeset
73 # 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
74 # 'zip' command.
a4dba058f70e Bug #639118 from Ollie Oldham: archiver should use zipfile before zip
Andrew M. Kuchling <amk@amk.ca>
parents: 26285
diff changeset
75 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
76 if verbose:
a4dba058f70e Bug #639118 from Ollie Oldham: archiver should use zipfile before zip
Andrew M. Kuchling <amk@amk.ca>
parents: 26285
diff changeset
77 zipoptions = "-r"
a4dba058f70e Bug #639118 from Ollie Oldham: archiver should use zipfile before zip
Andrew M. Kuchling <amk@amk.ca>
parents: 26285
diff changeset
78 else:
a4dba058f70e Bug #639118 from Ollie Oldham: archiver should use zipfile before zip
Andrew M. Kuchling <amk@amk.ca>
parents: 26285
diff changeset
79 zipoptions = "-rq"
32736
c3a096bfbd6b Whitespace normalization, via reindent.py.
Tim Peters <tim.peters@gmail.com>
parents: 26317
diff changeset
80
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
81 try:
26317
a4dba058f70e Bug #639118 from Ollie Oldham: archiver should use zipfile before zip
Andrew M. Kuchling <amk@amk.ca>
parents: 26285
diff changeset
82 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
83 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
84 except DistutilsExecError:
a4dba058f70e Bug #639118 from Ollie Oldham: archiver should use zipfile before zip
Andrew M. Kuchling <amk@amk.ca>
parents: 26285
diff changeset
85 # 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
86 # 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
87 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
88 ("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
89 "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
90 "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
91
26317
a4dba058f70e Bug #639118 from Ollie Oldham: archiver should use zipfile before zip
Andrew M. Kuchling <amk@amk.ca>
parents: 26285
diff changeset
92 else:
a4dba058f70e Bug #639118 from Ollie Oldham: archiver should use zipfile before zip
Andrew M. Kuchling <amk@amk.ca>
parents: 26285
diff changeset
93 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
94 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
95
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
96 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
97 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
98 compression=zipfile.ZIP_DEFLATED)
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
99
46767
dc573ee0d904 Replace instances of os.path.walk with os.walk
Benjamin Peterson <benjamin@python.org>
parents: 45413
diff changeset
100 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
101 for name in filenames:
dc573ee0d904 Replace instances of os.path.walk with os.walk
Benjamin Peterson <benjamin@python.org>
parents: 45413
diff changeset
102 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
103 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
104 zip.write(path, path)
46767
dc573ee0d904 Replace instances of os.path.walk with os.walk
Benjamin Peterson <benjamin@python.org>
parents: 45413
diff changeset
105 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
106 zip.close()
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
107
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
108 return zip_filename
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
109
12449
19f53d8bd298 Extracted the "what-do-I-do-for-this-format" logic from code in
Greg Ward <gward@python.net>
parents: 12226
diff changeset
110 ARCHIVE_FORMATS = {
12997
27f3a18fd296 Stylistic/formatting changes to Rene Liebscher's '--help-xxx' patch.
Greg Ward <gward@python.net>
parents: 12869
diff changeset
111 '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
112 '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
113 '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
114 '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
115 '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
116 }
19f53d8bd298 Extracted the "what-do-I-do-for-this-format" logic from code in
Greg Ward <gward@python.net>
parents: 12226
diff changeset
117
54164
178f70fc478d pep8-fied distutils.archive_util + added minimum test coverage
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
118 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
119 """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
120
178f70fc478d pep8-fied distutils.archive_util + added minimum test coverage
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
121 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
122 """
12449
19f53d8bd298 Extracted the "what-do-I-do-for-this-format" logic from code in
Greg Ward <gward@python.net>
parents: 12226
diff changeset
123 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
124 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
125 return format
54164
178f70fc478d pep8-fied distutils.archive_util + added minimum test coverage
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
126 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
127
54164
178f70fc478d pep8-fied distutils.archive_util + added minimum test coverage
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
128 def make_archive(base_name, format, root_dir=None, base_dir=None, verbose=0,
178f70fc478d pep8-fied distutils.archive_util + added minimum test coverage
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
129 dry_run=0):
178f70fc478d pep8-fied distutils.archive_util + added minimum test coverage
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
130 """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
131
178f70fc478d pep8-fied distutils.archive_util + added minimum test coverage
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
132 '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
133 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
134 or "gztar".
178f70fc478d pep8-fied distutils.archive_util + added minimum test coverage
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
135
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
136 '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
137 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
138 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
139 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
140 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
141 to the current directory. Returns the name of the archive file.
258eed4570ce Ensure that 'make_archive()' returns the name of the new archive file.
Greg Ward <gward@python.net>
parents: 12803
diff changeset
142 """
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
143 save_cwd = os.getcwd()
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
144 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
145 log.debug("changing into '%s'", root_dir)
14864
56fda4e9ece9 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 14114
diff changeset
146 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
147 if not dry_run:
14864
56fda4e9ece9 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 14114
diff changeset
148 os.chdir(root_dir)
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
149
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
150 if base_dir is None:
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
151 base_dir = os.curdir
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
152
54164
178f70fc478d pep8-fied distutils.archive_util + added minimum test coverage
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
153 kwargs = {'dry_run': dry_run}
21267
7a7f557b4d54 Whitespace normalization.
Fred Drake <fdrake@acm.org>
parents: 17515
diff changeset
154
12449
19f53d8bd298 Extracted the "what-do-I-do-for-this-format" logic from code in
Greg Ward <gward@python.net>
parents: 12226
diff changeset
155 try:
19f53d8bd298 Extracted the "what-do-I-do-for-this-format" logic from code in
Greg Ward <gward@python.net>
parents: 12226
diff changeset
156 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
157 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
158 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
159
12449
19f53d8bd298 Extracted the "what-do-I-do-for-this-format" logic from code in
Greg Ward <gward@python.net>
parents: 12226
diff changeset
160 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
161 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
162 kwargs[arg] = val
14864
56fda4e9ece9 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 14114
diff changeset
163 filename = apply(func, (base_name, base_dir), kwargs)
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
164
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
165 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
166 log.debug("changing back to '%s'", save_cwd)
14864
56fda4e9ece9 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 14114
diff changeset
167 os.chdir(save_cwd)
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
168
12809
258eed4570ce Ensure that 'make_archive()' returns the name of the new archive file.
Greg Ward <gward@python.net>
parents: 12803
diff changeset
169 return filename