2 from __future__
import absolute_import
3 from __future__
import print_function
6 from subprocess
import Popen, PIPE
12 import project_utilities, root_metadata
13 from larbatch_utilities
import convert_str
29 Purpose: To extract metadata from output file on worker node, generate JSON file
34 """Base class to hold / interpret general metadata"""
35 __metaclass__ = abc.ABCMeta
42 """Extract metadata from inputfile into a pipe for further processing."""
43 local = project_utilities.path_to_local(self.
inputfile)
45 proc = Popen([
"sam_metadata_dumper", local], stdout=PIPE,
48 url = project_utilities.path_to_url(inputfile)
49 proc = Popen([
"sam_metadata_dumper", url], stdout=PIPE,
51 if len(local) > 0
and local != self.
inputfile:
56 """Run the proc in a 60-sec timeout queue, return stdout, stderr"""
60 thread.join(timeout=60)
62 print(
'Terminating subprocess because of timeout.')
69 raise RuntimeError(
'sam_metadata_dumper returned nonzero exit status {}.'.
format(rc))
74 """Run jobinfo, put the return code, stdout, and stderr into a queue"""
75 jobout, joberr = jobinfo.communicate()
79 for item
in (rc, jobout, joberr):
85 """Take Jobout and Joberr (in jobtuple) and return mdart object from that"""
86 mdtext =
''.
join(line.replace(
", ,",
",")
for line
in jobtuple[0].split(
'\n')
if line[-3:-1] !=
' ,')
87 mdtop = json.JSONDecoder().
decode(mdtext)
88 if len(
list(mdtop.keys())) == 0:
89 print(
'No top-level key in extracted metadata.')
91 file_name =
list(mdtop.keys())[0]
92 return mdtop[file_name]
96 """If there's no application key in md dict, create the key with a blank dictionary.
97 Then return md['application'], along with mdval"""
98 if 'application' not in md:
99 md[
'application'] = {}
100 return md[
'application']
104 """Class to hold/interpret experiment-specific metadata"""
106 MetaData.__init__(self, inputfile)
111 from experiment_utilities
import MetaDataKey
113 print(
"You have not defined an experiment-specific metadata and key-translating module in experiment_utilities. Exiting")
116 metaDataModule = MetaDataKey()
117 self.metadataList, self.
translateKeyf = metaDataModule.metadataList(), metaDataModule.translateKey
120 """Returns the output of the imported translateKey function (as translateKeyf) called on key"""
124 """Loop through art metdata, generate metadata dictionary"""
134 for mdkey, mdval
in list(mdart.items()):
140 if mdkey
in [
'file_format_version',
'file_format_era',
'run_type']:
142 elif mdkey
in [
'art.file_format_version',
'art.file_format_era',
'art.run_type']:
146 elif mdkey
in [
'art.first_event',
'art.last_event']:
155 elif mdkey ==
'data_stream' and mdval ==
'out':
158 elif mdkey ==
'data_stream' and mdval[:3] ==
'out' and \
159 mdval[3] >=
'0' and mdval[3] <=
'9':
163 elif mdkey ==
'applicationFamily' or mdkey ==
'application.family':
165 elif mdkey ==
'process_name' or mdkey ==
'art.process_name':
167 elif mdkey ==
'applicationVersion' or mdkey ==
'application.version':
171 elif mdkey ==
'parents':
172 md[
'parents'] = [{
'file_name': parent}
for parent
in mdval]
174 elif mdkey.startswith(
'mixparent'):
175 mixparents.append(mdval.strip(
' ,"') )
178 elif mdkey
in [
'first_event',
'last_event']:
179 if (
type(mdval) ==
type([])
or type(mdval) ==
type(()))
and len(mdval) >= 3:
183 elif mdkey
in self.metadataList:
188 elif mdkey ==
'fclName':
189 md[
'fcl.name'] = mdval
190 elif mdkey ==
'fclVersion':
191 md[
'fcl.version'] = mdval
200 for mixparent
in mixparents:
201 mixparent_dict = {
'file_name': mixparent}
202 md[
'parents'].append(mixparent_dict)
206 md[
'file_name'] = self.inputfile.split(
"/")[-1]
207 if 'file_size' in md0:
208 md[
'file_size'] = md0[
'file_size']
210 md[
'file_size'] = os.path.getsize(self.
inputfile)
212 md[
'crc'] = md0[
'crc']
214 md[
'crc'] = root_metadata.fileEnstoreChecksum(self.
inputfile)
221 """ Get metadata from input file and return as python dictionary.
222 Calls other methods in class and returns metadata dictionary"""
226 return self.
md_gen(mdart, md0)
230 expSpecificMetadata =
expMetaData(os.environ[
'SAM_EXPERIMENT'], str(sys.argv[1]))
232 print(
'You have not implemented a defineMetaData function by providing an experiment.')
233 print(
'No metadata keys will be saved')
235 mdtext = json.dumps(expSpecificMetadata.getmetadata(), indent=2, sort_keys=
True)
241 if __name__ ==
"__main__":
static std::string format(PyObject *obj, unsigned int pos, unsigned int indent, unsigned int maxlen, unsigned int depth)
do one_file $F done echo for F in find $TOP name CMakeLists txt print
S join(S const &sep, Coll const &s)
Returns a concatenation of strings in s separated by sep.
void decode(std::any const &src, Interval< Args...> &iv)
Decodes an interval.