Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 

66 rindas
2.1 KiB

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