Skip to content

Commit

Permalink
Split out add_source_dist_entry
Browse files Browse the repository at this point in the history
  • Loading branch information
konstin committed Nov 13, 2024
1 parent c334638 commit c6f8d59
Showing 1 changed file with 52 additions and 40 deletions.
92 changes: 52 additions & 40 deletions crates/uv-build-backend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ use std::fs::FileType;
use std::io::{BufReader, Cursor, Read, Write};
use std::path::{Path, PathBuf, StripPrefixError};
use std::{io, mem};
use tar::{EntryType, Header};
use tar::{Builder, EntryType, Header};
use thiserror::Error;
use tracing::{debug, trace};
use uv_distribution_filename::{SourceDistExtension, SourceDistFilename, WheelFilename};
use uv_fs::Simplified;
use uv_globfilter::{parse_portable_glob, GlobDirFilter, PortableGlobError};
use walkdir::WalkDir;
use walkdir::{DirEntry, WalkDir};
use zip::{CompressionMethod, ZipWriter};

#[derive(Debug, Error)]
Expand Down Expand Up @@ -514,44 +514,7 @@ pub fn build_source_dist(
continue;
};

debug!("Including {}", relative.user_display());

let metadata = fs_err::metadata(entry.path())?;
let mut header = Header::new_gnu();
#[cfg(unix)]
{
header.set_mode(std::os::unix::fs::MetadataExt::mode(&metadata));
}
#[cfg(not(unix))]
{
header.set_mode(0o644);
}

if entry.file_type().is_dir() {
header.set_entry_type(EntryType::Directory);
header
.set_path(Path::new(&top_level).join(relative))
.map_err(|err| Error::TarWrite(source_dist_path.clone(), err))?;
header.set_size(0);
header.set_cksum();
tar.append(&header, io::empty())
.map_err(|err| Error::TarWrite(source_dist_path.clone(), err))?;
continue;
} else if entry.file_type().is_file() {
header.set_size(metadata.len());
header.set_cksum();
tar.append_data(
&mut header,
Path::new(&top_level).join(relative),
BufReader::new(File::open(entry.path())?),
)
.map_err(|err| Error::TarWrite(source_dist_path.clone(), err))?;
} else {
return Err(Error::UnsupportedFileType(
relative.clone(),
entry.file_type(),
));
}
add_source_dist_entry(&mut tar, entry, &top_level, &source_dist_path, &relative)?;
}

tar.finish()
Expand All @@ -560,6 +523,55 @@ pub fn build_source_dist(
Ok(filename)
}

/// Add a file or a directory to a source distribution.
fn add_source_dist_entry(
tar: &mut Builder<GzEncoder<File>>,
entry: DirEntry,
top_level: &str,
source_dist_path: &Path,
relative: &Path,
) -> Result<(), Error> {
debug!("Including {}", relative.user_display());

let metadata = fs_err::metadata(entry.path())?;
let mut header = Header::new_gnu();
#[cfg(unix)]
{
header.set_mode(std::os::unix::fs::MetadataExt::mode(&metadata));
}
#[cfg(not(unix))]
{
header.set_mode(0o644);
}

if entry.file_type().is_dir() {
header.set_entry_type(EntryType::Directory);
header
.set_path(Path::new(&top_level).join(relative))
.map_err(|err| Error::TarWrite(source_dist_path.to_path_buf(), err))?;
header.set_size(0);
header.set_cksum();
tar.append(&header, io::empty())
.map_err(|err| Error::TarWrite(source_dist_path.to_path_buf(), err))?;
Ok(())
} else if entry.file_type().is_file() {
header.set_size(metadata.len());
header.set_cksum();
tar.append_data(
&mut header,
Path::new(&top_level).join(relative),
BufReader::new(File::open(entry.path())?),
)
.map_err(|err| Error::TarWrite(source_dist_path.to_path_buf(), err))?;
Ok(())
} else {
Err(Error::UnsupportedFileType(
relative.to_path_buf(),
entry.file_type(),
))
}
}

/// Write the dist-info directory to the output directory without building the wheel.
pub fn metadata(
source_tree: &Path,
Expand Down

0 comments on commit c6f8d59

Please sign in to comment.