From f703ea40e43be258e7592d5e34dab4c9443fc957 Mon Sep 17 00:00:00 2001 From: Julien Marrec Date: Thu, 12 Dec 2024 11:33:27 +0100 Subject: [PATCH 1/4] Fix #5315? --- ruby/engine/embedded_help.rb | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/ruby/engine/embedded_help.rb b/ruby/engine/embedded_help.rb index 95e205dd9db..9205d60a367 100644 --- a/ruby/engine/embedded_help.rb +++ b/ruby/engine/embedded_help.rb @@ -3,6 +3,8 @@ # See also https://openstudio.net/license ######################################################################################################################## +require 'stringio' + module EmbeddedScripting @@fileNames = EmbeddedScripting::allFileNamesAsString.split(';') @@ -56,6 +58,17 @@ def self.preprocess_ruby_script(s) # DLM: ignore for now #Encoding.default_external = Encoding::ASCII +# We patch Kernel.open, and IO.open +# to read embedded files as a StringIO, not as a FileIO +# But Gem.open_file for eg expects it to be a File, not a StringIO, and on +# Windows it will call File::flock (file lock) to protect access, but StringIO +# does not have this method +class FakeFileAsStringIO < StringIO + def flock + return 0 + end +end + module Kernel # ":" is our root path to the embedded file system # make sure it is in the ruby load path @@ -329,7 +342,7 @@ def open(name, *args, **options) #puts "string = #{string}" if block_given? # if a block is given, then a new IO is created and closed - io = StringIO.open(string) + io = FakeFileAsStringIO.open(string) begin result = yield(io) ensure @@ -337,13 +350,13 @@ def open(name, *args, **options) end return result else - return StringIO.open(string) + return FakeFileAsStringIO.open(string) end else #puts "IO.open cannot find embedded file '#{absolute_path}' for '#{name}'" if block_given? # if a block is given, then a new IO is created and closed - io = StringIO.open("") + io = FakeFileAsStringIO.open("") begin result = yield(io) ensure @@ -459,7 +472,7 @@ def self.open(name, *args, **options) #puts "string = #{string}" if block_given? # if a block is given, then a new IO is created and closed - io = StringIO.open(string) + io = FakeFileAsStringIO.open(string) begin result = yield(io) ensure @@ -467,13 +480,13 @@ def self.open(name, *args, **options) end return result else - return StringIO.open(string) + return FakeFileAsStringIO.open(string) end else puts "IO.open cannot find embedded file '#{absolute_path}' for '#{name}'" if block_given? # if a block is given, then a new IO is created and closed - io = StringIO.open("") + io = FakeFileAsStringIO.open("") begin result = yield(io) ensure From 0fdf34a27a48ef171652b712e04f33c2c538ed5a Mon Sep 17 00:00:00 2001 From: Julien Marrec Date: Thu, 12 Dec 2024 12:28:01 +0100 Subject: [PATCH 2/4] #5315 - Add a ctest to require 'uuid' with CLI --- src/cli/CMakeLists.txt | 5 +++++ src/cli/test/ensure_uuid_can_be_loaded.rb | 5 +++++ 2 files changed, 10 insertions(+) create mode 100644 src/cli/test/ensure_uuid_can_be_loaded.rb diff --git a/src/cli/CMakeLists.txt b/src/cli/CMakeLists.txt index e79684fcdfc..74c557322a6 100644 --- a/src/cli/CMakeLists.txt +++ b/src/cli/CMakeLists.txt @@ -203,6 +203,11 @@ if(BUILD_TESTING) PASS_REGULAR_EXPRESSION "Hello from -x, at National Renewable Energy Laboratory" ) + # Test for 5315 + add_test(NAME OpenStudioCLI.EmbeddedScripting.uuid + COMMAND $ ${CMAKE_CURRENT_SOURCE_DIR}/test/ensure_uuid_can_be_loaded.rb + ) + add_test(NAME OpenStudioCLI.Classic.Run_RubyOnly COMMAND $ classic run -w compact_ruby_only.osw WORKING_DIRECTORY "${PROJECT_BINARY_DIR}/resources/Examples/compact_osw/" diff --git a/src/cli/test/ensure_uuid_can_be_loaded.rb b/src/cli/test/ensure_uuid_can_be_loaded.rb new file mode 100644 index 00000000000..41e4cfd19b5 --- /dev/null +++ b/src/cli/test/ensure_uuid_can_be_loaded.rb @@ -0,0 +1,5 @@ +require 'uuid' + +u = UUID.new + +puts u.inspect From 91e9b4a5f22ee2ffd98f5ec1c4fd069af5b3e381 Mon Sep 17 00:00:00 2001 From: Julien Marrec Date: Thu, 12 Dec 2024 15:34:23 +0100 Subject: [PATCH 3/4] Handle the add_dll_directory when running in build_dir --- python/module/openstudio.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/python/module/openstudio.py b/python/module/openstudio.py index 62e8260b440..ee9c1bad671 100644 --- a/python/module/openstudio.py +++ b/python/module/openstudio.py @@ -50,7 +50,10 @@ # When we're using system python to load the **installed** C:\openstudio-X.Y-Z\Python stuff (not PyPi package) # This allows finding openstudiolib.dll and the msvc ones in the bin/ folder while we're in the Python/ folder # Otherwise you'd have to manually copy these DLLs from bin/ to Python/ - os.add_dll_directory(os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'bin'))) + bin_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'bin')) + if os.path.isdir(bin_dir): + os.add_dll_directory(bin_dir) + import openstudioairflow as airflow import openstudioenergyplus as energyplus import openstudioepjson as epjson From d3822140a93a20e3247dca71b1d7ad29aecef6cd Mon Sep 17 00:00:00 2001 From: Julien Marrec Date: Thu, 12 Dec 2024 15:39:06 +0100 Subject: [PATCH 4/4] Flock takes an operation param (eg File::LOCK_EX) --- ruby/engine/embedded_help.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ruby/engine/embedded_help.rb b/ruby/engine/embedded_help.rb index 9205d60a367..6a9666a982c 100644 --- a/ruby/engine/embedded_help.rb +++ b/ruby/engine/embedded_help.rb @@ -64,7 +64,7 @@ def self.preprocess_ruby_script(s) # Windows it will call File::flock (file lock) to protect access, but StringIO # does not have this method class FakeFileAsStringIO < StringIO - def flock + def flock(operation) return 0 end end