|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import logging |
| 4 | +import argparse |
| 5 | +import dryable |
| 6 | +from qcrepocleaner.Ccdb import Ccdb, ObjectVersion |
| 7 | +from qcrepocleaner.binUtils import prepare_main_logger |
| 8 | +import csv |
| 9 | + |
| 10 | + |
| 11 | +def parseArgs(): |
| 12 | + """Parse the arguments passed to the script.""" |
| 13 | + logging.info("Parsing arguments") |
| 14 | + parser = argparse.ArgumentParser(description='Remove all objects in a given path, if they dont belong to any of ' |
| 15 | + 'the runs. The runs are provided in a csv file whose format is the ' |
| 16 | + 'one from the bookkeeping export.') |
| 17 | + parser.add_argument('--url', dest='url', action='store', help='URL of the CCDB, with http[s]://', required=True) |
| 18 | + parser.add_argument('--log-level', dest='log_level', action='store', default="20", |
| 19 | + help='Log level (CRITICAL->50, ERROR->40, WARNING->30, INFO->20,DEBUG->10)') |
| 20 | + parser.add_argument('--dry-run', action='store_true', |
| 21 | + help='Dry run, no actual deletion nor modification to the CCDB.') |
| 22 | + parser.add_argument('--path', dest='path', action='store', default="", |
| 23 | + help='Clean this path (without initial slash and without .* at the end, e.g. qc/TST/MO/Bob)', |
| 24 | + required=True) |
| 25 | + parser.add_argument('--runs-csv-file', dest='runs_file', action='store', help='A csv file with a header and the ' |
| 26 | + 'column `runNumber` contains the run', |
| 27 | + required=True) |
| 28 | + parser.add_argument('--one-by-one', action='store_true', help='Ask confirmation for each deletion') |
| 29 | + parser.add_argument('--print-list', action='store_true', help='Only print the list of objects that would be deleted') |
| 30 | + args = parser.parse_args() |
| 31 | + dryable.set(args.dry_run) |
| 32 | + logging.info(args) |
| 33 | + return args |
| 34 | + |
| 35 | + |
| 36 | +def run(args): |
| 37 | + ccdb = Ccdb(args.url) |
| 38 | + |
| 39 | + file = open(args.runs_file) |
| 40 | + csvreader = csv.DictReader(file) |
| 41 | + list_runs = [] |
| 42 | + for row in csvreader: |
| 43 | + list_runs.append(row["runNumber"]) |
| 44 | + logging.debug(f"List of runs in CSV: {list_runs}") |
| 45 | + |
| 46 | + versions = ccdb.getVersionsList(args.path + "/.*") |
| 47 | + nb_deleted = 0 |
| 48 | + for v in versions: |
| 49 | + logging.debug(f"Processing {v}") |
| 50 | + run_number = v.metadata["RunNumber"] |
| 51 | + if run_number is not None and list_runs.count(run_number) == 0: |
| 52 | + logging.info(f"Ready to delete {v}") |
| 53 | + if args.one_by_one: |
| 54 | + answer = input(" Continue? y/n\n ") |
| 55 | + if answer.lower() in ["y", "yes"]: |
| 56 | + ccdb.deleteVersion(v) |
| 57 | + nb_deleted += 1 |
| 58 | + elif answer.lower() in ["n", "no"]: |
| 59 | + logging.info(" skipping") |
| 60 | + else: |
| 61 | + logging.error(" wrong input, skipping") |
| 62 | + else: |
| 63 | + if not args.print_list: |
| 64 | + ccdb.deleteVersion(v) |
| 65 | + nb_deleted += 1 |
| 66 | + |
| 67 | + logging.info(f"Deleted items: {nb_deleted}") |
| 68 | + |
| 69 | + |
| 70 | +# **************** |
| 71 | +# We start here ! |
| 72 | +# **************** |
| 73 | + |
| 74 | +def main(): |
| 75 | + |
| 76 | + prepare_main_logger() |
| 77 | + |
| 78 | + # Parse arguments |
| 79 | + args = parseArgs() |
| 80 | + logging.getLogger().setLevel(int(args.log_level)) |
| 81 | + |
| 82 | + run(args) |
| 83 | + |
| 84 | + |
| 85 | +if __name__ == "__main__": |
| 86 | + main() |
0 commit comments