CMS 3D CMS Logo

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 #include <fstream>
12 #include <unistd.h>
13 #include <pwd.h>
14 #include <climits>
15 //
16 #include <boost/regex.hpp>
17 
18 namespace cond {
19 
20  namespace {
21 
22  inline std::string demangledName( const std::type_info& typeInfo ){
23  int status = 0;
24  std::string ret("");
25  char* realname = abi::__cxa_demangle( typeInfo.name(), nullptr, nullptr, &status);
26  if( status == 0 && realname ){
27  ret = realname;
28  free(realname);
29  }
30  // clean up the spaces... ( to be removed after getting rid of reflex - that does not have spaces...)
31  ret.erase( std::remove( ret.begin(), ret.end(), ' ' ), ret.end() );
32  return ret;
33  }
34 
35  inline std::string currentCMSSWVersion(){
36  std::string version("");
37  const char* envVersion = ::getenv( "CMSSW_VERSION" );
38  if(envVersion){
39  version += envVersion;
40  }
41  return version;
42  }
43 
44  inline std::string currentArchitecture(){
45  std::string arch("");
46  const char* archEnv = ::getenv( "SCRAM_ARCH" );
47  if(archEnv){
48  arch += archEnv;
49  }
50  return arch;
51  }
52 
53  inline std::string getUserName(){
54  struct passwd* user_creds = getpwuid(getuid());
55  if (user_creds==NULL) return std::string("USER_NOT_AVAILABLE");
56  return std::string(user_creds->pw_name);
57  }
58 
59  inline std::string getHostName(){
60  char hostname[HOST_NAME_MAX];
61  int retcode = gethostname(hostname,HOST_NAME_MAX);
62  if( retcode ) return "";
63  return std::string(hostname);
64  }
65 
66  inline std::string getCommand(){
67  std::string commName("");
68  try{
69  std::ifstream comm("/proc/self/cmdline");
70  std::getline(comm,commName);
71  size_t ind = commName.find('\0');
72  while( ind != std::string::npos ){
73  commName.replace(ind,1,1,' ');
74  ind = commName.find('\0');
75  }
76  } catch ( std::ifstream::failure const& ){
77  commName = "unknown";
78  }
79  return commName;
80  }
81  }
82 
83  namespace persistency {
84 
85  inline std::string getConnectionProtocol( const std::string& connectionString ){
86  size_t techEnd = connectionString.find( ':' );
87  if( techEnd == std::string::npos ) throwException( "Could not resolve the connection protocol on "+connectionString+".",
88  "getConnectionProtocol" );
89  std::string technology = connectionString.substr(0,techEnd);
90  return technology;
91  }
92 
93  inline std::tuple<std::string,std::string,std::string> parseConnectionString( const std::string& connectionString ){
94  std::string protocol = getConnectionProtocol( connectionString );
96  std::string databaseName("");
97  if( protocol == "sqlite" || protocol == "sqlite_file" || protocol == "sqlite_fip" ){
98  databaseName = connectionString.substr( protocol.size()+1 );
99  } else if ( protocol == "oracle" || protocol == "frontier" ){
100  size_t ptr = protocol.size()+1;
101  if( connectionString.substr( ptr,2 )!="//" ) throwException( "Connection string "+connectionString+
102  " is invalid format for technology \""+
103  protocol+"\".","parseConnectionString" );
104  ptr += 2;
105  size_t serviceEnd = connectionString.find( '/', ptr );
106  if( serviceEnd == std::string::npos ) throwException( "Connection string "+connectionString+" is invalid.",
107  "parseConnectionString" );
108  serviceName = connectionString.substr( ptr, serviceEnd-ptr );
109  ptr = serviceEnd+1;
110  databaseName = connectionString.substr( ptr );
111  } else throwException( "Technology "+protocol+" is not known.","parseConnectionString" );
112 
113  return std::make_tuple( protocol, serviceName, databaseName );
114  }
115 
117 
118  // leave the connection string unmodified for sqlite
119  if( input.find("sqlite") == 0 || input.find("oracle") == 0) return input;
120 
121  //static const boost::regex trivial("oracle://(cms_orcon_adg|cms_orcoff_prep)/([_[:alnum:]]+?)");
122  static const boost::regex short_frontier("frontier://([[:alnum:]]+?)/([_[:alnum:]]+?)");
123  static const boost::regex long_frontier("frontier://((\\([-[:alnum:]]+?=[^\\)]+?\\))+)/([_[:alnum:]]+?)");
124  static const boost::regex long_frontier_serverurl("\\(serverurl=[^\\)]+?/([[:alnum:]]+?)\\)");
125 
126  static const std::map<std::string, std::string> frontierMap = {
127  {"PromptProd", "cms_orcon_adg"},
128  {"FrontierProd", "cms_orcon_adg"},
129  {"FrontierArc", "cms_orcon_adg"},
130  {"FrontierOnProd", "cms_orcon_adg"},
131  {"FrontierPrep", "cms_orcoff_prep"},
132  };
133 
134  boost::smatch matches;
135 
136  static const std::string technology("oracle://");
137  std::string service("");
138  std::string account("");
139 
140  bool match = false;
141  if (boost::regex_match(input, matches, short_frontier)){
142  service = matches[1];
143  account = matches[2];
144  match = true;
145  }
146 
147  if (boost::regex_match(input, matches, long_frontier)) {
148  std::string frontier_config(matches[1]);
149  boost::smatch matches2;
150  if (not boost::regex_search(frontier_config, matches2, long_frontier_serverurl))
151  throwException("No serverurl in matched long frontier","convertoToOracleConnection");
152  service = matches2[1];
153  account = matches[3];
154  match = true;
155  }
156 
157  if( !match ) throwException("Connection string "+input+" can't be converted to oracle connection.","convertoToOracleConnection");
158 
159  if( service == "FrontierArc" ){
160  size_t len = account.size()-5;
161  account = account.substr(0,len);
162  }
163 
164  auto it = frontierMap.find( service );
165  if( it == frontierMap.end() ) throwException("Connection string can't be converted.","convertoToOracleConnection");
166  service = it->second;
167 
168  return technology+service+"/"+account;
169  }
170 
171  }
172 
173 }
174 
175 #endif
std::string getConnectionProtocol(const std::string &connectionString)
Definition: Utils.h:85
#define NULL
Definition: scimark2.h:8
static const std::string serviceName
static std::string const input
Definition: EdmProvDump.cc:48
std::tuple< std::string, std::string, std::string > parseConnectionString(const std::string &connectionString)
Definition: Utils.h:93
std::string convertoToOracleConnection(const std::string &input)
Definition: Utils.h:116
def remove(d, key, TELL=False)
Definition: MatrixUtil.py:212
Definition: plugin.cc:24
#define HOST_NAME_MAX
std::pair< typename Association::data_type::first_type, double > match(Reference key, Association association, bool bestMatchByMaxValue)
Generic matching function.
Definition: Utils.h:10
void throwException(const std::string &message, const std::string &methodName)
Definition: Exception.cc:14