Belle II Software development
clustercontrolbase.py
1#!/usr/bin/env python3
2
3
10
11# std
12import logging
13import os
14import stat
15from abc import abstractmethod
16
17# ours
18from validationscript import Script
19
20
22 """
23 Base class which provides basic functionality to wrap basf2 into a shell
24 script setting up the environment and checking for completion of script
25 """
26
27 def __init__(self):
28 """!
29 The default constructor.
30 """
31
33 self.path = os.getcwd()
34
35
39 self.logger = logging.getLogger("validate_basf2")
40
41 # We need to set up the same environment on the cluster like on the
42 # local machine. The information can be extracted from $BELLE2_TOOLS,
43 # $BELLE2_RELEASE_DIR and $BELLE2_LOCAL_DIR
44
45
46 self.tools = self.adjust_path(os.environ["BELLE2_TOOLS"])
47 belle2_release_dir = os.environ.get("BELLE2_RELEASE_DIR", None)
48 belle2_local_dir = os.environ.get("BELLE2_LOCAL_DIR", None)
49
50
51 self.b2setup = "b2setup"
52 if belle2_release_dir is not None:
53 self.b2setup += " " + belle2_release_dir.split("/")[-1]
54 if belle2_local_dir is not None:
55 self.b2setup = (
56 "MY_BELLE2_DIR="
57 + self.adjust_path(belle2_local_dir)
58 + " "
59 + self.b2setup
60 )
61 if os.environ.get("BELLE2_OPTION") != "debug":
62 self.b2setup += "; b2code-option " + os.environ.get("BELLE2_OPTION")
63
64 # Write to log which revision we are using
65 self.logger.debug(f"Setting up the following release: {self.b2setup}")
66
67 # Define the folder in which the log of the cluster messages will be
68 # stored (same folder like the log for validate_basf2.py)
69 clusterlog_dir = "./html/logs/__general__/"
70 if not os.path.exists(clusterlog_dir):
71 os.makedirs(clusterlog_dir)
72
73
74 self.clusterlog = open(clusterlog_dir + "clusterlog.log", "w+")
75
76 def createDoneFileName(self, job: Script) -> str:
77 """!
78 Generate the file name used for the done output
79 """
80 return f"{self.path}/script_{job.name}.done"
81
82 def prepareSubmission(self, job: Script, options, tag):
83 """!
84 Setup output folders and create the wrapping shell script. Will return
85 the full file name of the generated wrapper script.
86 """
87
88 # Define the folder in which the results (= the ROOT files) should be
89 # created. This is where the files containing plots will end up. By
90 # convention, data files will be stored in the parent dir.
91 # Then make sure the folder exists (create if it does not exist) and
92 # change to cwd to this folder.
93 output_dir = os.path.abspath(f"./results/{tag}/{job.package}")
94 if not os.path.exists(output_dir):
95 os.makedirs(output_dir)
96
97 # Path where log file is supposed to be created
98 # log_file = output_dir + '/' + os.path.basename(job.path) + '.log'
99
100 # Remove any left over done files
101 donefile_path = self.createDoneFileName(job)
102 if os.path.isfile(donefile_path):
103 os.remove(donefile_path)
104
105 # Now we need to distinguish between .py and .C files:
106 extension = os.path.splitext(job.path)[1]
107 if extension == ".C":
108 # .c files are executed with root
109 command = "root -b -q " + job.path
110 else:
111 # .py files are executed with basf2
112 # 'options' contains an option-string for basf2, e.g. '-n 100'
113 command = f"basf2 {job.path} {options}"
114
115 # Create a helpfile-shellscript, which contains all the commands that
116 # need to be executed by the cluster.
117 # First, set up the basf2 tools and perform b2setup with the correct
118 # revision. The execute the command (i.e. run basf2 or ROOT on a
119 # steering file). Write the return code of that into a *.done file.
120 # Delete the helpfile-shellscript.
121 tmp_name = self.path + "/" + "script_" + job.name + ".sh"
122 with open(tmp_name, "w+") as tmp_file:
123 tmp_file.write(
124 "#!/bin/bash \n\n"
125 + "BELLE2_NO_TOOLS_CHECK=1 \n"
126 + f"source {self.tools}/b2setup \n"
127 + f"cd {self.adjust_path(output_dir)} \n"
128 + f"{command} \n"
129 + f"echo $? > {self.path}/script_{job.name}.done \n"
130 + f"rm {tmp_name} \n"
131 )
132
133 # Make the helpfile-shellscript executable
134 st = os.stat(tmp_name)
135 os.chmod(tmp_name, st.st_mode | stat.S_IEXEC)
136
137 return tmp_name
138
139 def checkDoneFile(self, job):
140 """!
141 Checks whether the '.done'-file has been created for a job. If so, it
142 returns True, else it returns False in the first part of the tuple.
143 Also deletes the .done-File it if exists.
144 The second entry in the tuple will be the exit code read from the done file
145 """
146
147 # If there is a file indicating the job is done, that is its name:
148 donefile_path = self.createDoneFileName(job)
149
150 donefile_exists = False
151 # Check if such a file exists. If so, this means that the job has
152 # finished.
153 if os.path.isfile(donefile_path):
154
155 # Read the returncode/exit_status for the job from the *.done-file
156 with open(donefile_path) as f:
157 try:
158 returncode = int(f.read().strip())
159 except ValueError:
160 returncode = -654
161
162 print(f"donefile found with return code {returncode}")
163 donefile_exists = True
164 os.remove(donefile_path)
165 else:
166 print("no donefile found")
167 returncode = -555
168
169 return [donefile_exists, returncode]
170
171 def terminate(self, job: Script):
172 """! Terminate running job.
173 """
174 self.logger.error("Script termination not supported.")
175
176 @abstractmethod
177 def adjust_path(self, path):
178 """!
179 This method can be used if path names are different on submission
180 and execution hosts.
181 @param path: The past that needs to be adjusted
182 @return: The adjusted path
183 """
logger
Contains a reference to the logger-object from validate_basf2 Set up the logging functionality for th...
str createDoneFileName(self, Script job)
Generate the file name used for the done output.
tools
Path to the basf2 tools and central/local release.
b2setup
The command for b2setup (and b2code-option)
clusterlog
The file object to which all cluster messages will be written.
path
The default constructor.
def checkDoneFile(self, job)
Checks whether the '.done'-file has been created for a job.
def adjust_path(self, path)
This method can be used if path names are different on submission and execution hosts.
def prepareSubmission(self, Script job, options, tag)
Setup output folders and create the wrapping shell script.
def terminate(self, Script job)
Terminate running job.
def __init__(self)
The default constructor.
Definition: __init__.py:1