CYCLUS
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 
17 namespace fs = boost::filesystem;
18 
19 namespace cyclus {
20 
21 fs::path Env::cwd_ = fs::current_path();
22 
23 std::string Env::instdir_ = "";
24 
26  std::string base;
27  int index;
28 
29  index = path.rfind("/");
30  base = path.substr(0, index);
31  return base;
32 }
33 
35  // On some (most?) posix shells - notably BASH and ksh - the environment
36  // variable '_' is set to the absolute path of the currently executing
37  // program. This allows us to have a somewhat platform independent way
38  // of figuring out the install directory. If this fails for any reason,
39  // we go back to the install directory specified at compile time.
40  if (instdir_.length() > 0)
41  return instdir_;
42  std::string instdir = GetEnv("_");
43  if (instdir.length() > 0)
44  instdir = "/home/stomps/.local";
45  else // This should be the '/path/to/bin/cyclus/../..' (ie '/path/to')
46  instdir = fs::path(instdir).parent_path().parent_path().string();
47  if (!fs::exists(instdir))
48  instdir = "/home/stomps/.local";
49  if (std::strlen(instdir.c_str()) < instdir.length()) {
50  // This is needed to remove multiple trailing NULL bytes, which then
51  // cause module lookup failure because instdir + NULL + lib will be
52  // truncated to just instdir by c_str() conversion.
53  instdir.resize(std::strlen(instdir.c_str()));
54  }
55  instdir_ = instdir;
56  return instdir;
57 }
58 
60  std::string blddir = "/home/stomps/dev-cyclus/cyclus/_tmp_build";
61  if (std::strlen(blddir.c_str()) < blddir.length()) {
62  // This is needed to remove multiple trailing NULL bytes, which then
63  // cause module lookup failure because blddir + NULL + lib will be
64  // truncated to just blddir by c_str() conversion.
65  blddir.resize(std::strlen(blddir.c_str()));
66  std::cout << "resized builddir " << blddir << "\n";
67  }
68  return blddir;
69 }
70 
72  char* pVar = getenv(varname.c_str());
73  if (pVar == NULL) {
74  return "";
75  }
76  return pVar;
77 }
78 
80 #if _WIN32
81  return ";";
82 #else
83  return ":";
84 #endif
85 }
86 
88 #if _WIN32
89  return "\\";
90 #else
91  return "/";
92 #endif
93 }
94 
96  std::string p = GetEnv("CYCLUS_NUC_DATA");
97  if (p != "" && fs::exists(p)) {
98  return p;
99  }
100 
101  p = GetInstallPath() + "/share/cyclus/cyclus_nuc_data.h5";
102  if (fs::exists(p)) {
103  return p;
104  }
105 
106  p = GetBuildPath() + "/share/cyclus/cyclus_nuc_data.h5";
107  if (fs::exists(p)) {
108  return p;
109  }
110 
111  throw IOError("cyclus_nuc_data.h5 not found in "
112  " environment variable CYCLUS_NUC_DATA or "
113  + Env::GetInstallPath() + "/share/cyclus or "
114  + Env::GetBuildPath() + "/share/cyclus");
115 }
116 
117 const std::string Env::rng_schema(bool flat) {
118  std::string p = GetEnv("CYCLUS_RNG_SCHEMA");
119  if (p != "" && fs::exists(p)) {
120  return p;
121  }
122 
123  if (flat) {
124  p = GetInstallPath() + "/share/cyclus/cyclus-flat.rng.in";
125  if (fs::exists(p)) {
126  return p;
127  }
128  p = GetBuildPath() + "/share/cyclus/cyclus-flat.rng.in";
129  if (fs::exists(p)) {
130  return p;
131  }
132  throw IOError("cyclus.rng.in not found in "
133  " environment variable CYCLUS_RNG_SCHEMA or "
134  + Env::GetInstallPath() + "/share/cyclus or "
135  + Env::GetBuildPath() + "/share/cyclus");
136  } else {
137  p = GetInstallPath() + "/share/cyclus/cyclus.rng.in";
138  if (fs::exists(p)) {
139  return p;
140  }
141  p = GetBuildPath() + "/share/cyclus/cyclus.rng.in";
142  if (fs::exists(p)) {
143  return p;
144  }
145  throw IOError("cyclus-flat.rng.in not found in "
146  " environment variable CYCLUS_RNG_SCHEMA or "
147  + Env::GetInstallPath() + "/share/cyclus or "
148  + Env::GetBuildPath() + "/share/cyclus");
149  }
150 }
151 
152 const std::vector<std::string> Env::cyclus_path() {
153  std::string s = GetEnv("CYCLUS_PATH");
154  std::vector<std::string> strs;
155  boost::split(strs, s, boost::is_any_of(EnvDelimiter()));
156  strs.push_back(GetInstallPath() + "/lib/cyclus");
157  strs.push_back(GetBuildPath() + "/lib/cyclus");
158  return strs;
159 }
160 
161 const bool Env::allow_milps() {
162  char* envvar = getenv("ALLOW_MILPS");
163  if (envvar == NULL) {
164  return false;
165  } else {
166  // convert envvar to lowercase string
167  std::string s = std::string(envvar);
168  std::transform(s.begin(), s.end(), s.begin(), ::tolower);
169  return !(s == "" || s == "f" || s == "false" || s == "0");
170  }
171 }
172 
173 #define SHOW(X) \
174  std::cout << __FILE__ << ":" << __LINE__ << ": "#X" = " << X << "\n"
175 
177  return Env::FindModule(path, std::string(""));
178 }
179 
181  boost::system::error_code errc;
182  std::vector<std::string> strs = cyclus_path();
183 
184  for (int i = 0; i < strs.size(); ++i) {
185  fs::path full = fs::path(strs[i]) / path;
186  if (fs::exists(full, errc)) {
187  return full.string();
188  }
189  }
190  if (!lib.empty()) {
191  std::string pymod = PyFindModule(lib);
192  if (!pymod.empty()) {
193  return pymod;
194  }
195  }
196  throw IOError("No module found for path " + path);
197 }
198 
199 } // namespace cyclus
static const std::string GetBuildPath()
The relative path to the root build directory (containing bin, lib, etc.)
Definition: env.cc:59
static const bool allow_milps()
Definition: env.cc:161
std::string PyFindModule(std::string lib)
Finds a Python module and returns its filename.
Definition: pyhooks.cc:121
static const std::vector< std::string > cyclus_path()
Definition: env.cc:152
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:176
static std::string GetEnv(std::string var)
Method to check the existence of and return an environment variable.
Definition: env.cc:71
for failed reading/writing to files, network connections, etc..
Definition: error.h:59
static std::string PathBase(std::string path)
the path basis
Definition: env.cc:25
static const std::string rng_schema(bool flat=false)
Returns the current rng schema.
Definition: env.cc:117
static const std::string nuc_data()
Definition: env.cc:95
Code providing rudimentary logging capability for the Cyclus core.
taken directly from OsiSolverInterface.cpp on 2/17/14 from https://projects.coin-or.org/Osi/browser/trunk.
Definition: agent.cc:14
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:87
static const std::string EnvDelimiter()
Definition: env.cc:79
std::vector< std::string > split(const std::string &s, char delim)
Definition: sqlite_back.cc:28