10 #include <google/protobuf/util/json_util.h>
17 inline bool is_equal(
double a,
double b,
double error_factor = 1.0)
20 std::abs(a - b) < std::abs(std::fmin(a, b)) * std::numeric_limits<double>::epsilon() * error_factor;
23 inline void asci_to_protobuf(std::string a, std::string & p)
25 std::stringstream sstream(a);
27 while (sstream.good()) {
30 char c = char(bits.to_ulong());
35 inline void protobuf_to_asci(std::string p, std::string & a)
37 for (std::size_t i = 0; i < p.size(); ++i) {
38 std::bitset<8> xx = std::bitset<8>(p[i]);
42 inline int mkpath(
const char * dir, mode_t mode)
49 if (!stat(dir, &st))
return 0;
50 mkpath(dirname(strdupa(dir)), mode);
51 return mkdir(dir, mode);
54 inline std::string get_working_path()
57 return (getcwd(temp,
sizeof(temp)) ? std::string(temp) : std::string(
""));
60 inline bool ndm_init(std::string dir =
"/tmp/")
63 ndm_yaml.append(
"base: \"" + dir +
"/content\"\n");
64 ndm_yaml.append(
"cmd: \"" + dir +
"/ndm.sh\"\n");
65 ndm_yaml.append(
"space:\n");
66 ndm_yaml.append(
" axes:\n");
67 ndm_yaml.append(
" - name: \"axis1\"\n");
68 ndm_yaml.append(
" min: 0\n");
69 ndm_yaml.append(
" max: 1\n");
70 ndm_yaml.append(
" level: 0\n");
71 std::ofstream ndm_yaml_out(
"ndm.yaml");
72 ndm_yaml_out << ndm_yaml;
75 std::string ndm_script;
76 ndm_script.append(
"#!/bin/bash\n");
77 ndm_script.append(
"export WKDIR=\"$NDMBASE/$NDMPATH\"\n");
78 ndm_script.append(
"mkdir -p $WKDIR && cd $WKDIR || exit 1\n");
79 ndm_script.append(
"(\n");
80 ndm_script.append(
"export\n");
81 ndm_script.append(
"ndm info\n");
82 ndm_script.append(
"# my job\n");
83 ndm_script.append(
"sleep 1\n");
84 ndm_script.append(
") |& tee $WKDIR/ndm.log\n");
85 std::ofstream ndm_script_out(
"ndm.sh");
86 ndm_script_out << ndm_script;
87 chmod(
"ndm.sh", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
88 ndm_script_out.close();
90 std::string ndm_runscript;
91 ndm_runscript.append(
"#!/bin/bash\n");
92 ndm_runscript.append(
"SLS_URL=${1-\"tcp://toolbox.localhost:41000\"}\n\n");
93 ndm_runscript.append(
"salsa-ndm -s $SLS_URL -c ndm.yaml\n");
94 std::ofstream ndm_runscript_out(
"run.sh");
95 ndm_runscript_out << ndm_runscript;
96 chmod(
"run.sh", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
97 ndm_runscript_out.close();
99 spdlog::info(
"Files 'ndm.yaml', 'ndm.sh' and 'run.sh' generated");
104 inline bool ndm_info(std::string out =
"")
106 const char* e = getenv(
"NDMPOINT");
107 std::string ndmpoint;
110 if (ndmpoint.empty()) {
111 spdlog::error(
"Missing env variable '$NDMPOINT' !!!");
114 std::string ndmpoint_out;
115 NDM::Utils::asci_to_protobuf(ndmpoint, ndmpoint_out);
117 bool b = p.ParseFromString(ndmpoint_out);
119 google::protobuf::util::JsonPrintOptions options;
120 options.add_whitespace =
true;
121 options.always_print_primitive_fields =
true;
123 MessageToJsonString(p, &result, options);
124 spdlog::info(
"{}", result);
128 std::ofstream outputfile(out);
129 options.add_whitespace =
false;
130 MessageToJsonString(p, &result, options);
131 outputfile << result << std::endl;
133 spdlog::info(
"File '{}' was created.", out);