28 from pprint
import PrettyPrinter
29 pp = PrettyPrinter(indent=2)
32 file_path_patterns = [
"/hsm/belle2/bdata/Data/Raw/e0003/r0495[5,6]*/**/*.root"]
35 def bad_run_finder(filepath):
37 Returns True if a file path contains '.bad' after the run number
39 return fnmatch.fnmatch(filepath,
"*r?????.bad/*")
44 def from_raw_data_file_paths(file_path_patterns):
45 from caf.utils
import find_absolute_file_paths, parse_raw_data_iov
47 file_paths = find_absolute_file_paths(file_path_patterns)
50 file_paths = list(itertools.filterfalse(bad_run_finder, file_paths))
54 for file_path
in file_paths:
55 file_to_iov[file_path] = parse_raw_data_iov(file_path)
61 def from_metadata_of_files(file_path_patterns):
63 from caf.utils
import make_file_to_iov_dictionary
65 def run_in_one_process():
67 Creates the file_to_iov dictionary but only one file at a time.
68 Uses bad_run_finder to filter out runs marked as bad from our glob pattern.
70 return make_file_to_iov_dictionary(file_path_patterns, filterfalse=bad_run_finder)
72 def run_with_multiprocessing(max_processes):
74 Creates the file_to_iov dictionary but using a Pool object to control the number of subprocesses.
75 Note that even though we're using a ThreadPool, we aren't bound by the GIL because we are subprocessing to
76 run b2file-metadata-show in each Thread.
78 Uses bad_run_finder to filter out runs marked as bad from our glob pattern.
80 from multiprocessing.pool
import ThreadPool
81 tp = ThreadPool(processes=max_processes)
82 mapping = make_file_to_iov_dictionary(file_path_patterns, polling_time=5, pool=tp, filterfalse=bad_run_finder)
87 return run_in_one_process()
92 function_map = {
"filepath": from_raw_data_file_paths,
93 "metadata": from_metadata_of_files}
98 parser = argparse.ArgumentParser()
99 parser.add_argument(
"method", help=
"The method by which you want to create the mapping of file paths -> IoVs.",
100 choices=function_map.keys())
106 parser = get_argparser()
107 args = parser.parse_args()
110 file_to_iov = function_map[args.method](file_path_patterns)
112 print(
"Created the file to IoV map:")
113 pp.pprint(file_to_iov)
117 filename =
"file_iov_map.pkl"
118 with open(filename,
'bw')
as iov_map_file:
119 pickle.dump(file_to_iov, iov_map_file)
120 print(
"Saved dictionary to the file '{}' for later use.".format(filename))
126 if __name__ ==
"__main__":
int main(int argc, char **argv)
Run all tests.