-
-
Notifications
You must be signed in to change notification settings - Fork 419
/
publish.rs
280 lines (244 loc) · 8.87 KB
/
publish.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
use std::io::{Read, Write};
use std::path::PathBuf;
use std::process::{Command, Stdio};
use age::{
secrecy::{ExposeSecret, Secret},
Decryptor, Encryptor,
};
use anyhow::{bail, Context, Error};
use clap::Parser;
use toml_edit::{Item, Table};
use url::Url;
use crate::bootstrap::ensure_self_venv;
use crate::platform::{get_credentials, write_credentials};
use crate::pyproject::PyProject;
use crate::utils::{escape_string, get_venv_python_bin, tui_theme, CommandOutput};
/// Publish packages to a package repository.
#[derive(Parser, Debug)]
pub struct Args {
/// The distribution files to upload to the repository (defaults to <workspace-root>/dist/*).
dist: Option<Vec<PathBuf>>,
/// The repository to publish to.
#[arg(short, long, default_value = "pypi")]
repository: String,
/// The repository url to publish to.
#[arg(long)]
repository_url: Option<Url>,
/// The username to authenticate to the repository with.
#[arg(short, long)]
username: Option<String>,
/// An access token used for the upload.
#[arg(long)]
token: Option<String>,
/// Sign files to upload using GPG.
#[arg(long)]
sign: bool,
/// GPG identity used to sign files.
#[arg(short, long)]
identity: Option<String>,
/// Path to alternate CA bundle.
#[arg(long)]
cert: Option<PathBuf>,
/// Skip prompts.
#[arg(short, long)]
yes: bool,
/// Enables verbose diagnostics.
#[arg(short, long)]
verbose: bool,
/// Turns off all output.
#[arg(short, long, conflicts_with = "verbose")]
quiet: bool,
}
pub fn execute(cmd: Args) -> Result<(), Error> {
let output = CommandOutput::from_quiet_and_verbose(cmd.quiet, cmd.verbose);
let venv = ensure_self_venv(output)?;
let project = PyProject::discover()?;
if project.is_virtual() {
bail!("virtual packages cannot be published");
}
// Get the files to publish.
let files = match cmd.dist {
Some(paths) => paths,
None => vec![project.workspace_path().join("dist").join("*")],
};
// a. Get token from arguments and offer encryption, then store in credentials file.
// b. Get token from ~/.rye/credentials keyed by provided repository and provide decryption option.
// c. Otherwise prompt for token and provide encryption option, storing the result in credentials.
let repository = &cmd.repository;
let mut credentials = get_credentials()?;
credentials
.entry(repository)
.or_insert(Item::Table(Table::new()));
let repository_url = match cmd.repository_url {
Some(url) => url,
None => {
let default_repository_url = Url::parse("https://upload.pypi.org/legacy/")?;
credentials
.get(repository)
.and_then(|table| table.get("repository-url"))
.map(|url| match Url::parse(&escape_string(url.to_string())) {
Ok(url) => url,
Err(_) => default_repository_url.clone(),
})
.unwrap_or(default_repository_url)
}
};
// If -r is pypi but the url isn't pypi then bail
if repository == "pypi" && repository_url.domain() != Some("upload.pypi.org") {
bail!("invalid pypi url {} (use -h for help)", repository_url);
}
let username = match cmd.username {
Some(username) => username,
None => credentials
.get(repository)
.and_then(|table| table.get("username"))
.map(|username| username.to_string())
.map(escape_string)
.unwrap_or("__token__".to_string()),
};
let token = if let Some(token) = cmd.token {
let secret = Secret::new(token);
let maybe_encrypted = maybe_encrypt(&secret, cmd.yes)?;
let maybe_encoded = maybe_encode(&secret, &maybe_encrypted);
credentials[repository]["token"] = Item::Value(maybe_encoded.expose_secret().into());
write_credentials(&credentials)?;
secret
} else if let Some(token) = credentials
.get(repository)
.and_then(|table| table.get("token"))
.map(|token| token.to_string())
.map(escape_string)
{
let secret = Secret::new(token);
maybe_decrypt(&secret, cmd.yes)?
} else {
echo!("No access token found, generate one at: https://pypi.org/manage/account/token/");
let token = if !cmd.yes {
prompt_for_token()?
} else {
"".to_string()
};
if token.is_empty() {
bail!("an access token is required")
}
let secret = Secret::new(token);
let maybe_encrypted = maybe_encrypt(&secret, cmd.yes)?;
let maybe_encoded = maybe_encode(&secret, &maybe_encrypted);
credentials[repository]["token"] = Item::Value(maybe_encoded.expose_secret().into());
secret
};
credentials[repository]["repository-url"] = Item::Value(repository_url.to_string().into());
credentials[repository]["username"] = Item::Value(username.clone().into());
write_credentials(&credentials)?;
let mut publish_cmd = Command::new(get_venv_python_bin(&venv));
publish_cmd
.arg("-mtwine")
.arg("--no-color")
.arg("upload")
.args(files)
.arg("--username")
.arg(username)
.arg("--password")
.arg(token.expose_secret())
.arg("--repository-url")
.arg(repository_url.to_string());
if cmd.sign {
publish_cmd.arg("--sign");
}
if let Some(identity) = cmd.identity {
publish_cmd.arg("--identity").arg(identity);
}
if let Some(cert) = cmd.cert {
publish_cmd.arg("--cert").arg(cert);
}
if output == CommandOutput::Quiet {
publish_cmd.stdout(Stdio::null());
publish_cmd.stderr(Stdio::null());
}
let status = publish_cmd.status()?;
if !status.success() {
bail!("failed to publish files");
}
Ok(())
}
fn prompt_for_token() -> Result<String, Error> {
eprint!("Access token: ");
let token = get_trimmed_user_input().context("failed to read provided token")?;
Ok(token)
}
fn maybe_encrypt(secret: &Secret<String>, yes: bool) -> Result<Secret<Vec<u8>>, Error> {
let phrase = if !yes {
dialoguer::Password::with_theme(tui_theme())
.with_prompt("Encrypt with passphrase (optional)")
.allow_empty_password(true)
.report(false)
.interact()
.map(Secret::new)?
} else {
Secret::new("".to_string())
};
let token = if phrase.expose_secret().is_empty() {
secret.expose_secret().as_bytes().to_vec()
} else {
// Do the encryption
let encryptor = Encryptor::with_user_passphrase(phrase);
let mut encrypted = vec![];
let mut writer = encryptor.wrap_output(&mut encrypted)?;
writer.write_all(secret.expose_secret().as_bytes())?;
writer.finish()?;
encrypted
};
Ok(Secret::new(token.to_vec()))
}
fn maybe_decrypt(secret: &Secret<String>, yes: bool) -> Result<Secret<String>, Error> {
let phrase = if !yes {
dialoguer::Password::with_theme(tui_theme())
.with_prompt("Decrypt with passphrase (optional)")
.allow_empty_password(true)
.report(false)
.interact()
.map(Secret::new)?
} else {
Secret::new("".to_string())
};
if phrase.expose_secret().is_empty() {
return Ok(secret.clone());
}
// If a passphrase is provided we assume the secret is encoded bytes from encryption.
let bytes = hex::decode(pad_hex(secret.expose_secret().clone()))?;
if let Decryptor::Passphrase(decryptor) = Decryptor::new(bytes.as_slice())? {
// Do the decryption
let mut decrypted = vec![];
let mut reader = decryptor.decrypt(&phrase, None)?;
reader.read_to_end(&mut decrypted)?;
let token = String::from_utf8(decrypted).context("failed to parse utf-8")?;
let secret = Secret::new(token);
return Ok(secret);
}
bail!("failed to decrypt")
}
fn get_trimmed_user_input() -> Result<String, Error> {
std::io::stderr().flush()?;
let mut input = String::new();
std::io::stdin().read_line(&mut input)?;
Ok(input.trim().to_string())
}
/// Helper function to manage potentially encoding secret data.
///
/// If the original secret data (bytes) are not the same as the new secret's
/// then we encode, assuming the new data is encrypted data. Otherwise return
/// a new secret with the same string.
fn maybe_encode(original_secret: &Secret<String>, new_secret: &Secret<Vec<u8>>) -> Secret<String> {
if original_secret.expose_secret().as_bytes() != new_secret.expose_secret() {
let encoded = hex::encode(new_secret.expose_secret());
return Secret::new(encoded);
}
original_secret.clone()
}
fn pad_hex(s: String) -> String {
if s.len() % 2 == 1 {
format!("0{}", s)
} else {
s
}
}