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/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 84 of file genconf.cpp.

#define LOG_ERROR   BOOST_LOG_TRIVIAL(error)

Definition at line 81 of file genconf.cpp.

#define LOG_INFO   BOOST_LOG_TRIVIAL(info)

Definition at line 83 of file genconf.cpp.

#define LOG_WARNING   BOOST_LOG_TRIVIAL(warning)

Definition at line 82 of file genconf.cpp.

Typedef Documentation

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

Definition at line 92 of file genconf.cpp.

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

Definition at line 88 of file genconf.cpp.

Function Documentation

int createAppMgr ( )

Definition at line 903 of file genconf.cpp.

905 {
907  SmartIF<IAppMgrUI> appUI ( iface );
908  auto propMgr = appUI.as<IProperty>();
909 
910  if ( !propMgr || !appUI ) return EXIT_FAILURE;
911  propMgr->setProperty( "JobOptionsType", "NONE" ); // No job options
912  propMgr->setProperty( "AppName", ""); // No initial printout message
913  propMgr->setProperty( "OutputLevel", "7"); // No other printout messages
914  appUI->configure();
916  msgSvc->setProperty("setWarning", "['DefaultName', 'PropertyMgr']");
917  msgSvc->setProperty("Format", "%T %0W%M");
918  return EXIT_SUCCESS;
919 }
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 211 of file genconf.cpp.

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

Definition at line 237 of file genconf.cpp.

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