Skip to content

Commit

Permalink
Ignore sdist output files when building sdist
Browse files Browse the repository at this point in the history
  • Loading branch information
messense committed Sep 1, 2023
1 parent 4f642df commit bd396e3
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 49 deletions.
31 changes: 17 additions & 14 deletions src/build_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,30 +486,33 @@ impl BuildContext {
Ok(())
}

fn excludes(&self, format: Format) -> Result<Option<Override>> {
fn excludes(&self, format: Format) -> Result<Override> {
let project_dir = match self.pyproject_toml_path.normalize() {
Ok(pyproject_toml_path) => pyproject_toml_path.into_path_buf(),
Err(_) => self.manifest_path.normalize()?.into_path_buf(),
};
let mut excludes = OverrideBuilder::new(project_dir.parent().unwrap());
if let Some(pyproject) = self.pyproject_toml.as_ref() {
let pyproject_dir = self
.pyproject_toml_path
.normalize()
.with_context(|| {
format!(
"failed to normalize path `{}`",
self.pyproject_toml_path.display()
)
})?
.into_path_buf();
if let Some(glob_patterns) = &pyproject.exclude() {
let mut excludes = OverrideBuilder::new(pyproject_dir.parent().unwrap());
for glob in glob_patterns
.iter()
.filter_map(|glob_pattern| glob_pattern.targets(format))
{
excludes.add(glob)?;
}
return Ok(Some(excludes.build()?));
}
}
Ok(None)
// Ignore sdist output files so that we don't include them in the sdist
if matches!(format, Format::Sdist) {
let glob_pattern = format!(
"{}{}{}-*.tar.gz",
self.out.display(),
std::path::MAIN_SEPARATOR,
&self.metadata21.get_distribution_escaped(),
);
excludes.add(&glob_pattern)?;
}
Ok(excludes.build()?)
}

/// Returns the platform part of the tag for the wheel name
Expand Down
57 changes: 23 additions & 34 deletions src/module_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ pub struct WheelWriter {
record: Vec<(String, String, usize)>,
record_file: PathBuf,
wheel_path: PathBuf,
excludes: Option<Override>,
excludes: Override,
}

impl ModuleWriter for WheelWriter {
Expand Down Expand Up @@ -266,7 +266,7 @@ impl WheelWriter {
wheel_dir: &Path,
metadata21: &Metadata21,
tags: &[String],
excludes: Option<Override>,
excludes: Override,
) -> Result<WheelWriter> {
let wheel_path = wheel_dir.join(format!(
"{}-{}-{}.whl",
Expand Down Expand Up @@ -321,11 +321,7 @@ impl WheelWriter {

/// Returns `true` if the given path should be excluded
fn exclude(&self, path: impl AsRef<Path>) -> bool {
if let Some(excludes) = &self.excludes {
excludes.matched(path.as_ref(), false).is_whitelist()
} else {
false
}
self.excludes.matched(path.as_ref(), false).is_whitelist()
}

/// Returns a DateTime representing the value SOURCE_DATE_EPOCH environment variable
Expand Down Expand Up @@ -375,10 +371,10 @@ impl WheelWriter {

/// Creates a .tar.gz archive containing the source distribution
pub struct SDistWriter {
tar: tar::Builder<GzEncoder<File>>,
tar: tar::Builder<GzEncoder<Vec<u8>>>,
path: PathBuf,
files: HashSet<PathBuf>,
excludes: Option<Override>,
excludes: Override,
}

impl ModuleWriter for SDistWriter {
Expand Down Expand Up @@ -423,13 +419,6 @@ impl ModuleWriter for SDistWriter {
return Ok(());
}
let target = target.as_ref();
if source == self.path {
eprintln!(
"⚠️ Warning: Attempting to include the sdist output tarball {} into itself! Check 'cargo package --list' output.",
source.display()
);
return Ok(());
}
if self.files.contains(target) {
// Ignore duplicate files
return Ok(());
Expand All @@ -453,16 +442,19 @@ impl SDistWriter {
pub fn new(
wheel_dir: impl AsRef<Path>,
metadata21: &Metadata21,
excludes: Option<Override>,
excludes: Override,
) -> Result<Self, io::Error> {
let path = wheel_dir.as_ref().join(format!(
"{}-{}.tar.gz",
&metadata21.get_distribution_escaped(),
&metadata21.get_version_escaped()
));

let tar_gz = File::create(&path)?;
let enc = GzEncoder::new(tar_gz, Compression::default());
let path = wheel_dir
.as_ref()
.normalize()?
.join(format!(
"{}-{}.tar.gz",
&metadata21.get_distribution_escaped(),
&metadata21.get_version_escaped()
))
.into_path_buf();

let enc = GzEncoder::new(Vec::new(), Compression::default());
let tar = tar::Builder::new(enc);

Ok(Self {
Expand All @@ -475,16 +467,13 @@ impl SDistWriter {

/// Returns `true` if the given path should be excluded
fn exclude(&self, path: impl AsRef<Path>) -> bool {
if let Some(excludes) = &self.excludes {
excludes.matched(path.as_ref(), false).is_whitelist()
} else {
false
}
self.excludes.matched(path.as_ref(), false).is_whitelist()
}

/// Finished the .tar.gz archive
pub fn finish(mut self) -> Result<PathBuf, io::Error> {
self.tar.finish()?;
pub fn finish(self) -> Result<PathBuf, io::Error> {
let archive = self.tar.into_inner()?;
fs::write(&self.path, archive.finish()?)?;
Ok(self.path)
}
}
Expand Down Expand Up @@ -1299,7 +1288,7 @@ mod tests {

// No excludes
let tmp_dir = TempDir::new()?;
let mut writer = SDistWriter::new(&tmp_dir, &metadata, None)?;
let mut writer = SDistWriter::new(&tmp_dir, &metadata, Override::empty())?;
assert!(writer.files.is_empty());
writer.add_bytes_with_permissions("test", &[], perm)?;
assert_eq!(writer.files.len(), 1);
Expand All @@ -1311,7 +1300,7 @@ mod tests {
let mut excludes = OverrideBuilder::new(&tmp_dir);
excludes.add("test*")?;
excludes.add("!test2")?;
let mut writer = SDistWriter::new(&tmp_dir, &metadata, Some(excludes.build()?))?;
let mut writer = SDistWriter::new(&tmp_dir, &metadata, excludes.build()?)?;
writer.add_bytes_with_permissions("test1", &[], perm)?;
writer.add_bytes_with_permissions("test3", &[], perm)?;
assert!(writer.files.is_empty());
Expand Down
2 changes: 1 addition & 1 deletion src/source_distribution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ fn add_cargo_package_files_to_sdist(
pub fn source_distribution(
build_context: &BuildContext,
pyproject: &PyProjectToml,
excludes: Option<Override>,
excludes: Override,
) -> Result<PathBuf> {
let pyproject_toml_path = build_context
.pyproject_toml_path
Expand Down

0 comments on commit bd396e3

Please sign in to comment.