1use eyre::{Context, bail, eyre};
12use std::{
13 env::consts::{DLL_PREFIX, DLL_SUFFIX},
14 path::Path,
15};
16
17pub use dora_message::{config, uhlc};
18
19#[cfg(feature = "build")]
20pub mod build;
21pub mod debug_topic;
22pub mod descriptor;
23pub mod manifest;
24pub mod schema_once;
25pub mod topics;
26pub mod types;
27
28pub fn adjust_shared_library_path(path: &Path) -> Result<std::path::PathBuf, eyre::ErrReport> {
50 let file_name = path
51 .file_name()
52 .ok_or_else(|| eyre!("shared library path has no file name"))?
53 .to_str()
54 .ok_or_else(|| eyre!("shared library file name is not valid UTF8"))?;
55 if file_name.starts_with("lib") {
56 bail!("Shared library file name must not start with `lib`, prefix is added automatically");
57 }
58 if path.extension().is_some() {
59 bail!("Shared library file name must have no extension, it is added automatically");
60 }
61
62 let library_filename = format!("{DLL_PREFIX}{file_name}{DLL_SUFFIX}");
63
64 let path = path.with_file_name(library_filename);
65 Ok(path)
66}
67
68pub fn get_python_path() -> Result<std::path::PathBuf, eyre::ErrReport> {
76 if let Ok(uv_path) = get_uv_path() {
78 let output = std::process::Command::new(&uv_path)
79 .args(["python", "find"])
80 .output();
81
82 if let Ok(output) = output
83 && output.status.success()
84 {
85 let python_path = String::from_utf8_lossy(&output.stdout).trim().to_string();
86 if !python_path.is_empty() {
87 let path = std::path::PathBuf::from(&python_path);
88 if path.exists() {
89 return Ok(path);
90 }
91 }
92 }
93 }
94
95 if let Ok(python) = which::which("python") {
97 if !is_python2(&python) {
99 return Ok(python);
100 }
101 }
102
103 which::which("python3").context(
105 "failed to find a valid Python 3 installation. Make sure that python3 is available.",
106 )
107}
108
109fn is_python2(python_path: &std::path::Path) -> bool {
110 let output = std::process::Command::new(python_path)
111 .args(["--version"])
112 .output();
113
114 match output {
115 Ok(output) => {
116 let version = if output.stdout.is_empty() {
118 String::from_utf8_lossy(&output.stderr)
119 } else {
120 String::from_utf8_lossy(&output.stdout)
121 };
122 version.starts_with("Python 2")
123 }
124 Err(_) => false,
125 }
126}
127
128pub fn get_uv_path() -> Result<std::path::PathBuf, eyre::ErrReport> {
131 which::which("uv")
132 .context("failed to find `uv`. Make sure to install it using: https://docs.astral.sh/uv/getting-started/installation/")
133}