genconf.cpp File Reference
#include "boost/program_options.hpp"
#include "boost/filesystem/operations.hpp"
#include "boost/filesystem/exception.hpp"
#include "boost/filesystem/convenience.hpp"
#include "boost/algorithm/string/split.hpp"
#include "boost/algorithm/string/classification.hpp"
#include "boost/algorithm/string/trim.hpp"
#include "boost/algorithm/string/case_conv.hpp"
#include "boost/algorithm/string/replace.hpp"
#include "boost/format.hpp"
#include "boost/regex.hpp"
#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/utility/setup/console.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include "GaudiKernel/System.h"
#include "GaudiKernel/ISvcLocator.h"
#include "GaudiKernel/IProperty.h"
#include "GaudiKernel/IAppMgrUI.h"
#include "GaudiKernel/IAlgorithm.h"
#include "GaudiKernel/IAlgTool.h"
#include "GaudiKernel/IAuditor.h"
#include "GaudiKernel/Service.h"
#include "GaudiKernel/Bootstrap.h"
#include "GaudiKernel/SmartIF.h"
#include "GaudiKernel/HashMap.h"
#include "GaudiKernel/GaudiHandle.h"
#include "GaudiKernel/DataObjectDescriptor.h"
#include "GaudiKernel/Auditor.h"
#include "GaudiKernel/AlgTool.h"
#include "GaudiKernel/Algorithm.h"
#include "GaudiKernel/Time.h"
#include <Gaudi/PluginService.h>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <sstream>
#include <exception>
#include <set>
#include <vector>
#include "DsoUtils.h"
#include "GaudiKernel/IMessageSvc.h"
Include dependency graph for genconf.cpp:

Go to the source code of this file.

Classes

class  configGenerator
 

Macros

#define LOG_ERROR   BOOST_LOG_TRIVIAL(error)
 
#define LOG_WARNING   BOOST_LOG_TRIVIAL(warning)
 
#define LOG_INFO   BOOST_LOG_TRIVIAL(info)
 
#define LOG_DEBUG   BOOST_LOG_TRIVIAL(debug)
 

Typedefs

typedef std::vector< std::string > Strings_t
 
typedef std::vector< fs::path > LibPathNames_t
 

Functions

int createAppMgr ()
 
void init_logging (boost::log::trivial::severity_level level)
 
int main (int argc, char **argv)
 

Macro Definition Documentation

#define LOG_DEBUG   BOOST_LOG_TRIVIAL(debug)

Definition at line 85 of file genconf.cpp.

#define LOG_ERROR   BOOST_LOG_TRIVIAL(error)

Definition at line 82 of file genconf.cpp.

#define LOG_INFO   BOOST_LOG_TRIVIAL(info)

Definition at line 84 of file genconf.cpp.

#define LOG_WARNING   BOOST_LOG_TRIVIAL(warning)

Definition at line 83 of file genconf.cpp.

Typedef Documentation

typedef std::vector<fs::path> LibPathNames_t

Definition at line 93 of file genconf.cpp.

typedef std::vector<std::string> Strings_t

Definition at line 89 of file genconf.cpp.

Function Documentation

int createAppMgr ( )

Definition at line 930 of file genconf.cpp.

932 {
934  SmartIF<IAppMgrUI> appUI ( iface );
935  auto propMgr = appUI.as<IProperty>();
936 
937  if ( !propMgr || !appUI ) return EXIT_FAILURE;
938  propMgr->setProperty( "JobOptionsType", "NONE" ); // No job options
939  propMgr->setProperty( "AppName", ""); // No initial printout message
940  propMgr->setProperty( "OutputLevel", "7"); // No other printout messages
941  appUI->configure();
943  msgSvc->setProperty("setWarning", "['DefaultName', 'PropertyMgr']");
944  msgSvc->setProperty("Format", "%T %0W%M");
945  return EXIT_SUCCESS;
946 }
Definition of the basic interface.
Definition: IInterface.h:234
virtual StatusCode setProperty(const Property &p)=0
Set the property by property.
The IProperty is the basic interface for all components which have properties that can be set or get...
Definition: IProperty.h:21
GAUDI_API IAppMgrUI * createApplicationMgr(const std::string &dllname, const std::string &factname)
void init_logging ( boost::log::trivial::severity_level  level)

Definition at line 215 of file genconf.cpp.

216 {
217  namespace logging = boost::log;
218  namespace keywords = boost::log::keywords;
219  namespace expr = boost::log::expressions;
220 
221  logging::add_console_log
222  (
223  std::cout,
224  keywords::format = (
225  expr::stream
226  << "[" << std::setw(7) << std::left
227  << logging::trivial::severity
228  << "] " << expr::smessage
229  )
230  );
231 
232  logging::core::get()->set_filter
233  (
234  logging::trivial::severity >= level
235  );
236 
237 }
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:119
int main ( int  argc,
char **  argv 
)

Definition at line 241 of file genconf.cpp.

243 {
244  init_logging((System::isEnvSet("VERBOSE") && !System::getEnv("VERBOSE").empty())
245  ? boost::log::trivial::info
246  : boost::log::trivial::warning);
247 
248  fs::path pwd = fs::initial_path();
249  fs::path out;
250  Strings_t libs;
251  std::string pkgName;
252  std::string userModule;
253 
254  // declare a group of options that will be allowed only on command line
255  po::options_description generic("Generic options");
256  generic.add_options()
257  ("help,h",
258  "produce this help message")
259  ("package-name,p",
260  po::value<string>(),
261  "name of the package for which we create the configurables file")
262  ("input-libraries,i",
263  po::value<string>(),
264  "libraries to extract the component configurables from")
265  ("input-cfg,c",
266  po::value<string>(),
267  "path to the cfg file holding the description of the Configurable base "
268  "classes, the python module holding the Configurable definitions, etc...")
269  ("output-dir,o",
270  po::value<string>()->default_value("../genConf"),
271  "output directory for genconf files.")
272  ("debug-level,d",
273  po::value<int>()->default_value(0),
274  "debug level")
275  ("load-library,l",
276  po::value< Strings_t >()->composing(),
277  "preloading library")
278  ("user-module,m",
279  po::value<string>(),
280  "user-defined module to be imported by the genConf-generated one")
281  ("no-init",
282  "do not generate the (empty) __init__.py")
283  ;
284 
285  // declare a group of options that will be allowed both on command line
286  // _and_ in configuration file
287  po::options_description config("Configuration");
288  config.add_options()
289  ("configurable-module",
290  po::value<string>()->default_value("AthenaCommon"),
291  "Name of the module holding the configurable classes")
292  ("configurable-default-name",
293  po::value<string>()->default_value("Configurable.DefaultName"),
294  "Default name for the configurable instance")
295  ("configurable-algorithm",
296  po::value<string>()->default_value("ConfigurableAlgorithm"),
297  "Name of the configurable base class for Algorithm components")
298  ("configurable-algtool",
299  po::value<string>()->default_value("ConfigurableAlgTool"),
300  "Name of the configurable base class for AlgTool components")
301  ("configurable-auditor",
302  po::value<string>()->default_value("ConfigurableAuditor"),
303  "Name of the configurable base class for Auditor components")
304  ("configurable-service",
305  po::value<string>()->default_value("ConfigurableService"),
306  "Name of the configurable base class for Service components")
307  ;
308 
309  po::options_description cmdline_options;
310  cmdline_options.add(generic).add(config);
311 
312  po::options_description config_file_options;
313  config_file_options.add(config);
314 
315  po::options_description visible("Allowed options");
316  visible.add(generic).add(config);
317 
318  po::variables_map vm;
319 
320  try {
321  po::store( po::command_line_parser(argc, argv).
322  options(cmdline_options).run(),
323  vm );
324 
325  po::notify(vm);
326 
327  // try to read configuration from the optionally given configuration file
328  if( vm.count("input-cfg") ) {
329  string cfgFileName = vm["input-cfg"].as<string>();
330  cfgFileName = fs::system_complete( fs::path( cfgFileName ) ).string();
331  std::ifstream ifs( cfgFileName );
332  po::store( parse_config_file( ifs, config_file_options ), vm );
333  }
334 
335  po::notify(vm);
336  }
337  catch ( po::error& err ) {
338  LOG_ERROR
339  << "error detected while parsing command options: "
340  << err.what();
341  return EXIT_FAILURE;
342  }
343 
344  //--- Process command options -----------------------------------------------
345  if( vm.count("help")) {
346  cout << visible << endl;
347  return EXIT_FAILURE;
348  }
349 
350  if( vm.count("package-name") ) {
351  pkgName = vm["package-name"].as<string>();
352  }
353  else {
354  LOG_ERROR << "'package-name' required";
355  cout << visible << endl;
356  return EXIT_FAILURE;
357  }
358 
359  if( vm.count("user-module") ) {
360  userModule = vm["user-module"].as<string>();
361  LOG_INFO << "INFO: will import user module " << userModule;
362  }
363 
364  if( vm.count("input-libraries") ) {
365  // re-shape the input arguments:
366  // - removes spurious spaces,
367  // - split into tokens.
368  Strings_t inputLibs;
369  {
370  string tmp = vm["input-libraries"].as<string>();
371  boost::trim(tmp);
372  boost::split( inputLibs, tmp,
373  boost::is_any_of(" "),
374  boost::token_compress_on );
375  }
376 
377  //
378  libs.reserve( inputLibs.size() );
379  for ( Strings_t::const_iterator iLib = inputLibs.begin();
380  iLib != inputLibs.end();
381  ++iLib ) {
382  std::string lib = fs::path(*iLib).stem().string();
383  if ( 0 == lib.find("lib") ) {
384  lib = lib.substr(3); // For *NIX remove "lib"
385  }
386  // remove duplicates
387  if ( !lib.empty() &&
388  std::find( libs.begin(), libs.end(), lib ) == libs.end() ) {
389  libs.push_back( lib );
390  }
391  } //> end loop over input-libraries
392  if ( libs.empty() ) {
393  LOG_ERROR << "input component library(ies) required !\n";
394  LOG_ERROR << "'input-libraries' argument was ["
395  << vm["input-libraries"].as<string>()
396  << "]";
397  return EXIT_FAILURE;
398  }
399  }
400  else {
401  LOG_ERROR << "input component library(ies) required";
402  cout << visible << endl;
403  return EXIT_FAILURE;
404  }
405 
406  if( vm.count("output-dir") ) {
407  out = fs::system_complete( fs::path( vm["output-dir"].as<string>() ) );
408  }
409 
410  if ( vm.count("debug-level") ) {
411  Gaudi::PluginService::SetDebug( vm["debug-level"].as<int>() );
412  }
413 
414  if ( vm.count("load-library") ) {
415  Strings_t lLib_list = vm["load-library"].as< Strings_t >();
416  for (Strings_t::const_iterator lLib=lLib_list.begin();
417  lLib != lLib_list.end();
418  ++lLib) {
419  // load done through Gaudi helper class
420  System::ImageHandle tmp; // we ignore the library handle
421  unsigned long err = System::loadDynamicLib(*lLib, &tmp);
422  if (err != 1) {
423  LOG_WARNING << "failed to load: "<< *lLib;
424  }
425  }
426  }
427 
428 
429  if ( !fs::exists( out ) ) {
430  try {
431  fs::create_directory(out);
432  }
433  catch ( fs::filesystem_error &err ) {
434  LOG_ERROR << "error creating directory: "<< err.what();
435  return EXIT_FAILURE;
436  }
437  }
438 
439  {
440  std::ostringstream msg;
441  msg << ":::::: libraries : [ ";
442  copy( libs.begin(), libs.end(), ostream_iterator<string>(msg, " ") );
443  msg << "] ::::::";
444  LOG_INFO << msg.str();
445  }
446 
447  configGenerator py( pkgName, out.string() );
448  py.setConfigurableModule (vm["configurable-module"].as<string>());
449  py.setConfigurableDefaultName(vm["configurable-default-name"].as<string>());
450  py.setConfigurableAlgorithm (vm["configurable-algorithm"].as<string>());
451  py.setConfigurableAlgTool (vm["configurable-algtool"].as<string>());
452  py.setConfigurableAuditor (vm["configurable-auditor"].as<string>());
453  py.setConfigurableService (vm["configurable-service"].as<string>());
454 
455  int sc = EXIT_FAILURE;
456  try {
457  sc = py.genConfig( libs, userModule );
458  }
459  catch ( exception& e ) {
460  cout << "ERROR: Could not generate Configurable(s) !\n"
461  << "ERROR: Got exception: " << e.what() << endl;
462  return EXIT_FAILURE;
463  }
464 
465  if ( EXIT_SUCCESS == sc && ! vm.count("no-init")) {
466  // create an empty __init__.py file in the output dir
467  fstream initPy( ( out / fs::path( "__init__.py" ) ).string(),
468  std::ios_base::out|std::ios_base::trunc );
469  initPy << "## Hook for " << pkgName << " genConf module\n" << flush;
470  }
471 
472  {
473  std::ostringstream msg;
474  msg << ":::::: libraries : [ ";
475  copy( libs.begin(), libs.end(), ostream_iterator<string>(msg, " ") );
476  msg << "] :::::: [DONE]";
477  LOG_INFO << msg.str();
478  }
479  return sc;
480 }
std::vector< std::string > Strings_t
Definition: genconf.cpp:89
list path
Definition: __init__.py:15
#define LOG_ERROR
Definition: genconf.cpp:82
void * ImageHandle
Definition of an image handle.
Definition: ModuleInfo.h:30
void setConfigurableModule(const std::string &moduleName)
customize the Module name where configurable base classes are defined
Definition: genconf.cpp:153
list argv
Definition: gaudirun.py:227
list options
Definition: gaudirun.py:396
#define LOG_INFO
Definition: genconf.cpp:84
GAUDI_API bool isEnvSet(const char *var)
Check if an environment variable is set or not.
Definition: System.cpp:637
GAUDI_API std::string getEnv(const char *var)
get a particular environment variable (returning "UNKNOWN" if not set)
Definition: System.cpp:617
void init_logging(boost::log::trivial::severity_level level)
Definition: genconf.cpp:215
GAUDIPS_API void SetDebug(int debugLevel)
Backward compatibility with Reflex.
#define LOG_WARNING
Definition: genconf.cpp:83
GAUDI_API unsigned long loadDynamicLib(const std::string &name, ImageHandle *handle)
Load dynamic link library.
Definition: System.cpp:124