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