test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Classes | Functions | Variables
conddblib Namespace Reference

Classes

class  _Col
 
class  Connection
 
class  DbRef
 
class  Enum
 
class  EnumMetaclass
 
class  GlobalTag
 
class  GlobalTagMap
 
class  IOV
 
class  Payload
 
class  Synchronization
 
class  Tag
 
class  TagLog
 
class  TimeType
 

Functions

def _exists
 
def _getCMSFrontierConnectionString
 
def _getCMSFrontierSQLAlchemyConnectionString
 
def _getCMSOracleSQLAlchemyConnectionString
 
def _inserted_before
 
def connect
 
def fq_col
 
def fq_name
 
def getPayload
 
def getSchema
 
def hash
 
def listObject
 
def make_dbtype
 
def make_url
 

Variables

string __author__ = 'Miguel Ojeda'
 
string __copyright__ = 'Copyright 2013, CERN'
 
list __credits__ = ['Giacomo Govi', 'Miguel Ojeda', 'Andreas Pfeiffer']
 
string __email__ = 'mojedasa@cern.ch'
 
string __license__ = 'Unknown'
 
string __maintainer__ = 'Miguel Ojeda'
 
tuple _Base = sqlalchemy.ext.declarative.declarative_base()
 
string authPathEnvVar = 'COND_AUTH_PATH'
 
string contact_help = 'If you need assistance, please write an email to %s and %s. If you need immediate/urgent assistance, you can call the Offline DB expert on call (%s).'
 
string database_help
 
dictionary db_models = {}
 
string dbreader_user_name = 'cms_cond_general_r'
 
string dbuser_name = 'cms_conditions'
 
string dbwriter_user_name = 'cms_cond_general_w'
 
int description_length = 4000
 
string devdbwriter_user_name = 'cms_test_conditions'
 
string empty_label = '-'
 
tuple hash_length = len(hash(''))
 
tuple logger = logging.getLogger(__name__)
 
int name_length = 100
 
string offline_db_experts_email = 'cms-offlinedb-exp@cern.ch'
 
string offline_db_experts_phone = '+41 22 76 70817, or 70817 from CERN; check https://twiki.cern.ch/twiki/bin/viewauth/CMS/DBShifterHelpPage if it does not work; availability depends on the state of the LHC'
 
dictionary ora_types = {}
 
string schema_name = 'CMS_CONDITIONS'
 
dictionary sqlite_types = {}
 
string web_experts_email = 'cms-cond-dev@cern.ch'
 

Detailed Description

CMS Conditions DB Python library.

Function Documentation

def conddblib._exists (   session,
  primary_key,
  value 
)
private

Definition at line 553 of file conddblib.py.

References KineDebug3.count(), and alcazmumu_cfi.filter.

Referenced by listObject().

554 def _exists(session, primary_key, value):
555  ret = None
556  try:
557  ret = session.query(primary_key).\
558  filter(primary_key == value).\
559  count() != 0
560  except sqlalchemy.exc.OperationalError:
561  pass
562 
563  return ret
def _exists
Definition: conddblib.py:553
def conddblib._getCMSFrontierConnectionString (   database)
private

Definition at line 452 of file conddblib.py.

References communicate().

Referenced by _getCMSFrontierSQLAlchemyConnectionString().

453 def _getCMSFrontierConnectionString(database):
454  import subprocess
455  return subprocess.Popen(['cmsGetFnConnect', 'frontier://%s' % database], stdout = subprocess.PIPE).communicate()[0].strip()
456 
static void * communicate(void *obj)
Definition: DQMNet.cc:1246
def _getCMSFrontierConnectionString
Definition: conddblib.py:452
def conddblib._getCMSFrontierSQLAlchemyConnectionString (   database,
  schema = 'cms_conditions' 
)
private

Definition at line 457 of file conddblib.py.

References _getCMSFrontierConnectionString().

Referenced by make_url().

458 def _getCMSFrontierSQLAlchemyConnectionString(database, schema = 'cms_conditions'):
459  import urllib
460  return 'oracle+frontier://@%s/%s' % (urllib.quote_plus(_getCMSFrontierConnectionString(database)), schema)
461 
def _getCMSFrontierConnectionString
Definition: conddblib.py:452
def _getCMSFrontierSQLAlchemyConnectionString
Definition: conddblib.py:457
def conddblib._getCMSOracleSQLAlchemyConnectionString (   database,
  schema = 'cms_conditions' 
)
private

Definition at line 462 of file conddblib.py.

Referenced by make_url().

463 def _getCMSOracleSQLAlchemyConnectionString(database, schema = 'cms_conditions'):
464  return 'oracle://%s@%s' % (schema, database)
465 
466 
467 # Entry point
def _getCMSOracleSQLAlchemyConnectionString
Definition: conddblib.py:462
def conddblib._inserted_before (   timestamp)
private
To be used inside filter().

Definition at line 564 of file conddblib.py.

Referenced by listObject().

565 def _inserted_before(timestamp):
566  '''To be used inside filter().
567  '''
568 
569  if timestamp is None:
570  # XXX: Returning None does not get optimized (skipped) by SQLAlchemy,
571  # and returning True does not work in Oracle (generates "and 1"
572  # which breaks Oracle but not SQLite). For the moment just use
573  # this dummy condition.
574  return sqlalchemy.literal(True) == sqlalchemy.literal(True)
575 
576  return conddb.IOV.insertion_time <= _parse_timestamp(timestamp)
def _inserted_before
Definition: conddblib.py:564
def conddblib.connect (   url,
  init = False,
  authPath = None,
  verbose = 0 
)
Returns a Connection instance to the CMS Condition DB.

See database_help for the description of the database parameter.

The verbosity level is as follows:

    0 = No output (default).
    1 = SQL statements issued, including their parameters.
    2 = In addition, results of the queries (all rows and the column headers).

Definition at line 506 of file conddblib.py.

507 def connect(url, init=False, authPath=None, verbose=0):
508  '''Returns a Connection instance to the CMS Condition DB.
509 
510  See database_help for the description of the database parameter.
511 
512  The verbosity level is as follows:
513 
514  0 = No output (default).
515  1 = SQL statements issued, including their parameters.
516  2 = In addition, results of the queries (all rows and the column headers).
517  '''
518 
519  if url.drivername == 'oracle' and url.password is None:
520  if authPath is None:
521  if authPathEnvVar in os.environ:
522  authPath = os.environ[authPathEnvVar]
523  authFile = None
524  if authPath is not None:
525  authFile = os.path.join(authPath,'.netrc')
526  if authFile is not None:
527  entryKey = url.host+"/"+url.username
528  logging.debug('Looking up credentials for %s in file %s ' %(entryKey,authFile) )
529  import netrc
530  try:
531  # Try to find the netrc entry
532  (username, account, password) = netrc.netrc( authFile ).authenticators(entryKey)
533  url.password = password
534  except IOError as e:
535  logging.error('.netrc file expected in %s has not been found or cannot be open.' %authPath)
536  raise e
537  except TypeError as e:
538  logging.error('The .netrc file in %s is invalid, or the targeted entry has not been found.' %authPath)
539  except Exception as e:
540  logging.error('Problem with .netrc file in %s: %s' %(authPath,str(e)))
541  else:
542  import getpass
543  url.password = getpass.getpass('Password for %s: ' % str(url))
544 
545  if verbose >= 1:
546  logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO)
547 
548  if verbose >= 2:
549  logging.getLogger('sqlalchemy.engine').setLevel(logging.DEBUG)
550 
551  return Connection(url, init=init)
552 
def connect
Definition: conddblib.py:506
def conddblib.fq_col (   schema,
  table,
  column 
)

Definition at line 197 of file conddblib.py.

Referenced by make_dbtype().

198 def fq_col( schema, table, column ):
199  fqn = '%s.%s' %(table, column)
200  if schema is not None:
201  fqn = '%s.%s' %(schema,fqn)
202  return fqn
def fq_col
Definition: conddblib.py:197
def conddblib.fq_name (   schema_name,
  table_name 
)

Definition at line 177 of file conddblib.py.

178 def fq_name( schema_name, table_name ):
179  name = table_name
180  if schema_name is not None:
181  name = '%s.%s' %(schema_name, table_name)
182  return name
def fq_name
Definition: conddblib.py:177
def conddblib.getPayload (   session,
  hash 
)

Definition at line 616 of file conddblib.py.

References alcazmumu_cfi.filter.

617 def getPayload(session, hash):
618  # get payload from DB:
619  data, payloadType = session.query(Payload.data, Payload.object_type).filter(Payload.hash == hash).one()
620  return data
def getPayload
Definition: conddblib.py:616
def conddblib.getSchema (   tp)

Definition at line 237 of file conddblib.py.

Referenced by conddblib.Connection.is_valid().

238 def getSchema(tp):
239  if tp.__table_args__ is not None:
240  return tp.__table_args__['schema']
241  return None
242 
243 # notice: the GT table names are _LOWERCASE_. When turned to uppercase, the sqlalchemy ORM queries on GLOBAL_TAG and GLOBAL_TAG_MAP
244 # dont work ( probably clashes with a GLOBAL keyword in their code?
def getSchema
Definition: conddblib.py:237
def conddblib.hash (   data)

Definition at line 68 of file conddblib.py.

68 
69 def hash(data):
70  return hashlib.sha1(data).hexdigest()
71 
72 
# Constants
def hash
Definition: conddblib.py:68
def conddblib.listObject (   session,
  name,
  snapshot = None 
)

Definition at line 577 of file conddblib.py.

References _exists(), _inserted_before(), Vispa.Plugins.EdmBrowser.EdmDataAccessor.all(), alcazmumu_cfi.filter, reco.get(), join(), and hitfit.scalar().

578 def listObject(session, name, snapshot=None):
579 
580  is_tag = _exists(session, Tag.name, name)
581  result = {}
582  if is_tag:
583  result['type'] = 'Tag'
584  result['name'] = session.query(Tag).get(name).name
585  result['timeType'] = session.query(Tag.time_type).\
586  filter(Tag.name == name).\
587  scalar()
588 
589  result['iovs'] = session.query(IOV.since, IOV.insertion_time, IOV.payload_hash, Payload.object_type).\
590  join(IOV.payload).\
591  filter(
592  IOV.tag_name == name,
593  _inserted_before(snapshot),
594  ).\
595  order_by(IOV.since.desc(), IOV.insertion_time.desc()).\
596  from_self().\
597  order_by(IOV.since, IOV.insertion_time).\
598  all()
599 
600  try:
601  is_global_tag = _exists(session, GlobalTag.name, name)
602  if is_global_tag:
603  result['type'] = 'GlobalTag'
604  result['name'] = session.query(GlobalTag).get(name)
605  result['tags'] = session.query(GlobalTagMap.record, GlobalTagMap.label, GlobalTagMap.tag_name).\
606  filter(GlobalTagMap.global_tag_name == name).\
607  order_by(GlobalTagMap.record, GlobalTagMap.label).\
608  all()
609  except sqlalchemy.exc.OperationalError:
610  sys.stderr.write("No table for GlobalTags found in DB.\n\n")
611 
612  if not is_tag and not is_global_tag:
613  raise Exception('There is no tag or global tag named %s in the database.' % name)
614 
615  return result
def _exists
Definition: conddblib.py:553
def _inserted_before
Definition: conddblib.py:564
def listObject
Definition: conddblib.py:577
static std::string join(char **cmd)
Definition: RemoteFile.cc:18
T get(const Candidate &c)
Definition: component.h:55
double scalar(const CLHEP::HepGenMatrix &m)
Return the matrix as a scalar. Raise an assertion if the matris is not .
Definition: matutil.cc:183
def conddblib.make_dbtype (   backendName,
  schemaName,
  baseType 
)

Definition at line 203 of file conddblib.py.

References fq_col().

Referenced by conddblib.Connection.get_dbtype().

204 def make_dbtype( backendName, schemaName, baseType ):
205  members = {}
206  deps_reg = set()
207  dbtype_name = '%s_%s' %(baseType.__name__,backendName)
208  members['__tablename__'] = baseType.__tablename__
209  members['__table_args__'] = None
210  if schemaName is not None:
211  members['__table_args__'] = {'schema': schemaName }
212  for k,v in baseType.columns.items():
213  if isinstance(v[0],DbRef):
214  refColDbt = v[0].rtype.columns[v[0].rcol][0]
215  pk = (True if v[1]==_Col.pk else False)
216  if v[1]==_Col.pk:
217  members[k] = sqlalchemy.Column(refColDbt,sqlalchemy.ForeignKey(fq_col(schemaName,v[0].rtype.__tablename__,v[0].rcol)),primary_key=True)
218  else:
219  nullable = (False if v[1] == _Col.notNull else True)
220  members[k] = sqlalchemy.Column(refColDbt,sqlalchemy.ForeignKey(fq_col(schemaName,v[0].rtype.__tablename__,v[0].rcol)),nullable=nullable)
221  if v[0].rtype.__name__ not in deps_reg:
222  deps_reg.add(v[0].rtype.__name__)
223  reftype_name = '%s_%s' %(v[0].rtype.__name__,backendName)
224  members[(v[0].rtype.__name__).lower()] = sqlalchemy.orm.relationship(reftype_name)
225  else:
226  if v[1]==_Col.pk:
227  members[k] = sqlalchemy.Column(v[0],primary_key=True)
228  else:
229  nullable = (True if v[1]==_Col.nullable else False)
230  members[k] = sqlalchemy.Column(v[0],nullable=nullable)
231  dbType = type(dbtype_name,(_Base,),members)
232 
233  if backendName not in db_models.keys():
234  db_models[backendName] = {}
235  db_models[backendName][baseType.__name__] = dbType
236  return dbType
def make_dbtype
Definition: conddblib.py:203
def fq_col
Definition: conddblib.py:197
def conddblib.make_url (   database = 'pro',
  read_only = True 
)

Definition at line 468 of file conddblib.py.

References _getCMSFrontierSQLAlchemyConnectionString(), and _getCMSOracleSQLAlchemyConnectionString().

469 def make_url(database='pro',read_only = True):
470 
471  #schema = 'cms_conditions' # set the default
472  if ':' in database and '://' not in database: # check if we really got a shortcut like "pro:<schema>" (and not a url like proto://...), if so, disentangle
473  database, schema = database.split(':')
474 
475  # Lazy in order to avoid calls to cmsGetFnConnect
476  mapping = {
477  'pro_R': lambda: _getCMSFrontierSQLAlchemyConnectionString('PromptProd', schema_name),
478  'arc_R': lambda: _getCMSFrontierSQLAlchemyConnectionString('FrontierArc', schema_name),
479  'int_R': lambda: _getCMSFrontierSQLAlchemyConnectionString('FrontierInt', schema_name),
480  'dev_R': lambda: _getCMSFrontierSQLAlchemyConnectionString('FrontierPrep', schema_name),
481 
482  'orapro_R': lambda: _getCMSOracleSQLAlchemyConnectionString('cms_orcon_adg', dbreader_user_name),
483  'oraarc_R': lambda: _getCMSOracleSQLAlchemyConnectionString('cmsarc_lb', dbreader_user_name),
484  'oraint_R': lambda: _getCMSOracleSQLAlchemyConnectionString('cms_orcoff_int', dbreader_user_name),
485  'oraint_W': lambda: _getCMSOracleSQLAlchemyConnectionString('cms_orcoff_int', dbwriter_user_name),
486  'oradev_R': lambda: _getCMSOracleSQLAlchemyConnectionString('cms_orcoff_prep', dbreader_user_name),
487  'oradev_W': lambda: _getCMSOracleSQLAlchemyConnectionString('cms_orcoff_prep', devdbwriter_user_name),
488 
489  'onlineorapro_R': lambda: _getCMSOracleSQLAlchemyConnectionString('cms_orcon_prod', dbreader_user_name),
490  'onlineorapro_W': lambda: _getCMSOracleSQLAlchemyConnectionString('cms_orcon_prod', dbwriter_user_name),
491  'onlineoraint_R': lambda: _getCMSOracleSQLAlchemyConnectionString('cmsintr_lb', dbreader_user_name),
492  'onlineoraint_W': lambda: _getCMSOracleSQLAlchemyConnectionString('cmsintr_lb', dbwriter_user_name),
493  }
494 
495  key = database + ('_R' if read_only else '_W')
496  if key in mapping:
497  database = mapping[key]()
498 
499  logging.debug('connection string set to "%s"' % database)
500 
501  try:
502  url = sqlalchemy.engine.url.make_url(database)
503  except sqlalchemy.exc.ArgumentError:
504  url = sqlalchemy.engine.url.make_url('sqlite:///%s' % database)
505  return url
def make_url
Definition: conddblib.py:468
def _getCMSOracleSQLAlchemyConnectionString
Definition: conddblib.py:462
def _getCMSFrontierSQLAlchemyConnectionString
Definition: conddblib.py:457

Variable Documentation

string conddblib.__author__ = 'Miguel Ojeda'

Definition at line 4 of file conddblib.py.

string conddblib.__copyright__ = 'Copyright 2013, CERN'

Definition at line 5 of file conddblib.py.

list conddblib.__credits__ = ['Giacomo Govi', 'Miguel Ojeda', 'Andreas Pfeiffer']

Definition at line 6 of file conddblib.py.

string conddblib.__email__ = 'mojedasa@cern.ch'

Definition at line 9 of file conddblib.py.

string conddblib.__license__ = 'Unknown'

Definition at line 7 of file conddblib.py.

string conddblib.__maintainer__ = 'Miguel Ojeda'

Definition at line 8 of file conddblib.py.

tuple conddblib._Base = sqlalchemy.ext.declarative.declarative_base()

Definition at line 175 of file conddblib.py.

string conddblib.authPathEnvVar = 'COND_AUTH_PATH'

Definition at line 19 of file conddblib.py.

string conddblib.contact_help = 'If you need assistance, please write an email to %s and %s. If you need immediate/urgent assistance, you can call the Offline DB expert on call (%s).'

Definition at line 83 of file conddblib.py.

string conddblib.database_help

Definition at line 84 of file conddblib.py.

dictionary conddblib.db_models = {}

Definition at line 183 of file conddblib.py.

string conddblib.dbreader_user_name = 'cms_cond_general_r'

Definition at line 22 of file conddblib.py.

string conddblib.dbuser_name = 'cms_conditions'

Definition at line 21 of file conddblib.py.

string conddblib.dbwriter_user_name = 'cms_cond_general_w'

Definition at line 23 of file conddblib.py.

int conddblib.description_length = 4000

Definition at line 76 of file conddblib.py.

string conddblib.devdbwriter_user_name = 'cms_test_conditions'

Definition at line 24 of file conddblib.py.

string conddblib.empty_label = '-'

Definition at line 73 of file conddblib.py.

tuple conddblib.hash_length = len(hash(''))

Definition at line 77 of file conddblib.py.

tuple conddblib.logger = logging.getLogger(__name__)

Definition at line 25 of file conddblib.py.

Referenced by cscdqm::Configuration.printStats().

int conddblib.name_length = 100

Definition at line 75 of file conddblib.py.

string conddblib.offline_db_experts_email = 'cms-offlinedb-exp@cern.ch'

Definition at line 80 of file conddblib.py.

string conddblib.offline_db_experts_phone = '+41 22 76 70817, or 70817 from CERN; check https://twiki.cern.ch/twiki/bin/viewauth/CMS/DBShifterHelpPage if it does not work; availability depends on the state of the LHC'

Definition at line 81 of file conddblib.py.

dictionary conddblib.ora_types = {}

Definition at line 184 of file conddblib.py.

string conddblib.schema_name = 'CMS_CONDITIONS'

Definition at line 20 of file conddblib.py.

dictionary conddblib.sqlite_types = {}

Definition at line 185 of file conddblib.py.

string conddblib.web_experts_email = 'cms-cond-dev@cern.ch'

Definition at line 79 of file conddblib.py.