blob: 703b3b59a0187eb7c9bd3fb1aaab6fd15bd5b36f [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
Guido van Rossuma2baf461997-04-29 14:06:46 +000070 fsrc = None
71 fdst = None
Antoine Pitrou1fc02312009-05-01 20:55:35 +000072 for fn in [src, dst]:
73 try:
74 st = os.stat(fn)
75 except OSError:
76 # File most likely does not exist
77 pass
Benjamin Petersona663a372009-06-05 19:09:28 +000078 else:
79 # XXX What about other special files? (sockets, devices...)
80 if stat.S_ISFIFO(st.st_mode):
81 raise SpecialFileError("`%s` is a named pipe" % fn)
Guido van Rossuma2baf461997-04-29 14:06:46 +000082 try:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000083 fsrc = open(src, 'rb')
84 fdst = open(dst, 'wb')
Greg Stein42bb8b32000-07-12 09:55:30 +000085 copyfileobj(fsrc, fdst)
Guido van Rossuma2baf461997-04-29 14:06:46 +000086 finally:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000087 if fdst:
88 fdst.close()
89 if fsrc:
90 fsrc.close()
Guido van Rossumc6360141990-10-13 19:23:40 +000091
Guido van Rossumc6360141990-10-13 19:23:40 +000092def copymode(src, dst):
Guido van Rossum9d0a3df1997-04-29 14:45:19 +000093 """Copy mode bits from src to dst"""
Tim Peters0c947242001-01-21 20:00:00 +000094 if hasattr(os, 'chmod'):
95 st = os.stat(src)
Walter Dörwald294bbf32002-06-06 09:48:13 +000096 mode = stat.S_IMODE(st.st_mode)
Tim Peters0c947242001-01-21 20:00:00 +000097 os.chmod(dst, mode)
Guido van Rossumc6360141990-10-13 19:23:40 +000098
Guido van Rossumc6360141990-10-13 19:23:40 +000099def copystat(src, dst):
Martin v. Löwis382abef2007-02-19 10:55:19 +0000100 """Copy all stat info (mode bits, atime, mtime, flags) from src to dst"""
Guido van Rossuma2baf461997-04-29 14:06:46 +0000101 st = os.stat(src)
Walter Dörwald294bbf32002-06-06 09:48:13 +0000102 mode = stat.S_IMODE(st.st_mode)
Tim Peters0c947242001-01-21 20:00:00 +0000103 if hasattr(os, 'utime'):
Walter Dörwald294bbf32002-06-06 09:48:13 +0000104 os.utime(dst, (st.st_atime, st.st_mtime))
Tim Peters0c947242001-01-21 20:00:00 +0000105 if hasattr(os, 'chmod'):
106 os.chmod(dst, mode)
Martin v. Löwis382abef2007-02-19 10:55:19 +0000107 if hasattr(os, 'chflags') and hasattr(st, 'st_flags'):
Antoine Pitrou513d9ae2010-03-22 19:59:46 +0000108 try:
109 os.chflags(dst, st.st_flags)
110 except OSError, why:
Tarek Ziadéf1c28b72010-04-19 21:13:03 +0000111 if (not hasattr(errno, 'EOPNOTSUPP') or
112 why.errno != errno.EOPNOTSUPP):
Antoine Pitrou513d9ae2010-03-22 19:59:46 +0000113 raise
Guido van Rossum9d0a3df1997-04-29 14:45:19 +0000114
Guido van Rossumc6360141990-10-13 19:23:40 +0000115def copy(src, dst):
Guido van Rossum9d0a3df1997-04-29 14:45:19 +0000116 """Copy data and mode bits ("cp src dst").
Tim Peters495ad3c2001-01-15 01:36:40 +0000117
Guido van Rossum9d0a3df1997-04-29 14:45:19 +0000118 The destination may be a directory.
119
120 """
Guido van Rossuma2baf461997-04-29 14:06:46 +0000121 if os.path.isdir(dst):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000122 dst = os.path.join(dst, os.path.basename(src))
Guido van Rossuma2baf461997-04-29 14:06:46 +0000123 copyfile(src, dst)
124 copymode(src, dst)
Guido van Rossumc6360141990-10-13 19:23:40 +0000125
Guido van Rossumc6360141990-10-13 19:23:40 +0000126def copy2(src, dst):
Guido van Rossum9d0a3df1997-04-29 14:45:19 +0000127 """Copy data and all stat info ("cp -p src dst").
128
129 The destination may be a directory.
130
131 """
Guido van Rossuma2baf461997-04-29 14:06:46 +0000132 if os.path.isdir(dst):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000133 dst = os.path.join(dst, os.path.basename(src))
Guido van Rossuma2baf461997-04-29 14:06:46 +0000134 copyfile(src, dst)
135 copystat(src, dst)
Guido van Rossumc6360141990-10-13 19:23:40 +0000136
Georg Brandle78fbcc2008-07-05 10:13:36 +0000137def ignore_patterns(*patterns):
138 """Function that can be used as copytree() ignore parameter.
Guido van Rossum9d0a3df1997-04-29 14:45:19 +0000139
Georg Brandle78fbcc2008-07-05 10:13:36 +0000140 Patterns is a sequence of glob-style patterns
141 that are used to exclude files"""
142 def _ignore_patterns(path, names):
143 ignored_names = []
144 for pattern in patterns:
145 ignored_names.extend(fnmatch.filter(names, pattern))
146 return set(ignored_names)
147 return _ignore_patterns
148
149def copytree(src, dst, symlinks=False, ignore=None):
Guido van Rossum9d0a3df1997-04-29 14:45:19 +0000150 """Recursively copy a directory tree using copy2().
151
152 The destination directory must not already exist.
Neal Norwitza4c93b62003-02-23 21:36:32 +0000153 If exception(s) occur, an Error is raised with a list of reasons.
Guido van Rossum9d0a3df1997-04-29 14:45:19 +0000154
155 If the optional symlinks flag is true, symbolic links in the
156 source tree result in symbolic links in the destination tree; if
157 it is false, the contents of the files pointed to by symbolic
158 links are copied.
159
Georg Brandle78fbcc2008-07-05 10:13:36 +0000160 The optional ignore argument is a callable. If given, it
161 is called with the `src` parameter, which is the directory
162 being visited by copytree(), and `names` which is the list of
163 `src` contents, as returned by os.listdir():
164
165 callable(src, names) -> ignored_names
166
167 Since copytree() is called recursively, the callable will be
168 called once for each directory that is copied. It returns a
169 list of names relative to the `src` directory that should
170 not be copied.
171
Guido van Rossum9d0a3df1997-04-29 14:45:19 +0000172 XXX Consider this example code rather than the ultimate tool.
173
174 """
Guido van Rossuma2baf461997-04-29 14:06:46 +0000175 names = os.listdir(src)
Georg Brandle78fbcc2008-07-05 10:13:36 +0000176 if ignore is not None:
177 ignored_names = ignore(src, names)
178 else:
179 ignored_names = set()
180
Johannes Gijsberse4172ea2005-01-08 12:31:29 +0000181 os.makedirs(dst)
Martin v. Löwise9ce0b02002-10-07 13:23:24 +0000182 errors = []
Guido van Rossuma2baf461997-04-29 14:06:46 +0000183 for name in names:
Georg Brandle78fbcc2008-07-05 10:13:36 +0000184 if name in ignored_names:
185 continue
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000186 srcname = os.path.join(src, name)
187 dstname = os.path.join(dst, name)
188 try:
189 if symlinks and os.path.islink(srcname):
190 linkto = os.readlink(srcname)
191 os.symlink(linkto, dstname)
192 elif os.path.isdir(srcname):
Georg Brandle78fbcc2008-07-05 10:13:36 +0000193 copytree(srcname, dstname, symlinks, ignore)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000194 else:
Antoine Pitrou1fc02312009-05-01 20:55:35 +0000195 # Will raise a SpecialFileError for unsupported file types
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000196 copy2(srcname, dstname)
Georg Brandla1be88e2005-08-31 22:48:45 +0000197 # catch the Error from the recursive copytree so that we can
198 # continue with other files
199 except Error, err:
200 errors.extend(err.args[0])
Antoine Pitrou1fc02312009-05-01 20:55:35 +0000201 except EnvironmentError, why:
202 errors.append((srcname, dstname, str(why)))
Martin v. Löwis4e678382006-07-30 13:00:31 +0000203 try:
204 copystat(src, dst)
Martin v. Löwis4e678382006-07-30 13:00:31 +0000205 except OSError, why:
Antoine Pitrou9fcd4b32008-08-11 17:21:36 +0000206 if WindowsError is not None and isinstance(why, WindowsError):
207 # Copying file access times may fail on Windows
208 pass
209 else:
210 errors.extend((src, dst, str(why)))
Martin v. Löwise9ce0b02002-10-07 13:23:24 +0000211 if errors:
212 raise Error, errors
Guido van Rossumd7673291998-02-06 21:38:09 +0000213
Barry Warsaw234d9a92003-01-24 17:36:15 +0000214def rmtree(path, ignore_errors=False, onerror=None):
Guido van Rossumd7673291998-02-06 21:38:09 +0000215 """Recursively delete a directory tree.
216
Johannes Gijsbersef5ffc42004-10-31 12:05:31 +0000217 If ignore_errors is set, errors are ignored; otherwise, if onerror
218 is set, it is called to handle the error with arguments (func,
219 path, exc_info) where func is os.listdir, os.remove, or os.rmdir;
220 path is the argument to that function that caused it to fail; and
221 exc_info is a tuple returned by sys.exc_info(). If ignore_errors
222 is false and onerror is None, an exception is raised.
223
Guido van Rossumd7673291998-02-06 21:38:09 +0000224 """
Johannes Gijsbersef5ffc42004-10-31 12:05:31 +0000225 if ignore_errors:
226 def onerror(*args):
Barry Warsaw234d9a92003-01-24 17:36:15 +0000227 pass
Johannes Gijsbersef5ffc42004-10-31 12:05:31 +0000228 elif onerror is None:
229 def onerror(*args):
230 raise
Georg Brandl52353982008-01-20 14:17:42 +0000231 try:
232 if os.path.islink(path):
233 # symlinks to directories are forbidden, see bug #1669
234 raise OSError("Cannot call rmtree on a symbolic link")
235 except OSError:
236 onerror(os.path.islink, path, sys.exc_info())
237 # can't continue even if onerror hook returns
238 return
Johannes Gijsbersef5ffc42004-10-31 12:05:31 +0000239 names = []
240 try:
241 names = os.listdir(path)
242 except os.error, err:
243 onerror(os.listdir, path, sys.exc_info())
244 for name in names:
245 fullname = os.path.join(path, name)
246 try:
247 mode = os.lstat(fullname).st_mode
248 except os.error:
249 mode = 0
250 if stat.S_ISDIR(mode):
251 rmtree(fullname, ignore_errors, onerror)
Barry Warsaw234d9a92003-01-24 17:36:15 +0000252 else:
Johannes Gijsbersef5ffc42004-10-31 12:05:31 +0000253 try:
254 os.remove(fullname)
255 except os.error, err:
256 onerror(os.remove, fullname, sys.exc_info())
257 try:
258 os.rmdir(path)
259 except os.error:
260 onerror(os.rmdir, path, sys.exc_info())
Guido van Rossumd7673291998-02-06 21:38:09 +0000261
Martin v. Löwise9ce0b02002-10-07 13:23:24 +0000262
Sean Reifscheider493894c2008-03-18 17:24:12 +0000263def _basename(path):
264 # A basename() variant which first strips the trailing slash, if present.
265 # Thus we always get the last component of the path, even for directories.
266 return os.path.basename(path.rstrip(os.path.sep))
267
268def move(src, dst):
269 """Recursively move a file or directory to another location. This is
270 similar to the Unix "mv" command.
271
272 If the destination is a directory or a symlink to a directory, the source
273 is moved inside the directory. The destination path must not already
274 exist.
275
276 If the destination already exists but is not a directory, it may be
277 overwritten depending on os.rename() semantics.
278
279 If the destination is on our current filesystem, then rename() is used.
280 Otherwise, src is copied to the destination and then removed.
Martin v. Löwise9ce0b02002-10-07 13:23:24 +0000281 A lot more could be done here... A look at a mv.c shows a lot of
282 the issues this implementation glosses over.
283
284 """
Sean Reifscheider493894c2008-03-18 17:24:12 +0000285 real_dst = dst
286 if os.path.isdir(dst):
287 real_dst = os.path.join(dst, _basename(src))
288 if os.path.exists(real_dst):
289 raise Error, "Destination path '%s' already exists" % real_dst
Martin v. Löwise9ce0b02002-10-07 13:23:24 +0000290 try:
Sean Reifscheider493894c2008-03-18 17:24:12 +0000291 os.rename(src, real_dst)
Martin v. Löwise9ce0b02002-10-07 13:23:24 +0000292 except OSError:
293 if os.path.isdir(src):
Benjamin Peterson096c3ad2009-02-07 19:08:22 +0000294 if _destinsrc(src, dst):
Brett Cannon1c3fa182004-06-19 21:11:35 +0000295 raise Error, "Cannot move a directory '%s' into itself '%s'." % (src, dst)
Sean Reifscheider493894c2008-03-18 17:24:12 +0000296 copytree(src, real_dst, symlinks=True)
Martin v. Löwise9ce0b02002-10-07 13:23:24 +0000297 rmtree(src)
298 else:
Sean Reifscheider493894c2008-03-18 17:24:12 +0000299 copy2(src, real_dst)
Martin v. Löwise9ce0b02002-10-07 13:23:24 +0000300 os.unlink(src)
Brett Cannon1c3fa182004-06-19 21:11:35 +0000301
Benjamin Peterson096c3ad2009-02-07 19:08:22 +0000302def _destinsrc(src, dst):
Antoine Pitrou707c5932009-01-29 20:19:34 +0000303 src = abspath(src)
304 dst = abspath(dst)
305 if not src.endswith(os.path.sep):
306 src += os.path.sep
307 if not dst.endswith(os.path.sep):
308 dst += os.path.sep
309 return dst.startswith(src)
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000310
311def _get_gid(name):
312 """Returns a gid, given a group name."""
313 if getgrnam is None or name is None:
314 return None
315 try:
316 result = getgrnam(name)
317 except KeyError:
318 result = None
319 if result is not None:
320 return result[2]
321 return None
322
323def _get_uid(name):
324 """Returns an uid, given a user name."""
325 if getpwnam is None or name is None:
326 return None
327 try:
328 result = getpwnam(name)
329 except KeyError:
330 result = None
331 if result is not None:
332 return result[2]
333 return None
334
335def _make_tarball(base_name, base_dir, compress="gzip", verbose=0, dry_run=0,
336 owner=None, group=None, logger=None):
337 """Create a (possibly compressed) tar file from all the files under
338 'base_dir'.
339
Tarek Ziadée593fad2010-04-20 21:09:06 +0000340 'compress' must be "gzip" (the default), "bzip2", or None.
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000341
342 'owner' and 'group' can be used to define an owner and a group for the
343 archive that is being built. If not provided, the current owner and group
344 will be used.
345
346 The output tar file will be named 'base_dir' + ".tar", possibly plus
Tarek Ziadée593fad2010-04-20 21:09:06 +0000347 the appropriate compression extension (".gz", or ".bz2").
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000348
349 Returns the output filename.
350 """
Tarek Ziadée593fad2010-04-20 21:09:06 +0000351 tar_compression = {'gzip': 'gz', 'bzip2': 'bz2', None: ''}
352 compress_ext = {'gzip': '.gz', 'bzip2': '.bz2'}
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000353
354 # flags for compression program, each element of list will be an argument
355 if compress is not None and compress not in compress_ext.keys():
356 raise ValueError, \
Tarek Ziadée593fad2010-04-20 21:09:06 +0000357 ("bad value for 'compress': must be None, 'gzip' or 'bzip2'")
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000358
Tarek Ziadée593fad2010-04-20 21:09:06 +0000359 archive_name = base_name + '.tar' + compress_ext.get(compress, '')
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000360 archive_dir = os.path.dirname(archive_name)
Tarek Ziadée593fad2010-04-20 21:09:06 +0000361
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000362 if not os.path.exists(archive_dir):
363 logger.info("creating %s" % archive_dir)
364 if not dry_run:
365 os.makedirs(archive_dir)
366
367
368 # creating the tarball
369 import tarfile # late import so Python build itself doesn't break
370
371 if logger is not None:
372 logger.info('Creating tar archive')
373
374 uid = _get_uid(owner)
375 gid = _get_gid(group)
376
377 def _set_uid_gid(tarinfo):
378 if gid is not None:
379 tarinfo.gid = gid
380 tarinfo.gname = group
381 if uid is not None:
382 tarinfo.uid = uid
383 tarinfo.uname = owner
384 return tarinfo
385
386 if not dry_run:
387 tar = tarfile.open(archive_name, 'w|%s' % tar_compression[compress])
388 try:
389 tar.add(base_dir, filter=_set_uid_gid)
390 finally:
391 tar.close()
392
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000393 return archive_name
394
Tarek Ziadé62e17ad2010-04-21 13:32:26 +0000395def _call_external_zip(base_dir, zip_filename, verbose=False, dry_run=False):
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000396 # XXX see if we want to keep an external call here
397 if verbose:
398 zipoptions = "-r"
399 else:
400 zipoptions = "-rq"
401 from distutils.errors import DistutilsExecError
402 from distutils.spawn import spawn
403 try:
404 spawn(["zip", zipoptions, zip_filename, base_dir], dry_run=dry_run)
405 except DistutilsExecError:
406 # XXX really should distinguish between "couldn't find
407 # external 'zip' command" and "zip failed".
408 raise ExecError, \
409 ("unable to create zip file '%s': "
410 "could neither import the 'zipfile' module nor "
411 "find a standalone zip utility") % zip_filename
412
413def _make_zipfile(base_name, base_dir, verbose=0, dry_run=0, logger=None):
414 """Create a zip file from all the files under 'base_dir'.
415
416 The output zip file will be named 'base_dir' + ".zip". Uses either the
417 "zipfile" Python module (if available) or the InfoZIP "zip" utility
418 (if installed and found on the default search path). If neither tool is
419 available, raises ExecError. Returns the name of the output zip
420 file.
421 """
422 zip_filename = base_name + ".zip"
423 archive_dir = os.path.dirname(base_name)
424
425 if not os.path.exists(archive_dir):
426 if logger is not None:
427 logger.info("creating %s", archive_dir)
428 if not dry_run:
429 os.makedirs(archive_dir)
430
431 # If zipfile module is not available, try spawning an external 'zip'
432 # command.
433 try:
434 import zipfile
435 except ImportError:
436 zipfile = None
437
438 if zipfile is None:
Tarek Ziadé62e17ad2010-04-21 13:32:26 +0000439 _call_external_zip(base_dir, zip_filename, verbose, dry_run)
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000440 else:
441 if logger is not None:
442 logger.info("creating '%s' and adding '%s' to it",
443 zip_filename, base_dir)
444
445 if not dry_run:
446 zip = zipfile.ZipFile(zip_filename, "w",
447 compression=zipfile.ZIP_DEFLATED)
448
449 for dirpath, dirnames, filenames in os.walk(base_dir):
450 for name in filenames:
451 path = os.path.normpath(os.path.join(dirpath, name))
452 if os.path.isfile(path):
453 zip.write(path, path)
454 if logger is not None:
455 logger.info("adding '%s'", path)
456 zip.close()
457
458 return zip_filename
459
460_ARCHIVE_FORMATS = {
461 'gztar': (_make_tarball, [('compress', 'gzip')], "gzip'ed tar-file"),
462 'bztar': (_make_tarball, [('compress', 'bzip2')], "bzip2'ed tar-file"),
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000463 'tar': (_make_tarball, [('compress', None)], "uncompressed tar file"),
464 'zip': (_make_zipfile, [],"ZIP file")
465 }
466
467def get_archive_formats():
468 """Returns a list of supported formats for archiving and unarchiving.
469
470 Each element of the returned sequence is a tuple (name, description)
471 """
472 formats = [(name, registry[2]) for name, registry in
473 _ARCHIVE_FORMATS.items()]
474 formats.sort()
475 return formats
476
477def register_archive_format(name, function, extra_args=None, description=''):
478 """Registers an archive format.
479
480 name is the name of the format. function is the callable that will be
481 used to create archives. If provided, extra_args is a sequence of
482 (name, value) tuples that will be passed as arguments to the callable.
483 description can be provided to describe the format, and will be returned
484 by the get_archive_formats() function.
485 """
486 if extra_args is None:
487 extra_args = []
Florent Xicluna1f3b4e12010-03-07 12:14:25 +0000488 if not isinstance(function, collections.Callable):
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000489 raise TypeError('The %s object is not callable' % function)
490 if not isinstance(extra_args, (tuple, list)):
491 raise TypeError('extra_args needs to be a sequence')
492 for element in extra_args:
493 if not isinstance(element, (tuple, list)) or len(element) !=2 :
494 raise TypeError('extra_args elements are : (arg_name, value)')
495
496 _ARCHIVE_FORMATS[name] = (function, extra_args, description)
497
498def unregister_archive_format(name):
499 del _ARCHIVE_FORMATS[name]
500
501def make_archive(base_name, format, root_dir=None, base_dir=None, verbose=0,
502 dry_run=0, owner=None, group=None, logger=None):
503 """Create an archive file (eg. zip or tar).
504
505 'base_name' is the name of the file to create, minus any format-specific
Tarek Ziadée593fad2010-04-20 21:09:06 +0000506 extension; 'format' is the archive format: one of "zip", "tar", "bztar"
507 or "gztar".
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000508
509 'root_dir' is a directory that will be the root directory of the
510 archive; ie. we typically chdir into 'root_dir' before creating the
511 archive. 'base_dir' is the directory where we start archiving from;
512 ie. 'base_dir' will be the common prefix of all files and
513 directories in the archive. 'root_dir' and 'base_dir' both default
514 to the current directory. Returns the name of the archive file.
515
516 'owner' and 'group' are used when creating a tar archive. By default,
517 uses the current owner and group.
518 """
519 save_cwd = os.getcwd()
520 if root_dir is not None:
521 if logger is not None:
522 logger.debug("changing into '%s'", root_dir)
523 base_name = os.path.abspath(base_name)
524 if not dry_run:
525 os.chdir(root_dir)
526
527 if base_dir is None:
528 base_dir = os.curdir
529
530 kwargs = {'dry_run': dry_run, 'logger': logger}
531
532 try:
533 format_info = _ARCHIVE_FORMATS[format]
534 except KeyError:
535 raise ValueError, "unknown archive format '%s'" % format
536
537 func = format_info[0]
538 for arg, val in format_info[1]:
539 kwargs[arg] = val
540
541 if format != 'zip':
542 kwargs['owner'] = owner
543 kwargs['group'] = group
544
545 try:
546 filename = func(base_name, base_dir, **kwargs)
547 finally:
548 if root_dir is not None:
549 if logger is not None:
550 logger.debug("changing back to '%s'", save_cwd)
551 os.chdir(save_cwd)
552
553 return filename