Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix/issue86] join path instead of string literal #126

Merged
merged 1 commit into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 50 additions & 3 deletions crates/edgen_core/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,9 +303,14 @@ impl SettingsParams {
impl Default for SettingsParams {
fn default() -> Self {
let data_dir = PROJECT_DIRS.data_dir();
let chat_completions_dir = data_dir.join(Path::new("models/chat/completions"));
let audio_transcriptions_dir = data_dir.join(Path::new("models/audio/transcriptions"));
let embeddings_dir = data_dir.join(Path::new("models/embeddings"));
let chat_completions_dir =
data_dir.join(&join_path_components(&["models", "chat", "completions"]));
let audio_transcriptions_dir = data_dir.join(&join_path_components(&[
"models",
"audio",
"transcriptions",
]));
let embeddings_dir = data_dir.join(&join_path_components(&["models", "embeddings"]));

let chat_completions_str = chat_completions_dir.into_os_string().into_string().unwrap();
let audio_transcriptions_str = audio_transcriptions_dir
Expand Down Expand Up @@ -339,6 +344,10 @@ impl Default for SettingsParams {
}
}

fn join_path_components(comps: &[&str]) -> PathBuf {
comps.iter().collect::<PathBuf>()
}

pub struct SettingsInner {
params: SettingsParams,
changed_params: SettingsParams,
Expand Down Expand Up @@ -632,13 +641,51 @@ impl DerefMut for StaticSettings {

#[cfg(test)]
mod tests {
use std::ffi::OsString;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;

use crate::settings::*;

const TEST_FILE: &str = "tfile";

#[test]
fn test_join_path_components() {
for i in 0..4 {
let (have, expected) = if i == 0 {
(
join_path_components(&["path", "to", "my", "file.ext"]).into_os_string(),
#[cfg(target_family = "windows")]
OsString::from("path\\to\\my\\file.ext"),
#[cfg(not(target_family = "windows"))]
OsString::from("path/to/my/file.ext"),
)
} else if i == 1 {
(
#[cfg(target_family = "windows")]
join_path_components(&["c:\\absolute", "path", "to", "my", "file.ext"])
.into_os_string(),
#[cfg(not(target_family = "windows"))]
join_path_components(&["/absolute", "path", "to", "my", "file.ext"])
.into_os_string(),
#[cfg(target_family = "windows")]
OsString::from("c:\\absolute\\path\\to\\my\\file.ext"),
#[cfg(not(target_family = "windows"))]
OsString::from("/absolute/path/to/my/file.ext"),
)
} else if i == 2 {
(
join_path_components(&["file.ext"]).into_os_string(),
OsString::from("file.ext"),
)
} else {
(OsString::from(""), OsString::from(""))
};
println!("Path from components: {:?}", have);
assert_eq!(have, expected);
}
}

// Trying to avoid doing too many disk writes in unit tests by performing every test using the
// same file.
#[tokio::test]
Expand Down
23 changes: 15 additions & 8 deletions crates/edgen_server/src/model_man.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use axum::http::StatusCode;
use axum::response::{IntoResponse, Json, Response};
use serde::{Deserialize, Serialize};
use thiserror;
use tracing::warn;
use tracing::{info, warn};
use utoipa::ToSchema;

use edgen_core::settings;
Expand Down Expand Up @@ -139,28 +139,35 @@ async fn list_all_models() -> Result<ModelList, PathError> {

async fn list_models_in_dir(path: &Path, v: &mut Vec<ModelDesc>) -> Result<(), PathError> {
let es = tokio::fs::read_dir(path).await;
if es.is_err() {
warn!("model manager: cannot read directory {:?} ({:?})", path, es);
return Err(PathError::IOError(es.unwrap_err()));
if let Err(error) = es {
warn!(
"model manager: cannot read directory {:?} ({:?})",
path, error
);
return Err(PathError::IOError(error));
};
let mut es = es.unwrap();
loop {
let e = es.next_entry().await;
if e.is_err() {
warn!("model manager: cannot get entry: {:?}", e);
if let Err(error) = e {
warn!("model manager: cannot get entry: {:?}", error);
break;
}
let tmp = e.unwrap();
if tmp.is_none() {
break;
}
let tmp = tmp.unwrap();
if tmp.file_name() == "tmp" {
continue;
}
match path_to_model_desc(tmp.path().as_path()).await {
Ok(m) => v.push(m),
Err(e) => {
warn!(
info!(
"model manager: invalid entry in directory {:?}: {:?}",
path, e
tmp.path(),
e
);
}
}
Expand Down
Loading