fix: 使用正则截取欧拉 rpm 包的 nvr 信息

Signed-off-by: Jia Chao <jiac13@chinaunicom.cn>
This commit is contained in:
Jia Chao 2024-07-25 14:49:01 +08:00
parent 0b34274085
commit 7e5f30e3e3
2 changed files with 27 additions and 4 deletions

View File

@ -8,6 +8,8 @@ edition = "2021"
[dependencies]
clap = { version = "4.0", features = ["derive"] }
cvrf-xmlparser = { git = "https://git.zhgsun.com:8089/jiachao2130/cvrf-xmlparser.git", version = "0.1.0" }
lazy_static = { version = "1" }
regex = { version = "1" }
serde = { version = "1", features = ["serde_derive"] }
serde_json = { version = "1.0" }
tracing = { version = "0.1" }

View File

@ -3,6 +3,8 @@ use std::fs;
use std::io::Write;
use std::path::{Path, PathBuf};
use lazy_static::lazy_static;
use regex::Regex;
use tracing::{debug, error, info, trace};
use tracing_subscriber::{fmt, EnvFilter};
@ -24,6 +26,14 @@ pub type Error = Box<dyn std::error::Error + Send + Sync>;
/// 定义 crate::Result
pub type Result<T> = std::result::Result<T, Error>;
lazy_static! {
pub static ref NVR_RE: Regex = Regex::new(
// %{name}-%{version}-%{release}.[oexxxx.xxx.rpm]
r"^([a-zA-Z0-9\-_+]+)-([0-9a-zA-Z\._+]+)-([0-9a-zA-Z\._-]+).(oe[0-9a-z]+.[0-9a-z]+.rpm)"
)
.unwrap();
}
pub fn cumain() -> Result<()> {
set_up_logging()?;
let cli = cli::parse();
@ -241,8 +251,9 @@ fn _save_2_cusa_db(dbpath: &str, cvrf: &CVRF) -> Result<()> {
let component = cvrf.affected_component().unwrap();
trace!("Get affected_component: {}", component);
// 这里随便取一个 src 包名
let _src = cvrf.producttree.packages["src"][0].productid.as_str();
let _src = cvrf.producttree.packages["src"][0].content.as_str();
/*
// TODO: may empty
if _src == "" {
error!(
@ -251,8 +262,9 @@ fn _save_2_cusa_db(dbpath: &str, cvrf: &CVRF) -> Result<()> {
);
return Ok(());
}
*/
db.push(_src.chars().next().unwrap().to_string());
// TODO!()
// 不理会此问题
// db: "cusas/l/log4j,mybatis,netty,springframework,wildfly-security-manager,wildfly-elytron,wildfly-build-tools,wildfly-common,wildfly-core,thrift,json-lib,datanucleus-core,jgroups,mx4j,jboss-logging,infinispan,datanucleus-rdbms,avalon-logkit,datanucleus-api-jdo,avalon-framework,HikariCP,metrics"
// Error: Os { code: 63, kind: InvalidFilename, message: "File name too long" }
db.push(component);
@ -269,7 +281,12 @@ fn _save_2_cusa_db(dbpath: &str, cvrf: &CVRF) -> Result<()> {
match fs::create_dir_all(parent) {
Ok(_) => {}
Err(e) => {
error!("sa_id: {}, {}", cvrf.id(), e.to_string());
error!(
"sa_id: {}, create_dir_all({:?}): {}",
cvrf.id(),
parent,
e.to_string()
);
return Ok(());
}
}
@ -309,7 +326,11 @@ fn _save_2_cusa_db(dbpath: &str, cvrf: &CVRF) -> Result<()> {
let mut nvr = String::new();
for product in &cvrf.producttree.packages["src"] {
if product.cpe.ends_with(repairconfig.upstream()) {
nvr = product.productid.clone();
nvr = if let Some(caps) = NVR_RE.captures(&product.content) {
format!("{}-{}-{}", &caps[1], &caps[2], &caps[3])
} else {
product.content.clone()
};
nvr.push_str(&format!("_{}.json", cvrf.id()));
break;
}