v0.16.0
Loading...
Searching...
No Matches
Functions
JsonConfigManagerPrograms.cpp File Reference

Go to the source code of this file.

Functions

std::string executableBasename (const std::string &path)
 
MoFEMErrorCode selectProgramConfig (const JsonObject &root_object, const std::string &file_name, boost::json::value &selected_config)
 

Function Documentation

◆ executableBasename()

std::string executableBasename ( const std::string &  path)

Definition at line 1 of file JsonConfigManagerPrograms.cpp.

1 {
2 const auto separator = path.find_last_of("/\\");
3 if (separator == std::string::npos)
4 return path;
5 return path.substr(separator + 1);
6}

◆ selectProgramConfig()

MoFEMErrorCode selectProgramConfig ( const JsonObject &  root_object,
const std::string &  file_name,
boost::json::value &  selected_config 
)

Definition at line 8 of file JsonConfigManagerPrograms.cpp.

10 {
12 if (!root_object.if_contains("programs")) {
13 selected_config = root_object;
14 } else {
15 for (const auto &item : root_object) {
16 const auto key = toStdString(item.key());
17 if (key == "$schema" || key == "version" || key == "programs")
18 continue;
19 CHKERR failConfig(file_name, makeContext("root", key),
20 "cannot be used together with root.programs; move it "
21 "into a program entry");
22 }
23
24 const auto &programs =
25 requireArray(root_object.at("programs"), file_name, "root.programs");
26 char program_name[PETSC_MAX_PATH_LEN] = "";
27 CHKERR PetscGetProgramName(program_name, sizeof(program_name));
28 const auto current_executable = executableBasename(program_name);
29 if (current_executable.empty()) {
30 CHKERR failConfig(file_name, "root.programs",
31 "unable to determine current executable name");
32 }
33
34 const JsonObject *matching_program = nullptr;
35 std::string matching_context;
36 for (size_t program_index = 0; program_index != programs.size();
37 ++program_index) {
38 const auto context =
39 "root.programs[" +
40 boost::lexical_cast<std::string>(program_index) + "]";
41 const auto &program =
42 requireObject(programs[program_index], file_name, context);
43 const auto executable_it = program.find("executable");
44 if (executable_it == program.end()) {
45 CHKERR failConfig(file_name, makeContext(context, "executable"),
46 "missing required executable field");
47 }
48
49 const auto executable = requireString(
50 executable_it->value(), file_name, makeContext(context, "executable"));
51 if (executable != current_executable &&
52 executableBasename(executable) != current_executable) {
53 continue;
54 }
55 if (matching_program) {
56 CHKERR failConfig(file_name, context,
57 "multiple program entries match executable '" +
58 current_executable + "'");
59 }
60 matching_program = &program;
61 matching_context = context;
62 }
63
64 if (!matching_program) {
65 CHKERR failConfig(file_name, "root.programs",
66 "no program entry matches executable '" +
67 current_executable + "'");
68 }
69
70 boost::json::object config_object;
71 if (const auto version_it = root_object.find("version");
72 version_it != root_object.end()) {
73 config_object["version"] = version_it->value();
74 }
75 for (const auto &item : *matching_program) {
76 const auto key = toStdString(item.key());
77 if (key == "executable" || key == "working_directory" ||
78 key == "parameters" ||
79 key == "$schema" || key == "version") {
80 continue;
81 }
82 config_object[item.key()] = item.value();
83 }
84 selected_config = std::move(config_object);
85
86 MOFEM_LOG("WORLD", Sev::inform)
87 << file_name << ": selected JSON program config " << matching_context
88 << " for executable '" << current_executable << "'";
89 }
90
92}
std::string executableBasename(const std::string &path)
std::string key
#define MoFEMFunctionBegin
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
#define MoFEMFunctionReturn(a)
Last executable line of each PETSc function used for error handling. Replaces return()
#define CHKERR
Inline error check.
#define MOFEM_LOG(channel, severity)
Log.