10 from collections
import defaultdict
11 from datetime
import datetime
13 from argparse
import ArgumentParser
14 import logging; log = logging.getLogger(
'dq_defect_list_iovs')
16 from DQDefects
import DefectsDB
17 from DQDefects.db
import DEFAULT_CONNECTION_STRING
19 from DQUtils
import IOVSet, process_iovs, define_iov_type
20 from DQUtils.sugar
import RunLumi, RANGEIOV_VAL
21 from DQUtils.periods
import fetch_project_period_runs
30 Try to unpickle project dict from file first,
31 unless file doesn't exist or is older than 5 days
33 filename =
'project_period_runs.pickle'
34 def get_file_age(name):
35 delta = datetime.fromtimestamp(os.path.getmtime(name)) - datetime.now()
37 if os.path.exists(filename)
and get_file_age(filename) < 5:
38 print(
'using existing pickled project dict:', filename)
39 with open(filename)
as f:
40 project_dict = pickle.load(f)
43 print(
'storing project:period:runs dictionary to', filename)
44 with open(filename,
'w')
as f:
46 pickle.dump(project_dict, f)
51 parser = ArgumentParser(description=
'List defect IOVs')
52 add_arg = parser.add_argument
55 add_arg(
'-p',
'--project', default=
'data15_13TeV',
56 help=
'Data project (default: data15_13TeV)')
57 add_arg(
'-P',
'--period', default=
None, nargs=
'*', help=
'Data period(s)')
59 add_arg(
'-r',
'--run', default=
None, type=int, help=
'Run number to process')
60 add_arg(
'-R',
'--range', help=
'Inclusive run range: e.g. 150000-151000')
63 add_arg(
'-d',
'--defects', default=
None, nargs=
'*',
64 help=
'Defects to process. Use * for wildcard (default: None)')
67 add_arg(
'-c',
'--connection-string', default=DEFAULT_CONNECTION_STRING,
68 help=
'Database connection to use (default: %s)' % DEFAULT_CONNECTION_STRING)
71 add_arg(
'-t',
'--tag', default=
'HEAD',
72 help=
'Tag to use (default: HEAD)')
75 args = parser.parse_args()
78 since, until =
None,
None
83 since, until = (args.run, 0), (args.run+1, 0)
84 range_iovs = IOVSet.from_runs([args.run])
86 run1, run2 = map(int, args.range.split(
'-'))
87 since, until = (run1, 0), (run2, 0)
93 period_dict = project_dict[args.project]
96 if args.period
and len(args.period) > 0:
97 for period
in args.period:
98 run_set.update(period_dict[period])
101 for period, runs
in period_dict.iteritems():
103 since, until = (
min(run_set), 0), (
max(run_set)+1, 0)
104 range_iovs = IOVSet.from_runs(run_set)
107 print(
'range to process:')
108 print(
'since, until =', since, until)
111 db = DefectsDB(args.connection_string, tag=args.tag)
114 defect_iovs = db.retrieve(since, until, channels=args.defects)
115 defect_iovset_list = defect_iovs.by_channel.values()
119 result_dict = defaultdict(IOVSet)
120 for since, until, states
in process_iovs(range_iovs, *defect_iovset_list):
121 in_range, defects = states[0], states[1:]
125 result_dict[d.channel].
add(since, until, d.channel, d.comment)
128 for channel, result
in result_dict.iteritems():
129 result.solidify(DefectIOV)
130 print(
'\n' + channel +
'\n')
134 if __name__ ==
'__main__':