Skip to content

Commit

Permalink
Finish the upgrade to reqwest 0.9
Browse files Browse the repository at this point in the history
  • Loading branch information
konstin committed Sep 20, 2018
1 parent ea7486a commit bafd0f9
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 53 deletions.
8 changes: 5 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
authors = ["konstin <[email protected]>"]
name = "pyo3-pack"
version = "0.3.4"
version = "0.3.5"
description = "Build and publish crates with pyo3 bindings as python packages"
exclude = ["get-fourtytwo/**/*", "integration-test/**/*", "sysconfig/*"]
readme = "Readme.md"
Expand All @@ -26,7 +26,7 @@ cargo_metadata = "0.6.0"
digest = { version = "0.7.5", features = ["std"] }
failure = "0.1.2"
keyring = { version = "0.6.0", optional = true }
reqwest = { git = "https://github.com/seanmonstar/reqwest", rev = "2698148743c9ab6dcf3ca664d235b458b2cfd426", optional = true }
reqwest = { git = "https://github.com/seanmonstar/reqwest", rev = "15857a11a43ee6782bb46b87043cb647c5f149c6", optional = true }
rpassword = "2.0.0"
serde_derive = "1.0.79"
serde_json = "1.0.28"
Expand All @@ -45,15 +45,17 @@ atty = "0.2.11"
tempfile = "3.0.4"
goblin = { version = "0.0.17", optional = true }
openssl = { version = "0.10.12", features = ["vendored"], optional = true }
pretty_env_logger = { version = "0.2.4", optional = true }

[dev-dependencies]
indoc = "0.2.8"

[features]
default = ["auditwheel", "upload"]
default = ["auditwheel", "upload", "log"]
auditwheel = ["goblin"]
upload = ["reqwest"]
password-storage = ["upload", "keyring"]
log = ["pretty_env_logger"]

# This will make rewquest use a statically linked version of openssl
musl = ["openssl"]
Expand Down
11 changes: 11 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.3.5] - 2018-09-20

### Changed

* Upgraded to reqwest 0.9

### Fixed

* "Broken Pipe" with musl builds (through the reqwest upgrade)

## [0.3.4] - 2018-09-18

### Added
Expand Down Expand Up @@ -69,6 +79,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Initial Release

[Unreleased]: https://github.com/pyo3/pyo3-pack/compare/v0.3.3...HEAD
[0.3.5]: https://github.com/pyo3/pyo3-pack/compare/v0.3.4...v0.3.5
[0.3.4]: https://github.com/pyo3/pyo3-pack/compare/v0.3.3...v0.3.4
[0.3.3]: https://github.com/pyo3/pyo3-pack/compare/v0.3.1...v0.3.3
[0.3.1]: https://github.com/pyo3/pyo3-pack/compare/v0.3.0...v0.3.1
Expand Down
2 changes: 1 addition & 1 deletion get-fourtytwo/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
authors = ["konstin <[email protected]>"]
name = "get-fourtytwo"
version = "2.0.0"
version = "2.0.1"
description = "This implements a dummy function (get_fortytwo.DummyClass.get_42()) in rust"
readme = "Readme.md"

Expand Down
107 changes: 66 additions & 41 deletions src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@ use std::str;
use BuildContext;
use PythonInterpreter;

#[derive(Deserialize)]
#[derive(Deserialize, Debug, Clone)]
struct BuildPlanEntry {
package_name: String,
program: String,
}

/// The (abbreviated) format of `cargo build --build-plan`
/// For the real thing, see
/// https://github.com/rust-lang/cargo/blob/master/src/cargo/core/compiler/build_plan.rs
#[derive(Deserialize)]
#[derive(Deserialize, Debug, Clone)]
struct SerializedBuildPlan {
invocations: Vec<BuildPlanEntry>,
}
Expand All @@ -40,7 +41,7 @@ struct SerializedBuildPlan {
/// reason: "build-script-executed",
/// }
/// ```
#[derive(Serialize, Deserialize)]
#[derive(Serialize, Deserialize, Debug, Clone)]
struct CargoBuildOutput {
cfgs: Vec<String>,
env: Vec<String>,
Expand All @@ -52,25 +53,26 @@ struct CargoBuildOutput {

/// This kind of message is printed by `cargo build --message-format=json
/// --quiet` for an artifact such as an .so/.dll
#[derive(Serialize, Deserialize)]
#[derive(Serialize, Deserialize, Debug, Clone)]
struct CompilerArtifactMessage {
filenames: Vec<PathBuf>,
target: CompilerTargetMessage,
package_id: String,
}

#[derive(Serialize, Deserialize)]
#[derive(Serialize, Deserialize, Debug, Clone)]
struct CompilerTargetMessage {
crate_types: Vec<String>,
name: String,
}

#[derive(Serialize, Deserialize)]
#[derive(Serialize, Deserialize, Debug, Clone)]
struct CompilerErrorMessage {
message: CompilerErrorMessageMessage,
reason: String,
}

#[derive(Serialize, Deserialize)]
#[derive(Serialize, Deserialize, Debug, Clone)]
struct CompilerErrorMessageMessage {
rendered: String,
}
Expand All @@ -86,7 +88,7 @@ fn get_build_plan(shared_args: &[&str]) -> Result<SerializedBuildPlan, Error> {
"--build-plan",
];

let command_formated = ["cargo"]
let command_formatted = ["cargo"]
.iter()
.chain(build_plan_args)
.chain(shared_args)
Expand All @@ -104,15 +106,15 @@ fn get_build_plan(shared_args: &[&str]) -> Result<SerializedBuildPlan, Error> {
format_err!(
"Failed to get a build plan from cargo: {} ({})",
e,
command_formated
command_formatted
)
})?;

if !build_plan.status.success() {
bail!(
"Failed to get a build plan from cargo with '{}': `{}`",
build_plan.status,
command_formated
command_formatted
);
}

Expand All @@ -121,6 +123,39 @@ fn get_build_plan(shared_args: &[&str]) -> Result<SerializedBuildPlan, Error> {
Ok(plan)
}

fn get_progress_plan(shared_args: &[&str]) -> Option<(ProgressBar, Vec<String>)> {
if atty::is(Stream::Stderr) {
match get_build_plan(shared_args) {
Ok(build_plan) => {
let mut packages: Vec<String> = build_plan
.invocations
.iter()
.filter(|x| x.program == "rustc") // Only those gives artifact messages
.map(|x| x.package_name.clone())
.collect();

let progress_bar = ProgressBar::new(packages.len() as u64);
progress_bar.set_style(
ProgressStyle::default_bar()
.template("[{bar:60}] {pos:>3}/{len:3} {msg}")
.progress_chars("=> "),
);

if let Some(first) = packages.first() {
progress_bar.set_message(first);
} else {
eprintln!("Warning: The build plan is empty");
}

Some((progress_bar, packages))
}
Err(_) => None,
}
} else {
None
}
}

/// Builds the rust crate into a native module (i.e. an .so or .dll) for a
/// specific python version
///
Expand Down Expand Up @@ -163,28 +198,12 @@ pub fn compile(
let mut cargo_args = vec!["rustc", "--message-format", "json"];

// Mimicks cargo's -Z compile-progress, just without the long result log
let progress_plan = if atty::is(Stream::Stderr) {
match get_build_plan(&shared_args) {
Ok(build_plan) => {
let progress_bar = ProgressBar::new(build_plan.invocations.len() as u64);
progress_bar.set_style(
ProgressStyle::default_bar()
.template("[{bar:60}] {pos:>3}/{len:3} {msg}")
.progress_chars("=> "),
);

progress_bar.set_message(&build_plan.invocations[0].package_name);
let mut progress_plan = get_progress_plan(&shared_args);

// We have out own progess bar, so we don't need cargo's bar
cargo_args.push("--quiet");

Some((progress_bar, build_plan))
}
Err(_) => None,
}
} else {
None
};
if progress_plan.is_some() {
// We have out own progess bar, so we don't need cargo's bar
cargo_args.push("--quiet");
}

let mut rustc_args: Vec<&str> = context
.rustc_extra_args
Expand Down Expand Up @@ -222,24 +241,30 @@ pub fn compile(
let mut cargo_build = build_command.spawn().context("Failed to run cargo")?;

let mut artifact_messages = Vec::new();
let mut build_plan_pos = 0;
let reader = BufReader::new(cargo_build.stdout.take().unwrap());
for line in reader.lines().map(|line| line.unwrap()) {
if let Ok(message) = serde_json::from_str::<CompilerArtifactMessage>(&line) {
// Extract the location of the .so/.dll/etc. from cargo's json output
if message.target.name == context.module_name
|| message.target.name == context.metadata21.name
{
artifact_messages.push(message);
let target_name = message.target.name.clone();
if target_name == context.module_name || target_name == context.metadata21.name {
artifact_messages.push(message.clone());
}

let crate_name = message.package_id.split(" ").nth(0).unwrap().to_string();

// The progress bar isn't an exact science and stuff might get out-of-sync,
// but that isn't big problem since the bar is only to give the user an estimate
if let Some((ref progress_bar, ref build_plan)) = progress_plan {
progress_bar.inc(1);
build_plan_pos += 1;
if let Some(package) = build_plan.invocations.get(build_plan_pos) {
progress_bar.set_message(&package.package_name);
if let Some((ref progress_bar, ref mut packages)) = progress_plan {
match packages.iter().position(|x| x == &crate_name) {
Some(pos) => {
packages.remove(pos);
progress_bar.inc(1);
}
None => eprintln!("WARN: {} not found in build plan", crate_name),
}

if let Some(package) = packages.first() {
progress_bar.set_message(&package);
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ extern crate rpassword;
#[allow(unused_imports)]
#[macro_use]
extern crate structopt;
#[cfg(feature = "log")]
extern crate pretty_env_logger;

use failure::Error;
#[cfg(all(feature = "upload", feature = "keyring"))]
Expand Down Expand Up @@ -159,6 +161,9 @@ enum Opt {
}

fn run() -> Result<(), Error> {
#[cfg(feature = "log")]
pretty_env_logger::init();

let opt = Opt::from_args();

match opt {
Expand Down
3 changes: 0 additions & 3 deletions src/target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,6 @@ impl Target {
pub fn get_shared_platform_tag(&self) -> &'static str {
match self.os {
OS::Linux => {
if target_info::Target::env() != "gnu" {
panic!("Expected a gnu target, musl is not supported");
}
if self.is_64_bit {
"x86_64-linux-gnu"
} else {
Expand Down
17 changes: 12 additions & 5 deletions src/upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,21 @@ use Registry;
#[fail(display = "Uploading to the registry failed")]
pub enum UploadError {
/// Any reqwest error
#[fail(display = "{}", _0)]
#[fail(display = "Http error")]
RewqestError(#[cause] reqwest::Error),
/// The registry returned a "403 Forbidden"
#[fail(display = "Username or password are incorrect")]
AuthenticationError,
/// Reading the wheel failed
#[fail(display = "{}", _0)]
#[fail(display = "IO Error")]
IOError(#[cause] io::Error),
/// The registry returned something else than 200
#[fail(display = "Failed to upload the wheel {}", _0)]
StatusCodeError(String),
#[fail(
display = "Failed to upload the wheel with status {}: {}",
_0,
_1
)]
StatusCodeError(String, String),
}

impl From<io::Error> for UploadError {
Expand Down Expand Up @@ -114,6 +118,9 @@ pub fn upload(
e
)
});
Err(UploadError::StatusCodeError(err_text))
Err(UploadError::StatusCodeError(
response.status().to_string(),
err_text,
))
}
}

0 comments on commit bafd0f9

Please sign in to comment.