CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Utils.h
Go to the documentation of this file.
1 #ifndef CondCore_CondDB_Utils_h
2 #define CondCore_CondDB_Utils_h
3 
5 //
6 #include <string>
7 #include <cxxabi.h>
8 #include <algorithm>
9 #include <iostream>
10 #include <tuple>
11 //
12 #include <boost/regex.hpp>
13 
14 namespace cond {
15 
16  namespace {
17 
18  inline std::string demangledName( const std::type_info& typeInfo ){
19  int status = 0;
20  std::string ret("");
21  char* realname = abi::__cxa_demangle( typeInfo.name(), 0, 0, &status);
22  if( status == 0 && realname ){
23  ret = realname;
24  free(realname);
25  }
26  // clean up the spaces... ( to be removed after getting rid of reflex - that does not have spaces...)
27  ret.erase( std::remove( ret.begin(), ret.end(), ' ' ), ret.end() );
28  return ret;
29  }
30 
31  inline std::string currentCMSSWVersion(){
32  std::string version("");
33  const char* envVersion = ::getenv( "CMSSW_VERSION" );
34  if(envVersion){
35  version += envVersion;
36  }
37  return version;
38  }
39 
40  inline std::string currentArchitecture(){
41  std::string arch("");
42  const char* archEnv = ::getenv( "SCRAM_ARCH" );
43  if(archEnv){
44  arch += archEnv;
45  }
46  return arch;
47  }
48 
49  }
50 
51  namespace persistency {
52 
54  size_t techEnd = connectionString.find( ':' );
55  if( techEnd == std::string::npos ) throwException( "Could not resolve the connection protocol on "+connectionString+".",
56  "getConnectionProtocol" );
57  std::string technology = connectionString.substr(0,techEnd);
58  return technology;
59  }
60 
61  inline std::tuple<std::string,std::string,std::string> parseConnectionString( const std::string& connectionString ){
62  std::string protocol = getConnectionProtocol( connectionString );
64  std::string databaseName("");
65  if( protocol == "sqlite" || protocol == "sqlite_file" || protocol == "sqlite_fip" ){
66  databaseName = connectionString.substr( protocol.size()+1 );
67  } else if ( protocol == "oracle" || protocol == "frontier" ){
68  size_t ptr = protocol.size()+1;
69  if( connectionString.substr( ptr,2 )!="//" ) throwException( "Connection string "+connectionString+
70  " is invalid format for technology \""+
71  protocol+"\".","parseConnectionString" );
72  ptr += 2;
73  size_t serviceEnd = connectionString.find( '/', ptr );
74  if( serviceEnd == std::string::npos ) throwException( "Connection string "+connectionString+" is invalid.",
75  "parseConnectionString" );
76  serviceName = connectionString.substr( ptr, serviceEnd-ptr );
77  ptr = serviceEnd+1;
78  databaseName = connectionString.substr( ptr );
79  } else throwException( "Technology "+protocol+" is not known.","parseConnectionString" );
80 
81  return std::make_tuple( protocol, serviceName, databaseName );
82  }
83 
85 
86  // leave the connection string unmodified for sqlite
87  if( input.find("sqlite") == 0 || input.find("oracle") == 0) return input;
88 
89  //static const boost::regex trivial("oracle://(cms_orcon_adg|cms_orcoff_prep)/([_[:alnum:]]+?)");
90  static const boost::regex short_frontier("frontier://([[:alnum:]]+?)/([_[:alnum:]]+?)");
91  static const boost::regex long_frontier("frontier://((\\([-[:alnum:]]+?=[^\\)]+?\\))+)/([_[:alnum:]]+?)");
92  static const boost::regex long_frontier_serverurl("\\(serverurl=[^\\)]+?/([[:alnum:]]+?)\\)");
93 
94  static const std::map<std::string, std::string> frontierMap = {
95  {"PromptProd", "cms_orcon_adg"},
96  {"FrontierProd", "cms_orcon_adg"},
97  {"FrontierArc", "cms_orcon_adg"},
98  {"FrontierOnProd", "cms_orcon_adg"},
99  {"FrontierPrep", "cms_orcoff_prep"},
100  };
101 
102  boost::smatch matches;
103 
104  static const std::string technology("oracle://");
105  std::string service("");
106  std::string account("");
107 
108  bool match = false;
109  if (boost::regex_match(input, matches, short_frontier)){
110  service = matches[1];
111  account = matches[2];
112  match = true;
113  }
114 
115  if (boost::regex_match(input, matches, long_frontier)) {
116  std::string frontier_config(matches[1]);
117  boost::smatch matches2;
118  if (not boost::regex_search(frontier_config, matches2, long_frontier_serverurl))
119  throwException("No serverurl in matched long frontier","convertoToOracleConnection");
120  service = matches2[1];
121  account = matches[3];
122  match = true;
123  }
124 
125  if( !match ) throwException("Connection string "+input+" can't be converted to oracle connection.","convertoToOracleConnection");
126 
127  if( service == "FrontierArc" ){
128  size_t len = account.size()-5;
129  account = account.substr(0,len);
130  }
131 
132  auto it = frontierMap.find( service );
133  if( it == frontierMap.end() ) throwException("Connection string can't be converted.","convertoToOracleConnection");
134  service = it->second;
135 
136  return technology+service+"/"+account;
137  }
138 
139  }
140 
141 }
142 
143 #endif
std::string getConnectionProtocol(const std::string &connectionString)
Definition: Utils.h:53
list account
Definition: dbtoconf.py:80
static const std::string serviceName
static std::string const input
Definition: EdmProvDump.cc:43
std::string demangledName(const std::type_info &typeInfo)
Definition: ClassUtils.cc:159
std::tuple< std::string, std::string, std::string > parseConnectionString(const std::string &connectionString)
Definition: Utils.h:61
std::string convertoToOracleConnection(const std::string &input)
Definition: Utils.h:84
string connectionString
Definition: autoCondHLT.py:4
std::pair< typename Association::data_type::first_type, double > match(Reference key, Association association, bool bestMatchByMaxValue)
Generic matching function.
Definition: Utils.h:10
tuple status
Definition: ntuplemaker.py:245
void throwException(const std::string &message, const std::string &methodName)
Definition: Exception.cc:11