-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #540 from cgwalters/packagesystem-cleanup
Packagesystem cleanup
- Loading branch information
Showing
5 changed files
with
90 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
use std::collections::{BTreeMap, BTreeSet}; | ||
use std::io::Write; | ||
use std::path::Path; | ||
|
||
use anyhow::{bail, Context, Result}; | ||
use chrono::prelude::*; | ||
|
||
use crate::model::*; | ||
use crate::ostreeutil; | ||
|
||
/// Parse the output of `rpm -q` | ||
fn rpm_parse_metadata(stdout: &[u8]) -> Result<ContentMetadata> { | ||
let pkgs = std::str::from_utf8(stdout)? | ||
.split_whitespace() | ||
.map(|s| -> Result<_> { | ||
let parts: Vec<_> = s.splitn(2, ',').collect(); | ||
let name = parts[0]; | ||
if let Some(ts) = parts.get(1) { | ||
let nt = DateTime::parse_from_str(ts, "%s") | ||
.context("Failed to parse rpm buildtime")? | ||
.with_timezone(&chrono::Utc); | ||
Ok((name, nt)) | ||
} else { | ||
bail!("Failed to parse: {}", s); | ||
} | ||
}) | ||
.collect::<Result<BTreeMap<&str, DateTime<Utc>>>>()?; | ||
if pkgs.is_empty() { | ||
bail!("Failed to find any RPM packages matching files in source efidir"); | ||
} | ||
let timestamps: BTreeSet<&DateTime<Utc>> = pkgs.values().collect(); | ||
// Unwrap safety: We validated pkgs has at least one value above | ||
let largest_timestamp = timestamps.iter().last().unwrap(); | ||
let version = pkgs.keys().fold("".to_string(), |mut s, n| { | ||
if !s.is_empty() { | ||
s.push(','); | ||
} | ||
s.push_str(n); | ||
s | ||
}); | ||
Ok(ContentMetadata { | ||
timestamp: **largest_timestamp, | ||
version, | ||
}) | ||
} | ||
|
||
/// Query the rpm database and list the package and build times. | ||
pub(crate) fn query_files<T>( | ||
sysroot_path: &str, | ||
paths: impl IntoIterator<Item = T>, | ||
) -> Result<ContentMetadata> | ||
where | ||
T: AsRef<Path>, | ||
{ | ||
let mut c = ostreeutil::rpm_cmd(sysroot_path); | ||
c.args(["-q", "--queryformat", "%{nevra},%{buildtime} ", "-f"]); | ||
for arg in paths { | ||
c.arg(arg.as_ref()); | ||
} | ||
|
||
let rpmout = c.output()?; | ||
if !rpmout.status.success() { | ||
std::io::stderr().write_all(&rpmout.stderr)?; | ||
bail!("Failed to invoke rpm -qf"); | ||
} | ||
|
||
rpm_parse_metadata(&rpmout.stdout) | ||
} | ||
|
||
#[test] | ||
fn test_parse_rpmout() { | ||
let testdata = "grub2-efi-x64-1:2.06-95.fc38.x86_64,1681321788 grub2-efi-x64-1:2.06-95.fc38.x86_64,1681321788 shim-x64-15.6-2.x86_64,1657222566 shim-x64-15.6-2.x86_64,1657222566 shim-x64-15.6-2.x86_64,1657222566"; | ||
let parsed = rpm_parse_metadata(testdata.as_bytes()).unwrap(); | ||
assert_eq!( | ||
parsed.version, | ||
"grub2-efi-x64-1:2.06-95.fc38.x86_64,shim-x64-15.6-2.x86_64" | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters