fromdistutils.utilimportconvert_pathfromdistutilsimportlogfromdistutils.errorsimportDistutilsOptionErrorimportosimportshutilfromsetuptoolsimportCommandclassrotate(Command):"""Delete older distributions"""description="delete older distributions, keeping N newest files"user_options=[('match=','m',"patterns to match (required)"),('dist-dir=','d',"directory where the distributions are"),('keep=','k',"number of matching distributions to keep"),]boolean_options=[]definitialize_options(self):self.match=Noneself.dist_dir=Noneself.keep=Nonedeffinalize_options(self):ifself.matchisNone:raiseDistutilsOptionError("Must specify one or more (comma-separated) match patterns ""(e.g. '.zip' or '.egg')")ifself.keepisNone:raiseDistutilsOptionError("Must specify number of files to keep")try:self.keep=int(self.keep)exceptValueErrorase:raiseDistutilsOptionError("--keep must be an integer")fromeifisinstance(self.match,str):self.match=[convert_path(p.strip())forpinself.match.split(',')]self.set_undefined_options('bdist',('dist_dir','dist_dir'))defrun(self):self.run_command("egg_info")fromglobimportglobforpatterninself.match:pattern=self.distribution.get_name()+'*'+patternfiles=glob(os.path.join(self.dist_dir,pattern))files=[(os.path.getmtime(f),f)forfinfiles]files.sort()files.reverse()log.info("%d file(s) matching %s",len(files),pattern)files=files[self.keep:]for (t,f) infiles:log.info("Deleting %s",f)ifnotself.dry_run:ifos.path.isdir(f):shutil.rmtree(f)else:os.unlink(f)