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