-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.rs
260 lines (231 loc) · 8.36 KB
/
lib.rs
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
pub mod check;
pub mod cli;
pub mod conda_deny_config;
mod conda_meta_entry;
mod conda_meta_package;
pub mod expression_utils;
mod license_info;
pub mod license_whitelist;
pub mod list;
mod pixi_lock;
use std::{env, path::PathBuf};
use cli::CondaDenyCliConfig;
use conda_deny_config::CondaDenyTomlConfig;
use license_info::LicenseInfo;
use license_whitelist::{get_license_information_from_toml_config, IgnorePackage};
use anyhow::{Context, Result};
use log::debug;
use rattler_conda_types::Platform;
use serde::Deserialize;
use spdx::Expression;
use crate::license_info::LicenseInfos;
#[derive(Debug)]
pub enum CondaDenyConfig {
Check(CondaDenyCheckConfig),
List(CondaDenyListConfig),
}
#[derive(Debug, Clone, clap::ValueEnum, Default, Deserialize, Copy)]
#[serde(rename_all = "kebab-case")]
pub enum OutputFormat {
#[default]
Default,
Json,
JsonPretty,
Csv,
}
/// Configuration for the check command
#[derive(Debug)]
pub struct CondaDenyCheckConfig {
pub lockfile_or_prefix: LockfileOrPrefix,
pub osi: bool,
pub safe_licenses: Vec<Expression>,
pub ignore_packages: Vec<IgnorePackage>,
pub output_format: OutputFormat,
}
/// Shared configuration between check and list commands
#[derive(Debug)]
pub struct CondaDenyListConfig {
pub lockfile_or_prefix: LockfileOrPrefix,
pub output_format: OutputFormat,
}
#[derive(Debug, Clone)]
pub struct LockfileSpec {
lockfiles: Vec<PathBuf>,
platforms: Option<Vec<Platform>>,
environments: Option<Vec<String>>,
ignore_pypi: bool,
}
#[derive(Debug, Clone)]
pub enum LockfileOrPrefix {
Lockfile(LockfileSpec),
Prefix(Vec<PathBuf>),
}
pub type CheckOutput = (Vec<LicenseInfo>, Vec<LicenseInfo>);
pub fn fetch_license_infos(lockfile_or_prefix: LockfileOrPrefix) -> Result<LicenseInfos> {
match lockfile_or_prefix {
LockfileOrPrefix::Lockfile(lockfile_spec) => {
LicenseInfos::from_pixi_lockfiles(lockfile_spec)
.with_context(|| "Getting license information from config file failed.")
}
LockfileOrPrefix::Prefix(prefixes) => LicenseInfos::from_conda_prefixes(&prefixes)
.with_context(|| "Getting license information from conda prefixes failed."),
}
}
const IGNORE_PYPI_DEFAULT: bool = false;
fn get_lockfile_or_prefix(
cli_config: CondaDenyCliConfig,
toml_config: CondaDenyTomlConfig,
) -> Result<LockfileOrPrefix> {
// cli overrides toml configuration
Ok(LockfileOrPrefix::Prefix(vec!["adwd".into()]))
// if lockfile.is_empty() && prefix.is_empty() {
// // test if pixi.lock exists next to config file, otherwise error
// let default_lockfile_path = env::current_dir()?.join("pixi.lock");
// if !default_lockfile_path.is_file() {
// Err(anyhow::anyhow!("No lockfiles or conda prefixes provided"))
// } else {
// Ok(LockfileOrPrefix::Lockfile(LockfileSpec {
// lockfiles: vec![default_lockfile_path],
// platforms,
// environments,
// ignore_pypi: ignore_pypi.unwrap_or(IGNORE_PYPI_DEFAULT),
// }))
// }
// } else if !lockfile.is_empty() && !prefix.is_empty() {
// // TODO: Specified prefixes override lockfiles
// Err(anyhow::anyhow!(
// "Both lockfiles and conda prefixes provided. Please only provide either or."
// ))
// } else if !lockfile.is_empty() {
// Ok(LockfileOrPrefix::Lockfile(LockfileSpec {
// lockfiles: lockfile.iter().map(|s| s.into()).collect(),
// platforms,
// environments,
// ignore_pypi: ignore_pypi.unwrap_or(IGNORE_PYPI_DEFAULT),
// }))
// } else {
// assert!(!prefix.is_empty());
// if platforms.is_some() {
// Err(anyhow::anyhow!(
// "Cannot specify platforms and conda prefixes at the same time"
// ))
// } else if environments.is_some() {
// Err(anyhow::anyhow!(
// "Cannot specify pixi environments and conda prefixes at the same time"
// ))
// } else if ignore_pypi.is_some() {
// Err(anyhow::anyhow!(
// "Cannot specify ignore-pypi and conda prefixes at the same time"
// ))
// } else {
// Ok(LockfileOrPrefix::Prefix(
// prefix.iter().map(|s| s.into()).collect(),
// ))
// }
// }
}
pub fn get_config_options(
config: Option<PathBuf>,
cli_config: CondaDenyCliConfig,
) -> Result<CondaDenyConfig> {
// if config provided, use config
// else, try to load pixi.toml, then pyproject.toml and if nothing helps, use empty config
let toml_config = if let Some(config_path) = config {
CondaDenyTomlConfig::from_path(config_path.clone())
.with_context(|| format!("Failed to parse config file {:?}", config_path))?
} else {
match CondaDenyTomlConfig::from_path("pixi.toml".into())
.with_context(|| "Failed to parse config file pixi.toml")
{
Ok(config) => {
debug!("Successfully loaded config from pixi.toml");
config
}
Err(e) => {
debug!(
"Error parsing config file: pixi.toml: {}. Attempting to use pyproject.toml instead...",
e
);
match CondaDenyTomlConfig::from_path("pyproject.toml".into())
.context(e)
.with_context(|| "Failed to parse config file pyproject.toml")
{
Ok(config) => config,
Err(e) => {
debug!(
"Error parsing config file: pyproject.toml: {}. Using empty config instead...",
e
);
CondaDenyTomlConfig::empty()
}
}
}
}
};
debug!("Parsed TOML config: {:?}", toml_config);
// cli overrides toml configuration
// let lockfile = cli_config
// .lockfile()
// .unwrap_or(toml_config.get_lockfile_spec());
// let prefix = cli_config.prefix().unwrap_or_default();
// let platforms = if cli_config.platform().is_some() {
// cli_config.platform()
// } else {
// toml_config.get_platform_spec()
// };
// if platforms.is_some() && !prefix.is_empty() {
// return Err(anyhow::anyhow!(
// "Cannot specify platforms and conda prefixes at the same time"
// ));
// }
// let environments = if cli_config.environment().is_some() {
// cli_config.environment()
// } else {
// toml_config.get_environment_spec()
// };
// if environments.is_some() && !prefix.is_empty() {
// return Err(anyhow::anyhow!(
// "Cannot specify environments and conda prefixes at the same time"
// ));
// }
// let ignore_pypi = if cli_config.ignore_pypi().is_some() {
// cli_config.ignore_pypi()
// } else {
// toml_config.get_ignore_pypi()
// };
// let output_format = cli_config.output_format().unwrap_or_default();
let lockfile_or_prefix = get_lockfile_or_prefix(cli_config, toml_config)?;
let config = match cli_config {
CondaDenyCliConfig::Check { osi, .. } => {
// defaults to false
let osi = if osi.is_some() {
osi
} else {
toml_config.get_osi()
}
.unwrap_or(false);
let (safe_licenses, ignore_packages) =
get_license_information_from_toml_config(&toml_config)?;
if osi && !safe_licenses.is_empty() {
return Err(anyhow::anyhow!(
"Cannot use OSI mode and safe-licenses at the same time"
));
}
if !osi && safe_licenses.is_empty() {
return Err(anyhow::anyhow!("No license whitelist provided"));
}
CondaDenyConfig::Check(CondaDenyCheckConfig {
lockfile_or_prefix,
osi,
safe_licenses,
ignore_packages,
output_format,
})
}
CondaDenyCliConfig::List { .. } => CondaDenyConfig::List(CondaDenyListConfig {
lockfile_or_prefix,
output_format,
}),
};
Ok(config)
}