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