Skip to content

Commit

Permalink
Merge pull request #83 from newpavlov/clap
Browse files Browse the repository at this point in the history
Update rpm, migrate to clap and add source_date_epoch flag
  • Loading branch information
cat-in-136 committed Aug 8, 2023
2 parents f844017 + 8205d7a commit 750bbaa
Show file tree
Hide file tree
Showing 11 changed files with 374 additions and 424 deletions.
6 changes: 2 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@ repository = "https://github.com/cat-in-136/cargo-generate-rpm"
version = "0.11.1"
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
glob = "0.3.0"
rpm = { version = "0.11.0", default-features = false }
rpm = { version = "0.12", default-features = false }
toml = "0.7"
cargo_toml = "0.15"
getopts = "0.2"
clap = { version = "4.3", features = ["derive"] }
thiserror = "1"
elf = "0.7"

Expand Down
34 changes: 15 additions & 19 deletions src/auto_req/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,17 @@ fn find_requires_by_ldd(
.map_err(|e| AutoReqError::ProcessError(OsString::from("ldd"), e))?;

let unversioned_libraries = s
.split("\n")
.split('\n')
.take_while(|&line| !line.trim().is_empty())
.filter_map(|line| line.trim_start().splitn(2, " ").nth(0));
.filter_map(|line| line.trim_start().split(' ').next());
let versioned_libraries = s
.split("\n")
.split('\n')
.skip_while(|&line| !line.contains("Version information:"))
.skip(1)
.skip_while(|&line| !line.contains(path.to_str().unwrap()))
.skip(1)
.take_while(|&line| line.contains(" => "))
.filter_map(|line| line.trim_start().splitn(2, " => ").nth(0));
.filter_map(|line| line.trim_start().split(" => ").next());

let marker = marker.unwrap_or_default();
let mut requires = BTreeSet::new();
Expand All @@ -98,22 +98,18 @@ fn find_requires_by_ldd(
{
if name.contains(" (") {
// Insert "unversioned" library name
requires.insert(format!(
"{}(){}",
name.splitn(2, " ").nth(0).unwrap(),
marker
));
requires.insert(format!("{}{}", name.replace(" ", ""), marker));
requires.insert(format!("{}(){}", name.split(' ').next().unwrap(), marker));
requires.insert(format!("{}{}", name.replace(' ', ""), marker));
} else {
requires.insert(format!("{}(){}", name.replace(" ", ""), marker));
requires.insert(format!("{}(){}", name.replace(' ', ""), marker));
}
}
Ok(requires)
}

fn find_requires_of_elf(path: &Path) -> Result<Option<BTreeSet<String>>, AutoReqError> {
if let Ok(info) = ElfInfo::new(&path) {
let mut requires = find_requires_by_ldd(&path, info.marker())?;
if let Ok(info) = ElfInfo::new(path) {
let mut requires = find_requires_by_ldd(path, info.marker())?;
if info.got_gnu_hash && !info.got_hash {
requires.insert("rtld(GNU_HASH)".to_string());
}
Expand Down Expand Up @@ -142,16 +138,16 @@ fn find_require_of_shebang(path: &Path) -> Result<Option<String>, AutoReqError>
let mut line = String::new();
read.read_line(&mut line)?;
line.trim()
.splitn(2, |c: char| !c.is_ascii() || c.is_whitespace())
.nth(0)
.map(&String::from)
.split(|c: char| !c.is_ascii() || c.is_whitespace())
.next()
.map(String::from)
} else {
None
}
};

Ok(match interpreter {
Some(i) if Path::new(&i).exists() => Some(i.to_string()),
Some(i) if Path::new(&i).exists() => Some(i),
_ => None,
})
}
Expand All @@ -172,8 +168,8 @@ fn test_find_require_of_shebang() {
fn is_executable(path: &Path) -> bool {
use std::os::unix::fs::MetadataExt;
std::fs::metadata(path)
.and_then(|metadata| Ok(metadata.mode()))
.and_then(|mode| Ok(mode & 0o111 != 0))
.map(|metadata| metadata.mode())
.map(|mode| mode & 0o111 != 0)
.unwrap_or_default()
}

Expand Down
50 changes: 12 additions & 38 deletions src/auto_req/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::error::AutoReqError;
use std::convert::TryFrom;
use crate::{cli, error::AutoReqError};
use std::path::{Path, PathBuf};

mod builtin;
Expand All @@ -20,46 +19,21 @@ pub enum AutoReqMode {
BuiltIn,
}

impl TryFrom<String> for AutoReqMode {
type Error = AutoReqError;

fn try_from(value: String) -> Result<Self, Self::Error> {
match value.as_str() {
"auto" | "" => Ok(AutoReqMode::Auto),
"no" | "disabled" => Ok(AutoReqMode::Disabled),
"builtin" => Ok(AutoReqMode::BuiltIn),
"find-requires" => Ok(AutoReqMode::Script(PathBuf::from(RPM_FIND_REQUIRES))),
v if Path::new(v).exists() => Ok(AutoReqMode::Script(PathBuf::from(v))),
_ => Err(AutoReqError::WrongMode),
impl From<&Option<cli::AutoReqMode>> for AutoReqMode {
fn from(value: &Option<cli::AutoReqMode>) -> Self {
use cli::AutoReqMode as M;
use AutoReqMode::*;

match value {
None => Auto,
Some(M::Disabled) => Disabled,
Some(M::Builtin) => BuiltIn,
Some(M::Script(path)) => Script(path.into()),
Some(M::FindRequires) => Script(PathBuf::from(RPM_FIND_REQUIRES)),
}
}
}

#[test]
pub fn test_try_from_for_auto_req_mode() {
for (text, auto_req_mode) in &[
("auto", AutoReqMode::Auto),
("", AutoReqMode::Auto),
("no", AutoReqMode::Disabled),
("disabled", AutoReqMode::Disabled),
(
"find-requires",
AutoReqMode::Script(PathBuf::from(RPM_FIND_REQUIRES)),
),
(file!(), AutoReqMode::Script(PathBuf::from(file!()))),
] {
assert_eq!(
AutoReqMode::try_from(text.to_string()).unwrap(),
*auto_req_mode
);
}

assert!(matches!(
AutoReqMode::try_from("invalid-value".to_string()),
Err(AutoReqError::WrongMode)
));
}

/// Find requires
pub fn find_requires<T: IntoIterator<Item = P>, P: AsRef<Path>>(
files: T,
Expand Down
2 changes: 1 addition & 1 deletion src/auto_req/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub(super) fn find_requires<P: AsRef<Path>, S: AsRef<OsStr>>(
.read_to_string(&mut requires)
.map_err(|e| AutoReqError::ProcessError(script_path.as_ref().to_os_string(), e))?;

Ok(requires.trim().split("\n").map(&String::from).collect())
Ok(requires.trim().split('\n').map(&String::from).collect())
}

#[test]
Expand Down
45 changes: 31 additions & 14 deletions src/build_target.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
use std::env::consts::ARCH;
use std::path::{Path, PathBuf};

#[derive(Debug, Default)]
use crate::cli::Cli;

#[derive(Debug, Clone)]
pub struct BuildTarget {
pub target_dir: Option<String>,
pub target: Option<String>,
pub profile: Option<String>,
pub arch: Option<String>,
target_dir: Option<String>,
target: Option<String>,
profile: String,
arch: Option<String>,
}

impl BuildTarget {
pub fn new(args: &Cli) -> Self {
Self {
target_dir: args.target_dir.clone(),
target: args.target.clone(),
profile: args.profile.clone(),
arch: args.arch.clone(),
}
}

pub fn profile(&self) -> &str {
self.profile.as_str()
}

pub fn build_target_path(&self) -> PathBuf {
if let Some(target_dir) = &self.target_dir {
PathBuf::from(&target_dir)
Expand All @@ -36,8 +51,8 @@ impl BuildTarget {
let arch = self
.target
.as_ref()
.and_then(|v| v.splitn(2, "-").nth(0))
.unwrap_or_else(|| ARCH);
.and_then(|v| v.split('-').next())
.unwrap_or(ARCH);

match arch {
"x86" => "i586",
Expand All @@ -57,12 +72,13 @@ mod test {

#[test]
fn test_build_target_path() {
let target = BuildTarget::default();
let args = crate::cli::Cli::default();
let target = BuildTarget::new(&args);
assert_eq!(target.build_target_path(), PathBuf::from("target"));

let target = BuildTarget {
target_dir: Some("/tmp/foobar/target".to_string()),
..Default::default()
..target
};
assert_eq!(
target.build_target_path(),
Expand All @@ -72,15 +88,16 @@ mod test {

#[test]
fn test_target_path() {
let target = BuildTarget::default();
let args = crate::cli::Cli::default();
let default_target = BuildTarget::new(&args);
assert_eq!(
target.target_path("release"),
default_target.target_path("release"),
PathBuf::from("target/release")
);

let target = BuildTarget {
target: Some("x86_64-unknown-linux-gnu".to_string()),
..Default::default()
..default_target.clone()
};
assert_eq!(
target.target_path("release"),
Expand All @@ -89,7 +106,7 @@ mod test {

let target = BuildTarget {
target_dir: Some("/tmp/foobar/target".to_string()),
..Default::default()
..default_target.clone()
};
assert_eq!(
target.target_path("debug"),
Expand All @@ -99,7 +116,7 @@ mod test {
let target = BuildTarget {
target_dir: Some("/tmp/foobar/target".to_string()),
target: Some("x86_64-unknown-linux-gnu".to_string()),
..Default::default()
..default_target
};
assert_eq!(
target.target_path("debug"),
Expand Down
Loading

0 comments on commit 750bbaa

Please sign in to comment.