CYCLUS
Loading...
Searching...
No Matches
env.cc
Go to the documentation of this file.
1#include "env.h"
2
3#include <cstring>
4#include <string>
5#include <algorithm>
6#include <sys/stat.h>
7#include <utility>
8#include <vector>
9
10#include "boost/algorithm/string.hpp"
11#include "boost/filesystem.hpp"
12
13#include "logger.h"
14#include "platform.h"
15#include "pyhooks.h"
16
17namespace fs = boost::filesystem;
18
19namespace cyclus {
20
21fs::path Env::cwd_ = fs::current_path();
22
23std::string Env::instdir_ = "";
24
25std::string Env::PathBase(std::string path) {
26 std::string base;
27 int index;
28
29 index = path.rfind("/");
30 base = path.substr(0, index);
31 return base;
32}
33
34const std::string Env::GetInstallPath() {
35 if (instdir_.length() > 0)
36 return instdir_;
37 std::array<char, PATH_MAX> buffer;
38 std::string instdir;
39 std::unique_ptr<FILE, decltype(&pclose)> pipe(popen("which cyclus", "r"), pclose);
40 if (!pipe) {
41 throw std::runtime_error("popen() failed!");
42 }
43 while (fgets(buffer.data(), static_cast<int>(buffer.size()), pipe.get()) != nullptr) {
44 instdir += buffer.data();
45 }
46 if (instdir.length() <= 0)
47 instdir = "/root/.local";
48 else // This should be the '/path/to/bin/cyclus/../..' (ie '/path/to')
49 instdir = fs::path(instdir).parent_path().parent_path().string();
50 if (!fs::exists(instdir))
51 instdir = "/root/.local";
52 if (std::strlen(instdir.c_str()) < instdir.length()) {
53 // This is needed to remove multiple trailing NULL bytes, which then
54 // cause module lookup failure because instdir + NULL + lib will be
55 // truncated to just instdir by c_str() conversion.
56 instdir.resize(std::strlen(instdir.c_str()));
57 }
58 instdir_ = instdir;
59 return instdir;
60}
61
62const std::string Env::GetBuildPath() {
63 std::string blddir = "/cyclus/build";
64 if (std::strlen(blddir.c_str()) < blddir.length()) {
65 // This is needed to remove multiple trailing NULL bytes, which then
66 // cause module lookup failure because blddir + NULL + lib will be
67 // truncated to just blddir by c_str() conversion.
68 blddir.resize(std::strlen(blddir.c_str()));
69 std::cout << "resized builddir " << blddir << "\n";
70 }
71 return blddir;
72}
73
74std::string Env::GetEnv(std::string varname) {
75 char* pVar = getenv(varname.c_str());
76 if (pVar == NULL) {
77 return "";
78 }
79 return pVar;
80}
81
82const std::string Env::EnvDelimiter() {
83#if _WIN32
84 return ";";
85#else
86 return ":";
87#endif
88}
89
90const std::string Env::PathDelimiter() {
91#if _WIN32
92 return "\\";
93#else
94 return "/";
95#endif
96}
97
98const std::string Env::nuc_data() {
99 std::string p = GetEnv("CYCLUS_NUC_DATA");
100 if (p != "" && fs::exists(p)) {
101 return p;
102 }
103
104 p = GetInstallPath() + "/share/cyclus/cyclus_nuc_data.h5";
105 if (fs::exists(p)) {
106 return p;
107 }
108
109 p = GetBuildPath() + "/share/cyclus/cyclus_nuc_data.h5";
110 if (fs::exists(p)) {
111 return p;
112 }
113
114 throw IOError("cyclus_nuc_data.h5 not found in "
115 " environment variable CYCLUS_NUC_DATA or "
116 + Env::GetInstallPath() + "/share/cyclus or "
117 + Env::GetBuildPath() + "/share/cyclus");
118}
119
120const std::string Env::rng_schema(bool flat) {
121 std::string p = GetEnv("CYCLUS_RNG_SCHEMA");
122 if (p != "" && fs::exists(p)) {
123 return p;
124 }
125
126 if (flat) {
127 p = GetInstallPath() + "/share/cyclus/cyclus-flat.rng.in";
128 if (fs::exists(p)) {
129 return p;
130 }
131 p = GetBuildPath() + "/share/cyclus/cyclus-flat.rng.in";
132 if (fs::exists(p)) {
133 return p;
134 }
135 throw IOError("cyclus.rng.in not found in "
136 " environment variable CYCLUS_RNG_SCHEMA or "
137 + Env::GetInstallPath() + "/share/cyclus or "
138 + Env::GetBuildPath() + "/share/cyclus");
139 } else {
140 p = GetInstallPath() + "/share/cyclus/cyclus.rng.in";
141 if (fs::exists(p)) {
142 return p;
143 }
144 p = GetBuildPath() + "/share/cyclus/cyclus.rng.in";
145 if (fs::exists(p)) {
146 return p;
147 }
148 throw IOError("cyclus-flat.rng.in not found in "
149 " environment variable CYCLUS_RNG_SCHEMA or "
150 + Env::GetInstallPath() + "/share/cyclus or "
151 + Env::GetBuildPath() + "/share/cyclus");
152 }
153}
154
155const std::vector<std::string> Env::cyclus_path() {
156 std::string s = GetEnv("CYCLUS_PATH");
157 std::vector<std::string> strs;
158 boost::split(strs, s, boost::is_any_of(EnvDelimiter()));
159 strs.push_back(GetInstallPath() + "/lib/cyclus");
160 strs.push_back(GetBuildPath() + "/lib/cyclus");
161 return strs;
162}
163
164const bool Env::allow_milps() {
165 char* envvar = getenv("ALLOW_MILPS");
166 if (envvar == NULL) {
167 return true;
168 } else {
169 // convert envvar to lowercase string
170 std::string s = std::string(envvar);
171 std::transform(s.begin(), s.end(), s.begin(), ::tolower);
172 return !(s == "" || s == "f" || s == "false" || s == "0");
173 }
174}
175
176#define SHOW(X) \
177 std::cout << __FILE__ << ":" << __LINE__ << ": "#X" = " << X << "\n"
178
179std::string Env::FindModule(std::string path) {
180 return Env::FindModule(path, std::string(""));
181}
182
183std::string Env::FindModule(std::string path, std::string lib) {
184 boost::system::error_code errc;
185 std::vector<std::string> strs = cyclus_path();
186
187 for (int i = 0; i < strs.size(); ++i) {
188 fs::path full = fs::path(strs[i]) / path;
189 if (fs::exists(full, errc)) {
190 return full.string();
191 }
192 }
193 if (!lib.empty()) {
194 std::string pymod = PyFindModule(lib);
195 if (!pymod.empty()) {
196 return pymod;
197 }
198 }
199 throw IOError("No module found for path " + path);
200}
201
202} // namespace cyclus
static const std::string rng_schema(bool flat=false)
Returns the current rng schema.
Definition env.cc:120
static const std::string GetInstallPath()
The relative path to the root install directory (containing bin, lib, etc.) This first checks the pat...
Definition env.cc:34
static const std::string PathDelimiter()
Definition env.cc:90
static const bool allow_milps()
Definition env.cc:164
static std::string FindModule(std::string path)
Returns the full path to a module by searching through default install and CYCLUS_PATH directories.
Definition env.cc:179
static std::string GetEnv(std::string var)
Method to check the existence of and return an environment variable.
Definition env.cc:74
static std::string PathBase(std::string path)
the path basis
Definition env.cc:25
static const std::string EnvDelimiter()
Definition env.cc:82
static const std::string GetBuildPath()
The relative path to the root build directory (containing bin, lib, etc.)
Definition env.cc:62
static const std::string nuc_data()
Definition env.cc:98
static const std::vector< std::string > cyclus_path()
Definition env.cc:155
for failed reading/writing to files, network connections, etc..
Definition error.h:59
Code providing rudimentary logging capability for the Cyclus core.
taken directly from OsiSolverInterface.cpp on 2/17/14 from https://projects.coin-or....
Definition agent.cc:14
std::string PyFindModule(std::string lib)
Finds a Python module and returns its filename.
Definition pyhooks.cc:77
T OptionalQuery(InfileTree *tree, std::string query, T default_val)
a query method for optional parameters