Skip to content

Add a configure req, rename project to workspace #101

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

Merged
merged 2 commits into from
Jul 10, 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
2 changes: 1 addition & 1 deletion crates/pet-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub struct LocatorResult {
#[derive(Debug, Default, Clone)]
pub struct Configuration {
/// These are paths like workspace folders, where we can look for environments.
pub project_directories: Option<Vec<PathBuf>>,
pub workspace_directories: Option<Vec<PathBuf>>,
pub conda_executable: Option<PathBuf>,
pub poetry_executable: Option<PathBuf>,
/// Custom locations where environments can be found.
Expand Down
24 changes: 12 additions & 12 deletions crates/pet-poetry/src/environment_locations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,23 @@ lazy_static! {

pub fn list_environments(
env: &EnvVariables,
project_dirs: &[PathBuf],
workspace_dirs: &[PathBuf],
manager: Option<PoetryManager>,
) -> Option<Vec<PythonEnvironment>> {
if project_dirs.is_empty() {
if workspace_dirs.is_empty() {
return None;
}

let project_dirs = project_dirs
let workspace_dirs = workspace_dirs
.iter()
.map(|project_dir| (project_dir, PyProjectToml::find(project_dir)))
.filter_map(|(project_dir, pyproject_toml)| {
pyproject_toml.map(|pyproject_toml| (project_dir, pyproject_toml))
.map(|workspace_dir| (workspace_dir, PyProjectToml::find(workspace_dir)))
.filter_map(|(workspace_dir, pyproject_toml)| {
pyproject_toml.map(|pyproject_toml| (workspace_dir, pyproject_toml))
})
.collect::<Vec<_>>();

// We're only interested in directories that have a pyproject.toml
if project_dirs.is_empty() {
if workspace_dirs.is_empty() {
return None;
}

Expand All @@ -53,17 +53,17 @@ pub fn list_environments(
global_envs = list_all_environments_from_config(&config).unwrap_or_default();
}

for (project_dir, pyproject_toml) in project_dirs {
let virtualenv_prefix = generate_env_name(&pyproject_toml.name, project_dir);
for (workspace_dir, pyproject_toml) in workspace_dirs {
let virtualenv_prefix = generate_env_name(&pyproject_toml.name, workspace_dir);
trace!(
"Found pyproject.toml ({}): {:?} in {:?}",
virtualenv_prefix,
pyproject_toml.name,
project_dir
workspace_dir
);

for virtual_env in [
list_all_environments_from_project_config(&global_config, project_dir, env)
list_all_environments_from_project_config(&global_config, workspace_dir, env)
.unwrap_or_default(),
global_envs.clone(),
]
Expand All @@ -78,7 +78,7 @@ pub fn list_environments(
// Look for .venv as well, in case we create the virtual envs in the local project folder.
if name.starts_with(&virtualenv_prefix) || name.starts_with(".venv") {
if let Some(env) =
create_poetry_env(&virtual_env, project_dir.clone(), manager.clone())
create_poetry_env(&virtual_env, workspace_dir.clone(), manager.clone())
{
envs.push(env);
}
Expand Down
49 changes: 29 additions & 20 deletions crates/pet-poetry/src/environment_locations_spawn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ lazy_static! {

pub fn list_environments(
executable: &PathBuf,
project_dirs: &Vec<PathBuf>,
workspace_dirs: &Vec<PathBuf>,
manager: &PoetryManager,
) -> Vec<PythonEnvironment> {
let mut envs = vec![];
for project_dir in project_dirs {
if let Some(project_envs) = get_environments(executable, project_dir) {
for project_env in project_envs {
for workspace_dir in workspace_dirs {
if let Some(workspace_envs) = get_environments(executable, workspace_dir) {
for workspace_env in workspace_envs {
if let Some(env) =
create_poetry_env(&project_env, project_dir.clone(), Some(manager.clone()))
create_poetry_env(&workspace_env, workspace_dir.clone(), Some(manager.clone()))
{
envs.push(env);
}
Expand All @@ -34,19 +34,19 @@ pub fn list_environments(
envs
}

fn get_environments(executable: &PathBuf, project_dir: &PathBuf) -> Option<Vec<PathBuf>> {
fn get_environments(executable: &PathBuf, workspace_dir: &PathBuf) -> Option<Vec<PathBuf>> {
let start = SystemTime::now();
let result = std::process::Command::new(executable)
.arg("env")
.arg("list")
.arg("--full-path")
.current_dir(project_dir)
.current_dir(workspace_dir)
.output();
trace!(
"Executed Poetry ({}ms): {:?} env list --full-path for {:?}",
start.elapsed().unwrap_or_default().as_millis(),
executable,
project_dir
workspace_dir
);
match result {
Ok(output) => {
Expand Down Expand Up @@ -89,19 +89,20 @@ pub struct PoetryConfig {
pub virtualenvs_path: Option<PathBuf>,
}

pub fn get_config(executable: &PathBuf, project_dir: &PathBuf) -> PoetryConfig {
let cache_dir = get_config_path(executable, project_dir, "cache-dir");
let virtualenvs_path = get_config_path(executable, project_dir, "virtualenvs.path");
let virtualenvs_in_project = get_config_bool(executable, project_dir, "virtualenvs.in-project");
pub fn get_config(executable: &PathBuf, workspace_dir: &PathBuf) -> PoetryConfig {
let cache_dir = get_config_path(executable, workspace_dir, "cache-dir");
let virtualenvs_path = get_config_path(executable, workspace_dir, "virtualenvs.path");
let virtualenvs_in_project =
get_config_bool(executable, workspace_dir, "virtualenvs.in-project");
PoetryConfig {
cache_dir,
virtualenvs_in_project,
virtualenvs_path,
}
}

fn get_config_bool(executable: &PathBuf, project_dir: &PathBuf, setting: &str) -> Option<bool> {
match get_config_value(executable, project_dir, setting) {
fn get_config_bool(executable: &PathBuf, workspace_dir: &PathBuf, setting: &str) -> Option<bool> {
match get_config_value(executable, workspace_dir, setting) {
Some(output) => {
let output = output.trim();
if output.starts_with("true") {
Expand All @@ -115,19 +116,27 @@ fn get_config_bool(executable: &PathBuf, project_dir: &PathBuf, setting: &str) -
None => None,
}
}
fn get_config_path(executable: &PathBuf, project_dir: &PathBuf, setting: &str) -> Option<PathBuf> {
get_config_value(executable, project_dir, setting).map(|output| PathBuf::from(output.trim()))
fn get_config_path(
executable: &PathBuf,
workspace_dir: &PathBuf,
setting: &str,
) -> Option<PathBuf> {
get_config_value(executable, workspace_dir, setting).map(|output| PathBuf::from(output.trim()))
}

fn get_config_value(executable: &PathBuf, project_dir: &PathBuf, setting: &str) -> Option<String> {
fn get_config_value(
executable: &PathBuf,
workspace_dir: &PathBuf,
setting: &str,
) -> Option<String> {
let start = SystemTime::now();
let result = std::process::Command::new(executable)
.arg("config")
.arg(setting)
.current_dir(project_dir)
.current_dir(workspace_dir)
.output();
trace!(
"Executed Poetry ({}ms): {executable:?} config {setting} {project_dir:?}",
"Executed Poetry ({}ms): {executable:?} config {setting} {workspace_dir:?}",
start.elapsed().unwrap_or_default().as_millis(),
);
match result {
Expand All @@ -137,7 +146,7 @@ fn get_config_value(executable: &PathBuf, project_dir: &PathBuf, setting: &str)
} else {
let stderr = String::from_utf8_lossy(&output.stderr).to_string();
trace!(
"Failed to get Poetry config {setting} using exe {executable:?} in {project_dir:?}, due to ({}) {}",
"Failed to get Poetry config {setting} using exe {executable:?} in {workspace_dir:?}, due to ({}) {}",
output.status.code().unwrap_or_default(),
stderr
);
Expand Down
26 changes: 13 additions & 13 deletions crates/pet-poetry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub trait PoetryLocator: Send + Sync {
}

pub struct Poetry {
pub project_directories: Arc<Mutex<Vec<PathBuf>>>,
pub workspace_directories: Arc<Mutex<Vec<PathBuf>>>,
pub env_vars: EnvVariables,
pub poetry_executable: Arc<Mutex<Option<PathBuf>>>,
searched: AtomicBool,
Expand All @@ -52,7 +52,7 @@ impl Poetry {
Poetry {
searched: AtomicBool::new(false),
search_result: Arc::new(Mutex::new(None)),
project_directories: Arc::new(Mutex::new(vec![])),
workspace_directories: Arc::new(Mutex::new(vec![])),
env_vars: EnvVariables::from(environment),
poetry_executable: Arc::new(Mutex::new(None)),
}
Expand All @@ -76,10 +76,10 @@ impl Poetry {
if let Some(manager) = &manager {
result.managers.push(manager.to_manager());
}
if let Ok(values) = self.project_directories.lock() {
let project_dirs = values.clone();
if let Ok(values) = self.workspace_directories.lock() {
let workspace_dirs = values.clone();
drop(values);
let envs = list_environments(&self.env_vars, &project_dirs.clone(), manager)
let envs = list_environments(&self.env_vars, &workspace_dirs.clone(), manager)
.unwrap_or_default();
result.environments.extend(envs.clone());
}
Expand Down Expand Up @@ -112,18 +112,18 @@ impl PoetryLocator for Poetry {
let manager = PoetryManager::find(poetry_executable.clone(), &self.env_vars)?;
let poetry_executable = manager.executable.clone();

let project_dirs = self.project_directories.lock().unwrap().clone();
let workspace_dirs = self.workspace_directories.lock().unwrap().clone();
let environments_using_spawn = environment_locations_spawn::list_environments(
&poetry_executable,
&project_dirs,
&workspace_dirs,
&manager,
);

let result = self.search_result.lock().unwrap().clone();
let _ = report_missing_envs(
reporter,
&poetry_executable,
project_dirs,
workspace_dirs,
&self.env_vars,
&environments_using_spawn,
result,
Expand All @@ -139,13 +139,13 @@ impl Locator for Poetry {
"Poetry"
}
fn configure(&self, config: &Configuration) {
if let Some(project_directories) = &config.project_directories {
self.project_directories.lock().unwrap().clear();
if !project_directories.is_empty() {
self.project_directories
if let Some(workspace_directories) = &config.workspace_directories {
self.workspace_directories.lock().unwrap().clear();
if !workspace_directories.is_empty() {
self.workspace_directories
.lock()
.unwrap()
.extend(project_directories.clone());
.extend(workspace_directories.clone());
}
}
if let Some(exe) = &config.poetry_executable {
Expand Down
14 changes: 7 additions & 7 deletions crates/pet-poetry/src/telemetry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ use crate::{config::Config, env_variables::EnvVariables, environment_locations_s
pub fn report_missing_envs(
reporter: &dyn Reporter,
executable: &PathBuf,
project_dirs: Vec<PathBuf>,
workspace_dirs: Vec<PathBuf>,
env_vars: &EnvVariables,
envs_discovered_by_poetry: &[PythonEnvironment],
envs_discovered_by_us: Option<LocatorResult>,
user_provided_poetry_exe: bool,
) -> Option<()> {
for projec_dir in project_dirs {
let config = get_config(executable, &projec_dir);
for workspace_dir in workspace_dirs {
let config = get_config(executable, &workspace_dir);
let global_config = Config::find_global(env_vars);
let local_config = Config::find_local(&projec_dir, env_vars);
let local_config = Config::find_local(&workspace_dir, env_vars);

let global_virtualenvs_path = global_config.clone().map(|c| c.virtualenvs_path.clone());
let local_virtualenvs_path = local_config.clone().map(|c| c.virtualenvs_path.clone());
Expand All @@ -47,12 +47,12 @@ pub fn report_missing_envs(
.map(|e| e.environments.clone())
.unwrap_or_default()
.iter()
.filter(|e| e.project == Some(projec_dir.clone()))
.filter(|e| e.project == Some(workspace_dir.clone()))
.flat_map(|e| e.prefix.clone())
.collect();
let envs_discovered_by_poetry: HashSet<_> = envs_discovered_by_poetry
.iter()
.filter(|e| e.project == Some(projec_dir.clone()))
.filter(|e| e.project == Some(workspace_dir.clone()))
.flat_map(|e| e.prefix.clone())
.collect();

Expand Down Expand Up @@ -118,7 +118,7 @@ pub fn report_missing_envs(
}
warn!(
"Missing Poetry envs: {:?} for {:?}",
missing_envs, projec_dir
missing_envs, workspace_dir
);

let missing_info = MissingPoetryEnvironments {
Expand Down
14 changes: 7 additions & 7 deletions crates/pet/src/find.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub struct Summary {
pub find_locators_time: Duration,
pub find_path_time: Duration,
pub find_global_virtual_envs_time: Duration,
pub find_search_paths_time: Duration,
pub find_workspace_directories_time: Duration,
}

pub fn find_and_report_envs(
Expand All @@ -42,13 +42,13 @@ pub fn find_and_report_envs(
find_locators_time: Duration::from_secs(0),
find_path_time: Duration::from_secs(0),
find_global_virtual_envs_time: Duration::from_secs(0),
find_search_paths_time: Duration::from_secs(0),
find_workspace_directories_time: Duration::from_secs(0),
}));
let start = std::time::Instant::now();

// From settings
let environment_directories = configuration.environment_directories.unwrap_or_default();
let project_directories = configuration.project_directories.unwrap_or_default();
let workspace_directories = configuration.workspace_directories.unwrap_or_default();
thread::scope(|s| {
// 1. Find using known global locators.
s.spawn(|| {
Expand Down Expand Up @@ -129,20 +129,20 @@ pub fn find_and_report_envs(
// & users can have a lot of workspace folders and can have a large number fo files/directories
// that could the discovery.
s.spawn(|| {
if project_directories.is_empty() {
if workspace_directories.is_empty() {
return;
}
trace!(
"Searching for environments in custom folders: {:?}",
project_directories
workspace_directories
);
let start = std::time::Instant::now();
find_python_environments_in_workspace_folders_recursive(
project_directories,
workspace_directories,
reporter,
locators,
);
summary.lock().unwrap().find_search_paths_time = start.elapsed();
summary.lock().unwrap().find_workspace_directories_time = start.elapsed();
});
});
summary.lock().unwrap().time = start.elapsed();
Expand Down
Loading
Loading