Path: blob/main/test/lib/python3.9/site-packages/setuptools/command/rotate.py
4799 views
from distutils.util import convert_path1from distutils import log2from distutils.errors import DistutilsOptionError3import os4import shutil56from setuptools import Command789class rotate(Command):10"""Delete older distributions"""1112description = "delete older distributions, keeping N newest files"13user_options = [14('match=', 'm', "patterns to match (required)"),15('dist-dir=', 'd', "directory where the distributions are"),16('keep=', 'k', "number of matching distributions to keep"),17]1819boolean_options = []2021def initialize_options(self):22self.match = None23self.dist_dir = None24self.keep = None2526def finalize_options(self):27if self.match is None:28raise DistutilsOptionError(29"Must specify one or more (comma-separated) match patterns "30"(e.g. '.zip' or '.egg')"31)32if self.keep is None:33raise DistutilsOptionError("Must specify number of files to keep")34try:35self.keep = int(self.keep)36except ValueError as e:37raise DistutilsOptionError("--keep must be an integer") from e38if isinstance(self.match, str):39self.match = [40convert_path(p.strip()) for p in self.match.split(',')41]42self.set_undefined_options('bdist', ('dist_dir', 'dist_dir'))4344def run(self):45self.run_command("egg_info")46from glob import glob4748for pattern in self.match:49pattern = self.distribution.get_name() + '*' + pattern50files = glob(os.path.join(self.dist_dir, pattern))51files = [(os.path.getmtime(f), f) for f in files]52files.sort()53files.reverse()5455log.info("%d file(s) matching %s", len(files), pattern)56files = files[self.keep:]57for (t, f) in files:58log.info("Deleting %s", f)59if not self.dry_run:60if os.path.isdir(f):61shutil.rmtree(f)62else:63os.unlink(f)646566