archive_util.py 8.4 KB

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