MCPcopy Create free account
hub / github.com/NatLabRockies/OpenStudio / executePythonScriptCommand

Function executePythonScriptCommand

src/cli/UpdateCommand.cpp:105–138  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

103 }
104
105 void executePythonScriptCommand(openstudio::path pythonScriptPath, ScriptEngineInstance& pythonEngine, const std::vector<std::string>& arguments) {
106 pythonScriptPath = openstudio::filesystem::system_complete(pythonScriptPath);
107 LOG_FREE(Debug, "executePythonScriptCommand", "Path for the file to run: " << pythonScriptPath);
108 if (!openstudio::filesystem::is_regular_file(pythonScriptPath)) {
109 throw std::runtime_error(fmt::format("Unable to find the file '{}' on the filesystem", pythonScriptPath.string()));
110 }
111 // There's probably better to be done, like instantiating the pythonEngine with the argc/argv then calling PyRun_SimpleFile but whatever
112 std::string cmd = fmt::format(R"python(import sys
113sys.argv.clear()
114sys.argv.append("{}")
115)python",
116 pythonScriptPath.filename().string());
117 for (const auto& arg : arguments) {
118 cmd += fmt::format("sys.argv.append(\"{}\")\n", arg);
119 }
120 cmd += fmt::format(R"python(
121import importlib.util
122module_name = '__main__'
123spec = importlib.util.spec_from_file_location(module_name, r'{}')
124module = importlib.util.module_from_spec(spec)
125sys.modules[module_name] = module
126spec.loader.exec_module(module)
127)python",
128 pythonScriptPath.generic_string());
129 // fmt::print("{}\n", cmd);
130 try {
131 pythonEngine->exec(cmd);
132 } catch (...) {
133 // Bail faster...
134 fmt::print(stderr, "Failed to execute '{}'\n", pythonScriptPath.generic_string());
135 exit(1);
136 // throw std::runtime_error(fmt::format("Failed to execute '{}'\n", pythonScriptPath.generic_string()));
137 }
138 }
139
140 void executeGemListCommand(ScriptEngineInstance& rubyEngine) {
141

Callers 1

mainFunction · 0.85

Calls 3

filenameMethod · 0.80
stringMethod · 0.45
execMethod · 0.45

Tested by

no test coverage detected