# Licensed under a 3-clause BSD style license - see LICENSE.rstimportsysfromfnmatchimportfnmatchcasefromasv_runner.consoleimportget_answer_defaultfromasvimportutilfrom..consoleimportlogfrom..resultsimportiter_resultsfrom.importCommand
[docs]defsetup_arguments(cls,subparsers):parser=subparsers.add_parser("rm",help="Remove results from the database",description=""" Removes entries from the results database. """,)parser.add_argument('patterns',nargs='+',help="""Pattern(s) to match, each of the form X=Y. X may be one of "benchmark", "commit_hash", "python" or any of the machine or environment params. Y is a case-sensitive glob pattern.""",)parser.add_argument("-y",action="store_true",help="""Don't prompt for confirmation.""")parser.set_defaults(func=cls.run_from_args)returnparser
[docs]defrun(cls,conf,patterns,y=True):global_patterns={}single_benchmark=Nonefiles_to_remove=set()count=0forpatterninpatterns:parts=pattern.split('=',1)iflen(parts)!=2:raiseutil.UserError(f"Invalid pattern '{pattern}'")ifparts[0]=='benchmark':ifsingle_benchmarkisnotNone:raiseutil.UserError("'benchmark' appears more than once")single_benchmark=parts[1]else:ifparts[0]inglobal_patterns:raiseutil.UserError(f"'{parts[0]}' appears more than once")global_patterns[parts[0]]=parts[1]forresultiniter_results(conf.results_dir):found=Trueforkey,valinglobal_patterns.items():ifkey=='commit_hash':ifnotutil.hash_equal(result.commit_hash,val):found=Falsebreakelifkey=='python':ifnotfnmatchcase(result.env.python,val):found=Falsebreakelse:ifnotfnmatchcase(result.params.get(key),val):found=Falsebreakifnotfound:continueifsingle_benchmarkisnotNone:found=Falseforbenchmarkinlist(result.get_all_result_keys()):iffnmatchcase(benchmark,single_benchmark):count+=1files_to_remove.add(result)result.remove_result(benchmark)else:files_to_remove.add(result)ifsingle_benchmarkisnotNone:log.info(f"Removing {count} benchmarks in {len(files_to_remove)} files")else:log.info(f"Removing {len(files_to_remove)} files")ifnoty:do=get_answer_default("Perform operations","n")iflen(do)anddo.lower()[0]!='y':sys.exit(0)ifsingle_benchmarkisnotNone:forresultinfiles_to_remove:result.save(conf.results_dir)else:forresultinfiles_to_remove:result.rm(conf.results_dir)