Skip to content

Commit be2d08c

Browse files
authored
Script to delete objects not in a list of runs (#1680)
* script to delete objects not in a list of runs * comment * Update test_MultiplePerRun.py
1 parent db1513f commit be2d08c

7 files changed

Lines changed: 114 additions & 55 deletions

File tree

Framework/script/RepoCleaner/ReleaseNotes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
1.3
44
- Add new tool `o2-qc-repo-find-objects-not-updated` to find all the objects under a path that did not get a new
55
version in the past X days.
6+
- Add new tool to delete objects not belonging to a list of runs.
67

78
1.2
89

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env python3
2+
3+
import logging
4+
import sys
5+
6+
7+
def prepare_main_logger():
8+
logger = logging.getLogger()
9+
# Logging (split between stderr and stdout)
10+
formatter = logging.Formatter(fmt='%(asctime)s - %(levelname)s - %(message)s', datefmt='%d-%b-%y %H:%M:%S')
11+
h1 = logging.StreamHandler(sys.stdout)
12+
h1.setLevel(logging.DEBUG)
13+
h1.addFilter(lambda record: record.levelno <= logging.INFO) # filter out everything that is above INFO level
14+
h1.setFormatter(formatter)
15+
logger.addHandler(h1)
16+
h2 = logging.StreamHandler(sys.stderr)
17+
h2.setLevel(logging.WARNING) # take only warnings and error logs
18+
h2.setFormatter(formatter)
19+
logger.addHandler(h2)
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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()

Framework/script/RepoCleaner/qcrepocleaner/o2-qc-repo-delete-objects-in-runs

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import argparse
55
import dryable
66
import time
77
from qcrepocleaner.Ccdb import Ccdb, ObjectVersion
8-
import sys
8+
from qcrepocleaner.binUtils import prepare_main_logger
99
import csv
1010

1111

@@ -34,21 +34,6 @@ def parseArgs():
3434
return args
3535

3636

37-
def prepare_main_logger():
38-
logger = logging.getLogger()
39-
# Logging (split between stderr and stdout)
40-
formatter = logging.Formatter(fmt='%(asctime)s - %(levelname)s - %(message)s', datefmt='%d-%b-%y %H:%M:%S')
41-
h1 = logging.StreamHandler(sys.stdout)
42-
h1.setLevel(logging.DEBUG)
43-
h1.addFilter(lambda record: record.levelno <= logging.INFO) # filter out everything that is above INFO level
44-
h1.setFormatter(formatter)
45-
logger.addHandler(h1)
46-
h2 = logging.StreamHandler(sys.stderr)
47-
h2.setLevel(logging.WARNING) # take only warnings and error logs
48-
h2.setFormatter(formatter)
49-
logger.addHandler(h2)
50-
51-
5237
def run(args):
5338
ccdb = Ccdb(args.url)
5439

Framework/script/RepoCleaner/qcrepocleaner/o2-qc-repo-delete-time-interval

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
import logging
44
import argparse
55
import dryable
6-
import time
76
from qcrepocleaner.Ccdb import Ccdb
8-
import sys
7+
from qcrepocleaner.binUtils import prepare_main_logger
98

109
def parseArgs():
1110
"""Parse the arguments passed to the script."""
@@ -30,21 +29,6 @@ def parseArgs():
3029
return args
3130

3231

33-
def prepare_main_logger():
34-
logger = logging.getLogger()
35-
# Logging (split between stderr and stdout)
36-
formatter = logging.Formatter(fmt='%(asctime)s - %(levelname)s - %(message)s', datefmt='%d-%b-%y %H:%M:%S')
37-
h1 = logging.StreamHandler(sys.stdout)
38-
h1.setLevel(logging.DEBUG)
39-
h1.addFilter(lambda record: record.levelno <= logging.INFO) # filter out everything that is above INFO level
40-
h1.setFormatter(formatter)
41-
logger.addHandler(h1)
42-
h2 = logging.StreamHandler(sys.stderr)
43-
h2.setLevel(logging.WARNING) # take only warnings and error logs
44-
h2.setFormatter(formatter)
45-
logger.addHandler(h2)
46-
47-
4832
def run(args):
4933
ccdb = Ccdb(args.url)
5034
path = args.path+"/" if args.only_path_no_subdir else args.path + "/.*"
@@ -80,12 +64,12 @@ def run(args):
8064

8165
logging.info(f"Deleted items: {len(versions)}")
8266

67+
8368
# ****************
8469
# We start here !
8570
# ****************
8671

8772
def main():
88-
start_time = time.time()
8973
prepare_main_logger()
9074

9175
# Parse arguments

Framework/script/RepoCleaner/qcrepocleaner/o2-qc-repo-move-objects

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import logging
44
import argparse
55
import dryable
66
from qcrepocleaner.Ccdb import Ccdb
7-
import sys
7+
from qcrepocleaner.binUtils import prepare_main_logger
88
import re
99

1010

@@ -29,21 +29,6 @@ def parse_args():
2929
return args
3030

3131

32-
def prepare_main_logger():
33-
logger = logging.getLogger()
34-
# Logging (split between stderr and stdout)
35-
formatter = logging.Formatter(fmt='%(asctime)s - %(levelname)s - %(message)s', datefmt='%d-%b-%y %H:%M:%S')
36-
h1 = logging.StreamHandler(sys.stdout)
37-
h1.setLevel(logging.DEBUG)
38-
h1.addFilter(lambda record: record.levelno <= logging.INFO) # filter out everything that is above INFO level
39-
h1.setFormatter(formatter)
40-
logger.addHandler(h1)
41-
h2 = logging.StreamHandler(sys.stderr)
42-
h2.setLevel(logging.WARNING) # take only warnings and error logs
43-
h2.setFormatter(formatter)
44-
logger.addHandler(h2)
45-
46-
4732
def run(args):
4833
ccdb = Ccdb(args.url)
4934

Framework/script/RepoCleaner/tests/test_MultiplePerRun.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,19 +77,18 @@ def test_5_runs(self):
7777

7878
# Prepare data
7979
test_path = self.path + "/test_5_runs"
80-
self.prepare_data(test_path, [60, 120, 190, 240, 300], [60, 120, 190, 240, 24*60], 123)
80+
self.prepare_data(test_path, [3, 3, 3, 3, 3], [60, 120, 190, 240, 24*60], 123)
8181

8282
stats = multiple_per_run.process(self.ccdb, test_path, delay=60*24, from_timestamp=1,
8383
to_timestamp=self.in_ten_years, extra_params=self.extra)
84-
8584
self.assertEqual(stats["deleted"], 60+120+190+240+300-18)
8685
self.assertEqual(stats["preserved"], 18)
8786
self.assertEqual(stats["updated"], 0)
88-
87+
8988
# and now re-run it to make sure we preserve the state
9089
stats = multiple_per_run.process(self.ccdb, test_path, delay=60*24, from_timestamp=1,
91-
to_timestamp=self.in_ten_years, extra_params=self.extra)
92-
90+
to_timestamp=self.in_ten_years, extra_params=self.extra)
91+
9392
self.assertEqual(stats["deleted"], 0)
9493
self.assertEqual(stats["preserved"], 18)
9594
self.assertEqual(stats["updated"], 0)

0 commit comments

Comments
 (0)