Skip to content

Commit

Permalink
Merge pull request #46 from kpcyrd/options
Browse files Browse the repository at this point in the history
Fix tree rendering, add options to ease development
  • Loading branch information
kpcyrd authored Oct 31, 2024
2 parents ea21108 + 3ac1f89 commit ec65d2a
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 23 deletions.
8 changes: 7 additions & 1 deletion src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub enum Opts {
Tree(Args),
}

#[derive(Parser)]
#[derive(Parser, Clone)]
pub struct Args {
#[clap(long = "package", short = 'p', value_name = "SPEC")]
/// Package to be used as the root of the tree
Expand All @@ -30,6 +30,12 @@ pub struct Args {
#[clap(long = "all-targets")]
/// Return dependencies for all targets. By default only the host target is matched.
pub all_targets: bool,
#[clap(long = "skip-cache")]
/// Do not read from disk cache for Debian database results
pub skip_cache: bool,
#[clap(long = "concurrency", short = 'j', default_value = "24")]
/// How many database connections to use concurrently
pub concurrency: usize,
#[clap(long = "no-dev-dependencies")]
/// Skip dev dependencies.
pub no_dev_dependencies: bool,
Expand Down
28 changes: 21 additions & 7 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ impl Connection {
if SystemTime::now().duration_since(cache.from)? > CACHE_EXPIRE {
Ok(None)
} else {
debug!("{package} {:?}", cache.info);
debug!("Found package in cache: {package} -> {:?}", cache.info);
Ok(Some(cache.info))
}
}
Expand All @@ -123,9 +123,16 @@ impl Connection {
Ok(())
}

pub fn search(&mut self, package: &str, version: &Version) -> Result<PkgInfo, Error> {
if let Some(info) = self.check_cache("sid", package, version)? {
return Ok(info);
pub fn search(
&mut self,
package: &str,
version: &Version,
skip_cache: bool,
) -> Result<PkgInfo, Error> {
if !skip_cache {
if let Some(info) = self.check_cache("sid", package, version)? {
return Ok(info);
}
}

// config.shell().status("Querying", format!("sid: {}", package))?;
Expand All @@ -140,9 +147,16 @@ impl Connection {
Ok(info)
}

pub fn search_new(&mut self, package: &str, version: &Version) -> Result<PkgInfo, Error> {
if let Some(info) = self.check_cache("new", package, version)? {
return Ok(info);
pub fn search_new(
&mut self,
package: &str,
version: &Version,
skip_cache: bool,
) -> Result<PkgInfo, Error> {
if !skip_cache {
if let Some(info) = self.check_cache("new", package, version)? {
return Ok(info);
}
}

// config.shell().status("Querying", format!("new: {}", package))?;
Expand Down
32 changes: 18 additions & 14 deletions src/debian.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::args::Args;
use crate::db::{Connection, PkgStatus};
use crate::errors::*;
use crate::graph::Graph;
Expand All @@ -8,8 +9,6 @@ use semver::Version;
use std::path::PathBuf;
use std::thread;

const QUERY_THREADS: usize = 24;

#[derive(Debug, Clone)]
pub struct Pkg {
pub id: PackageId,
Expand Down Expand Up @@ -122,7 +121,7 @@ pub struct DebianInfo {
pub version: String,
}

fn run_task(db: &mut Connection, pkg: Pkg) -> Result<DebianInfo> {
fn run_task(db: &mut Connection, pkg: Pkg, skip_cache: bool) -> Result<DebianInfo> {
let mut deb = DebianInfo {
in_unstable: false,
in_new: false,
Expand All @@ -132,9 +131,9 @@ fn run_task(db: &mut Connection, pkg: Pkg) -> Result<DebianInfo> {
version: String::new(),
};

let mut info = db.search(&pkg.name, &pkg.version).unwrap();
let mut info = db.search(&pkg.name, &pkg.version, skip_cache).unwrap();
if info.status == PkgStatus::NotFound {
info = db.search_new(&pkg.name, &pkg.version).unwrap();
info = db.search_new(&pkg.name, &pkg.version, skip_cache).unwrap();
if info.status != PkgStatus::NotFound {
deb.in_new = true;
deb.version = info.version;
Expand All @@ -154,12 +153,13 @@ fn run_task(db: &mut Connection, pkg: Pkg) -> Result<DebianInfo> {
Ok(deb)
}

pub fn populate(graph: &mut Graph) -> Result<(), Error> {
pub fn populate(graph: &mut Graph, args: &Args) -> Result<(), Error> {
let (task_tx, task_rx) = crossbeam_channel::unbounded();
let (return_tx, return_rx) = crossbeam_channel::unbounded();

info!("Creating thread-pool");
for _ in 0..QUERY_THREADS {
for _ in 0..args.concurrency {
let args = args.clone();
let task_rx = task_rx.clone();
let return_tx = return_tx.clone();

Expand All @@ -173,7 +173,7 @@ pub fn populate(graph: &mut Graph) -> Result<(), Error> {
};

for (idx, pkg) in task_rx {
let deb = run_task(&mut db, pkg);
let deb = run_task(&mut db, pkg, args.skip_cache);
if return_tx.send(Ok((idx, deb))).is_err() {
break;
}
Expand All @@ -196,12 +196,16 @@ pub fn populate(graph: &mut Graph) -> Result<(), Error> {

info!("Processing debian results");

let pb = ProgressBar::new(jobs as u64)
.with_style(
ProgressStyle::default_bar()
.template("[{pos:.green}/{len:.green}] {prefix:.bold} {wide_bar}")?,
)
.with_prefix("Resolving debian packages");
let pb = if args.quiet {
ProgressBar::hidden()
} else {
ProgressBar::new(jobs as u64)
.with_style(
ProgressStyle::default_bar()
.template("[{pos:.green}/{len:.green}] {prefix:.bold} {wide_bar}")?,
)
.with_prefix("Resolving debian packages")
};
pb.tick();

for result in return_rx.iter().take(jobs) {
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn main() -> Result<(), Error> {
info!("Building graph");
let mut graph = graph::build(&args, metadata)?;
info!("Populating with debian data");
debian::populate(&mut graph)?;
debian::populate(&mut graph, &args)?;
info!("Printing graph");
tree::print(&args, &graph)?;

Expand Down
5 changes: 5 additions & 0 deletions src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,11 +280,16 @@ fn print_dependencies<'a>(

if let Prefix::Indent = prefix {
if let Some(name) = name {
// start with padding used by packaging status icons
print!(" ");

// print tree graph parts
for continues in &**levels_continue {
let c = if *continues { symbols.down } else { " " };
print!("{c} ");
}

// print the actual texts
println!("{name}");
}
}
Expand Down

0 comments on commit ec65d2a

Please sign in to comment.