Skip to content

Commit

Permalink
Simplify upload retry logic
Browse files Browse the repository at this point in the history
  • Loading branch information
konstin committed Jul 20, 2019
1 parent b200633 commit 4087a4b
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 63 deletions.
124 changes: 63 additions & 61 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ fn upload_ui(build: BuildOptions, publish: &PublishOpt, no_sdist: bool) -> Resul
let build_context = build.into_build_context(!publish.debug, !publish.no_strip)?;

if !build_context.release {
eprintln!("⚠ Warning: You're publishing debug wheels");
eprintln!("⚠ Warning: You're publishing debug wheels");
}

let mut wheels = build_context.build_wheels()?;
Expand All @@ -320,72 +320,74 @@ fn upload_ui(build: BuildOptions, publish: &PublishOpt, no_sdist: bool) -> Resul
loop {
println!("🚀 Uploading {} packages", wheels.len());

for (wheel_path, supported_versions, _) in &wheels {
let result = upload(
&registry,
&wheel_path,
&build_context.metadata21,
&supported_versions,
);
match result {
Ok(()) => {
println!("✨ Packages uploaded succesfully");

#[cfg(feature = "keyring")]
{
// We know the password is correct, so we can save it in the keyring
let username = registry.username.clone();
let keyring = Keyring::new(&env!("CARGO_PKG_NAME"), &username);
let password = registry.password.clone();
keyring.set_password(&password).unwrap_or_else(|e| {
eprintln!("⚠ Failed to store the password in the keyring: {:?}", e)
});
let upload_result = wheels
.iter()
.map(|(wheel_path, supported_versions, _)| {
let result = upload(
&registry,
&wheel_path,
&build_context.metadata21,
&supported_versions,
);
result.map_err(|err| (wheel_path.clone(), err))
})
.collect();

match upload_result {
Ok(()) => break,
Err((_, UploadError::AuthenticationError)) if reenter => {
println!("⛔ Username and/or password are wrong");

#[cfg(feature = "keyring")]
{
// Delete the wrong password from the keyring
let old_username = registry.username.clone();
let keyring = Keyring::new(&env!("CARGO_PKG_NAME"), &old_username);
match keyring.delete_password() {
Ok(()) => {}
Err(KeyringError::NoPasswordFound) | Err(KeyringError::NoBackendFound) => {}
_ => eprintln!("⚠ Failed to remove password from keyring"),
}

return Ok(());
}
Err(UploadError::AuthenticationError) if reenter => {
println!("⛔ Username and/or password are wrong");

#[cfg(feature = "keyring")]
{
// Delete the wrong password from the keyring
let old_username = registry.username.clone();
let keyring = Keyring::new(&env!("CARGO_PKG_NAME"), &old_username);
match keyring.delete_password() {
Ok(()) => {}
Err(KeyringError::NoPasswordFound)
| Err(KeyringError::NoBackendFound) => {}
_ => eprintln!("⚠ Failed to remove password from keyring"),
}
}

let username = get_username();
let password =
rpassword::prompt_password_stdout("Please enter your password: ")
.unwrap_or_else(|_| {
// So we need this fallback for pycharm on windows
let mut password = String::new();
io::stdin()
.read_line(&mut password)
.expect("Failed to read line");
password.trim().to_string()
});

registry = Registry::new(username, password, registry.url);
println!("… Retrying");
break;
}
Err(UploadError::AuthenticationError) => {
bail!("Username and/or password are wrong");
}
Err(err) => {
let filename = wheel_path.file_name().unwrap_or(&wheel_path.as_os_str());
return Err(err).context(format!("💥 Failed to upload {:?}", filename))?;
}
let username = get_username();
let password = rpassword::prompt_password_stdout("Please enter your password: ")
.unwrap_or_else(|_| {
// So we need this fallback for pycharm on windows
let mut password = String::new();
io::stdin()
.read_line(&mut password)
.expect("Failed to read line");
password.trim().to_string()
});

registry = Registry::new(username, password, registry.url);
println!("… Retrying");
}
Err((_, UploadError::AuthenticationError)) => {
bail!("Username and/or password are wrong");
}
Err((wheel_path, err)) => {
let filename = wheel_path.file_name().unwrap_or(&wheel_path.as_os_str());
return Err(err).context(format!("💥 Failed to upload {:?}", filename))?;
}
}
}

println!("✨ Packages uploaded succesfully");

#[cfg(feature = "keyring")]
{
// We know the password is correct, so we can save it in the keyring
let username = registry.username.clone();
let keyring = Keyring::new(&env!("CARGO_PKG_NAME"), &username);
let password = registry.password.clone();
keyring.set_password(&password).unwrap_or_else(|e| {
eprintln!("⚠ Failed to store the password in the keyring: {:?}", e)
});
}

Ok(())
}

fn run() -> Result<(), Error> {
Expand Down
2 changes: 1 addition & 1 deletion test-crates/pyo3-mixed/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test-crates/pyo3-mixed/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
authors = ["konstin <konstin@mailbox.org>"]
name = "pyo3-mixed"
version = "2.1.0"
version = "2.1.1"
description = "Implements a dummy function combining rust and python"
readme = "Readme.md"
edition = "2018"
Expand Down

0 comments on commit 4087a4b

Please sign in to comment.