experiment_utilities.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 #----------------------------------------------------------------------
3 #
4 # Name: experiment_utilities.py
5 #
6 # Purpose: A python module containing various experiment-specific
7 # python utility functions.
8 #
9 # Created: 28-Oct-2013 H. Greenlee
10 # Updated: T. Yang, T. Junk
11 #
12 #----------------------------------------------------------------------
13 
14 import os
15 import larbatch_utilities
16 import subprocess
17 
18 # Don't fail (on import) if samweb is not available.
19 
20 try:
21  import samweb_cli
22 except ImportError:
23  pass
24 
25 def get_dropbox(filename):
26 
27  # Get metadata.
28 
29  md = {}
30  exp = 'dune'
31  if os.environ.has_key('SAM_EXPERIMENT'):
32  exp = os.environ['SAM_EXPERIMENT']
33  samweb = samweb_cli.SAMWebClient(experiment=exp)
34  try:
35  md = samweb.getMetadata(filenameorid=filename)
36  except:
37  pass
38 
39  # Extract the metadata fields that we need.
40 
41  file_type = ''
42 
43  if md.has_key('file_type'):
44  file_type = md['file_type']
45 
46  if not file_type:
47  raise RuntimeError('Missing or invalid metadata for file %s.' % filename)
48 
49  # Construct dropbox path.
50 
51  #path = '/dune/data/dunepro/dropbox/%s' % file_type
52  path = '/pnfs/dune/scratch/dunepro/dropbox/%s' % file_type
53  return path
54 
55 # Return fcl configuration for experiment-specific sam metadata.
56 
57 def get_sam_metadata(project, stage):
58  result = 'services.FileCatalogMetadataDUNE: {\n'
59  for key in project.parameters:
60  result = result + ' %s: "%s"\n' % (key, project.parameters[key])
61  for key in stage.parameters:
62  result = result + ' %s: "%s"\n' % (key, stage.parameters[key])
63  result = result + ' StageName: "%s"\n' % stage.name
64  result = result + '}\n'
65  result = result + 'services.TFileMetadataDUNE: @local::dune_tfile_metadata\n'
66  return result
67 
69  return 'dunetpc'
70 
71 # Function to return path to the setup_dune.sh script
72 
74 
75  OASIS_DIR="/cvmfs/dune.opensciencegrid.org/products/dune/"
76 
77  if os.path.isfile(OASIS_DIR+"setup_dune.sh"):
78  setup_script = OASIS_DIR+"setup_dune.sh"
79  else:
80  raise RuntimeError("Could not find setup script at "+OASIS_DIR)
81 
82  return setup_script
83 
84 # Construct dimension string for project, stage.
85 
86 def dimensions(project, stage, ana=False):
87 
88  dim = 'file_type %s' % project.file_type
89  dim = dim + ' and data_tier %s' % stage.data_tier
90  for key in project.parameters:
91  if key == 'MCName':
92  dim = dim + ' and lbne_MC.name %s' % project.parameters[key]
93  if key == 'DataName':
94  dim = dim + ' and lbne_data.name %s' % project.parameters[key]
95  dim = dim + ' and version %s' % project.release_tag
96  dim = dim + ' and application %s' % stage.name
97  dim = dim + ' and availability: anylocation'
98  return dim
99 
100 # Get grid proxy.
101 
102 def get_proxy():
103 
104  global proxy_ok
105  proxy_ok = False
106 
107  # Make sure we have a valid certificate.
108 
109  larbatch_utilities.test_kca()
110 
111  # Get proxy using either specified cert+key or default cert.
112 
113  if os.environ.has_key('X509_USER_CERT') and os.environ.has_key('X509_USER_KEY'):
114  cmd=['voms-proxy-init',
115  '-rfc',
116  '-cert', os.environ['X509_USER_CERT'],
117  '-key', os.environ['X509_USER_KEY'],
118  '-voms', '%s:/%s/Role=%s' % (larbatch_utilities.get_experiment(), larbatch_utilities.get_experiment(), larbatch_utilities.get_role())]
119  try:
120  subprocess.check_call(cmd, stdout=-1, stderr=-1)
121  proxy_ok = True
122  except:
123  pass
124  pass
125  else:
126  cmd=['voms-proxy-init',
127  '-noregen',
128  '-rfc',
129  '-voms',
130  '%s:/%s/Role=%s' % (larbatch_utilities.get_experiment(), larbatch_utilities.get_experiment(), larbatch_utilities.get_role())]
131  jobinfo = subprocess.Popen(cmd, stdout=subprocess.PIPE,
132  stderr=subprocess.PIPE)
133  jobout, joberr = jobinfo.communicate()
134  rc = jobinfo.poll()
135  if rc != 0 and rc!= 1:
136  proxy_ok = False
137  else:
138  proxy_ok = True
139  # Done
140 
141  return proxy_ok
142 
144 
145  def __init__(self):
146  self.expname = ''
147 
148  def metadataList(self):
149  return [self.expname + elt for elt in ('lbneMCGenerators','lbneMCName','lbneMCDetectorType','StageName')]
150 
151 
152  def translateKey(self, key):
153  if key == 'lbneMCDetectorType':
154  return 'lbne_MC.detector_type'
155  elif key == 'StageName':
156  return 'lbne_MC.miscellaneous'
157  else:
158  prefix = key[:4]
159  stem = key[4:]
160  projNoun = stem.split("MC")
161  return prefix + "_MC." + projNoun[1]
def dimensions(project, stage, ana=False)
def get_sam_metadata(project, stage)