archive.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. """
  2. Archive tools for wheel.
  3. """
  4. import logging
  5. import os.path
  6. import zipfile
  7. log = logging.getLogger("wheel")
  8. def archive_wheelfile(base_name, base_dir):
  9. '''Archive all files under `base_dir` in a whl file and name it like
  10. `base_name`.
  11. '''
  12. olddir = os.path.abspath(os.curdir)
  13. base_name = os.path.abspath(base_name)
  14. try:
  15. os.chdir(base_dir)
  16. return make_wheelfile_inner(base_name)
  17. finally:
  18. os.chdir(olddir)
  19. def make_wheelfile_inner(base_name, base_dir='.'):
  20. """Create a whl file from all the files under 'base_dir'.
  21. Places .dist-info at the end of the archive."""
  22. zip_filename = base_name + ".whl"
  23. log.info("creating '%s' and adding '%s' to it", zip_filename, base_dir)
  24. # XXX support bz2, xz when available
  25. zip = zipfile.ZipFile(open(zip_filename, "wb+"), "w",
  26. compression=zipfile.ZIP_DEFLATED)
  27. score = {'WHEEL': 1, 'METADATA': 2, 'RECORD': 3}
  28. deferred = []
  29. def writefile(path):
  30. zip.write(path, path)
  31. log.info("adding '%s'" % path)
  32. for dirpath, dirnames, filenames in os.walk(base_dir):
  33. for name in filenames:
  34. path = os.path.normpath(os.path.join(dirpath, name))
  35. if os.path.isfile(path):
  36. if dirpath.endswith('.dist-info'):
  37. deferred.append((score.get(name, 0), path))
  38. else:
  39. writefile(path)
  40. deferred.sort()
  41. for score, path in deferred:
  42. writefile(path)
  43. zip.close()
  44. return zip_filename