45 lines
1.5 KiB
Rust
45 lines
1.5 KiB
Rust
use std::fs;
|
|
use std::path::{Path, PathBuf};
|
|
|
|
use gethostname::gethostname;
|
|
|
|
/// Detect which console/beamline we're running on based on the short hostname.
|
|
pub fn detect_beamline() -> Result<String, String> {
|
|
let hostname = gethostname().to_string_lossy().into_owned();
|
|
if hostname.starts_with("x10sa") {
|
|
Ok("x10sa".to_string())
|
|
} else if hostname.starts_with("x06da") {
|
|
Ok("x06da".to_string())
|
|
} else if hostname.starts_with("x0sda") {
|
|
Ok("x06sa".to_string())
|
|
} else {
|
|
Err(format!(
|
|
"Error: unrecognized hostname '{hostname}' — cannot determine beamline"
|
|
))
|
|
}
|
|
}
|
|
|
|
/// Detect the Python virtual environments directly inside `dir`.
|
|
///
|
|
/// A subdirectory is treated as a virtualenv when it contains a `pyvenv.cfg`
|
|
/// file — the marker written by both `python -m venv` and `uv venv`. The
|
|
/// returned paths are the venv directories, sorted by name.
|
|
pub fn detect_venvs(dir: &Path) -> Result<Vec<PathBuf>, String> {
|
|
let entries =
|
|
fs::read_dir(dir).map_err(|e| format!("cannot read directory '{}': {e}", dir.display()))?;
|
|
|
|
let mut venvs: Vec<PathBuf> = entries
|
|
.filter_map(|entry| entry.ok().map(|entry| entry.path()))
|
|
.filter(|path| is_venv(path))
|
|
.collect();
|
|
|
|
venvs.sort();
|
|
Ok(venvs)
|
|
}
|
|
|
|
/// Whether `path` is a Python virtualenv, i.e. a directory holding a
|
|
/// `pyvenv.cfg` marker file.
|
|
fn is_venv(path: &Path) -> bool {
|
|
path.is_dir() && path.join("pyvenv.cfg").is_file()
|
|
}
|