Belle II Software development
validationpath.py
1#!/usr/bin/env python3
2
3
10
11import os
12import hashlib
13import json
14
15folder_name_results = "results"
16folder_name_html_static = "html_static"
17folder_name_html = "html"
18folder_name_html_plots = "plots"
19
20# the plots subfolder in the html folder
21folder_name_plots = "plots"
22folder_name_general = "__general__"
23file_name_results_json = "revision.json"
24file_name_comparison_json = "comparison.json"
25file_name_runtimes_dat = "runtimes.dat"
26
27
28def get_basepath():
29 return {
30 "local": os.environ.get("BELLE2_LOCAL_DIR", None),
31 "central": os.environ.get("BELLE2_RELEASE_DIR", None),
32 }
33
34
36 """ Because of issues of too long filenames if the number of revision
37 is growing, the simple recipe of creating folder names by concatenating
38 the names of the revisions does not work.
39 Therefore, we use a hashing algorithm. At the same time we want to be
40 able to look up the folder content easily, so we create a rainbow table,
41 that is a simple text file that contains hash <> revisions.
42 """
43
44 def update_from_json(self, path: str) -> None:
45 """ Read a json file which was produced by the ``to_json`` method and
46 update this dictionary. If the path does not exist, do nothing. """
47 if os.path.exists(path):
48 with open(path) as infile:
49 self.update(json.load(infile))
50
51 def to_json(self, path: str) -> None:
52 """ Write out this dictionary as a json file. """
53 os.makedirs(os.path.dirname(path), exist_ok=True)
54 with open(path, "w") as outfile:
55 json.dump(self, outfile, indent=4, sort_keys=True)
56
57 def update_to_json(self, path: str) -> None:
58 """ Read json file (if exists) for anything the dictionary
59 doesn't contain yet and write everything back. """
60 self.update_from_json(path)
61 self.to_json(path)
62
63
64# Note that it is enough to have one object, even in the (unexpected) case that
65# there is more than one output_base_dir (at the cost of having some unneeded
66# entries there, perhaps)
67RAINBOW_TABLE = TagFolderRainbowTable()
68
69
70def get_results_runtime_file(output_base_dir):
71 """!
72 Return the absolute path to the runtimes.dat file
73 As there is only the runtimes file of the last iteration stored, this is
74 located in the topmost work folder
75 """
76 return os.path.join(output_base_dir, file_name_runtimes_dat)
77
78
79def get_results_folder(output_base_dir):
80 """!
81 Return the absolute path to the results folder
82 """
83 return os.path.join(output_base_dir, folder_name_results)
84
85
86def get_html_folder(output_base_dir):
87 """!
88 Return the absolute path to the results folder
89 """
90 return os.path.join(output_base_dir, folder_name_html)
91
92
93def get_html_plots_folder(output_base_dir):
94 """!
95 Return the absolute path to generated plots in the html folder
96 """
97 return os.path.join(get_html_folder(output_base_dir), folder_name_plots)
98
99
100def get_html_plots_tag_comparison_folder(output_base_dir, tags):
101 """!
102 Return the absolute path to the results folder
103 """
104 string = ",".join(sorted(tags))
105 tag_folder = hashlib.sha1(string.encode("utf8")).hexdigest()[:10]
106 if tag_folder not in RAINBOW_TABLE:
107 RAINBOW_TABLE[tag_folder] = sorted(tags)
108 RAINBOW_TABLE.update_to_json(
109 os.path.join(get_html_plots_folder(output_base_dir), "rainbow.json")
110 )
111 return os.path.join(get_html_plots_folder(output_base_dir), tag_folder)
112
113
114def get_html_plots_tag_comparison_json(output_base_dir, tags):
115 """!
116 Return the absolute path json file with the comparison file
117 """
118 return os.path.join(
119 get_html_plots_tag_comparison_folder(output_base_dir, tags),
120 file_name_comparison_json,
121 )
122
123
124def get_results_tag_folder(output_base_dir, tag):
125 """!
126 Return the absolute path to the results folder for one specific tag
127 """
128 return os.path.join(get_results_folder(output_base_dir), tag)
129
130
131def get_results_tag_general_folder(output_base_dir, tag):
132 """!
133 Return the absolute path to the results folder for one specific
134 tag. In this general folder, the common log files will be placed
135 """
136 return os.path.join(
137 get_results_tag_folder(output_base_dir, tag), folder_name_general
138 )
139
140
141def get_results_tag_revision_file(output_base_dir, tag):
142 """!
143 Return the absolute path to the revision.json file for one tag folder
144 """
145 return os.path.join(
146 get_results_tag_folder(output_base_dir, tag), file_name_results_json
147 )
148
149
150def get_results_tag_package_folder(output_base_dir, tag, package):
151 """!
152 Returns the absolute path for a tag and package. This folder will contain
153 the ROOT files plots which will be displayed on the validation website
154 """
155 return os.path.join(get_results_tag_folder(output_base_dir, tag), package)
None update_from_json(self, str path)