All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
stream.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 ######################################################################
3 #
4 # Name: stream.py
5 #
6 # Purpose: Extract stream name from internal sam metadata in an
7 # artroot file.
8 #
9 # Created: 13-Oct-2015 H. Greenlee
10 #
11 # Command line usage:
12 #
13 # stream.py <artroot file>
14 #
15 ######################################################################
16 
17 from __future__ import absolute_import
18 from __future__ import print_function
19 
20 # Import stuff.
21 
22 import sys, subprocess, json
23 from larbatch_utilities import convert_str
24 
25 # Read and decode stream encoded in sam metadata.
26 
27 def get_stream(inputfile):
28 
29  result = ''
30 
31  # Extract sam metadata in form of json string.
32 
33  jobinfo = subprocess.Popen(['sam_metadata_dumper', inputfile],
34  stdout=subprocess.PIPE,
35  stderr=subprocess.PIPE)
36  jobout, joberr = jobinfo.communicate()
37  jobout = convert_str(jobout)
38  joberr = convert_str(joberr)
39  rc = jobinfo.poll()
40  if rc != 0:
41  raise RuntimeError('sam_metadata_dumper failed with status %d' % rc)
42 
43  # Decode json string to dictionary.
44  # Work around art bug by deleting "runs" line.
45 
46  json_str = ''
47  n = jobout.find('"runs"')
48  if n >= 0:
49  m = jobout.rfind('\n', 0, n)
50  if m > 0:
51  json_str = jobout[:m+1]
52  k = jobout.find('\n', n)
53  if k > n:
54  json_str += jobout[k+1:]
55  else:
56  json_str = jobout
57 
58  # End of workaround.
59 
60  js = json.loads(json_str)
61  md = js[inputfile]
62 
63  # Extract stream from json dictionary.
64 
65  if 'data_stream' in md:
66  result = md['data_stream']
67  else:
68  raise RuntimeError('Sam metadata does not contain stream.')
69 
70  # Done.
71 
72  return result
73 
74 # Main program.
75 
76 if __name__ == "__main__":
77  stream = get_stream(str(sys.argv[1]))
78  print(stream)
79  sys.exit(0)
do one_file $F done echo for F in find $TOP name CMakeLists txt print
def get_stream
Definition: stream.py:27