| 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 |
| 113 | sys.argv.clear() |
| 114 | sys.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( |
| 121 | import importlib.util |
| 122 | module_name = '__main__' |
| 123 | spec = importlib.util.spec_from_file_location(module_name, r'{}') |
| 124 | module = importlib.util.module_from_spec(spec) |
| 125 | sys.modules[module_name] = module |
| 126 | spec.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 | |