Skip to content

Commit

Permalink
Continue
Browse files Browse the repository at this point in the history
  • Loading branch information
Hofer-Julian committed Apr 23, 2024
1 parent 5f18857 commit 07c77fb
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 41 deletions.
24 changes: 24 additions & 0 deletions build/cli_wrapper/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions build/cli_wrapper/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ edition = "2021"

[dependencies]
clap = { version = "4.5.4", features = ["derive"] }
libc = "0.2.153"
libloading = "0.8.3"
38 changes: 32 additions & 6 deletions build/cli_wrapper/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
use std::{env, path::PathBuf};
use std::{
env::{self, consts::OS},
ffi::CString,
path::PathBuf,
};

use clap::{CommandFactory, Parser};
use clap::Parser;
use libloading::{Library, Symbol};
use std::process::ExitCode;

#[derive(Parser)]
Expand All @@ -15,7 +20,7 @@ fn main() -> ExitCode {
let exe_dir = env::current_exe().unwrap().parent().unwrap().to_owned();

// Set the appropriate environment variable for the current platform
if std::env::consts::OS == "windows" {
if OS == "windows" {
env::set_var(
"PATH",
format!(
Expand All @@ -26,7 +31,7 @@ fn main() -> ExitCode {
);
}

// TODO: Do I need to set LD_LIBRARY_PATH on linux?
// TODO: Do we need to set LD_LIBRARY_PATH on linux?

// Parse command line arguments
let cli = Cli::parse();
Expand All @@ -36,6 +41,27 @@ fn main() -> ExitCode {
return ExitCode::FAILURE;
}

// Call ribasim shared library and check for errors
todo!()
let shared_lib_path = match OS {
"windows" => exe_dir.join("bin/libribasim.dll"),
"linux" => exe_dir.join("lib/libribasim.so"),
_ => unimplemented!(),
};

unsafe {
// Load the library
let lib = Library::new(shared_lib_path).unwrap();

// Load the function from the library
let execute: Symbol<unsafe extern "C" fn(*const libc::c_char) -> i32> =
lib.get(b"execute").unwrap();

// Convert the path to a CString
let toml_path_c = CString::new(cli.toml_path.to_str().unwrap()).unwrap();

// Call the function
let exit_code = execute(toml_path_c.as_ptr());

// Return with same exit code as `execute` did
ExitCode::from(exit_code as u8)
}
}
5 changes: 1 addition & 4 deletions core/src/main.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ function run(config::Config)::Model
return model
end

main(toml_path::AbstractString)::Cint = main([toml_path])
main()::Cint = main(ARGS)

"""
main(toml_path::AbstractString)::Cint
main(ARGS::Vector{String})::Cint
Expand All @@ -26,7 +23,7 @@ This is the main entry point of the application.
Performs argument parsing and sets up logging for both terminal and file.
Calls Ribasim.run() and handles exceptions to convert to exit codes.
"""
function main(toml_path::String)::Cint
function main(toml_path::AbstractString)::Cint
try
# show progress bar in terminal
config = Config(toml_path)
Expand Down
31 changes: 1 addition & 30 deletions core/test/main_test.jl
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
@testitem "version" begin
using IOCapture: capture

(; value, output) = capture() do
Ribasim.main(["--version"])
end
@test value == 0
@test output == string(pkgversion(Ribasim))
end

@testitem "toml_path" begin
using IOCapture: capture
Expand All @@ -24,7 +15,7 @@ end

@test ispath(toml_path)
(; value, output, error, backtrace) = capture() do
Ribasim.main([toml_path])
Ribasim.main(toml_path)
end
@test value == 0
if value != 0
Expand All @@ -34,23 +25,3 @@ end
end
@test occursin("version in the TOML config file does not match", output)
end

@testitem "too many arguments for main" begin
using IOCapture: capture

(; value, output) = capture() do
Ribasim.main(["too", "many"])
end
@test value == 1
@test occursin("Exactly 1 argument expected, got 2", output)
end

@testitem "non-existing file for main" begin
using IOCapture: capture

(; value, output) = capture() do
Ribasim.main(["non-existing-file.toml"])
end
@test value == 1
@test occursin("File not found: non-existing-file.toml", output)
end
3 changes: 3 additions & 0 deletions python/ribasim_api/ribasim_api/ribasim_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,6 @@ def shutdown_julia(self) -> None:

def update_subgrid_level(self) -> None:
self.lib.update_subgrid_level()

def execute(self, config_file: str) -> None:
self._execute_function(self.lib.execute, config_file.encode())
2 changes: 1 addition & 1 deletion python/ribasim_api/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def libribasim_paths() -> tuple[Path, Path]:
repo_root = Path(__file__).parents[3].resolve()
lib_or_bin = "bin" if platform.system() == "Windows" else "lib"
extension = ".dll" if platform.system() == "Windows" else ".so"
lib_folder = repo_root / "build" / "libribasim" / lib_or_bin
lib_folder = repo_root / "build" / "ribasim" / lib_or_bin
lib_path = lib_folder / f"libribasim{extension}"
return lib_path, lib_folder

Expand Down
6 changes: 6 additions & 0 deletions python/ribasim_api/tests/test_bmi.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,9 @@ def test_get_version(libribasim):
config = tomli.load(fp)

assert libribasim.get_version() == config["version"]


def test_execute(libribasim, basic, tmp_path):
basic.write(tmp_path / "ribasim.toml")
config_file = str(tmp_path / "ribasim.toml")
libribasim.execute(config_file)

0 comments on commit 07c77fb

Please sign in to comment.