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 538 of file conddblib.py.

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

Referenced by listObject().

539 def _exists(session, primary_key, value):
540  ret = None
541  try:
542  ret = session.query(primary_key).\
543  filter(primary_key == value).\
544  count() != 0
545  except sqlalchemy.exc.OperationalError:
546  pass
547 
548  return ret
def _exists
Definition: conddblib.py:538
def conddblib._getCMSFrontierConnectionString (   database)
private

Definition at line 437 of file conddblib.py.

References communicate().

Referenced by _getCMSFrontierSQLAlchemyConnectionString().

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

Definition at line 442 of file conddblib.py.

References _getCMSFrontierConnectionString().

Referenced by make_url().

443 def _getCMSFrontierSQLAlchemyConnectionString(database, schema = 'cms_conditions'):
444  import urllib
445  return 'oracle+frontier://@%s/%s' % (urllib.quote_plus(_getCMSFrontierConnectionString(database)), schema)
446 
def _getCMSFrontierConnectionString
Definition: conddblib.py:437
def _getCMSFrontierSQLAlchemyConnectionString
Definition: conddblib.py:442
def conddblib._getCMSOracleSQLAlchemyConnectionString (   database,
  schema = 'cms_conditions' 
)
private

Definition at line 447 of file conddblib.py.

Referenced by make_url().

448 def _getCMSOracleSQLAlchemyConnectionString(database, schema = 'cms_conditions'):
449  return 'oracle://%s@%s' % (schema, database)
450 
451 
452 # Entry point
def _getCMSOracleSQLAlchemyConnectionString
Definition: conddblib.py:447
def conddblib._inserted_before (   timestamp)
private
To be used inside filter().

Definition at line 549 of file conddblib.py.

Referenced by listObject().

550 def _inserted_before(timestamp):
551  '''To be used inside filter().
552  '''
553 
554  if timestamp is None:
555  # XXX: Returning None does not get optimized (skipped) by SQLAlchemy,
556  # and returning True does not work in Oracle (generates "and 1"
557  # which breaks Oracle but not SQLite). For the moment just use
558  # this dummy condition.
559  return sqlalchemy.literal(True) == sqlalchemy.literal(True)
560 
561  return conddb.IOV.insertion_time <= _parse_timestamp(timestamp)
def _inserted_before
Definition: conddblib.py:549
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 491 of file conddblib.py.

492 def connect(url, init=False, authPath=None, verbose=0):
493  '''Returns a Connection instance to the CMS Condition DB.
494 
495  See database_help for the description of the database parameter.
496 
497  The verbosity level is as follows:
498 
499  0 = No output (default).
500  1 = SQL statements issued, including their parameters.
501  2 = In addition, results of the queries (all rows and the column headers).
502  '''
503 
504  if url.drivername == 'oracle' and url.password is None:
505  if authPath is None:
506  if authPathEnvVar in os.environ:
507  authPath = os.environ[authPathEnvVar]
508  authFile = None
509  if authPath is not None:
510  authFile = os.path.join(authPath,'.netrc')
511  if authFile is not None:
512  entryKey = url.host+"/"+url.username
513  logging.debug('Looking up credentials for %s in file %s ' %(entryKey,authFile) )
514  import netrc
515  try:
516  # Try to find the netrc entry
517  (username, account, password) = netrc.netrc( authFile ).authenticators(entryKey)
518  url.password = password
519  except IOError as e:
520  logging.error('.netrc file expected in %s has not been found or cannot be open.' %authPath)
521  raise e
522  except TypeError as e:
523  logging.error('The .netrc file in %s is invalid, or the targeted entry has not been found.' %authPath)
524  except Exception as e:
525  logging.error('Problem with .netrc file in %s: %s' %(authPath,str(e)))
526  else:
527  import getpass
528  url.password = getpass.getpass('Password for %s: ' % str(url))
529 
530  if verbose >= 1:
531  logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO)
532 
533  if verbose >= 2:
534  logging.getLogger('sqlalchemy.engine').setLevel(logging.DEBUG)
535 
536  return Connection(url, init=init)
537 
def connect
Definition: conddblib.py:491
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 601 of file conddblib.py.

References alcazmumu_cfi.filter.

602 def getPayload(session, hash):
603  # get payload from DB:
604  data, payloadType = session.query(Payload.data, Payload.object_type).filter(Payload.hash == hash).one()
605  return data
def getPayload
Definition: conddblib.py:601
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 562 of file conddblib.py.

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

563 def listObject(session, name, snapshot=None):
564 
565  is_tag = _exists(session, Tag.name, name)
566  result = {}
567  if is_tag:
568  result['type'] = 'Tag'
569  result['name'] = session.query(Tag).get(name).name
570  result['timeType'] = session.query(Tag.time_type).\
571  filter(Tag.name == name).\
572  scalar()
573 
574  result['iovs'] = session.query(IOV.since, IOV.insertion_time, IOV.payload_hash, Payload.object_type).\
575  join(IOV.payload).\
576  filter(
577  IOV.tag_name == name,
578  _inserted_before(snapshot),
579  ).\
580  order_by(IOV.since.desc(), IOV.insertion_time.desc()).\
581  from_self().\
582  order_by(IOV.since, IOV.insertion_time).\
583  all()
584 
585  try:
586  is_global_tag = _exists(session, GlobalTag.name, name)
587  if is_global_tag:
588  result['type'] = 'GlobalTag'
589  result['name'] = session.query(GlobalTag).get(name)
590  result['tags'] = session.query(GlobalTagMap.record, GlobalTagMap.label, GlobalTagMap.tag_name).\
591  filter(GlobalTagMap.global_tag_name == name).\
592  order_by(GlobalTagMap.record, GlobalTagMap.label).\
593  all()
594  except sqlalchemy.exc.OperationalError:
595  sys.stderr.write("No table for GlobalTags found in DB.\n\n")
596 
597  if not is_tag and not is_global_tag:
598  raise Exception('There is no tag or global tag named %s in the database.' % name)
599 
600  return result
def _exists
Definition: conddblib.py:538
def _inserted_before
Definition: conddblib.py:549
def listObject
Definition: conddblib.py:562
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 453 of file conddblib.py.

References _getCMSFrontierSQLAlchemyConnectionString(), and _getCMSOracleSQLAlchemyConnectionString().

454 def make_url(database='pro',read_only = True):
455 
456  #schema = 'cms_conditions' # set the default
457  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
458  database, schema = database.split(':')
459 
460  # Lazy in order to avoid calls to cmsGetFnConnect
461  mapping = {
462  'pro_R': lambda: _getCMSFrontierSQLAlchemyConnectionString('PromptProd', schema_name),
463  'arc_R': lambda: _getCMSFrontierSQLAlchemyConnectionString('FrontierArc', schema_name),
464  'int_R': lambda: _getCMSFrontierSQLAlchemyConnectionString('FrontierInt', schema_name),
465  'dev_R': lambda: _getCMSFrontierSQLAlchemyConnectionString('FrontierPrep', schema_name),
466 
467  'orapro_R': lambda: _getCMSOracleSQLAlchemyConnectionString('cms_orcon_adg', dbreader_user_name),
468  'oraarc_R': lambda: _getCMSOracleSQLAlchemyConnectionString('cmsarc_lb', dbreader_user_name),
469  'oraint_R': lambda: _getCMSOracleSQLAlchemyConnectionString('cms_orcoff_int', dbreader_user_name),
470  'oraint_W': lambda: _getCMSOracleSQLAlchemyConnectionString('cms_orcoff_int', dbwriter_user_name),
471  'oradev_R': lambda: _getCMSOracleSQLAlchemyConnectionString('cms_orcoff_prep', dbreader_user_name),
472  'oradev_W': lambda: _getCMSOracleSQLAlchemyConnectionString('cms_orcoff_prep', devdbwriter_user_name),
473 
474  'onlineorapro_R': lambda: _getCMSOracleSQLAlchemyConnectionString('cms_orcon_prod', dbreader_user_name),
475  'onlineorapro_W': lambda: _getCMSOracleSQLAlchemyConnectionString('cms_orcon_prod', dbwriter_user_name),
476  'onlineoraint_R': lambda: _getCMSOracleSQLAlchemyConnectionString('cmsintr_lb', dbreader_user_name),
477  'onlineoraint_W': lambda: _getCMSOracleSQLAlchemyConnectionString('cmsintr_lb', dbwriter_user_name),
478  }
479 
480  key = database + ('_R' if read_only else '_W')
481  if key in mapping:
482  database = mapping[key]()
483 
484  logging.debug('connection string set to "%s"' % database)
485 
486  try:
487  url = sqlalchemy.engine.url.make_url(database)
488  except sqlalchemy.exc.ArgumentError:
489  url = sqlalchemy.engine.url.make_url('sqlite:///%s' % database)
490  return url
def make_url
Definition: conddblib.py:453
def _getCMSOracleSQLAlchemyConnectionString
Definition: conddblib.py:447
def _getCMSFrontierSQLAlchemyConnectionString
Definition: conddblib.py:442

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.