Add an autogeneration header to pip-compile (#145)

Closes https://github.com/astral-sh/puffin/issues/132.
This commit is contained in:
Charlie Marsh 2023-10-19 20:57:27 -04:00 committed by GitHub
parent 0b60804db6
commit 03101c6a5c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 22 additions and 13 deletions

1
Cargo.lock generated
View file

@ -2136,6 +2136,7 @@ version = "0.0.1"
dependencies = [
"anyhow",
"bitflags 2.4.1",
"colored",
"futures",
"once_cell",
"pep440_rs 0.3.12",

View file

@ -1,3 +1,4 @@
use std::env;
use std::fmt::Write;
use std::io::{stdout, BufWriter};
use std::path::Path;
@ -19,6 +20,8 @@ use crate::commands::{elapsed, ExitStatus};
use crate::printer::Printer;
use crate::requirements::RequirementsSource;
const VERSION: &str = env!("CARGO_PKG_VERSION");
/// Resolve a set of requirements into a set of pinned versions.
pub(crate) async fn pip_compile(
requirements: &[RequirementsSource],
@ -91,11 +94,24 @@ pub(crate) async fn pip_compile(
.dimmed()
)?;
if let Some(output_file) = output_file {
resolution.write_requirement_format(&mut BufWriter::new(File::create(output_file)?))?;
let mut writer: Box<dyn std::io::Write> = if let Some(output_file) = output_file {
Box::new(BufWriter::new(File::create(output_file)?))
} else {
resolution.write_requirement_format(&mut stdout().lock())?;
Box::new(stdout())
};
writeln!(
writer,
"{}",
format!("# This file was autogenerated by Puffin v{VERSION} via the following command:")
.green()
)?;
writeln!(
writer,
"{}",
format!("# {}", env::args().join(" ")).green()
)?;
writeln!(writer, "{resolution}")?;
Ok(ExitStatus::Success)
}

View file

@ -21,6 +21,7 @@ wheel-filename = { path = "../wheel-filename" }
anyhow = { workspace = true }
bitflags = { workspace = true }
colored = { workspace = true }
futures = { workspace = true }
once_cell = { workspace = true }
thiserror = { workspace = true }

View file

@ -1,6 +1,4 @@
use std::collections::BTreeMap;
use std::io;
use std::io::Write;
use pep440_rs::Version;
use puffin_client::File;
@ -34,16 +32,9 @@ impl Resolution {
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
/// Write the resolution in the `{name}=={version}` format of requirements.txt that pip uses.
pub fn write_requirement_format(&self, writer: &mut impl Write) -> io::Result<()> {
for (name, package) in self.iter() {
writeln!(writer, "{}=={}", name, package.version())?;
}
Ok(())
}
}
/// Write the resolution in the `{name}=={version}` format of requirements.txt that pip uses.
impl std::fmt::Display for Resolution {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut first = true;