archive_util.py 8.3 KB

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