Example #1
0
//--- Command main program-----------------------------------------------------
int main ( int argc, char** argv )
//-----------------------------------------------------------------------------
{
  fs::path pwd = fs::initial_path();
  fs::path out;
  Strings_t libs;
  std::string pkgName;
  std::string userModule;

  // declare a group of options that will be allowed only on command line
  po::options_description generic("Generic options");
  generic.add_options()
    ("help,h",
     "produce this help message")
    ("package-name,p",
     po::value<string>(),
     "name of the package for which we create the configurables file")
    ("input-libraries,i",
     po::value<string>(),
     "libraries to extract the component configurables from")
    ("input-cfg,c",
     po::value<string>(),
     "path to the cfg file holding the description of the Configurable base "
     "classes, the python module holding the Configurable definitions, etc...")
    ("output-dir,o",
     po::value<string>()->default_value("../genConf"),
     "output directory for genconf files.")
    ("debug-level,d",
     po::value<int>()->default_value(0),
     "debug level")
    ("load-library,l",
     po::value< Strings_t >()->composing(),
     "preloading library")
    ("user-module,m",
     po::value<string>(),
     "user-defined module to be imported by the genConf-generated one")
    ;

  // declare a group of options that will be allowed both on command line
  // _and_ in configuration file
  po::options_description config("Configuration");
  config.add_options()
    ("configurable-module",
     po::value<string>()->default_value("AthenaCommon"),
     "Name of the module holding the configurable classes")
    ("configurable-default-name",
     po::value<string>()->default_value("Configurable.DefaultName"),
     "Default name for the configurable instance")
    ("configurable-algorithm",
     po::value<string>()->default_value("ConfigurableAlgorithm"),
     "Name of the configurable base class for Algorithm components")
    ("configurable-algtool",
     po::value<string>()->default_value("ConfigurableAlgTool"),
     "Name of the configurable base class for AlgTool components")
    ("configurable-auditor",
     po::value<string>()->default_value("ConfigurableAuditor"),
     "Name of the configurable base class for Auditor components")
    ("configurable-service",
     po::value<string>()->default_value("ConfigurableService"),
     "Name of the configurable base class for Service components")
    ;

  po::options_description cmdline_options;
  cmdline_options.add(generic).add(config);

  po::options_description config_file_options;
  config_file_options.add(config);

  po::options_description visible("Allowed options");
  visible.add(generic).add(config);

  po::variables_map vm;

  try {
    po::store( po::command_line_parser(argc, argv).
	       options(cmdline_options).run(),
	       vm );

    po::notify(vm);

    // try to read configuration from the optionally given configuration file
    if( vm.count("input-cfg") ) {
      string cfgFileName = vm["input-cfg"].as<string>();
      cfgFileName = fs::system_complete( fs::path( cfgFileName ) ).string();
      std::ifstream ifs( cfgFileName.c_str() );
      po::store( parse_config_file( ifs, config_file_options ), vm );
    }

    po::notify(vm);
  }
  catch ( po::error& err ) {
    cout << "ERR0R: error detected while parsing command options: "<< err.what() << endl;
    return EXIT_FAILURE;
  }

  //--- Process command options -----------------------------------------------
  if( vm.count("help")) {
    cout << visible << endl;
    return EXIT_FAILURE;
  }

  if( vm.count("package-name") ) {
    pkgName = vm["package-name"].as<string>();
  }
  else {
    cout << "ERROR: 'package-name' required" << endl;
    cout << visible << endl;
    return EXIT_FAILURE;
  }

  if( vm.count("user-module") ) {
    userModule = vm["user-module"].as<string>();
    cout << "INFO: will import user module " << userModule << endl; 
  }

  if( vm.count("input-libraries") ) {
    // re-shape the input arguments:
    //  - removes spurious spaces,
    //  - split into tokens.
    Strings_t inputLibs;
    {
      string tmp = vm["input-libraries"].as<string>();
      boost::trim(tmp);
      boost::split( inputLibs, tmp,
		    boost::is_any_of(" "),
		    boost::token_compress_on );
    }

    //
    libs.reserve( inputLibs.size() );
    for ( Strings_t::const_iterator iLib = inputLibs.begin();
	  iLib != inputLibs.end();
	  ++iLib ) {
      std::string lib = fs::path(*iLib).stem().string();
      if ( 0 == lib.find("lib") ) {
        lib = lib.substr(3); // For *NIX remove "lib"
      }
      // remove duplicates
      if ( !lib.empty() &&
          std::find( libs.begin(), libs.end(), lib ) == libs.end() ) {
        libs.push_back( lib );
      }
    } //> end loop over input-libraries
    if ( libs.empty() ) {
      cout << "ERROR: input component library(ies) required !\n"
           << "ERROR: 'input-libraries' argument was ["
           << vm["input-libraries"].as<string>()
           << "]"
           << endl;
      return EXIT_FAILURE;
    }
  }
  else {
    cout << "ERROR: input component library(ies) required" << endl;
    cout << visible << endl;
    return EXIT_FAILURE;
  }

  if( vm.count("output-dir") ) {
    out = fs::system_complete( fs::path( vm["output-dir"].as<string>() ) );
  }

  if ( vm.count("debug-level") ) {
    PluginService::SetDebug( vm["debug-level"].as<int>() );
  }

  if ( vm.count("load-library") ) {
    Strings_t lLib_list = vm["load-library"].as< Strings_t >();
    for (Strings_t::const_iterator lLib=lLib_list.begin();
	 lLib != lLib_list.end();
	 ++lLib) {
      // load done through ROOT::Reflex helper class
      SharedLibrary tmplib(*lLib) ;
      tmplib.Load() ;
    }
  }


  if ( !fs::exists( out ) ) {
    try {
      fs::create_directory(out);
    }
    catch ( fs::filesystem_error &err ) {
      cout << "ERR0R: error creating directory: "<< err.what() << endl;
      return EXIT_FAILURE;
    }
  }

  cout << ":::::: libraries : [ ";
  copy( libs.begin(), libs.end(), ostream_iterator<string>(cout, " ") );
  cout << "] ::::::" << endl;

  configGenerator py( pkgName, out.string() );
  py.setConfigurableModule     (vm["configurable-module"].as<string>());
  py.setConfigurableDefaultName(vm["configurable-default-name"].as<string>());
  py.setConfigurableAlgorithm  (vm["configurable-algorithm"].as<string>());
  py.setConfigurableAlgTool    (vm["configurable-algtool"].as<string>());
  py.setConfigurableAuditor    (vm["configurable-auditor"].as<string>());
  py.setConfigurableService    (vm["configurable-service"].as<string>());

  int sc = EXIT_FAILURE;
  try {
    sc = py.genConfig( libs, userModule );
  }
  catch ( exception& e ) {
    cout << "ERROR: Could not generate Configurable(s) !\n"
         << "ERROR: Got exception: " << e.what() << endl;
    return EXIT_FAILURE;
  }

  if ( EXIT_SUCCESS == sc ) {
    // create an empty __init__.py file in the output dir
    fstream initPy( ( out / fs::path( "__init__.py" ) ).string().c_str(),
        std::ios_base::out|std::ios_base::trunc );
    initPy << "## Hook for " << pkgName << " genConf module\n" << flush;
  }

  cout << ":::::: libraries : [ ";
  copy( libs.begin(), libs.end(), ostream_iterator<string>(cout, " ") );
  cout << "] :::::: [DONE]" << endl;

  return sc;
}