You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

62 lines
2.0 KiB

  1. from distutils.util import convert_path
  2. from distutils import log
  3. from distutils.errors import DistutilsOptionError
  4. import os
  5. from setuptools import Command
  6. from setuptools.compat import basestring
  7. class rotate(Command):
  8. """Delete older distributions"""
  9. description = "delete older distributions, keeping N newest files"
  10. user_options = [
  11. ('match=', 'm', "patterns to match (required)"),
  12. ('dist-dir=', 'd', "directory where the distributions are"),
  13. ('keep=', 'k', "number of matching distributions to keep"),
  14. ]
  15. boolean_options = []
  16. def initialize_options(self):
  17. self.match = None
  18. self.dist_dir = None
  19. self.keep = None
  20. def finalize_options(self):
  21. if self.match is None:
  22. raise DistutilsOptionError(
  23. "Must specify one or more (comma-separated) match patterns "
  24. "(e.g. '.zip' or '.egg')"
  25. )
  26. if self.keep is None:
  27. raise DistutilsOptionError("Must specify number of files to keep")
  28. try:
  29. self.keep = int(self.keep)
  30. except ValueError:
  31. raise DistutilsOptionError("--keep must be an integer")
  32. if isinstance(self.match, basestring):
  33. self.match = [
  34. convert_path(p.strip()) for p in self.match.split(',')
  35. ]
  36. self.set_undefined_options('bdist', ('dist_dir', 'dist_dir'))
  37. def run(self):
  38. self.run_command("egg_info")
  39. from glob import glob
  40. for pattern in self.match:
  41. pattern = self.distribution.get_name() + '*' + pattern
  42. files = glob(os.path.join(self.dist_dir, pattern))
  43. files = [(os.path.getmtime(f), f) for f in files]
  44. files.sort()
  45. files.reverse()
  46. log.info("%d file(s) matching %s", len(files), pattern)
  47. files = files[self.keep:]
  48. for (t, f) in files:
  49. log.info("Deleting %s", f)
  50. if not self.dry_run:
  51. os.unlink(f)