archive_util.py 8.4 KB

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