blob: 1d86df36c26b129b7beae2d52ac276e6b30f7341 [file] [log] [blame]
Tarek Ziadé2900c442010-02-23 05:36:41 +00001"""Utility functions for copying and archiving files and directory trees.
Guido van Rossum9d0a3df1997-04-29 14:45:19 +00002
Guido van Rossum959fa011999-08-18 20:03:17 +00003XXX The functions here don't copy the resource fork or other metadata on Mac.
Guido van Rossum9d0a3df1997-04-29 14:45:19 +00004
5"""
Guido van Rossumc6360141990-10-13 19:23:40 +00006
Guido van Rossumc96207a1992-03-31 18:55:40 +00007import os
Guido van Rossum83c03e21999-02-23 23:07:51 +00008import sys
Guido van Rossum9d0a3df1997-04-29 14:45:19 +00009import stat
Brett Cannon1c3fa182004-06-19 21:11:35 +000010from os.path import abspath
Georg Brandle78fbcc2008-07-05 10:13:36 +000011import fnmatch
Florent Xicluna1f3b4e12010-03-07 12:14:25 +000012import collections
Antoine Pitrou513d9ae2010-03-22 19:59:46 +000013import errno
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +000014
15try:
16 from pwd import getpwnam
17except ImportError:
18 getpwnam = None
19
20try:
21 from grp import getgrnam
22except ImportError:
23 getgrnam = None
Guido van Rossumc6360141990-10-13 19:23:40 +000024
Tarek Ziadé2900c442010-02-23 05:36:41 +000025__all__ = ["copyfileobj", "copyfile", "copymode", "copystat", "copy", "copy2",
26 "copytree", "move", "rmtree", "Error", "SpecialFileError",
27 "ExecError", "make_archive", "get_archive_formats",
28 "register_archive_format", "unregister_archive_format"]
Martin v. Löwise9ce0b02002-10-07 13:23:24 +000029
Neal Norwitz4ce69a52005-09-01 00:45:28 +000030class Error(EnvironmentError):
Martin v. Löwise9ce0b02002-10-07 13:23:24 +000031 pass
Guido van Rossumc6360141990-10-13 19:23:40 +000032
Antoine Pitrou1fc02312009-05-01 20:55:35 +000033class SpecialFileError(EnvironmentError):
34 """Raised when trying to do a kind of operation (e.g. copying) which is
35 not supported on a special file (e.g. a named pipe)"""
36
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +000037class ExecError(EnvironmentError):
38 """Raised when a command could not be executed"""
39
Antoine Pitrou9fcd4b32008-08-11 17:21:36 +000040try:
41 WindowsError
42except NameError:
43 WindowsError = None
44
Greg Stein42bb8b32000-07-12 09:55:30 +000045def copyfileobj(fsrc, fdst, length=16*1024):
46 """copy data from file-like object fsrc to file-like object fdst"""
47 while 1:
48 buf = fsrc.read(length)
49 if not buf:
50 break
51 fdst.write(buf)
52
Johannes Gijsbers46f14592004-08-14 13:30:02 +000053def _samefile(src, dst):
54 # Macintosh, Unix.
Tarek Ziadéf1c28b72010-04-19 21:13:03 +000055 if hasattr(os.path, 'samefile'):
Johannes Gijsbersf9a098e2004-08-14 14:51:01 +000056 try:
57 return os.path.samefile(src, dst)
58 except OSError:
59 return False
Johannes Gijsbers46f14592004-08-14 13:30:02 +000060
61 # All other platforms: check for same pathname.
62 return (os.path.normcase(os.path.abspath(src)) ==
63 os.path.normcase(os.path.abspath(dst)))
Tim Peters495ad3c2001-01-15 01:36:40 +000064
Guido van Rossumc6360141990-10-13 19:23:40 +000065def copyfile(src, dst):
Guido van Rossum9d0a3df1997-04-29 14:45:19 +000066 """Copy data from src to dst"""
Johannes Gijsbers46f14592004-08-14 13:30:02 +000067 if _samefile(src, dst):
Tarek Ziadéf1c28b72010-04-19 21:13:03 +000068 raise Error("`%s` and `%s` are the same file" % (src, dst))
Johannes Gijsbers46f14592004-08-14 13:30:02 +000069
Antoine Pitrou1fc02312009-05-01 20:55:35 +000070 for fn in [src, dst]:
71 try:
72 st = os.stat(fn)
73 except OSError:
74 # File most likely does not exist
75 pass
Benjamin Petersona663a372009-06-05 19:09:28 +000076 else:
77 # XXX What about other special files? (sockets, devices...)
78 if stat.S_ISFIFO(st.st_mode):
79 raise SpecialFileError("`%s` is a named pipe" % fn)
Tarek Ziadé31a673d2010-05-05 22:41:25 +000080
Tarek Ziadé38f81222010-05-05 22:15:31 +000081 with open(src, 'rb') as fsrc:
82 with open(dst, 'wb') as fdst:
83 copyfileobj(fsrc, fdst)
Guido van Rossumc6360141990-10-13 19:23:40 +000084
Guido van Rossumc6360141990-10-13 19:23:40 +000085def copymode(src, dst):
Guido van Rossum9d0a3df1997-04-29 14:45:19 +000086 """Copy mode bits from src to dst"""
Tim Peters0c947242001-01-21 20:00:00 +000087 if hasattr(os, 'chmod'):
88 st = os.stat(src)
Walter Dörwald294bbf32002-06-06 09:48:13 +000089 mode = stat.S_IMODE(st.st_mode)
Tim Peters0c947242001-01-21 20:00:00 +000090 os.chmod(dst, mode)
Guido van Rossumc6360141990-10-13 19:23:40 +000091
Guido van Rossumc6360141990-10-13 19:23:40 +000092def copystat(src, dst):
Martin v. Löwis382abef2007-02-19 10:55:19 +000093 """Copy all stat info (mode bits, atime, mtime, flags) from src to dst"""
Guido van Rossuma2baf461997-04-29 14:06:46 +000094 st = os.stat(src)
Walter Dörwald294bbf32002-06-06 09:48:13 +000095 mode = stat.S_IMODE(st.st_mode)
Tim Peters0c947242001-01-21 20:00:00 +000096 if hasattr(os, 'utime'):
Walter Dörwald294bbf32002-06-06 09:48:13 +000097 os.utime(dst, (st.st_atime, st.st_mtime))
Tim Peters0c947242001-01-21 20:00:00 +000098 if hasattr(os, 'chmod'):
99 os.chmod(dst, mode)
Martin v. Löwis382abef2007-02-19 10:55:19 +0000100 if hasattr(os, 'chflags') and hasattr(st, 'st_flags'):
Antoine Pitrou513d9ae2010-03-22 19:59:46 +0000101 try:
102 os.chflags(dst, st.st_flags)
103 except OSError, why:
Tarek Ziadéf1c28b72010-04-19 21:13:03 +0000104 if (not hasattr(errno, 'EOPNOTSUPP') or
105 why.errno != errno.EOPNOTSUPP):
Antoine Pitrou513d9ae2010-03-22 19:59:46 +0000106 raise
Guido van Rossum9d0a3df1997-04-29 14:45:19 +0000107
Guido van Rossumc6360141990-10-13 19:23:40 +0000108def copy(src, dst):
Guido van Rossum9d0a3df1997-04-29 14:45:19 +0000109 """Copy data and mode bits ("cp src dst").
Tim Peters495ad3c2001-01-15 01:36:40 +0000110
Guido van Rossum9d0a3df1997-04-29 14:45:19 +0000111 The destination may be a directory.
112
113 """
Guido van Rossuma2baf461997-04-29 14:06:46 +0000114 if os.path.isdir(dst):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000115 dst = os.path.join(dst, os.path.basename(src))
Guido van Rossuma2baf461997-04-29 14:06:46 +0000116 copyfile(src, dst)
117 copymode(src, dst)
Guido van Rossumc6360141990-10-13 19:23:40 +0000118
Guido van Rossumc6360141990-10-13 19:23:40 +0000119def copy2(src, dst):
Guido van Rossum9d0a3df1997-04-29 14:45:19 +0000120 """Copy data and all stat info ("cp -p src dst").
121
122 The destination may be a directory.
123
124 """
Guido van Rossuma2baf461997-04-29 14:06:46 +0000125 if os.path.isdir(dst):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000126 dst = os.path.join(dst, os.path.basename(src))
Guido van Rossuma2baf461997-04-29 14:06:46 +0000127 copyfile(src, dst)
128 copystat(src, dst)
Guido van Rossumc6360141990-10-13 19:23:40 +0000129
Georg Brandle78fbcc2008-07-05 10:13:36 +0000130def ignore_patterns(*patterns):
131 """Function that can be used as copytree() ignore parameter.
Guido van Rossum9d0a3df1997-04-29 14:45:19 +0000132
Georg Brandle78fbcc2008-07-05 10:13:36 +0000133 Patterns is a sequence of glob-style patterns
134 that are used to exclude files"""
135 def _ignore_patterns(path, names):
136 ignored_names = []
137 for pattern in patterns:
138 ignored_names.extend(fnmatch.filter(names, pattern))
139 return set(ignored_names)
140 return _ignore_patterns
141
142def copytree(src, dst, symlinks=False, ignore=None):
Guido van Rossum9d0a3df1997-04-29 14:45:19 +0000143 """Recursively copy a directory tree using copy2().
144
145 The destination directory must not already exist.
Neal Norwitza4c93b62003-02-23 21:36:32 +0000146 If exception(s) occur, an Error is raised with a list of reasons.
Guido van Rossum9d0a3df1997-04-29 14:45:19 +0000147
148 If the optional symlinks flag is true, symbolic links in the
149 source tree result in symbolic links in the destination tree; if
150 it is false, the contents of the files pointed to by symbolic
151 links are copied.
152
Georg Brandle78fbcc2008-07-05 10:13:36 +0000153 The optional ignore argument is a callable. If given, it
154 is called with the `src` parameter, which is the directory
155 being visited by copytree(), and `names` which is the list of
156 `src` contents, as returned by os.listdir():
157
158 callable(src, names) -> ignored_names
159
160 Since copytree() is called recursively, the callable will be
161 called once for each directory that is copied. It returns a
162 list of names relative to the `src` directory that should
163 not be copied.
164
Guido van Rossum9d0a3df1997-04-29 14:45:19 +0000165 XXX Consider this example code rather than the ultimate tool.
166
167 """
Guido van Rossuma2baf461997-04-29 14:06:46 +0000168 names = os.listdir(src)
Georg Brandle78fbcc2008-07-05 10:13:36 +0000169 if ignore is not None:
170 ignored_names = ignore(src, names)
171 else:
172 ignored_names = set()
173
Johannes Gijsberse4172ea2005-01-08 12:31:29 +0000174 os.makedirs(dst)
Martin v. Löwise9ce0b02002-10-07 13:23:24 +0000175 errors = []
Guido van Rossuma2baf461997-04-29 14:06:46 +0000176 for name in names:
Georg Brandle78fbcc2008-07-05 10:13:36 +0000177 if name in ignored_names:
178 continue
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000179 srcname = os.path.join(src, name)
180 dstname = os.path.join(dst, name)
181 try:
182 if symlinks and os.path.islink(srcname):
183 linkto = os.readlink(srcname)
184 os.symlink(linkto, dstname)
185 elif os.path.isdir(srcname):
Georg Brandle78fbcc2008-07-05 10:13:36 +0000186 copytree(srcname, dstname, symlinks, ignore)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000187 else:
Antoine Pitrou1fc02312009-05-01 20:55:35 +0000188 # Will raise a SpecialFileError for unsupported file types
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000189 copy2(srcname, dstname)
Georg Brandla1be88e2005-08-31 22:48:45 +0000190 # catch the Error from the recursive copytree so that we can
191 # continue with other files
192 except Error, err:
193 errors.extend(err.args[0])
Antoine Pitrou1fc02312009-05-01 20:55:35 +0000194 except EnvironmentError, why:
195 errors.append((srcname, dstname, str(why)))
Martin v. Löwis4e678382006-07-30 13:00:31 +0000196 try:
197 copystat(src, dst)
Martin v. Löwis4e678382006-07-30 13:00:31 +0000198 except OSError, why:
Antoine Pitrou9fcd4b32008-08-11 17:21:36 +0000199 if WindowsError is not None and isinstance(why, WindowsError):
200 # Copying file access times may fail on Windows
201 pass
202 else:
203 errors.extend((src, dst, str(why)))
Martin v. Löwise9ce0b02002-10-07 13:23:24 +0000204 if errors:
205 raise Error, errors
Guido van Rossumd7673291998-02-06 21:38:09 +0000206
Barry Warsaw234d9a92003-01-24 17:36:15 +0000207def rmtree(path, ignore_errors=False, onerror=None):
Guido van Rossumd7673291998-02-06 21:38:09 +0000208 """Recursively delete a directory tree.
209
Johannes Gijsbersef5ffc42004-10-31 12:05:31 +0000210 If ignore_errors is set, errors are ignored; otherwise, if onerror
211 is set, it is called to handle the error with arguments (func,
212 path, exc_info) where func is os.listdir, os.remove, or os.rmdir;
213 path is the argument to that function that caused it to fail; and
214 exc_info is a tuple returned by sys.exc_info(). If ignore_errors
215 is false and onerror is None, an exception is raised.
216
Guido van Rossumd7673291998-02-06 21:38:09 +0000217 """
Johannes Gijsbersef5ffc42004-10-31 12:05:31 +0000218 if ignore_errors:
219 def onerror(*args):
Barry Warsaw234d9a92003-01-24 17:36:15 +0000220 pass
Johannes Gijsbersef5ffc42004-10-31 12:05:31 +0000221 elif onerror is None:
222 def onerror(*args):
223 raise
Georg Brandl52353982008-01-20 14:17:42 +0000224 try:
225 if os.path.islink(path):
226 # symlinks to directories are forbidden, see bug #1669
227 raise OSError("Cannot call rmtree on a symbolic link")
228 except OSError:
229 onerror(os.path.islink, path, sys.exc_info())
230 # can't continue even if onerror hook returns
231 return
Johannes Gijsbersef5ffc42004-10-31 12:05:31 +0000232 names = []
233 try:
234 names = os.listdir(path)
235 except os.error, err:
236 onerror(os.listdir, path, sys.exc_info())
237 for name in names:
238 fullname = os.path.join(path, name)
239 try:
240 mode = os.lstat(fullname).st_mode
241 except os.error:
242 mode = 0
243 if stat.S_ISDIR(mode):
244 rmtree(fullname, ignore_errors, onerror)
Barry Warsaw234d9a92003-01-24 17:36:15 +0000245 else:
Johannes Gijsbersef5ffc42004-10-31 12:05:31 +0000246 try:
247 os.remove(fullname)
248 except os.error, err:
249 onerror(os.remove, fullname, sys.exc_info())
250 try:
251 os.rmdir(path)
252 except os.error:
253 onerror(os.rmdir, path, sys.exc_info())
Guido van Rossumd7673291998-02-06 21:38:09 +0000254
Martin v. Löwise9ce0b02002-10-07 13:23:24 +0000255
Sean Reifscheider493894c2008-03-18 17:24:12 +0000256def _basename(path):
257 # A basename() variant which first strips the trailing slash, if present.
258 # Thus we always get the last component of the path, even for directories.
259 return os.path.basename(path.rstrip(os.path.sep))
260
261def move(src, dst):
262 """Recursively move a file or directory to another location. This is
263 similar to the Unix "mv" command.
264
265 If the destination is a directory or a symlink to a directory, the source
266 is moved inside the directory. The destination path must not already
267 exist.
268
269 If the destination already exists but is not a directory, it may be
270 overwritten depending on os.rename() semantics.
271
272 If the destination is on our current filesystem, then rename() is used.
273 Otherwise, src is copied to the destination and then removed.
Martin v. Löwise9ce0b02002-10-07 13:23:24 +0000274 A lot more could be done here... A look at a mv.c shows a lot of
275 the issues this implementation glosses over.
276
277 """
Sean Reifscheider493894c2008-03-18 17:24:12 +0000278 real_dst = dst
279 if os.path.isdir(dst):
280 real_dst = os.path.join(dst, _basename(src))
281 if os.path.exists(real_dst):
282 raise Error, "Destination path '%s' already exists" % real_dst
Martin v. Löwise9ce0b02002-10-07 13:23:24 +0000283 try:
Sean Reifscheider493894c2008-03-18 17:24:12 +0000284 os.rename(src, real_dst)
Martin v. Löwise9ce0b02002-10-07 13:23:24 +0000285 except OSError:
286 if os.path.isdir(src):
Benjamin Peterson096c3ad2009-02-07 19:08:22 +0000287 if _destinsrc(src, dst):
Brett Cannon1c3fa182004-06-19 21:11:35 +0000288 raise Error, "Cannot move a directory '%s' into itself '%s'." % (src, dst)
Sean Reifscheider493894c2008-03-18 17:24:12 +0000289 copytree(src, real_dst, symlinks=True)
Martin v. Löwise9ce0b02002-10-07 13:23:24 +0000290 rmtree(src)
291 else:
Sean Reifscheider493894c2008-03-18 17:24:12 +0000292 copy2(src, real_dst)
Martin v. Löwise9ce0b02002-10-07 13:23:24 +0000293 os.unlink(src)
Brett Cannon1c3fa182004-06-19 21:11:35 +0000294
Benjamin Peterson096c3ad2009-02-07 19:08:22 +0000295def _destinsrc(src, dst):
Antoine Pitrou707c5932009-01-29 20:19:34 +0000296 src = abspath(src)
297 dst = abspath(dst)
298 if not src.endswith(os.path.sep):
299 src += os.path.sep
300 if not dst.endswith(os.path.sep):
301 dst += os.path.sep
302 return dst.startswith(src)
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000303
304def _get_gid(name):
305 """Returns a gid, given a group name."""
306 if getgrnam is None or name is None:
307 return None
308 try:
309 result = getgrnam(name)
310 except KeyError:
311 result = None
312 if result is not None:
313 return result[2]
314 return None
315
316def _get_uid(name):
317 """Returns an uid, given a user name."""
318 if getpwnam is None or name is None:
319 return None
320 try:
321 result = getpwnam(name)
322 except KeyError:
323 result = None
324 if result is not None:
325 return result[2]
326 return None
327
328def _make_tarball(base_name, base_dir, compress="gzip", verbose=0, dry_run=0,
329 owner=None, group=None, logger=None):
330 """Create a (possibly compressed) tar file from all the files under
331 'base_dir'.
332
Tarek Ziadée593fad2010-04-20 21:09:06 +0000333 'compress' must be "gzip" (the default), "bzip2", or None.
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000334
335 'owner' and 'group' can be used to define an owner and a group for the
336 archive that is being built. If not provided, the current owner and group
337 will be used.
338
339 The output tar file will be named 'base_dir' + ".tar", possibly plus
Tarek Ziadée593fad2010-04-20 21:09:06 +0000340 the appropriate compression extension (".gz", or ".bz2").
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000341
342 Returns the output filename.
343 """
Tarek Ziadée593fad2010-04-20 21:09:06 +0000344 tar_compression = {'gzip': 'gz', 'bzip2': 'bz2', None: ''}
345 compress_ext = {'gzip': '.gz', 'bzip2': '.bz2'}
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000346
347 # flags for compression program, each element of list will be an argument
348 if compress is not None and compress not in compress_ext.keys():
349 raise ValueError, \
Tarek Ziadée593fad2010-04-20 21:09:06 +0000350 ("bad value for 'compress': must be None, 'gzip' or 'bzip2'")
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000351
Tarek Ziadée593fad2010-04-20 21:09:06 +0000352 archive_name = base_name + '.tar' + compress_ext.get(compress, '')
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000353 archive_dir = os.path.dirname(archive_name)
Tarek Ziadée593fad2010-04-20 21:09:06 +0000354
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000355 if not os.path.exists(archive_dir):
356 logger.info("creating %s" % archive_dir)
357 if not dry_run:
358 os.makedirs(archive_dir)
359
360
361 # creating the tarball
362 import tarfile # late import so Python build itself doesn't break
363
364 if logger is not None:
365 logger.info('Creating tar archive')
366
367 uid = _get_uid(owner)
368 gid = _get_gid(group)
369
370 def _set_uid_gid(tarinfo):
371 if gid is not None:
372 tarinfo.gid = gid
373 tarinfo.gname = group
374 if uid is not None:
375 tarinfo.uid = uid
376 tarinfo.uname = owner
377 return tarinfo
378
379 if not dry_run:
380 tar = tarfile.open(archive_name, 'w|%s' % tar_compression[compress])
381 try:
382 tar.add(base_dir, filter=_set_uid_gid)
383 finally:
384 tar.close()
385
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000386 return archive_name
387
Tarek Ziadé62e17ad2010-04-21 13:32:26 +0000388def _call_external_zip(base_dir, zip_filename, verbose=False, dry_run=False):
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000389 # XXX see if we want to keep an external call here
390 if verbose:
391 zipoptions = "-r"
392 else:
393 zipoptions = "-rq"
394 from distutils.errors import DistutilsExecError
395 from distutils.spawn import spawn
396 try:
397 spawn(["zip", zipoptions, zip_filename, base_dir], dry_run=dry_run)
398 except DistutilsExecError:
399 # XXX really should distinguish between "couldn't find
400 # external 'zip' command" and "zip failed".
401 raise ExecError, \
402 ("unable to create zip file '%s': "
403 "could neither import the 'zipfile' module nor "
404 "find a standalone zip utility") % zip_filename
405
406def _make_zipfile(base_name, base_dir, verbose=0, dry_run=0, logger=None):
407 """Create a zip file from all the files under 'base_dir'.
408
409 The output zip file will be named 'base_dir' + ".zip". Uses either the
410 "zipfile" Python module (if available) or the InfoZIP "zip" utility
411 (if installed and found on the default search path). If neither tool is
412 available, raises ExecError. Returns the name of the output zip
413 file.
414 """
415 zip_filename = base_name + ".zip"
416 archive_dir = os.path.dirname(base_name)
417
418 if not os.path.exists(archive_dir):
419 if logger is not None:
420 logger.info("creating %s", archive_dir)
421 if not dry_run:
422 os.makedirs(archive_dir)
423
424 # If zipfile module is not available, try spawning an external 'zip'
425 # command.
426 try:
427 import zipfile
428 except ImportError:
429 zipfile = None
430
431 if zipfile is None:
Tarek Ziadé62e17ad2010-04-21 13:32:26 +0000432 _call_external_zip(base_dir, zip_filename, verbose, dry_run)
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000433 else:
434 if logger is not None:
435 logger.info("creating '%s' and adding '%s' to it",
436 zip_filename, base_dir)
437
438 if not dry_run:
439 zip = zipfile.ZipFile(zip_filename, "w",
440 compression=zipfile.ZIP_DEFLATED)
441
442 for dirpath, dirnames, filenames in os.walk(base_dir):
443 for name in filenames:
444 path = os.path.normpath(os.path.join(dirpath, name))
445 if os.path.isfile(path):
446 zip.write(path, path)
447 if logger is not None:
448 logger.info("adding '%s'", path)
449 zip.close()
450
451 return zip_filename
452
453_ARCHIVE_FORMATS = {
454 'gztar': (_make_tarball, [('compress', 'gzip')], "gzip'ed tar-file"),
455 'bztar': (_make_tarball, [('compress', 'bzip2')], "bzip2'ed tar-file"),
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000456 'tar': (_make_tarball, [('compress', None)], "uncompressed tar file"),
457 'zip': (_make_zipfile, [],"ZIP file")
458 }
459
460def get_archive_formats():
461 """Returns a list of supported formats for archiving and unarchiving.
462
463 Each element of the returned sequence is a tuple (name, description)
464 """
465 formats = [(name, registry[2]) for name, registry in
466 _ARCHIVE_FORMATS.items()]
467 formats.sort()
468 return formats
469
470def register_archive_format(name, function, extra_args=None, description=''):
471 """Registers an archive format.
472
473 name is the name of the format. function is the callable that will be
474 used to create archives. If provided, extra_args is a sequence of
475 (name, value) tuples that will be passed as arguments to the callable.
476 description can be provided to describe the format, and will be returned
477 by the get_archive_formats() function.
478 """
479 if extra_args is None:
480 extra_args = []
Florent Xicluna1f3b4e12010-03-07 12:14:25 +0000481 if not isinstance(function, collections.Callable):
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000482 raise TypeError('The %s object is not callable' % function)
483 if not isinstance(extra_args, (tuple, list)):
484 raise TypeError('extra_args needs to be a sequence')
485 for element in extra_args:
486 if not isinstance(element, (tuple, list)) or len(element) !=2 :
487 raise TypeError('extra_args elements are : (arg_name, value)')
488
489 _ARCHIVE_FORMATS[name] = (function, extra_args, description)
490
491def unregister_archive_format(name):
492 del _ARCHIVE_FORMATS[name]
493
494def make_archive(base_name, format, root_dir=None, base_dir=None, verbose=0,
495 dry_run=0, owner=None, group=None, logger=None):
496 """Create an archive file (eg. zip or tar).
497
498 'base_name' is the name of the file to create, minus any format-specific
Tarek Ziadée593fad2010-04-20 21:09:06 +0000499 extension; 'format' is the archive format: one of "zip", "tar", "bztar"
500 or "gztar".
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000501
502 'root_dir' is a directory that will be the root directory of the
503 archive; ie. we typically chdir into 'root_dir' before creating the
504 archive. 'base_dir' is the directory where we start archiving from;
505 ie. 'base_dir' will be the common prefix of all files and
506 directories in the archive. 'root_dir' and 'base_dir' both default
507 to the current directory. Returns the name of the archive file.
508
509 'owner' and 'group' are used when creating a tar archive. By default,
510 uses the current owner and group.
511 """
512 save_cwd = os.getcwd()
513 if root_dir is not None:
514 if logger is not None:
515 logger.debug("changing into '%s'", root_dir)
516 base_name = os.path.abspath(base_name)
517 if not dry_run:
518 os.chdir(root_dir)
519
520 if base_dir is None:
521 base_dir = os.curdir
522
523 kwargs = {'dry_run': dry_run, 'logger': logger}
524
525 try:
526 format_info = _ARCHIVE_FORMATS[format]
527 except KeyError:
528 raise ValueError, "unknown archive format '%s'" % format
529
530 func = format_info[0]
531 for arg, val in format_info[1]:
532 kwargs[arg] = val
533
534 if format != 'zip':
535 kwargs['owner'] = owner
536 kwargs['group'] = group
537
538 try:
539 filename = func(base_name, base_dir, **kwargs)
540 finally:
541 if root_dir is not None:
542 if logger is not None:
543 logger.debug("changing back to '%s'", save_cwd)
544 os.chdir(save_cwd)
545
546 return filename