archive_util.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. """Utilities for extracting common archive formats"""
  2. import zipfile
  3. import tarfile
  4. import os
  5. import shutil
  6. import posixpath
  7. import contextlib
  8. from distutils.errors import DistutilsError
  9. from ._path import ensure_directory
  10. __all__ = [
  11. "unpack_archive", "unpack_zipfile", "unpack_tarfile", "default_filter",
  12. "UnrecognizedFormat", "extraction_drivers", "unpack_directory",
  13. ]
  14. class UnrecognizedFormat(DistutilsError):
  15. """Couldn't recognize the archive type"""
  16. def default_filter(src, dst):
  17. """The default progress/filter callback; returns True for all files"""
  18. return dst
  19. def unpack_archive(
  20. filename, extract_dir, progress_filter=default_filter,
  21. drivers=None):
  22. """Unpack `filename` to `extract_dir`, or raise ``UnrecognizedFormat``
  23. `progress_filter` is a function taking two arguments: a source path
  24. internal to the archive ('/'-separated), and a filesystem path where it
  25. will be extracted. The callback must return the desired extract path
  26. (which may be the same as the one passed in), or else ``None`` to skip
  27. that file or directory. The callback can thus be used to report on the
  28. progress of the extraction, as well as to filter the items extracted or
  29. alter their extraction paths.
  30. `drivers`, if supplied, must be a non-empty sequence of functions with the
  31. same signature as this function (minus the `drivers` argument), that raise
  32. ``UnrecognizedFormat`` if they do not support extracting the designated
  33. archive type. The `drivers` are tried in sequence until one is found that
  34. does not raise an error, or until all are exhausted (in which case
  35. ``UnrecognizedFormat`` is raised). If you do not supply a sequence of
  36. drivers, the module's ``extraction_drivers`` constant will be used, which
  37. means that ``unpack_zipfile`` and ``unpack_tarfile`` will be tried, in that
  38. order.
  39. """
  40. for driver in drivers or extraction_drivers:
  41. try:
  42. driver(filename, extract_dir, progress_filter)
  43. except UnrecognizedFormat:
  44. continue
  45. else:
  46. return
  47. else:
  48. raise UnrecognizedFormat(
  49. "Not a recognized archive type: %s" % filename
  50. )
  51. def unpack_directory(filename, extract_dir, progress_filter=default_filter):
  52. """"Unpack" a directory, using the same interface as for archives
  53. Raises ``UnrecognizedFormat`` if `filename` is not a directory
  54. """
  55. if not os.path.isdir(filename):
  56. raise UnrecognizedFormat("%s is not a directory" % filename)
  57. paths = {
  58. filename: ('', extract_dir),
  59. }
  60. for base, dirs, files in os.walk(filename):
  61. src, dst = paths[base]
  62. for d in dirs:
  63. paths[os.path.join(base, d)] = src + d + '/', os.path.join(dst, d)
  64. for f in files:
  65. target = os.path.join(dst, f)
  66. target = progress_filter(src + f, target)
  67. if not target:
  68. # skip non-files
  69. continue
  70. ensure_directory(target)
  71. f = os.path.join(base, f)
  72. shutil.copyfile(f, target)
  73. shutil.copystat(f, target)
  74. def unpack_zipfile(filename, extract_dir, progress_filter=default_filter):
  75. """Unpack zip `filename` to `extract_dir`
  76. Raises ``UnrecognizedFormat`` if `filename` is not a zipfile (as determined
  77. by ``zipfile.is_zipfile()``). See ``unpack_archive()`` for an explanation
  78. of the `progress_filter` argument.
  79. """
  80. if not zipfile.is_zipfile(filename):
  81. raise UnrecognizedFormat("%s is not a zip file" % (filename,))
  82. with zipfile.ZipFile(filename) as z:
  83. _unpack_zipfile_obj(z, extract_dir, progress_filter)
  84. def _unpack_zipfile_obj(zipfile_obj, extract_dir, progress_filter=default_filter):
  85. """Internal/private API used by other parts of setuptools.
  86. Similar to ``unpack_zipfile``, but receives an already opened :obj:`zipfile.ZipFile`
  87. object instead of a filename.
  88. """
  89. for info in zipfile_obj.infolist():
  90. name = info.filename
  91. # don't extract absolute paths or ones with .. in them
  92. if name.startswith('/') or '..' in name.split('/'):
  93. continue
  94. target = os.path.join(extract_dir, *name.split('/'))
  95. target = progress_filter(name, target)
  96. if not target:
  97. continue
  98. if name.endswith('/'):
  99. # directory
  100. ensure_directory(target)
  101. else:
  102. # file
  103. ensure_directory(target)
  104. data = zipfile_obj.read(info.filename)
  105. with open(target, 'wb') as f:
  106. f.write(data)
  107. unix_attributes = info.external_attr >> 16
  108. if unix_attributes:
  109. os.chmod(target, unix_attributes)
  110. def _resolve_tar_file_or_dir(tar_obj, tar_member_obj):
  111. """Resolve any links and extract link targets as normal files."""
  112. while tar_member_obj is not None and (
  113. tar_member_obj.islnk() or tar_member_obj.issym()):
  114. linkpath = tar_member_obj.linkname
  115. if tar_member_obj.issym():
  116. base = posixpath.dirname(tar_member_obj.name)
  117. linkpath = posixpath.join(base, linkpath)
  118. linkpath = posixpath.normpath(linkpath)
  119. tar_member_obj = tar_obj._getmember(linkpath)
  120. is_file_or_dir = (
  121. tar_member_obj is not None and
  122. (tar_member_obj.isfile() or tar_member_obj.isdir())
  123. )
  124. if is_file_or_dir:
  125. return tar_member_obj
  126. raise LookupError('Got unknown file type')
  127. def _iter_open_tar(tar_obj, extract_dir, progress_filter):
  128. """Emit member-destination pairs from a tar archive."""
  129. # don't do any chowning!
  130. tar_obj.chown = lambda *args: None
  131. with contextlib.closing(tar_obj):
  132. for member in tar_obj:
  133. name = member.name
  134. # don't extract absolute paths or ones with .. in them
  135. if name.startswith('/') or '..' in name.split('/'):
  136. continue
  137. prelim_dst = os.path.join(extract_dir, *name.split('/'))
  138. try:
  139. member = _resolve_tar_file_or_dir(tar_obj, member)
  140. except LookupError:
  141. continue
  142. final_dst = progress_filter(name, prelim_dst)
  143. if not final_dst:
  144. continue
  145. if final_dst.endswith(os.sep):
  146. final_dst = final_dst[:-1]
  147. yield member, final_dst
  148. def unpack_tarfile(filename, extract_dir, progress_filter=default_filter):
  149. """Unpack tar/tar.gz/tar.bz2 `filename` to `extract_dir`
  150. Raises ``UnrecognizedFormat`` if `filename` is not a tarfile (as determined
  151. by ``tarfile.open()``). See ``unpack_archive()`` for an explanation
  152. of the `progress_filter` argument.
  153. """
  154. try:
  155. tarobj = tarfile.open(filename)
  156. except tarfile.TarError as e:
  157. raise UnrecognizedFormat(
  158. "%s is not a compressed or uncompressed tar file" % (filename,)
  159. ) from e
  160. for member, final_dst in _iter_open_tar(
  161. tarobj, extract_dir, progress_filter,
  162. ):
  163. try:
  164. # XXX Ugh
  165. tarobj._extract_member(member, final_dst)
  166. except tarfile.ExtractError:
  167. # chown/chmod/mkfifo/mknode/makedev failed
  168. pass
  169. return True
  170. extraction_drivers = unpack_directory, unpack_zipfile, unpack_tarfile