ATLAS Offline Software
periodInfo.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 # Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
4 
5 from __future__ import print_function
6 
7 """
8  Generate run list for each period using the pyAMI interface to the COMA db.
9 
10  Need to authenticate (once) and setup pyami via:
11 
12 source /afs/cern.ch/atlas/software/tools/atlasmeta/setup.sh
13 export PYAMI_CONFIG_DIR=~/private/.pyami
14 ami auth
15 """
16 
17 __author__ = 'Carl Gwilliam'
18 __version__ = ''
19 __usage__ = '''%prog [options]'''
20 
21 import os,sys
22  # the above line needed to ensure correct python environment since pyAMI 4.0.3
23 import pyAMI.client
24 
25 from optparse import OptionParser
26 parser = OptionParser(usage=__usage__, version=__version__)
27 parser.add_option('-c', '--config', dest='config', default='', help='AMI authentication file')
28 (options,args) = parser.parse_args()
29 
30 class AMIWrapper:
31  def __init__(self):
32  "Initalise AMI"
33  self.ami=pyAMI.client.Client('atlas')
34  #if options.config:
35  # self.configFileName = os.path.expanduser(options.config)
36  #else:
37  #sys.exit('No authentication file specified')
38  #self.ami.readConfig(self.configFileName)
39  if options.config:
40  self.configFileName = os.path.expanduser(options.config)
41  else:
42  sys.exit('No authentication file specified')
43  print (self.configFileName)
44  #self.ami=pyAMI.client.Client('atlas',key_file=self.configFileName,ignore_proxy=True,verbose=True)
45 
46  def run(self, cmd):
47  """
48  Execute an AMI command given as a list of command and paramters (ami format) or
49  space separated string (for convenience)
50  """
51  if isinstance(cmd, str):
52  cmd = cmd.split()
53 
54  print ('PRINT AMI CMD', cmd )
55  results = self.ami.execute(cmd,format='dict_object')
56 
57  return results.get_rows()
58 
59  def periods(self, period=None, level=None, project=None, status=None):
60  "Get list of periods. By default return all periods"
61 
62  cmd = 'ListDataPeriods'
63  if period: cmd += ' period='+period
64  if level: cmd += ' periodLevel='+level
65  if project: cmd += ' projectName='+project
66  if status: cmd += ' status='+status
67  return self.run(cmd)
68 
69  def runsForPeriod(self, period, project):
70  "Get info on runs for a particular period"
71 
72  cmd = 'GetRunsForDataPeriod period='+period
73  cmd += ' projectName='+project
74  print (cmd)
75  return self.run(cmd)
76 
77  def runListsPerPeriod(self, location='/afs/cern.ch/user/a/atlidbs/nt/DataPeriods/'):
78  """
79  Generate files containing run lists for periods if they don't already exist
80  """
81  # Don't only update unlocked periods since they may change as looked so need to check at least once more
82  # Need to find a better way fo doing this but for now update all
83  periods = self.periods() #status='unlocked')
84  path = os.path
85  num = 0
86 
87  for p in periods:
88  #print ('Looking at period: ', p )
89  projectDir =path.normpath(location + '/' + p['projectName'])
90 
91  if not path.exists(projectDir):
92  os.makedirs(projectDir)
93 
94  filename = '%s/%s.runs.list' % (projectDir,p['period'])
95 
96  #if path.exists(filename): continue
97 
98  # Need to remove the file as it might have changed from last time
99  if path.exists(filename):
100  os.system('rm ' + filename)
101 
102  print ('* Creating run list for %(projectName)s %(period)s ...' % p)
103 
104  num += 1
105  #if p['period'] == 'A1' and p['projectName'] == 'data15_1beam':
106  # continue
107  try:
108  runs = self.runsForPeriod(p['period'], p['projectName'])
109  except:
110  continue
111  runList = '\n'.join(sorted([r['runNumber'] for r in runs]))
112  runList += '\n' # Needed for cat to work!
113  print (runList)
114  with open(filename, 'w') as f:
115  f.write(runList)
116 
117  print (' ... written to', filename, '\n' )
118 
119  if not num:
120  print ('* No new period infomation to create')
121 
122 if __name__ == '__main__':
123  ami = AMIWrapper()
124  ami.runListsPerPeriod()
125 
126 
periodInfo.AMIWrapper.run
def run(self, cmd)
Definition: periodInfo.py:46
periodInfo.AMIWrapper.runListsPerPeriod
def runListsPerPeriod(self, location='/afs/cern.ch/user/a/atlidbs/nt/DataPeriods/')
Definition: periodInfo.py:77
periodInfo.AMIWrapper.configFileName
configFileName
Definition: periodInfo.py:40
periodInfo.AMIWrapper
Definition: periodInfo.py:30
periodInfo.AMIWrapper.__init__
def __init__(self)
Definition: periodInfo.py:31
LArG4FSStartPointFilterLegacy.execute
execute
Definition: LArG4FSStartPointFilterLegacy.py:20
periodInfo.AMIWrapper.ami
ami
Definition: periodInfo.py:33
DerivationFramework::TriggerMatchingUtils::sorted
std::vector< typename T::value_type > sorted(T begin, T end)
Helper function to create a sorted vector from an unsorted one.
TCS::join
std::string join(const std::vector< std::string > &v, const char c=',')
Definition: Trigger/TrigT1/L1Topo/L1TopoCommon/Root/StringUtils.cxx:10
Trk::open
@ open
Definition: BinningType.h:40
periodInfo.AMIWrapper.periods
def periods(self, period=None, level=None, project=None, status=None)
Definition: periodInfo.py:59
periodInfo.AMIWrapper.runsForPeriod
def runsForPeriod(self, period, project)
Definition: periodInfo.py:69