dora_core/
lib.rs

1use eyre::{bail, eyre, Context};
2use std::{
3    env::consts::{DLL_PREFIX, DLL_SUFFIX},
4    ffi::OsStr,
5    path::Path,
6};
7
8pub use dora_message::{config, uhlc};
9
10pub mod descriptor;
11pub mod metadata;
12pub mod topics;
13
14pub fn adjust_shared_library_path(path: &Path) -> Result<std::path::PathBuf, eyre::ErrReport> {
15    let file_name = path
16        .file_name()
17        .ok_or_else(|| eyre!("shared library path has no file name"))?
18        .to_str()
19        .ok_or_else(|| eyre!("shared library file name is not valid UTF8"))?;
20    if file_name.starts_with("lib") {
21        bail!("Shared library file name must not start with `lib`, prefix is added automatically");
22    }
23    if path.extension().is_some() {
24        bail!("Shared library file name must have no extension, it is added automatically");
25    }
26
27    let library_filename = format!("{DLL_PREFIX}{file_name}{DLL_SUFFIX}");
28
29    let path = path.with_file_name(library_filename);
30    Ok(path)
31}
32
33// Search for python binary.
34// Match `python` for windows and macos and `python3` for other platforms.
35pub fn get_python_path() -> Result<std::path::PathBuf, eyre::ErrReport> {
36    let python = if cfg!(windows) || cfg!(target_os = "macos") {
37        which::which("python")
38            .context("failed to find `python` or `python3`. Make sure that python is available.")?
39    } else {
40        which::which("python3")
41            .context("failed to find `python` or `python3`. Make sure that python is available.")?
42    };
43
44    Ok(python)
45}
46
47// Search for pip binary.
48// First search for `pip3` as for ubuntu <20, `pip` can resolves to `python2,7 -m pip`
49// Then search for `pip`, this will resolve for windows to python3 -m pip.
50pub fn get_pip_path() -> Result<std::path::PathBuf, eyre::ErrReport> {
51    let python = match which::which("pip3") {
52        Ok(python) => python,
53        Err(_) => which::which("pip")
54            .context("failed to find `pip3` or `pip`. Make sure that python is available.")?,
55    };
56    Ok(python)
57}
58
59// Search for uv binary.
60pub fn get_uv_path() -> Result<std::path::PathBuf, eyre::ErrReport> {
61    which::which("uv")
62        .context("failed to find `uv`. Make sure to install it using: https://docs.astral.sh/uv/getting-started/installation/")
63}
64
65// Helper function to run a program
66pub async fn run<S>(program: S, args: &[&str], pwd: Option<&Path>) -> eyre::Result<()>
67where
68    S: AsRef<OsStr>,
69{
70    let mut run = tokio::process::Command::new(program);
71    run.args(args);
72
73    if let Some(pwd) = pwd {
74        run.current_dir(pwd);
75    }
76    if !run.status().await?.success() {
77        eyre::bail!("failed to run {args:?}");
78    };
79    Ok(())
80}