cepgen
is hosted by
Hepforge
,
IPPP Durham
CepGen
1.2.5
Central exclusive processes event generator
Loading...
Searching...
No Matches
Environment.cpp
Go to the documentation of this file.
1
/*
2
* CepGen: a central exclusive processes event generator
3
* Copyright (C) 2022-2024 Laurent Forthomme
4
*
5
* This program is free software: you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation, either version 3 of the License, or
8
* any later version.
9
*
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
14
*
15
* You should have received a copy of the GNU General Public License
16
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17
*/
18
19
// clang-format off
20
#include "
CepGenAddOns/PythonWrapper/Environment.h
"
21
// clang-format on
22
23
#include <algorithm>
24
25
#include "
CepGen/Core/Exception.h
"
26
#include "
CepGen/Utils/Environment.h
"
27
#include "
CepGen/Utils/Filesystem.h
"
28
29
namespace
cepgen
{
30
namespace
python
{
31
//------------------------------------------------------------------
32
// Python API helpers
33
//------------------------------------------------------------------
34
35
Environment::Environment
(
const
ParametersList
& params) :
SteeredObject
(params) {
36
const
auto
cepgen_path = fs::path(
utils::env::get
(
"CEPGEN_PATH"
,
"."
));
37
CG_DEBUG
(
"python:Environment"
) <<
"CEPGEN_PATH set to "
<< cepgen_path <<
"."
;
38
for
(
const
auto
& path :
39
std::vector<std::string>{cepgen_path,
40
cepgen_path /
"python"
,
41
cepgen_path /
"python_modules"
,
42
cepgen_path /
"build"
/
"python"
,
43
cepgen_path /
"build"
/
"python_modules"
,
44
fs::current_path(),
45
fs::current_path() /
"python"
,
46
fs::current_path() /
"python_modules"
,
47
fs::current_path().parent_path() /
"python"
,
48
fs::current_path().parent_path() /
"python_modules"
,
49
fs::current_path().parent_path().parent_path() /
"python"
,
50
fs::current_path().parent_path().parent_path() /
"python_modules"
,
51
"/usr/share/CepGen/python"
,
52
"/usr/share/CepGen/python_modules"
})
53
utils::env::append
(
"PYTHONPATH"
, path);
54
CG_DEBUG
(
"Python:Environment"
) <<
"PYTHONPATH set to "
<<
utils::env::get
(
"PYTHONPATH"
) <<
"."
;
55
56
#if PY_VERSION_HEX >= 0x03080000
57
PyConfig_InitPythonConfig(&config_);
58
config_.parser_debug = steer<int>(
"debug"
);
59
config_.verbose = steer<int>(
"verbosity"
);
60
Py_InitializeFromConfig(&config_);
61
#else
62
Py_DebugFlag = steer<int>(
"debug"
);
63
Py_VerboseFlag = steer<int>(
"verbosity"
);
64
Py_InitializeEx(1);
65
#endif
66
if
(!
initialised
())
67
throw
CG_FATAL
(
"Python:Environment"
) <<
"Failed to initialise the Python environment!"
;
68
utils::env::set
(
"PYTHONDONTWRITEBYTECODE"
,
"1"
);
69
if
(
const
auto
& name = steer<std::string>(
"name"
); !name.empty())
70
setProgramName
(name);
71
}
72
73
Environment::~Environment
() {
74
if
(!
initialised
())
75
CG_WARNING
(
"Python:Environment"
)
76
<<
"Python environment is set to be finalised while it was not initialised in the first place."
;
77
else
78
Py_Finalize();
79
}
80
81
bool
Environment::initialised
() {
return
Py_IsInitialized(); }
82
83
void
Environment::setProgramName
(
const
std::string& filename) {
84
const
size_t
fn_len = filename.length() + 1;
85
#ifdef PYTHON2
86
char
* sfilename =
new
char
[fn_len];
87
snprintf(sfilename, fn_len,
"%s"
, filename.c_str());
88
const
std::string readable_s_filename(sfilename);
89
#else
90
wchar_t
* sfilename =
new
wchar_t
[fn_len];
91
swprintf(sfilename, fn_len, L
"%s"
, filename.c_str());
92
const
std::wstring readable_s_filename(sfilename);
93
#endif
94
#if PY_VERSION_HEX >= 0x03080000
95
config_.program_name = sfilename;
96
#else
97
Py_SetProgramName(sfilename);
98
#endif
99
delete
[] sfilename;
100
CG_DEBUG
(
"Python:setProgramName"
) <<
"Programme name set to \""
<< readable_s_filename <<
"\"."
;
101
}
102
103
ParametersDescription
Environment::description
() {
104
auto
desc =
ParametersDescription
();
105
desc.add<
int
>(
"verbosity"
, 0).setDescription(
"overall Python verbosity"
);
106
desc.add<
int
>(
"debug"
, 0).setDescription(
"debugging level"
);
107
return
desc;
108
}
109
}
// namespace python
110
}
// namespace cepgen
Exception.h
CG_FATAL
#define CG_FATAL(mod)
Definition
Exception.h:61
Filesystem.h
CG_WARNING
#define CG_WARNING(mod)
Definition
Message.h:228
CG_DEBUG
#define CG_DEBUG(mod)
Definition
Message.h:220
Environment.h
cepgen::ParametersDescription
A description object for parameters collection.
Definition
ParametersDescription.h:26
cepgen::ParametersList
Definition
ParametersList.h:52
cepgen::SteeredObject
Base user-steerable object.
Definition
SteeredObject.h:41
cepgen::python::Environment::~Environment
~Environment()
Finalise the python environment.
Definition
Environment.cpp:73
cepgen::python::Environment::setProgramName
void setProgramName(const std::string &)
Set the name of the Python program.
Definition
Environment.cpp:83
cepgen::python::Environment::description
static ParametersDescription description()
Definition
Environment.cpp:103
cepgen::python::Environment::Environment
Environment(const ParametersList &)
Initialise the python environment.
Definition
Environment.cpp:35
cepgen::python::Environment::initialised
bool initialised()
Is the python environment already initialised?
Definition
Environment.cpp:81
Environment.h
cepgen::utils::env::get
std::string get(const std::string &var, const std::string &def)
Get an environment variable.
Definition
Environment.cpp:28
cepgen::utils::env::set
void set(const std::string &var, const std::string &value)
Set an environment variable.
Definition
Environment.cpp:49
cepgen::utils::env::append
void append(const std::string &var, const std::string &value)
Add a value to an environment variable.
Definition
Environment.cpp:57
cepgen
Common namespace for this Monte Carlo generator.
Definition
CommandLineHandler.cpp:36
python
Definition
__init__.py:1
CepGenAddOns
PythonWrapper
Environment.cpp
Generated on Mon Jul 29 2024 for CepGen by
1.9.7