-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathlib.rs
More file actions
403 lines (362 loc) · 12.8 KB
/
lib.rs
File metadata and controls
403 lines (362 loc) · 12.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
use find::find_and_report_envs;
use find::SearchScope;
use locators::create_locators;
use pet_conda::Conda;
use pet_conda::CondaLocator;
use pet_core::os_environment::Environment;
use pet_core::python_environment::PythonEnvironmentKind;
use pet_core::Locator;
use pet_core::{os_environment::EnvironmentApi, reporter::Reporter, Configuration};
use pet_fs::glob::expand_glob_patterns;
use pet_poetry::Poetry;
use pet_poetry::PoetryLocator;
use pet_python_utils::cache::set_cache_directory;
use pet_reporter::{self, cache::CacheReporter, collect, stdio};
use resolve::resolve_environment;
use serde::Serialize;
use std::path::PathBuf;
use std::{collections::BTreeMap, env, sync::Arc, time::SystemTime};
use tracing_subscriber::{fmt, layer::SubscriberExt, util::SubscriberInitExt, EnvFilter};
pub mod find;
pub mod locators;
pub mod resolve;
/// Initialize tracing subscriber for performance profiling.
/// Set RUST_LOG=info or RUST_LOG=pet=debug for more detailed traces.
/// Set PET_TRACE_FORMAT=json for JSON output (useful for analysis tools).
///
/// Note: This replaces the env_logger initialization since tracing-subscriber
/// provides a log compatibility layer via tracing-log.
pub fn initialize_tracing(verbose: bool) {
use std::sync::Once;
static INIT: Once = Once::new();
INIT.call_once(|| {
let filter = if verbose {
EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("pet=debug"))
} else {
EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("warn"))
};
let use_json = env::var("PET_TRACE_FORMAT")
.map(|v| v == "json")
.unwrap_or(false);
if use_json {
tracing_subscriber::registry()
.with(filter)
.with(fmt::layer().json().with_writer(std::io::stderr))
.init();
} else {
tracing_subscriber::registry()
.with(filter)
.with(
fmt::layer()
.with_target(true)
.with_timer(fmt::time::uptime())
.with_writer(std::io::stderr),
)
.init();
}
});
}
#[derive(Debug, Clone)]
pub struct FindOptions {
pub print_list: bool,
pub print_summary: bool,
pub verbose: bool,
pub report_missing: bool,
pub search_paths: Option<Vec<PathBuf>>,
pub workspace_only: bool,
pub cache_directory: Option<PathBuf>,
pub kind: Option<PythonEnvironmentKind>,
pub json: bool,
pub conda_executable: Option<PathBuf>,
pub pipenv_executable: Option<PathBuf>,
pub poetry_executable: Option<PathBuf>,
pub environment_directories: Option<Vec<PathBuf>>,
}
pub fn find_and_report_envs_stdio(options: FindOptions) {
// Initialize tracing for performance profiling (includes log compatibility)
initialize_tracing(options.verbose);
// Note: We don't call stdio::initialize_logger here anymore since
// tracing-subscriber provides log compatibility via tracing-log crate.
// stdio::initialize_logger would conflict with our tracing subscriber.
let now = SystemTime::now();
let config = create_config(&options);
let search_scope = if options.workspace_only {
Some(SearchScope::Workspace)
} else {
options.kind.map(SearchScope::Global)
};
if let Some(cache_directory) = options.cache_directory.clone() {
set_cache_directory(cache_directory);
}
let environment = EnvironmentApi::new();
let conda_locator = Arc::new(Conda::from(&environment));
let poetry_locator = Arc::new(Poetry::from(&environment));
let locators = create_locators(conda_locator.clone(), poetry_locator.clone(), &environment);
for locator in locators.iter() {
locator.configure(&config);
}
if options.json {
find_envs_json(
&options,
&locators,
config,
conda_locator.as_ref(),
poetry_locator.as_ref(),
&environment,
search_scope,
);
} else {
find_envs(
&options,
&locators,
config,
conda_locator.as_ref(),
poetry_locator.as_ref(),
&environment,
search_scope,
);
println!("Completed in {}ms", now.elapsed().unwrap().as_millis())
}
}
fn create_config(options: &FindOptions) -> Configuration {
let mut config = Configuration::default();
let mut search_paths = vec![];
if let Some(dirs) = options.search_paths.as_ref() {
search_paths.extend(expand_glob_patterns(dirs));
}
// If workspace folders have been provided do not add cwd.
if search_paths.is_empty() {
if let Ok(cwd) = env::current_dir() {
search_paths.push(cwd);
}
}
search_paths.sort();
search_paths.dedup();
config.workspace_directories = Some(
search_paths
.iter()
.filter(|d| d.is_dir())
.cloned()
.collect(),
);
config.executables = Some(
search_paths
.iter()
.filter(|d| d.is_file())
.cloned()
.collect(),
);
config.conda_executable = options.conda_executable.clone();
config.pipenv_executable = options.pipenv_executable.clone();
config.poetry_executable = options.poetry_executable.clone();
config.environment_directories = options.environment_directories.as_ref().map(|dirs| {
expand_glob_patterns(dirs)
.into_iter()
.filter(|p| p.is_dir())
.collect()
});
config
}
fn find_envs(
options: &FindOptions,
locators: &Arc<Vec<Arc<dyn Locator>>>,
config: Configuration,
conda_locator: &Conda,
poetry_locator: &Poetry,
environment: &dyn Environment,
search_scope: Option<SearchScope>,
) {
let kind = match search_scope {
Some(SearchScope::Global(kind)) => Some(kind),
_ => None,
};
let stdio_reporter = Arc::new(stdio::create_reporter(options.print_list, kind));
let reporter = CacheReporter::new(stdio_reporter.clone());
let summary = find_and_report_envs(&reporter, config, locators, environment, search_scope);
if options.report_missing {
// By now all conda envs have been found
// Spawn conda
// & see if we can find more environments by spawning conda.
let _ =
conda_locator.find_and_report_missing_envs(&reporter, options.conda_executable.clone());
let _ = poetry_locator
.find_and_report_missing_envs(&reporter, options.poetry_executable.clone());
}
if options.print_summary {
let summary = summary.lock().expect("summary mutex poisoned");
if !summary.locators.is_empty() {
println!();
println!("Breakdown by each locator:");
println!("--------------------------");
for locator in summary.locators.iter() {
println!("{:<20} : {:?}", format!("{:?}", locator.0), locator.1);
}
println!()
}
if !summary.breakdown.is_empty() {
println!("Breakdown for finding Environments:");
println!("-----------------------------------");
for item in summary.breakdown.iter() {
println!("{:<20} : {:?}", item.0, item.1);
}
println!();
}
let summary = stdio_reporter.get_summary();
// If verbose, print the paths of discovered environments first
if options.verbose && !summary.environment_paths.is_empty() {
println!("Environment Paths:");
println!("------------------");
for (kind, envs) in summary.environment_paths.iter() {
let kind_str = kind
.map(|v| format!("{v:?}"))
.unwrap_or("Unknown".to_string());
println!("\n{kind_str}:");
for env in envs {
if let Some(executable) = &env.executable {
println!(" - {}", executable.display());
}
}
}
println!()
}
if !summary.managers.is_empty() {
println!("Managers:");
println!("---------");
for (k, v) in summary
.managers
.clone()
.into_iter()
.map(|(k, v)| (format!("{k:?}"), v))
.collect::<BTreeMap<String, u16>>()
{
println!("{k:<20} : {v:?}");
}
println!()
}
if !summary.environments.is_empty() {
let total = summary
.environments
.clone()
.iter()
.fold(0, |total, b| total + b.1);
println!("Environments ({total}):");
println!("------------------");
for (k, v) in summary
.environments
.clone()
.into_iter()
.map(|(k, v)| {
(
k.map(|v| format!("{v:?}")).unwrap_or("Unknown".to_string()),
v,
)
})
.collect::<BTreeMap<String, u16>>()
{
println!("{k:<20} : {v:?}");
}
println!()
}
}
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct JsonOutput {
managers: Vec<pet_core::manager::EnvManager>,
environments: Vec<pet_core::python_environment::PythonEnvironment>,
}
fn find_envs_json(
options: &FindOptions,
locators: &Arc<Vec<Arc<dyn Locator>>>,
config: Configuration,
conda_locator: &Conda,
poetry_locator: &Poetry,
environment: &dyn Environment,
search_scope: Option<SearchScope>,
) {
let collect_reporter = Arc::new(collect::create_reporter());
let reporter = CacheReporter::new(collect_reporter.clone());
find_and_report_envs(&reporter, config, locators, environment, search_scope);
if options.report_missing {
let _ =
conda_locator.find_and_report_missing_envs(&reporter, options.conda_executable.clone());
let _ = poetry_locator
.find_and_report_missing_envs(&reporter, options.poetry_executable.clone());
}
let managers = collect_reporter
.managers
.lock()
.expect("managers mutex poisoned")
.clone();
let mut environments = collect_reporter
.environments
.lock()
.expect("environments mutex poisoned")
.clone();
if let Some(kind) = options.kind {
environments.retain(|e| e.kind == Some(kind));
}
let output = JsonOutput {
managers,
environments,
};
println!(
"{}",
serde_json::to_string_pretty(&output).expect("failed to serialize environments as JSON")
);
}
pub fn resolve_report_stdio(
executable: PathBuf,
verbose: bool,
cache_directory: Option<PathBuf>,
json: bool,
) {
// Initialize tracing for performance profiling (includes log compatibility)
initialize_tracing(verbose);
// Note: We don't call stdio::initialize_logger here anymore since
// tracing-subscriber provides log compatibility via tracing-log crate.
let now = SystemTime::now();
if let Some(cache_directory) = cache_directory.clone() {
set_cache_directory(cache_directory);
}
let stdio_reporter = Arc::new(stdio::create_reporter(true, None));
let reporter = CacheReporter::new(stdio_reporter.clone());
let environment = EnvironmentApi::new();
let conda_locator = Arc::new(Conda::from(&environment));
let poetry_locator = Arc::new(Poetry::from(&environment));
let mut config = Configuration::default();
if let Ok(cwd) = env::current_dir() {
config.workspace_directories = Some(vec![cwd]);
}
let locators = create_locators(conda_locator.clone(), poetry_locator.clone(), &environment);
for locator in locators.iter() {
locator.configure(&config);
}
if let Some(result) = resolve_environment(&executable, &locators, &environment) {
let env = &result.resolved.unwrap_or(result.discovered);
if json {
println!(
"{}",
serde_json::to_string_pretty(env).expect("failed to serialize environment as JSON")
);
} else {
println!("Environment found for {executable:?}");
if let Some(manager) = &env.manager {
reporter.report_manager(manager);
}
reporter.report_environment(env);
}
} else if json {
println!("null");
} else {
println!("No environment found for {executable:?}");
}
if !json {
println!(
"Resolve completed in {}ms",
now.elapsed().unwrap().as_millis()
)
}
}