/kbe/res/scripts/common/Lib/distutils/archive_util.py

https://bitbucket.org/kbengine/kbengine · Python · 184 lines · 161 code · 15 blank · 8 comment · 15 complexity · daa541e79abab6cbd6c5874cee4662f6 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. def make_tarball(base_name, base_dir, compress="gzip", verbose=0, dry_run=0):
  16. """Create a (possibly compressed) tar file from all the files under
  17. 'base_dir'.
  18. 'compress' must be "gzip" (the default), "compress", "bzip2", or None.
  19. Both "tar" and the compression utility named by 'compress' must be on
  20. the default program search path, so this is probably Unix-specific.
  21. The output tar file will be named 'base_dir' + ".tar", possibly plus
  22. the appropriate compression extension (".gz", ".bz2" or ".Z").
  23. Returns the output filename.
  24. """
  25. tar_compression = {'gzip': 'gz', 'bzip2': 'bz2', None: '', 'compress': ''}
  26. compress_ext = {'gzip': '.gz', 'bzip2': '.bz2', 'compress': '.Z'}
  27. # flags for compression program, each element of list will be an argument
  28. if compress is not None and compress not in compress_ext.keys():
  29. raise ValueError(
  30. "bad value for 'compress': must be None, 'gzip', 'bzip2' "
  31. "or 'compress'")
  32. archive_name = base_name + '.tar'
  33. if compress != 'compress':
  34. archive_name += compress_ext.get(compress, '')
  35. mkpath(os.path.dirname(archive_name), dry_run=dry_run)
  36. # creating the tarball
  37. import tarfile # late import so Python build itself doesn't break
  38. log.info('Creating tar archive')
  39. if not dry_run:
  40. tar = tarfile.open(archive_name, 'w|%s' % tar_compression[compress])
  41. try:
  42. tar.add(base_dir)
  43. finally:
  44. tar.close()
  45. # compression using `compress`
  46. if compress == 'compress':
  47. warn("'compress' will be deprecated.", PendingDeprecationWarning)
  48. # the option varies depending on the platform
  49. compressed_name = archive_name + compress_ext[compress]
  50. if sys.platform == 'win32':
  51. cmd = [compress, archive_name, compressed_name]
  52. else:
  53. cmd = [compress, '-f', archive_name]
  54. spawn(cmd, dry_run=dry_run)
  55. return compressed_name
  56. return archive_name
  57. def make_zipfile(base_name, base_dir, verbose=0, dry_run=0):
  58. """Create a zip file from all the files under 'base_dir'.
  59. The output zip file will be named 'base_name' + ".zip". Uses either the
  60. "zipfile" Python module (if available) or the InfoZIP "zip" utility
  61. (if installed and found on the default search path). If neither tool is
  62. available, raises DistutilsExecError. Returns the name of the output zip
  63. file.
  64. """
  65. zip_filename = base_name + ".zip"
  66. mkpath(os.path.dirname(zip_filename), dry_run=dry_run)
  67. # If zipfile module is not available, try spawning an external
  68. # 'zip' command.
  69. if zipfile is None:
  70. if verbose:
  71. zipoptions = "-r"
  72. else:
  73. zipoptions = "-rq"
  74. try:
  75. spawn(["zip", zipoptions, zip_filename, base_dir],
  76. dry_run=dry_run)
  77. except DistutilsExecError:
  78. # XXX really should distinguish between "couldn't find
  79. # external 'zip' command" and "zip failed".
  80. raise DistutilsExecError(("unable to create zip file '%s': "
  81. "could neither import the 'zipfile' module nor "
  82. "find a standalone zip utility") % zip_filename)
  83. else:
  84. log.info("creating '%s' and adding '%s' to it",
  85. zip_filename, base_dir)
  86. if not dry_run:
  87. try:
  88. zip = zipfile.ZipFile(zip_filename, "w",
  89. compression=zipfile.ZIP_DEFLATED)
  90. except RuntimeError:
  91. zip = zipfile.ZipFile(zip_filename, "w",
  92. compression=zipfile.ZIP_STORED)
  93. for dirpath, dirnames, filenames in os.walk(base_dir):
  94. for name in filenames:
  95. path = os.path.normpath(os.path.join(dirpath, name))
  96. if os.path.isfile(path):
  97. zip.write(path, path)
  98. log.info("adding '%s'" % path)
  99. zip.close()
  100. return zip_filename
  101. ARCHIVE_FORMATS = {
  102. 'gztar': (make_tarball, [('compress', 'gzip')], "gzip'ed tar-file"),
  103. 'bztar': (make_tarball, [('compress', 'bzip2')], "bzip2'ed tar-file"),
  104. 'ztar': (make_tarball, [('compress', 'compress')], "compressed tar file"),
  105. 'tar': (make_tarball, [('compress', None)], "uncompressed tar file"),
  106. 'zip': (make_zipfile, [],"ZIP file")
  107. }
  108. def check_archive_formats(formats):
  109. """Returns the first format from the 'format' list that is unknown.
  110. If all formats are known, returns None
  111. """
  112. for format in formats:
  113. if format not in ARCHIVE_FORMATS:
  114. return format
  115. return None
  116. def make_archive(base_name, format, root_dir=None, base_dir=None, verbose=0,
  117. dry_run=0):
  118. """Create an archive file (eg. zip or tar).
  119. 'base_name' is the name of the file to create, minus any format-specific
  120. extension; 'format' is the archive format: one of "zip", "tar", "ztar",
  121. or "gztar".
  122. 'root_dir' is a directory that will be the root directory of the
  123. archive; ie. we typically chdir into 'root_dir' before creating the
  124. archive. 'base_dir' is the directory where we start archiving from;
  125. ie. 'base_dir' will be the common prefix of all files and
  126. directories in the archive. 'root_dir' and 'base_dir' both default
  127. to the current directory. Returns the name of the archive file.
  128. """
  129. save_cwd = os.getcwd()
  130. if root_dir is not None:
  131. log.debug("changing into '%s'", root_dir)
  132. base_name = os.path.abspath(base_name)
  133. if not dry_run:
  134. os.chdir(root_dir)
  135. if base_dir is None:
  136. base_dir = os.curdir
  137. kwargs = {'dry_run': dry_run}
  138. try:
  139. format_info = ARCHIVE_FORMATS[format]
  140. except KeyError:
  141. raise ValueError("unknown archive format '%s'" % format)
  142. func = format_info[0]
  143. for arg, val in format_info[1]:
  144. kwargs[arg] = val
  145. try:
  146. filename = func(base_name, base_dir, **kwargs)
  147. finally:
  148. if root_dir is not None:
  149. log.debug("changing back to '%s'", save_cwd)
  150. os.chdir(save_cwd)
  151. return filename