src/cli/UpdateCommand.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /*********************************************************************************************************************** | ||
| 2 | * OpenStudio(R), Copyright (c) Alliance for Energy Innovation, LLC. | ||
| 3 | * See also https://openstudio.net/license | ||
| 4 | ***********************************************************************************************************************/ | ||
| 5 | |||
| 6 | #include "UpdateCommand.hpp" | ||
| 7 | #include "../utilities/core/Filesystem.hpp" | ||
| 8 | #include "../utilities/core/FilesystemHelpers.hpp" | ||
| 9 | #include "../utilities/core/Logger.hpp" | ||
| 10 | #include "../osversion/VersionTranslator.hpp" | ||
| 11 | #include "../model/Model.hpp" | ||
| 12 | #include "../scriptengine/ScriptEngine.hpp" | ||
| 13 | |||
| 14 | #include <fmt/format.h> | ||
| 15 | |||
| 16 | #include <memory> | ||
| 17 | #include <stdexcept> | ||
| 18 | |||
| 19 | namespace openstudio { | ||
| 20 | namespace cli { | ||
| 21 | |||
| 22 | ✗ | bool runModelUpdateCommand(const openstudio::path& p, bool keep) { | |
| 23 | ✗ | std::vector<openstudio::path> osmPaths; | |
| 24 | |||
| 25 | ✗ | openstudio::osversion::VersionTranslator vt; | |
| 26 | |||
| 27 | ✗ | if (openstudio::filesystem::is_directory(p)) { | |
| 28 | ✗ | for (auto const& dir_entry : boost::filesystem::directory_iterator{p}) { | |
| 29 | ✗ | const auto& thePath = dir_entry.path(); | |
| 30 | ✗ | if (openstudio::filesystem::is_regular_file(thePath) && thePath.extension() == ".osm") { | |
| 31 | ✗ | osmPaths.emplace_back(thePath); | |
| 32 | } | ||
| 33 | ✗ | } | |
| 34 | } else { | ||
| 35 | ✗ | osmPaths.push_back(p); | |
| 36 | } | ||
| 37 | |||
| 38 | ✗ | bool result = true; | |
| 39 | ✗ | for (const auto& relPath : osmPaths) { | |
| 40 | ✗ | fmt::print("'{}'\n", relPath.string()); | |
| 41 | ✗ | auto osmPath = openstudio::filesystem::system_complete(relPath); | |
| 42 | ✗ | if (keep) { | |
| 43 | ✗ | openstudio::path backupPath = osmPath; | |
| 44 | ✗ | backupPath.replace_extension(openstudio::toPath(".osm.orig")); | |
| 45 | ✗ | openstudio::filesystem::copy_file(osmPath, backupPath, openstudio::filesystem::copy_options::overwrite_existing); | |
| 46 | ✗ | } | |
| 47 | ✗ | if (auto model_ = vt.loadModel(osmPath)) { | |
| 48 | ✗ | fmt::print("Updated '{}'\n", osmPath.string()); | |
| 49 | ✗ | model_->save(osmPath, true); | |
| 50 | } else { | ||
| 51 | ✗ | fmt::print("Could not read model at '{}'\n", osmPath.string()); | |
| 52 | ✗ | result = false; | |
| 53 | ✗ | } | |
| 54 | ✗ | } | |
| 55 | ✗ | return result; | |
| 56 | ✗ | } | |
| 57 | |||
| 58 | 188 | void executeRubyScriptCommand(openstudio::path rubyScriptPath, ScriptEngineInstance& rubyEngine, const std::vector<std::string>& arguments) { | |
| 59 |
1/1✓ Branch 1 taken 188 times.
|
188 | rubyScriptPath = openstudio::filesystem::system_complete(rubyScriptPath); |
| 60 |
6/6✓ Branch 1 taken 188 times.
✓ Branch 4 taken 188 times.
✓ Branch 7 taken 188 times.
✓ Branch 10 taken 188 times.
✓ Branch 13 taken 188 times.
✓ Branch 16 taken 188 times.
|
564 | LOG_FREE(Debug, "executeRubyScriptCommand", "Path for the file to run: " << rubyScriptPath); |
| 61 |
2/3✓ Branch 1 taken 188 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 188 times.
|
188 | if (!openstudio::filesystem::is_regular_file(rubyScriptPath)) { |
| 62 | ✗ | throw std::runtime_error(fmt::format("Unable to find the file '{}' on the filesystem", rubyScriptPath.string())); | |
| 63 | } | ||
| 64 | |||
| 65 |
1/1✓ Branch 1 taken 188 times.
|
188 | std::string cmd = "ARGV.clear\n"; |
| 66 |
2/2✓ Branch 4 taken 183 times.
✓ Branch 5 taken 188 times.
|
371 | for (const auto& arg : arguments) { |
| 67 |
1/1✓ Branch 2 taken 183 times.
|
366 | cmd += fmt::format("ARGV << \"{}\"\n", arg); |
| 68 | } | ||
| 69 | 376 | cmd += fmt::format(R"( | |
| 70 | begin | ||
| 71 | require '{}' | ||
| 72 | 0 | ||
| 73 | rescue SystemExit => e | ||
| 74 | # puts "System Exit: #{{e.status}}" | ||
| 75 | if !e.success? | ||
| 76 | STDERR.puts | ||
| 77 | STDERR.puts "#{{e.class}}: #{{e.message}} with return code #{{e.status}} " | ||
| 78 | STDERR.puts "Backtrace:\n\t" + e.backtrace.join("\n\t") | ||
| 79 | end | ||
| 80 | e.status | ||
| 81 | rescue Exception => e | ||
| 82 | STDERR.puts | ||
| 83 | STDERR.puts "#{{e.class}}: #{{e.message}}" | ||
| 84 | STDERR.puts "Backtrace:\n\t" + e.backtrace.join("\n\t") | ||
| 85 | raise | ||
| 86 | end | ||
| 87 | )", | ||
| 88 |
2/2✓ Branch 1 taken 188 times.
✓ Branch 4 taken 188 times.
|
564 | rubyScriptPath.generic_string()); |
| 89 | try { | ||
| 90 |
2/2✓ Branch 1 taken 187 times.
✓ Branch 6 taken 187 times.
|
188 | auto ret_so = rubyEngine->eval(cmd); |
| 91 |
2/2✓ Branch 1 taken 187 times.
✓ Branch 5 taken 187 times.
|
187 | auto ret_code = rubyEngine->getAs<int>(ret_so); |
| 92 | // If ret_code is already none zero, just exit faster | ||
| 93 | // Otherwise let it be, so that the at_exit(s) can run in particular (for minitest for eg) | ||
| 94 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 187 times.
|
187 | if (ret_code != 0) { |
| 95 | ✗ | exit(ret_code); | |
| 96 | } | ||
| 97 | 188 | } catch (...) { | |
| 98 | // Bail faster though ruby isn't slow like python | ||
| 99 |
1/1✓ Branch 1 taken 1 time.
|
2 | fmt::print(stderr, "Failed to execute '{}'\n", rubyScriptPath.generic_string()); |
| 100 | 1 | exit(1); | |
| 101 | // throw std::runtime_error(fmt::format("Failed to execute '{}'\n", rubyScriptPath.generic_string())); | ||
| 102 | ✗ | } | |
| 103 | 187 | } | |
| 104 | |||
| 105 | 14 | void executePythonScriptCommand(openstudio::path pythonScriptPath, ScriptEngineInstance& pythonEngine, const std::vector<std::string>& arguments) { | |
| 106 |
1/1✓ Branch 1 taken 14 times.
|
14 | pythonScriptPath = openstudio::filesystem::system_complete(pythonScriptPath); |
| 107 |
6/6✓ Branch 1 taken 14 times.
✓ Branch 4 taken 14 times.
✓ Branch 7 taken 14 times.
✓ Branch 10 taken 14 times.
✓ Branch 13 taken 14 times.
✓ Branch 16 taken 14 times.
|
42 | LOG_FREE(Debug, "executePythonScriptCommand", "Path for the file to run: " << pythonScriptPath); |
| 108 |
2/3✓ Branch 1 taken 14 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 14 times.
|
14 | 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 | 28 | pythonScriptPath.filename().string()); | |
| 117 |
2/2✓ Branch 4 taken 12 times.
✓ Branch 5 taken 14 times.
|
26 | for (const auto& arg : arguments) { |
| 118 |
1/1✓ Branch 2 taken 12 times.
|
24 | cmd += fmt::format("sys.argv.append(\"{}\")\n", arg); |
| 119 | } | ||
| 120 | 28 | 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 |
2/2✓ Branch 1 taken 14 times.
✓ Branch 4 taken 14 times.
|
42 | pythonScriptPath.generic_string()); |
| 129 | // fmt::print("{}\n", cmd); | ||
| 130 | try { | ||
| 131 |
2/2✓ Branch 1 taken 14 times.
✓ Branch 6 taken 13 times.
|
14 | 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 | 13 | } | |
| 139 | |||
| 140 | ✗ | void executeGemListCommand(ScriptEngineInstance& rubyEngine) { | |
| 141 | |||
| 142 | ✗ | std::string cmd = R"ruby( | |
| 143 | begin | ||
| 144 | embedded = [] | ||
| 145 | user = [] | ||
| 146 | Gem::Specification.find_all.each do |spec| | ||
| 147 | if spec.gem_dir.chars.first == ':' | ||
| 148 | embedded << spec | ||
| 149 | else | ||
| 150 | user << spec | ||
| 151 | end | ||
| 152 | end | ||
| 153 | |||
| 154 | embedded.each do |spec| | ||
| 155 | safe_puts "#{spec.name} (#{spec.version}) '#{spec.gem_dir}'" | ||
| 156 | end | ||
| 157 | |||
| 158 | user.each do |spec| | ||
| 159 | safe_puts "#{spec.name} (#{spec.version}) '#{spec.gem_dir}'" | ||
| 160 | end | ||
| 161 | |||
| 162 | rescue => e | ||
| 163 | $logger.error "Error listing gems: #{e.message} in #{e.backtrace.join("\n")}" | ||
| 164 | exit e.exit_code | ||
| 165 | end | ||
| 166 | )ruby"; | ||
| 167 | ✗ | rubyEngine->exec(cmd); | |
| 168 | ✗ | } | |
| 169 | |||
| 170 | ✗ | void executeRubyRepl(ScriptEngineInstance& rubyEngine) { | |
| 171 | |||
| 172 | ✗ | rubyEngine->exec("ARGV.clear"); | |
| 173 | |||
| 174 | ✗ | const std::string cmd = R"ruby( | |
| 175 | require 'irb' | ||
| 176 | require 'irb/lc/error' | ||
| 177 | |||
| 178 | # ENV["IRB_LANG"] = "C" | ||
| 179 | # puts "Reline encoding_system_needs: #{Reline.encoding_system_needs.name}" | ||
| 180 | # puts "Reline::IOGate.encoding: #{Reline::IOGate.encoding}" | ||
| 181 | |||
| 182 | # Workaround issue in line_editor.rb where it fails to read a full block | ||
| 183 | # unicode character. Probably because of the way we embbed it in C++ | ||
| 184 | # The full block is \xe2\x96\x88 | ||
| 185 | ENV['RELINE_ALT_SCROLLBAR'] = "1" | ||
| 186 | |||
| 187 | class Reline::LineEditor | ||
| 188 | def reset(prompt = '', encoding:) | ||
| 189 | @rest_height = (Reline::IOGate.get_screen_size.first - 1) - Reline::IOGate.cursor_pos.y | ||
| 190 | @screen_size = Reline::IOGate.get_screen_size | ||
| 191 | @screen_height = @screen_size.first | ||
| 192 | reset_variables(prompt, encoding: encoding) | ||
| 193 | Reline::IOGate.set_winch_handler do | ||
| 194 | @resized = true | ||
| 195 | end | ||
| 196 | if ENV.key?('RELINE_ALT_SCROLLBAR') | ||
| 197 | @full_block = '::' | ||
| 198 | @upper_half_block = "''" | ||
| 199 | @lower_half_block = '..' | ||
| 200 | @block_elem_width = 2 | ||
| 201 | elsif Reline::IOGate.win? | ||
| 202 | @full_block = '█' | ||
| 203 | @upper_half_block = '▀' | ||
| 204 | @lower_half_block = '▄' | ||
| 205 | @block_elem_width = 1 | ||
| 206 | elsif @encoding == Encoding::UTF_8 | ||
| 207 | @full_block = '█' | ||
| 208 | @upper_half_block = '▀' | ||
| 209 | @lower_half_block = '▄' | ||
| 210 | @block_elem_width = Reline::Unicode.calculate_width('█') | ||
| 211 | else | ||
| 212 | @full_block = '::' | ||
| 213 | @upper_half_block = "''" | ||
| 214 | @lower_half_block = '..' | ||
| 215 | @block_elem_width = 2 | ||
| 216 | end | ||
| 217 | end | ||
| 218 | end | ||
| 219 | |||
| 220 | module IRB # :nodoc: | ||
| 221 | class Locale | ||
| 222 | |||
| 223 | alias :original_load :load | ||
| 224 | alias :original_require :require | ||
| 225 | |||
| 226 | def require(file, priv = nil) | ||
| 227 | # puts "require: #{file}" | ||
| 228 | original_require(file, priv) | ||
| 229 | end | ||
| 230 | |||
| 231 | # Some shenanigans being done to detect localized files, and that relies ton File.readable? and co... | ||
| 232 | def load(file, priv=nil) | ||
| 233 | # puts "file=#{file}" | ||
| 234 | if file == 'irb/error.rb' | ||
| 235 | $".push file | ||
| 236 | @@loaded << 'irb/lc/error.rb' | ||
| 237 | return | ||
| 238 | end | ||
| 239 | original_load(file, priv) | ||
| 240 | end | ||
| 241 | |||
| 242 | end | ||
| 243 | |||
| 244 | |||
| 245 | def self.start_session(binding) | ||
| 246 | unless @__initialized | ||
| 247 | args = ARGV | ||
| 248 | ARGV.replace(ARGV.dup) | ||
| 249 | IRB.setup(nil) | ||
| 250 | # locale = @CONF[:LC_MESSAGES] | ||
| 251 | # p locale | ||
| 252 | |||
| 253 | ARGV.replace(args) | ||
| 254 | @__initialized = true | ||
| 255 | end | ||
| 256 | |||
| 257 | @CONF[:IRB_RC].call(irb.context) if @CONF[:IRB_RC] | ||
| 258 | # puts @CONF[:PROMPT_MODE] | ||
| 259 | os_version = "3.8.0" # OpenStudio::openStudioVersion | ||
| 260 | @CONF[:PROMPT][:OPENSTUDIO] = { | ||
| 261 | :PROMPT_I=>"(os #{os_version}) :%03n > ", | ||
| 262 | :PROMPT_S=>"(os #{os_version}) :%03n%l> ", | ||
| 263 | :PROMPT_C=>"(os #{os_version}) :%03n > ", | ||
| 264 | :PROMPT_N=>"(os #{os_version}) :%03n?> ", | ||
| 265 | :RETURN=>" => %s \n", | ||
| 266 | :AUTO_INDENT=>true | ||
| 267 | } | ||
| 268 | @CONF[:PROMPT_MODE] = :OPENSTUDIO | ||
| 269 | workspace = WorkSpace.new(binding) | ||
| 270 | input_method = IRB::RelineInputMethod.new | ||
| 271 | irb = Irb.new(workspace, input_method) | ||
| 272 | |||
| 273 | |||
| 274 | @CONF[:MAIN_CONTEXT] = irb.context | ||
| 275 | |||
| 276 | catch(:IRB_EXIT) do | ||
| 277 | irb.eval_input | ||
| 278 | end | ||
| 279 | end | ||
| 280 | end | ||
| 281 | |||
| 282 | IRB.start_session(binding) | ||
| 283 | )ruby"; | ||
| 284 | |||
| 285 | ✗ | rubyEngine->exec(cmd); | |
| 286 | ✗ | } | |
| 287 | |||
| 288 | ✗ | void executePythonRepl(ScriptEngineInstance& pythonEngine) { | |
| 289 | ✗ | const std::string cmd = R"python( | |
| 290 | import readline # optional, will allow Up/Down/History in the console | ||
| 291 | import code | ||
| 292 | variables = globals().copy() | ||
| 293 | variables.update(locals()) | ||
| 294 | shell = code.InteractiveConsole(variables) | ||
| 295 | shell.interact() | ||
| 296 | )python"; | ||
| 297 | ✗ | pythonEngine->exec(cmd); | |
| 298 | ✗ | } | |
| 299 | |||
| 300 | ✗ | void executePipListCommand(ScriptEngineInstance& pythonEngine) { | |
| 301 | // TODO: seems like the CLI is picking up your virtualenvironment, so we manipulate sys.path to keep only the E+ folder for now | ||
| 302 | ✗ | const std::string cmd = R"python( | |
| 303 | import sys | ||
| 304 | sys.path = [x for x in sys.path if "EnergyPlus" in x] | ||
| 305 | import importlib.metadata | ||
| 306 | mods = sorted([f"{x.name}=={x.version}" for x in importlib.metadata.distributions()]) | ||
| 307 | [print(x) for x in mods] | ||
| 308 | )python"; | ||
| 309 | ✗ | pythonEngine->exec(cmd); | |
| 310 | ✗ | } | |
| 311 | |||
| 312 | } // namespace cli | ||
| 313 | } // namespace openstudio | ||
| 314 |