Skip to content

Commit

Permalink
refactor RPM logic, clean up some more Clippy warnings, clean up non-…
Browse files Browse the repository at this point in the history
…idiomatic code
  • Loading branch information
korewaChino committed Oct 29, 2024
1 parent 911034e commit 0508f07
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 60 deletions.
4 changes: 2 additions & 2 deletions src/flatpak.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ impl FlatpakBuilder {
.arg(manifest)
.arg("--force-clean")
.arg("--repo")
.arg(&self.output_repo.canonicalize().unwrap());
.arg(self.output_repo.canonicalize().unwrap());

// add extra sources

Expand All @@ -122,7 +122,7 @@ impl FlatpakBuilder {

flatpak
.arg("build-bundle")
.arg(&self.output_repo.canonicalize().unwrap())
.arg(self.output_repo.canonicalize().unwrap())
.arg(&bundle_path)
.arg(app_id);

Expand Down
2 changes: 1 addition & 1 deletion src/oci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl OCIBuilder {
.arg(real_tag);

if latest {
cmd.arg("-t").arg(&format!("{}:latest", &self.tag));
cmd.arg("-t").arg(format!("{}:latest", &self.tag));
}

for label in &self.label {
Expand Down
104 changes: 48 additions & 56 deletions src/rpm_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,21 +136,17 @@ impl RPMBuilder {
take(&mut options.sources),
take(&mut options.resultdir),
);
for extra_repo in options.extra_repos.iter_mut().flatten() {
mock.add_extra_repo(take(extra_repo));
if let Some(extra_repos) = options.extra_repos.take() {
for extra_repo in extra_repos {
mock.add_extra_repo(extra_repo);
}
}
for (k, v) in &options.macros {
options.macros.iter().for_each(|(k, v)| {
mock.def_macro(k, v);
}
for with_flags in &mut options.with {
mock.with_flags_mut().push(take(with_flags));
}
for without_flags in &mut options.without {
mock.without_flags_mut().push(take(without_flags));
}
for config_opt in &mut options.config_opts {
mock.add_config_opt(take(config_opt));
}
});
mock.with_flags_mut().extend(take(&mut options.with));
mock.without_flags_mut().extend(take(&mut options.without));
mock.extend_config_opts(take(&mut options.config_opts));
mock.no_mirror(options.no_mirror);
mock.enable_scm(options.scm_enable);
mock.extend_scm_opts(take(&mut options.scm_opts));
Expand All @@ -161,17 +157,12 @@ impl RPMBuilder {
let mut rpmbuild =
RPMBuildBackend::new(take(&mut options.sources), take(&mut options.resultdir));

for (k, v) in &options.macros {
options.macros.iter().for_each(|(k, v)| {
rpmbuild.def_macro(k, v);
}
});

for with_flags in &mut options.with {
rpmbuild.with_flags_mut().push(take(with_flags));
}

for without_flags in &mut options.without {
rpmbuild.without_flags_mut().push(take(without_flags));
}
rpmbuild.with_flags_mut().extend(take(&mut options.with));
rpmbuild.without_flags_mut().extend(take(&mut options.without));

rpmbuild.build(spec).await
}
Expand Down Expand Up @@ -333,43 +324,44 @@ impl MockBackend {
pub fn mock(&self) -> Command {
let mut cmd = Command::new("mock");

if let Some(config) = self.mock_config.as_ref() {
if let Some(config) = &self.mock_config {
cmd.arg("-r").arg(config);
}

cmd.arg("--verbose");

for repo in &self.extra_repos {
self.extra_repos.iter().for_each(|repo| {
cmd.arg("-a").arg(repo);
}
});

for with in &self.with {
self.with.iter().for_each(|with| {
cmd.arg("--with").arg(with);
}
});

for without in &self.without {
self.without.iter().for_each(|without| {
cmd.arg("--without").arg(without);
}
});

for (name, value) in &self.macros {
self.macros.iter().for_each(|(name, value)| {
cmd.arg("-D").arg(format!("{name} {value}"));
}
});

if self.no_mirror {
cmd.arg("--config-opts").arg("mirrored=False");
}

for opt in &self.config_opts {
self.config_opts.iter().for_each(|opt| {
cmd.arg("--config-opts").arg(opt);
}
});

if self.scm_enable {
cmd.arg("--scm-enable");
}

for scm in &self.scm_opts {
self.scm_opts.iter().for_each(|scm| {
cmd.arg("--scm-option").arg(scm);
}
});

cmd
}
}
Expand All @@ -380,6 +372,9 @@ impl RPMSpecBackend for MockBackend {
let mut cmd = self.mock();
let tmp = tempfile::Builder::new().prefix("anda-srpm").tempdir()?;

// todo: Probably copy the spec file and the sources to rpmbuild/SOURCES or some kind of temp dir instead
// of building everything in the specfile's directory.

cmd.arg("--buildsrpm")
.arg("--spec")
.arg(spec)
Expand Down Expand Up @@ -585,7 +580,7 @@ impl RPMSpecBackend for RPMBuildBackend {

async fn build(&self, spec: &Path) -> Result<Vec<PathBuf>> {
let mut cmd = self.rpmbuild();
let tmp = TempDir::new()?;
let tmp = TempDir::with_prefix("anda-rpmbuild")?;
cmd.arg("-ba")
.arg(spec)
.arg("--define")
Expand All @@ -602,26 +597,23 @@ impl RPMSpecBackend for RPMBuildBackend {

for entry in walkdir::WalkDir::new(tmp.path()) {
let entry = entry?;
//eprintln!("entry: {:?}", entry.file_name());

if entry.file_name().to_string_lossy().ends_with(".src.rpm") {
//rpms.push(entry.path().to_path_buf());
debug!("found srpm: {rpms:?}");

let srpm_dir = self.resultdir.join("rpm/srpm");
std::fs::create_dir_all(&srpm_dir)?;
let dest = srpm_dir.join(entry.file_name());
std::fs::copy(entry.path(), dest)?;
//rpms.push(srpm_dir.join(entry.file_name()));
} else if entry.file_name().to_string_lossy().ends_with(".rpm") {
//rpms.push(entry.path().to_path_buf());
// eprintln!("found rpm: {:?}", rpms);

let rpms_dir = self.resultdir.join("rpm/rpms");
std::fs::create_dir_all(&rpms_dir)?;
let dest = rpms_dir.join(entry.file_name());
std::fs::copy(entry.path(), dest)?;
rpms.push(rpms_dir.join(entry.file_name()));
let entry_filename = entry.file_name().to_string_lossy();

let (subdir, is_rpm) = if entry_filename.ends_with(".src.rpm") {
("rpm/srpm", false)
} else if entry_filename.ends_with(".rpm") {
("rpm/rpms", true)
} else {
continue;
};

let target_dir = self.resultdir.join(subdir);
std::fs::create_dir_all(&target_dir)?;
let dest = target_dir.join(entry.file_name());
std::fs::copy(entry.path(), &dest)?;

if is_rpm {
rpms.push(dest);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ mod tests {
};
// update only nightly packages
let lbls = std::iter::once(("nightly", "1")).map(|(l, r)| (l.into(), r.into())).collect();
let mut test1 = filter_project(&lbls);
let test1 = filter_project(&lbls);
for (k, v) in transform(&[("nightly", "0"), ("hai", "bai"), ("large", "1")]) {
assert!(test1(&(k, v)));
}
Expand Down

0 comments on commit 0508f07

Please sign in to comment.