PageRenderTime 26ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/Lib/distutils/archive_util.py

https://gitlab.com/unofficial-mirrors/cpython
Python | 248 lines | 216 code | 22 blank | 10 comment | 31 complexity | 8788968389dfb2c821d2b7a826cda3d3 MD5 | raw file
  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. for dirpath, dirnames, filenames in os.walk(base_dir):
  139. for name in filenames:
  140. path = os.path.normpath(os.path.join(dirpath, name))
  141. if os.path.isfile(path):
  142. zip.write(path, path)
  143. log.info("adding '%s'", path)
  144. zip.close()
  145. return zip_filename
  146. ARCHIVE_FORMATS = {
  147. 'gztar': (make_tarball, [('compress', 'gzip')], "gzip'ed tar-file"),
  148. 'bztar': (make_tarball, [('compress', 'bzip2')], "bzip2'ed tar-file"),
  149. 'xztar': (make_tarball, [('compress', 'xz')], "xz'ed tar-file"),
  150. 'ztar': (make_tarball, [('compress', 'compress')], "compressed tar file"),
  151. 'tar': (make_tarball, [('compress', None)], "uncompressed tar file"),
  152. 'zip': (make_zipfile, [],"ZIP file")
  153. }
  154. def check_archive_formats(formats):
  155. """Returns the first format from the 'format' list that is unknown.
  156. If all formats are known, returns None
  157. """
  158. for format in formats:
  159. if format not in ARCHIVE_FORMATS:
  160. return format
  161. return None
  162. def make_archive(base_name, format, root_dir=None, base_dir=None, verbose=0,
  163. dry_run=0, owner=None, group=None):
  164. """Create an archive file (eg. zip or tar).
  165. 'base_name' is the name of the file to create, minus any format-specific
  166. extension; 'format' is the archive format: one of "zip", "tar", "gztar",
  167. "bztar", "xztar", or "ztar".
  168. 'root_dir' is a directory that will be the root directory of the
  169. archive; ie. we typically chdir into 'root_dir' before creating the
  170. archive. 'base_dir' is the directory where we start archiving from;
  171. ie. 'base_dir' will be the common prefix of all files and
  172. directories in the archive. 'root_dir' and 'base_dir' both default
  173. to the current directory. Returns the name of the archive file.
  174. 'owner' and 'group' are used when creating a tar archive. By default,
  175. uses the current owner and group.
  176. """
  177. save_cwd = os.getcwd()
  178. if root_dir is not None:
  179. log.debug("changing into '%s'", root_dir)
  180. base_name = os.path.abspath(base_name)
  181. if not dry_run:
  182. os.chdir(root_dir)
  183. if base_dir is None:
  184. base_dir = os.curdir
  185. kwargs = {'dry_run': dry_run}
  186. try:
  187. format_info = ARCHIVE_FORMATS[format]
  188. except KeyError:
  189. raise ValueError("unknown archive format '%s'" % format)
  190. func = format_info[0]
  191. for arg, val in format_info[1]:
  192. kwargs[arg] = val
  193. if format != 'zip':
  194. kwargs['owner'] = owner
  195. kwargs['group'] = group
  196. try:
  197. filename = func(base_name, base_dir, **kwargs)
  198. finally:
  199. if root_dir is not None:
  200. log.debug("changing back to '%s'", save_cwd)
  201. os.chdir(save_cwd)
  202. return filename