Skip to content

Commit

Permalink
add dependent packages flag to package cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszKielar committed Oct 21, 2020
1 parent 28aacd9 commit c7597dc
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 16 deletions.
20 changes: 16 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Installation is as simple as running `cargo install conda-leaves`.

### Commands

Basic usage of CLI is simple call of
Basic usage of CLI is as simple as:

```bash
conda-leaves
Expand All @@ -32,17 +32,21 @@ conda-leaves help

Prints tree view for the package. It helps to understand which libraries are required by the package.

Flags:

- `-d`, `--dependent-packages` - Prints libraries that depend on a given package.

Options:

- `-n`, `-name` - Name of the package that should be printed.
- `-n`, `--name` - Name of the package that should be printed.

Usage:

```bash
conda-leaves package --name <name>
conda-leaves package [Flags] --name <name>
```

Example:
Examples:

```bash
$ conda-leaves package -n jinja2
Expand All @@ -52,6 +56,14 @@ jinja2 (v2.11.2)
└── certifi (v2020.6.20)
```

```bash
$ conda-leaves package -n dask -d
Following packages depend on dask:
- dask-ml
- dask-xgboost
- dask-glm
```

#### export

Exports leaves to the file.
Expand Down
39 changes: 30 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ use structopt::StructOpt;
use crate::env::CondaEnv;
use crate::metadata::Metadata;
use crate::package::{print_package, Package};
use crate::utils::get_leaves;
use crate::utils::{get_dependent_packages, get_leaves};

/// Simple CLI tool that allows to pretty print all dependencies within conda environment
#[derive(Debug, StructOpt)]
#[structopt(name = "conda-leaves")]
struct Opts {
// TODO it causes CLI much more complex to handle
// I should use it to keep the state of the Opts struct, not to match on it
/// Prints packages installed by conda only
#[structopt(long)]
no_pip: bool,
Expand All @@ -31,6 +33,9 @@ enum Commands {
Package {
#[structopt(short = "n", long)]
name: String,
/// Prints libraries that depends on a given package
#[structopt(short = "d", long)]
dependent_packages: bool,
},
/// Exports leaves to the file
Export {
Expand Down Expand Up @@ -60,15 +65,31 @@ fn main() -> io::Result<()> {
}
}
Some(command) => match command {
Commands::Package { name } => match Metadata::from_name(name) {
Ok(m) => {
let p: Package = m.into();
print_package(&p);
}
Err(e) => {
eprintln!("{}", e);
std::process::exit(404)
Commands::Package {
name,
dependent_packages,
} => match dependent_packages {
true => {
let dep_packages = get_dependent_packages(&name);
if dep_packages.len() == 0 {
println!("{} is not required by any package in the environment", name)
} else {
println!("Following packages depend on {}:", name,)
}
for package in dep_packages {
println!("- {}", package)
}
}
false => match Metadata::from_name(name) {
Ok(m) => {
let p: Package = m.into();
print_package(&p);
}
Err(e) => {
eprintln!("{}", e);
std::process::exit(404)
}
},
},
Commands::Export { filename } => {
let leaves = get_leaves();
Expand Down
15 changes: 12 additions & 3 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ pub(crate) fn get_conda_metadata() -> HashMap<String, Metadata> {
conda_metadata
}

// TODO this function should take a conda_metadata as an argument
// it will be much more flexible
// TODO this function should take a reference to a name
/// Returns a list of dependencies for given package.
///
/// Panics if given package name is not present in the environment.
// TODO this should return Result<Vec<String>>
pub fn get_dependent_packages<T: AsRef<str>>(name: T) -> Vec<String> {
match CONDA_METADATA.get(name.as_ref()) {
Some(_) => (),
Expand Down Expand Up @@ -239,6 +239,15 @@ mod tests {
assert_eq!(conda_metadata, expected_conda_metadata)
}

#[test]
#[should_panic(expected = "Package 'pkg404' not installed")]
fn test_get_dependent_packages_invalid_package_panic() {
// given:
std::env::set_var("CONDA_PREFIX", "./tests/data");
// then:
get_dependent_packages(String::from("pkg404"));
}

#[test]
fn test_get_dependent_packages_empty() {
// given:
Expand Down

0 comments on commit c7597dc

Please sign in to comment.