The Gaudi Framework  v36r1 (3e2fb5a8)
JobOptionsSvc.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2021 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 // ============================================================================
12 // Local:
13 // ============================================================================
14 
15 #include "JobOptionsSvc.h"
16 
17 #include "Analyzer.h"
18 #include "Catalog.h"
19 #include "Messages.h"
20 #include "Node.h"
21 #include "PragmaOptions.h"
22 #include "PythonConfig.h"
23 #include "Units.h"
24 // ============================================================================
25 // Gaudi:
26 // ============================================================================
27 #include "GaudiKernel/MsgStream.h"
28 #include "GaudiKernel/System.h"
29 #include <Gaudi/Parsers/Factory.h>
30 
31 #include <algorithm>
32 #include <cctype>
33 
34 #if __cplusplus >= 201703
35 # include <string_view>
36 #else
37 # include <experimental/string_view>
38 namespace std {
39  using experimental::string_view;
40 }
41 #endif
42 
43 // ============================================================================
45 // ============================================================================
46 // Namespace aliases:
47 // ============================================================================
48 namespace gp = Gaudi::Parsers;
49 // ============================================================================
51  if ( System::isEnvSet( "JOBOPTSEARCHPATH" ) ) m_dir_search_path = System::getEnv( "JOBOPTSEARCHPATH" );
52  if ( System::isEnvSet( "JOBOPTSDUMPFILE" ) ) m_dump = System::getEnv( "JOBOPTSDUMPFILE" );
53 
57  for ( const auto& p : m_globalDefaultsProp ) { m_globalDefaults.emplace_back( p.first, p.second ); }
58  } );
59 }
60 // ============================================================================
62  // Call base class initializer
64  // Read the job options if needed
65  if ( sc ) {
66  if ( m_source_type == "NONE" ) {
67  return sc;
68  } else if ( m_source_type == "PYTHON" ) {
69  PythonConfig conf( this );
70  return conf.evaluateConfig( m_source_path, m_pythonParams, m_pythonAction );
71  } else {
73  }
74  }
75  return sc;
76 }
78  if ( m_reportUnused ) {
80  unused.reserve( m_options.size() );
81 
82  for ( const auto& p : m_options ) {
83  if ( !p.second.isBound() ) unused.emplace_back( p.first );
84  }
85 
86  if ( !unused.empty() ) {
87  std::sort( unused.begin(), unused.end() );
88  auto& log = warning();
89  log << unused.size() << " unused properties:";
90  for ( const auto& k : unused ) log << "\n - " << k;
91  log << endmsg;
92  }
93  }
94  return Service::stop();
95 }
96 
97 // ============================================================================
99  if ( !m_dump.empty() ) { dump( m_dump ); }
100  return StatusCode::SUCCESS;
101 }
102 
103 void JobOptionsSvc::dump( const std::string& file, const gp::Catalog& catalog ) const {
104  std::ofstream out( file, std::ios_base::out | std::ios_base::trunc );
105  if ( !out ) {
106  error() << "Unable to open dump-file \"" + file + "\"" << endmsg;
107  return; // RETURN
108  }
109  info() << "Properties are dumped into \"" + file + "\"" << endmsg;
110  // perform the actual dump:
111  out << catalog;
112 }
113 
114 void JobOptionsSvc::dump( const std::string& file ) const {
115  std::ofstream out( file, std::ios_base::out | std::ios_base::trunc );
116  if ( !out ) {
117  error() << "Unable to open dump-file \"" + file + "\"" << endmsg;
118  } else {
119  info() << "Properties are dumped into \"" + file + "\"" << endmsg;
120  for ( const auto& [key, value] : items() ) {
121  out << key << " = " << value << ';';
122  if ( !m_options.find( key )->second.isBound() ) out << " // unused";
123  out << '\n';
124  }
125  }
126 }
127 
129  for ( const auto& client : catalog ) {
130  for ( const auto& current : client.second ) {
131  set( client.first + '.' + current.NameInClient(), current.ValueAsString() );
132  }
133  }
134 }
135 
136 StatusCode JobOptionsSvc::readOptions( std::string_view file, std::string_view path ) {
137  std::string search_path = std::string{path};
138  if ( search_path.empty() && !m_dir_search_path.empty() ) { search_path = m_dir_search_path; }
139  //
140  if ( msgLevel( MSG::DEBUG ) )
141  debug() << "Reading options from the file "
142  << "'" << file << "'" << endmsg;
143  gp::Messages messages( msgStream() );
144  gp::Catalog catalog;
145  gp::Units units;
146  gp::PragmaOptions pragma;
147  gp::Node ast;
148  StatusCode sc = gp::ReadOptions( file, path, &messages, &catalog, &units, &pragma, &ast ) ? StatusCode::SUCCESS
150 
151  // --------------------------------------------------------------------------
152  if ( sc.isSuccess() ) {
153  if ( pragma.IsPrintOptions() ) { info() << "Print options" << std::endl << catalog << endmsg; }
154  if ( pragma.IsPrintTree() ) { info() << "Print tree:" << std::endl << ast.ToString() << endmsg; }
155  if ( pragma.HasDumpFile() ) dump( pragma.dumpFile(), catalog );
156  info() << "Job options successfully read in from " << file << endmsg;
157  fillServiceCatalog( catalog );
158  } else {
159  fatal() << "Job options errors." << endmsg;
160  }
161  // ----------------------------------------------------------------------------
162  return sc;
163 }
164 
166  const std::string key = prefix + '.' + property->name();
167 
168  std::tuple<bool, std::string_view> defaultValue{false, ""};
169  if ( !has( key ) && !m_globalDefaults.empty() ) { // look for a global default only if it was not set
170  std::smatch match;
171  for ( const auto& p : m_globalDefaults ) {
172  if ( regex_match( key, match, p.first ) ) { defaultValue = {true, p.second}; }
173  }
174  }
175 
176  m_options[key] = *property;
177 
178  // at this point the property is bound, so we can set the default if needed
179  if ( std::get<0>( defaultValue ) ) set( key, std::string{std::get<1>( defaultValue )} );
180 }
181 
182 void JobOptionsSvc::broadcast( const std::regex& filter, const std::string& value, OnlyDefaults defaults_only ) {
183  std::smatch match;
184  for ( auto& p : m_options ) {
185  if ( !defaults_only || !p.second.isSet() ) {
186  if ( regex_match( p.first, match, filter ) ) { p.second = value; }
187  }
188  }
189 }
MSG::DEBUG
@ DEBUG
Definition: IMessageSvc.h:25
Gaudi::Details::PropertyBase
PropertyBase base class allowing PropertyBase* collections to be "homogeneous".
Definition: PropertyBase.h:35
Service::initialize
StatusCode initialize() override
Definition: Service.cpp:118
std::string
STL class.
Gaudi.Configuration.log
log
Definition: Configuration.py:24
JobOptionsSvc::m_reportUnused
Gaudi::Property< bool > m_reportUnused
Definition: JobOptionsSvc.h:111
StatusCode::isSuccess
bool isSuccess() const
Definition: StatusCode.h:355
Gaudi::Parsers::Units
Definition: Units.h:28
System.h
std::vector::reserve
T reserve(T... args)
Gaudi::Details::WeakPropertyRef::isBound
bool isBound() const
Definition: PropertyBase.h:208
System::getEnv
GAUDI_API std::string getEnv(const char *var)
get a particular environment variable (returning "UNKNOWN" if not set)
Definition: System.cpp:379
std::vector
STL class.
std::unordered_map::find
T find(T... args)
std::unordered_map::size
T size(T... args)
ISvcLocator
Definition: ISvcLocator.h:46
JobOptionsSvc::m_pythonAction
Gaudi::Property< std::string > m_pythonAction
Definition: JobOptionsSvc.h:105
JobOptionsSvc::m_source_path
Gaudi::Property< std::string > m_source_path
Definition: JobOptionsSvc.h:102
JobOptionsSvc::set
void set(const std::string &key, const std::string &value) override
Definition: JobOptionsSvc.h:44
JobOptionsSvc::bind
void bind(const std::string &prefix, Gaudi::Details::PropertyBase *property) override
Definition: JobOptionsSvc.cpp:165
std::tuple
gaudirun.prefix
string prefix
Definition: gaudirun.py:343
Gaudi::Parsers::Node
Definition: Node.h:35
JobOptionsSvc::m_options
StorageType m_options
Definition: JobOptionsSvc.h:37
JobOptionsSvc::m_pythonParams
Gaudi::Property< std::string > m_pythonParams
Definition: JobOptionsSvc.h:106
std::sort
T sort(T... args)
CommonMessaging< implements< IService, IProperty, IStateful > >::msgLevel
MSG::Level msgLevel() const
get the cached level (originally extracted from the embedded MsgStream)
Definition: CommonMessaging.h:148
std::vector::clear
T clear(T... args)
PropertyHolder< CommonMessaging< implements< IService, IProperty, IStateful > > >::property
Gaudi::Details::PropertyBase * property(std::string_view name) const
\fixme property and bindPropertiesTo should be protected
Definition: PropertyHolder.h:238
JobOptionsSvc::broadcast
void broadcast(const std::regex &filter, const std::string &value, OnlyDefaults defaults_only) override
Definition: JobOptionsSvc.cpp:182
JobOptionsSvc::start
StatusCode start() override
Definition: JobOptionsSvc.cpp:98
Gaudi::Parsers::PragmaOptions
Definition: PragmaOptions.h:33
TimingHistograms.name
name
Definition: TimingHistograms.py:23
StatusCode
Definition: StatusCode.h:65
Gaudi::Parsers::Node::ToString
std::string ToString(int indent=0) const
Definition: Node.cpp:63
JobOptionsSvc::m_dump
Gaudi::Property< std::string > m_dump
Definition: JobOptionsSvc.h:104
JobOptionsSvc::stop
StatusCode stop() override
Definition: JobOptionsSvc.cpp:77
std::ofstream
STL class.
JobOptionsSvc::m_globalDefaultsProp
Gaudi::Property< std::vector< std::pair< std::string, std::string > > > m_globalDefaultsProp
Definition: JobOptionsSvc.h:108
CommonMessaging
Definition: CommonMessaging.h:66
Gaudi::Parsers::Messages
Definition: Messages.h:31
Gaudi::Property::declareUpdateHandler
Details::PropertyBase & declareUpdateHandler(std::function< void(Details::PropertyBase &)> fun) override
set new callback for update
Definition: Property.h:146
GaudiPython.HistoUtils.path
path
Definition: HistoUtils.py:943
JobOptionsSvc::readOptions
StatusCode readOptions(std::string_view file, std::string_view path="") override
look for file 'file' into search path 'path' and read it to update existing JobOptionsCatalogue
Definition: JobOptionsSvc.cpp:136
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:203
System::isEnvSet
GAUDI_API bool isEnvSet(const char *var)
Check if an environment variable is set or not.
Definition: System.cpp:399
std::regex
Units.h
Gaudi::Parsers::ReadOptions
bool ReadOptions(std::string_view filename, std::string_view search_path, Messages *messages, Catalog *catalog, Units *units, PragmaOptions *pragma, Node *root)
Parse and analyze filename, save all messages and properties.
Definition: Analyzer.cpp:379
JobOptionsSvc::dump
void dump(const std::string &file, const Gaudi::Parsers::Catalog &catalog) const
dump properties catalog to file
Definition: JobOptionsSvc.cpp:103
Messages.h
JobOptionsSvc::m_dir_search_path
Gaudi::Property< std::string > m_dir_search_path
Definition: JobOptionsSvc.h:103
JobOptionsSvc.h
PythonConfig.h
Factory.h
std::vector::emplace_back
T emplace_back(T... args)
Node.h
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
std::endl
T endl(T... args)
Service::stop
StatusCode stop() override
Definition: Service.cpp:181
JobOptionsSvc::initialize
StatusCode initialize() override
Definition: JobOptionsSvc.cpp:61
JobOptionsSvc::m_source_type
Gaudi::Property< std::string > m_source_type
Definition: JobOptionsSvc.h:101
Gaudi::Parsers::PragmaOptions::IsPrintOptions
bool IsPrintOptions()
Definition: PragmaOptions.h:49
JobOptionsSvc
Definition: JobOptionsSvc.h:30
std::vector::begin
T begin(T... args)
std
STL namespace.
DECLARE_COMPONENT
#define DECLARE_COMPONENT(type)
Definition: PluginServiceV1.h:46
std::vector::empty
T empty(T... args)
Gaudi::Parsers::PragmaOptions::HasDumpFile
bool HasDumpFile()
Definition: PragmaOptions.h:51
Gaudi::Parsers
Definition: DODBasicMapper.cpp:17
std::smatch
std::vector::end
T end(T... args)
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
Catalog.h
JobOptionsSvc::fillServiceCatalog
void fillServiceCatalog(const Gaudi::Parsers::Catalog &catalog)
Definition: JobOptionsSvc.cpp:128
ProduceConsume.key
key
Definition: ProduceConsume.py:52
JobOptionsSvc::items
std::vector< std::tuple< std::string, std::string > > items() const override
Definition: JobOptionsSvc.h:60
Gaudi::Parsers::PragmaOptions::IsPrintTree
bool IsPrintTree()
Definition: PragmaOptions.h:50
Gaudi::Parsers::PragmaOptions::dumpFile
const std::string & dumpFile() const
Definition: PragmaOptions.h:46
JobOptionsSvc::has
bool has(const std::string &key) const override
Definition: JobOptionsSvc.h:59
conf
Definition: conf.py:1
JobOptionsSvc::m_globalDefaults
std::vector< std::pair< std::regex, std::string > > m_globalDefaults
Definition: JobOptionsSvc.h:113
JobOptionsSvc::JobOptionsSvc
JobOptionsSvc(const std::string &name, ISvcLocator *svc)
Definition: JobOptionsSvc.cpp:50
Gaudi::Parsers::Catalog
Definition: Catalog.h:39
MsgStream.h
PythonConfig
Definition: PythonConfig.h:33
PrepareBase.out
out
Definition: PrepareBase.py:20
Analyzer.h
PragmaOptions.h