test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
confdbOfflineConverter.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 import sys, os
3 import os.path
4 import tempfile
5 import urllib
6 import shutil
7 import subprocess
8 import atexit
9 
11 
12  # the machine aliases and interfaces for the *online* database are
13  # cmsonr1-s.cms, cmsonr2-s.cms, cmsonr3-s.cms
14  # cmsonr1-v.cms, cmsonr2-v.cms, cmsonr3-v.cms
15  # but the -s and -v interfaces resolve to the same hosts.
16  # The actual machines and interfaces are
17  # CMSRAC11-S.cms, CMSRAC12-S.cms, CMSRAC21-S.cms
18  # CMSRAC11-V.cms, CMSRAC12-V.cms, CMSRAC21-V.cms
19 
20  # the possible machines and interfaces for the *offline* database are
21  # cmsr1-s.cms, cmsr2-s.cms, cmsr3-s.cms
22  # cmsr1-v.cms, cmsr2-v.cms, cmsr3-v.cms
23  # but the -s and -v interfaces resolve to the same hosts
24  # The actual machines and interfaces are
25  # itrac50011-s.cern.ch, itrac50063-s.cern.ch, itrac50078-s.cern.ch
26  # itrac50011-v.cern.ch, itrac50063-v.cern.ch, itrac50078-v.cern.ch
27 
28  databases = {}
29  databases['v1'] = {}
30  databases['v1']['offline'] = ( '-t', 'oracle', '-h', 'cmsr1-s.cern.ch', '-d', 'cms_cond.cern.ch', '-u', 'cms_hltdev_reader', '-s', 'convertme!' )
31  databases['v1']['hltdev'] = databases['v1']['offline'] # for backwards compatibility
32  databases['v1']['online'] = ( '-t', 'oracle', '-h', 'cmsonr1-s.cms', '-d', 'cms_rcms.cern.ch', '-u', 'cms_hlt_r', '-s', 'convertme!' )
33  databases['v1']['adg'] = ( '-t', 'oracle', '-h', 'cmsr1-s.cern.ch', '-d', 'cms_cond.cern.ch', '-u', 'cms_hlt_gui_r', '-s', 'convertme!' )
34  databases['v1']['orcoff'] = databases['v1']['adg'] # for backwards compatibility
35  databases['v2'] = {}
36  databases['v2']['offline'] = ( '-t', 'oracle', '-h', 'cmsr1-s.cern.ch', '-d', 'cms_cond.cern.ch', '-u', 'cms_hlt_gdr_r', '-s', 'convertme!' )
37  databases['v2']['online'] = ( '-t', 'oracle', '-h', 'cmsonr1-s.cms', '-d', 'cms_rcms.cern.ch', '-u', 'cms_hlt_gdr_r', '-s', 'convertme!' )
38  databases['v2']['adg'] = ( '-t', 'oracle', '-h', 'cmsonradg1-s.cern.ch', '-d', 'cms_orcon_adg.cern.ch', '-u', 'cms_hlt_gdr_r', '-s', 'convertme!' )
39 
40 
41  @staticmethod
43  dir = os.path.realpath(dir)
44  if not os.path.isdir(dir):
45  try:
46  os.makedirs(dir)
47  except:
48  return None
49  return dir
50 
51 
52  def __init__(self, version = 'v1', database = 'hltdev', url = None, verbose = False):
53  self.verbose = verbose
54  self.version = version
55  self.baseDir = '/afs/cern.ch/user/c/confdb/www/%s/lib' % version
56  self.baseUrl = 'http://confdb.web.cern.ch/confdb/%s/lib' % version
57  self.jars = ( 'ojdbc6.jar', 'cmssw-evf-confdb-converter.jar' )
58  self.workDir = ''
59 
60  # check the schema version
61  if version not in self.databases:
62  # unsupported database version
63  sys.stderr.write( "ERROR: unsupported database version \"%s\"\n" % version)
64 
65  # check the database
66  if database in self.databases[version]:
67  # load the connection parameters for the given database
68  self.connect = self.databases[version][database]
69  else:
70  # unsupported database
71  sys.stderr.write( "ERROR: unknown database \"%s\" for version \"%s\"\n" % (database, version))
72  sys.exit(1)
73 
74  # check for a custom base URL
75  if url is not None:
76  self.baseUrl = url
77 
78  # try to read the .jar files from AFS, or download them
79  if os.path.isdir(self.baseDir) and all(os.path.isfile(self.baseDir + '/' + jar) for jar in self.jars):
80  # read the .jar fles from AFS
81  self.workDir = self.baseDir
82  else:
83  # try to use $CMSSW_BASE/tmp
84  self.workDir = OfflineConverter.CheckTempDirectory(os.environ['CMSSW_BASE'] + '/tmp/confdb')
85  if not self.workDir:
86  # try to use $TMP
87  self.workDir = OfflineConverter.CheckTempDirectory(os.environ['TMP'] + '/confdb')
88  if not self.workDir:
89  # create a new temporary directory, and install a cleanup callback
90  self.workDir = tempfile.mkdtemp()
91  atexit.register(shutil.rmtree, self.workDir)
92  # download the .jar files
93  for jar in self.jars:
94  # check if the file is already present
95  if os.path.exists(self.workDir + '/' + jar):
96  continue
97  # download to a temporay name and use an atomic rename (in case an other istance is downloading the same file
98  handle, temp = tempfile.mkstemp(dir = self.workDir, prefix = jar + '.')
99  os.close(handle)
100  urllib.urlretrieve(self.baseUrl + '/' + jar, temp)
101  if not os.path.exists(self.workDir + '/' + jar):
102  os.rename(temp, self.workDir + '/' + jar)
103  else:
104  os.unlink(temp)
105 
106  # setup the java command line and CLASSPATH
107  if self.verbose:
108  sys.stderr.write("workDir = %s\n" % self.workDir)
109  # use non-blocking random number source /dev/urandom (instead of /dev/random), see:
110  # http://blockdump.blogspot.fr/2012/07/connection-problems-inbound-connection.html
111  # deal with timezone region not found
112  # http://stackoverflow.com/questions/9156379/ora-01882-timezone-region-not-found
113  # increase the thread stack size from the default of 1 MB to work around java.lang.StackOverflowError errors, see
114  # man java
115  self.javaCmd = ( 'java', '-cp', ':'.join(self.workDir + '/' + jar for jar in self.jars), '-Djava.security.egd=file:///dev/urandom', '-Doracle.jdbc.timezoneAsRegion=false', '-Xss32M', 'confdb.converter.BrowserConverter' )
116 
117 
118  def query(self, *args):
119  args = self.javaCmd + self.connect + args
120  if self.verbose:
121  sys.stderr.write("\n" + ' '.join(args) + "\n\n" )
122  sub = subprocess.Popen(
123  args,
124  stdin = None,
125  stdout = subprocess.PIPE,
126  stderr = subprocess.PIPE,
127  shell = False,
128  universal_newlines = True )
129  return sub.communicate()
130 
131 def help():
132  sys.stdout.write("""Usage: %s OPTIONS
133 
134  --v1|--v2 (specify the ConfDB version [default: v1])
135 
136  --offline|--online|--adg (specify the target db [default: offline])
137 
138  Note that for v1
139  --orcoff is a synonim of --adg
140  --offline is a synonim of --hltdev
141 
142  --configId <id> (specify the configuration by id)
143  --configName <name> (specify the configuration by name)
144  --runNumber <run> (specify the configuration by run number)
145  [exactly one of --configId OR --configName OR --runNumber is required]
146 
147  --cff (retrieve configuration *fragment*)
148  --input <f1.root[,f2.root]> (insert PoolSource with specified fileNames)
149  --input <files.list> (read a text file which lists input ROOT files)
150  --output <out.root> (insert PoolOutputModule w/ specified fileName)
151  --nopsets (exclude all globale psets)
152  --noedsources (exclude all edsources)
153  --noes (exclude all essources *and* esmodules)
154  --noessources (exclude all essources)
155  --noesmodules (exclude all esmodules)
156  --noservices (exclude all services)
157  --nooutput (exclude all output modules)
158  --nopaths (exclude all paths [+=referenced seqs&mods])
159  --nosequences (don't define sequences [+=referenced s&m])
160  --nomodules (don't define modules)
161  --psets <pset1[,pset2]> (include only specified global psets)
162  --psets <-pset1[,-pset2]> (include all global psets but the specified)
163  --essources <ess1[,ess2]> (include only specified essources)
164  --essources <-ess1[,-ess2]> (include all essources but the specified)
165  --esmodules <esm1[,esm2]> (include only specified esmodules)
166  --esmodules <-esm1[,-esm2]> (include all esmodules but the specified)
167  --services <svc1[,svc2]> (include only specified services)
168  --services <-svc1[,-svc2]> (include all services but the specified)
169  --paths <p1[,p2]> (include only specified paths)
170  --paths <-p1[,-p2]> (include all paths but the specified)
171  --streams <s1[,s2]> (include only specified streams)
172  --datasets <d1[,d2]> (include only specified datasets)
173  --sequences <s1[,s2]> (include sequences, referenced or not!)
174  --modules <p1[,p2]> (include modules, referenced or not!)
175  --blocks <m1::p1[,p2][,m2]> (generate parameter blocks)
176 
177  --verbose (print additional details)
178 """)
179 
180 
181 def main():
182  args = sys.argv[1:]
183  version = 'v1'
184  db = 'hltdev'
185  verbose = False
186 
187  if not args:
188  help()
189  sys.exit(1)
190 
191  if '--help' in args or '-h' in args:
192  help()
193  sys.exit(0)
194 
195  if '--verbose' in args:
196  verbose = True
197  args.remove('--verbose')
198 
199  if '--v1' in args and '--v2' in args:
200  sys.stderr.write( "ERROR: conflicting database version specifications \"--v1\" and \"--v2\"\n" )
201  sys.exit(1)
202 
203  if '--v1' in args:
204  version = 'v1'
205  db = 'hltdev'
206  args.remove('--v1')
207 
208  if '--v2' in args:
209  version = 'v2'
210  db = 'offline'
211  args.remove('--v2')
212 
213  _dbs = {}
214  _dbs['v1'] = [ '--%s' % _db for _db in OfflineConverter.databases['v1'] ] + [ '--runNumber' ]
215  _dbs['v2'] = [ '--%s' % _db for _db in OfflineConverter.databases['v2'] ] + [ '--runNumber' ]
216  _dbargs = set(args) & set(sum(_dbs.values(), []))
217 
218  if _dbargs:
219  if len(_dbargs) > 1:
220  sys.stderr.write( "ERROR: too many database specifications: \"" + "\", \"".join( _dbargs) + "\"\n" )
221  sys.exit(1)
222 
223  _arg = _dbargs.pop()
224  db = _arg[2:]
225  if db == 'runNumber':
226  db = 'adg'
227  else:
228  args.remove(_arg)
229 
230  if not db in OfflineConverter.databases[version]:
231  sys.stderr.write( "ERROR: database version \"%s\" incompatible with specification \"%s\"\n" % (version, db) )
232  sys.exit(1)
233 
234  converter = OfflineConverter(version = version, database = db, verbose = verbose)
235  out, err = converter.query( * args )
236  if 'ERROR' in err:
237  sys.stderr.write( "%s: error while retriving the HLT menu\n\n%s\n\n" % (sys.argv[0], err) )
238  sys.exit(1)
239  else:
240  sys.stdout.write( out )
241 
242 
243 if __name__ == "__main__":
244  main()
static std::string join(char **cmd)
Definition: RemoteFile.cc:18
Definition: main.py:1