-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathmain.rs
More file actions
166 lines (144 loc) · 5.07 KB
/
main.rs
File metadata and controls
166 lines (144 loc) · 5.07 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
use std::path::PathBuf;
use clap::{Parser, Subcommand};
use jsonrpc::start_jsonrpc_server;
use pet::{find_and_report_envs_stdio, resolve_report_stdio, FindOptions};
use pet_core::python_environment::PythonEnvironmentKind;
mod find;
mod jsonrpc;
mod locators;
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
pub struct Cli {
#[command(subcommand)]
command: Option<Commands>,
}
#[derive(Subcommand, Debug)]
enum Commands {
/// Finds the environments and reports them to the standard output.
Find {
/// List of files/folders to search for environments.
/// The current directory is automatically used as a workspace folder if none provided.
#[arg(value_name = "SEARCH PATHS")]
search_paths: Option<Vec<PathBuf>>,
/// List the environments found.
#[arg(short, long)]
list: bool,
/// Directory to cache the environment information after spawning Python.
#[arg(short, long, env = "PET_CACHE_DIRECTORY")]
cache_directory: Option<PathBuf>,
/// Display verbose output (defaults to warnings).
#[arg(short, long)]
verbose: bool,
/// Look for missing environments and report them (e.g. spawn conda and find what was missed).
#[arg(short, long)]
report_missing: bool,
/// Exclusively search just the workspace directories.
/// I.e. exclude all global environments.
#[arg(short, long, conflicts_with = "kind")]
workspace: bool,
/// Exclusively search for a specific Python environment kind.
/// Will not search in the workspace directories.
#[arg(short, long, conflicts_with = "workspace")]
kind: Option<PythonEnvironmentKind>,
/// Output results as JSON.
#[arg(short, long)]
json: bool,
/// Path to the conda or mamba executable.
#[arg(long, env = "PET_CONDA_EXECUTABLE")]
conda_executable: Option<PathBuf>,
/// Path to the pipenv executable.
#[arg(long, env = "PET_PIPENV_EXECUTABLE")]
pipenv_executable: Option<PathBuf>,
/// Path to the poetry executable.
#[arg(long, env = "PET_POETRY_EXECUTABLE")]
poetry_executable: Option<PathBuf>,
/// Additional directories where virtual environments can be found.
/// Use comma-separated values when setting via the environment variable.
#[arg(long, env = "PET_ENVIRONMENT_DIRECTORIES", value_delimiter = ',')]
environment_directories: Option<Vec<PathBuf>>,
},
/// Resolves & reports the details of the the environment to the standard output.
Resolve {
/// Fully qualified path to the Python executable
#[arg(value_name = "PYTHON EXE")]
executable: PathBuf,
/// Directory to cache the environment information after spawning Python.
#[arg(short, long, env = "PET_CACHE_DIRECTORY")]
cache_directory: Option<PathBuf>,
/// Whether to display verbose output (defaults to warnings).
#[arg(short, long)]
verbose: bool,
/// Output results as JSON.
#[arg(short, long)]
json: bool,
},
/// Starts the JSON RPC Server.
Server,
}
fn main() {
let cli = Cli::parse();
match cli.command.unwrap_or(Commands::Find {
list: true,
verbose: false,
report_missing: false,
search_paths: None,
workspace: false,
cache_directory: None,
kind: None,
json: false,
conda_executable: None,
pipenv_executable: None,
poetry_executable: None,
environment_directories: None,
}) {
Commands::Find {
list,
verbose,
report_missing,
search_paths,
workspace,
cache_directory,
kind,
json,
conda_executable,
pipenv_executable,
poetry_executable,
environment_directories,
} => {
let mut workspace_only = workspace;
if search_paths.clone().is_some()
&& search_paths
.clone()
.unwrap_or_default()
.iter()
.all(|f| f.is_file())
{
workspace_only = true;
}
find_and_report_envs_stdio(FindOptions {
print_list: list,
print_summary: true,
verbose,
report_missing,
search_paths,
workspace_only,
cache_directory,
kind,
json,
conda_executable,
pipenv_executable,
poetry_executable,
environment_directories,
});
}
Commands::Resolve {
executable,
verbose,
cache_directory,
json,
} => resolve_report_stdio(executable, verbose, cache_directory, json),
Commands::Server => start_jsonrpc_server(),
}
}