Skip to content

Commit

Permalink
start implementing exclude for globvec
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfv committed Jun 1, 2024
1 parent 2befee5 commit adbe65c
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 24 deletions.
8 changes: 6 additions & 2 deletions src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,14 @@ pub async fn skip_existing(
})
.collect::<std::collections::HashSet<_>>();


// Retain only the outputs that do not exist yet
outputs.retain(|output| {
tracing::info!("Checking: {}-{}-{}", output.name().as_normalized(), output.version(), output.build_string().unwrap_or_default());
tracing::info!(
"Checking: {}-{}-{}",
output.name().as_normalized(),
output.version(),
output.build_string().unwrap_or_default()
);
tracing::info!("Existing: {:?}", existing_set);
let exists = existing_set.contains(&format!(
"{}-{}-{}",
Expand Down
6 changes: 5 additions & 1 deletion src/packaging/file_finder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ impl Files {
/// Find all files in the given (host) prefix and remove all previously installed files (based on the PrefixRecord
/// of the conda environment). If always_include is Some, then all files matching the glob pattern will be included
/// in the new_files set.
pub fn from_prefix(prefix: &Path, always_include: Option<&GlobSet>, include_files: &GlobVec) -> Result<Self, io::Error> {
pub fn from_prefix(
prefix: &Path,
always_include: Option<&GlobSet>,
include_files: &GlobVec,
) -> Result<Self, io::Error> {
if !prefix.exists() {
return Ok(Files {
new_files: HashSet::new(),
Expand Down
62 changes: 41 additions & 21 deletions src/recipe/parser/glob_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,24 @@ use crate::recipe::error::{ErrorKind, PartialParsingError};
/// A vector of globs that is also immediately converted to a globset
/// to enhance parser errors.
#[derive(Default, Clone)]
pub struct GlobVec(Vec<Glob>, Option<GlobSet>);
pub struct GlobVec {
include: Vec<Glob>,
exclude: Vec<Glob>,
globset: Option<GlobSet>,
}

impl PartialEq for GlobVec {
fn eq(&self, other: &Self) -> bool {
self.0 == other.0
self.include == other.include && self.exclude == other.exclude
}
}

impl Eq for GlobVec {}

impl Serialize for GlobVec {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
let mut seq = serializer.serialize_seq(Some(self.0.len()))?;
for glob in self.0.iter() {
let mut seq = serializer.serialize_seq(Some(self.include.len()))?;
for glob in self.include.iter() {
seq.serialize_element(glob.glob())?;
}
seq.end()
Expand All @@ -39,7 +43,7 @@ impl Serialize for GlobVec {
impl Debug for GlobVec {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_list()
.entries(self.0.iter().map(|glob| glob.glob()))
.entries(self.include.iter().map(|glob| glob.glob()))
.finish()
}
}
Expand All @@ -57,38 +61,42 @@ impl<'de> Deserialize<'de> for GlobVec {
}

if globs.is_empty() {
Ok(Self(globs, None))
Ok(GlobVec::default())
} else {
let mut globset_builder = globset::GlobSetBuilder::new();
for glob in globs.iter() {
globset_builder.add(glob.clone());
}
let globset = globset_builder.build().map_err(serde::de::Error::custom)?;

Ok(Self(globs, Some(globset)))
Ok(Self {
include: globs,
exclude: Vec::new(),
globset: Some(globset),
})
}
}
}

impl GlobVec {
/// Returns true if the globvec is empty
pub fn is_empty(&self) -> bool {
self.0.is_empty()
self.include.is_empty()
}

/// Returns an iterator over the globs
pub fn globs(&self) -> impl Iterator<Item = &Glob> {
self.0.iter()
self.include.iter()
}

/// Returns the globset if it exists
pub fn globset(&self) -> Option<&GlobSet> {
self.1.as_ref()
self.globset.as_ref()
}

/// Returns true if the path matches any of the globs
pub fn is_match(&self, path: &Path) -> bool {
if let Some(globset) = self.1.as_ref() {
if let Some(globset) = self.globset.as_ref() {
globset.is_match(path)
} else {
false
Expand All @@ -104,20 +112,28 @@ impl GlobVec {
}

if glob_vec.is_empty() {
Self(glob_vec, None)
Self::default()
} else {
let mut globset_builder = globset::GlobSetBuilder::new();
for glob in glob_vec.iter() {
globset_builder.add(glob.clone());
}
let globset = globset_builder.build().unwrap();

Self(glob_vec, Some(globset))
Self {
include: glob_vec,
exclude: Vec::new(),
globset: Some(globset),
}
}
}

/// Let the positive globs pass and remove the negative / not-selected ones
pub(crate) fn filter_files(&self, difference: &mut std::collections::HashSet<std::path::PathBuf>, prefix: &Path) {
pub(crate) fn filter_files(
&self,
difference: &mut std::collections::HashSet<std::path::PathBuf>,
prefix: &Path,
) {
let mut to_keep = HashSet::new();
for file in difference.iter() {
if !self.is_match(file) {
Expand Down Expand Up @@ -155,7 +171,7 @@ impl TryConvertNode<GlobVec> for RenderedSequenceNode {
}

if vec.is_empty() {
Ok(GlobVec(vec, None))
Ok(GlobVec::default())
} else {
let mut globset_builder = globset::GlobSetBuilder::new();
for glob in vec.iter() {
Expand All @@ -165,7 +181,11 @@ impl TryConvertNode<GlobVec> for RenderedSequenceNode {
.build()
.map_err(|err| vec![_partialerror!(*self.span(), ErrorKind::GlobParsing(err),)])?;

Ok(GlobVec(vec, Some(globset)))
Ok(GlobVec {
include: vec,
exclude: Vec::new(),
globset: Some(globset),
})
}
}
}
Expand Down Expand Up @@ -264,14 +284,14 @@ mod tests {
.unwrap();
let tests_node = yaml_root.as_mapping().unwrap().get("globs").unwrap();
let globvec: GlobVec = tests_node.try_convert("globs").unwrap();
assert_eq!(globvec.0.len(), 3);
assert_eq!(globvec.1.as_ref().unwrap().len(), 3);
assert_eq!(globvec.include.len(), 3);
assert_eq!(globvec.globset.as_ref().unwrap().len(), 3);

let as_yaml = serde_yaml::to_string(&globvec).unwrap();
insta::assert_snapshot!(&as_yaml);
let parsed_again: GlobVec = serde_yaml::from_str(&as_yaml).unwrap();
assert_eq!(parsed_again.0.len(), 3);
assert_eq!(parsed_again.1.as_ref().unwrap().len(), 3);
assert_eq!(parsed_again.include.len(), 3);
assert_eq!(parsed_again.globset.as_ref().unwrap().len(), 3);
}

#[test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ Recipe {
ignore_binary_files: false,
},
post_process: [],
include_files: [],
},
requirements: Requirements {
build: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ Recipe {
ignore_binary_files: false,
},
post_process: [],
include_files: [],
},
requirements: Requirements {
build: [
Expand Down

0 comments on commit adbe65c

Please sign in to comment.