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