Skip to content

Commit

Permalink
Added -n, --name argument and conda install. (#1)
Browse files Browse the repository at this point in the history
* Added -n, --name argument and meta.yaml file so it can be built by conda.
* Fixed introduced bug if prefix not specified.
* Fixed spelling of leaves (left leafs for backward compatibility)/
  • Loading branch information
mforbes authored and rvalieris committed Oct 1, 2018
1 parent 2e5c2f5 commit a2dcedc
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 6 deletions.
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,21 @@

conda dependency tree helper

# Install

This helper requires only that `conda-tree.py` be on your path and
that the `conda` and `networkx` packages are installed. This can be
done with `conda` itself if you like:

```bash
conda install -c mforbes conda-tree
```

# Usage

```bash
# packages that no other package depends on
$ ./conda-tree.py leafs
$ ./conda-tree.py leaves
['samtools','bcftools',...]

# dependencies of a specific package
Expand All @@ -24,6 +34,10 @@ pip -> python -> pip
pip -> wheel -> python -> pip

# query a different conda prefix/env
$ ./conda-tree.py -p /conda/envs/trinity leafs
$ ./conda-tree.py -p /conda/envs/trinity leaves
['trinity']

# query by name
$ ./conda-tree.py -n trinity leaves
['trinity']
```
29 changes: 25 additions & 4 deletions conda-tree.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#!/usr/bin/env python
import sys
import argparse
import json
import os
import sys
import subprocess

import conda.install
import conda.resolve
import conda.api
Expand Down Expand Up @@ -40,16 +44,33 @@ def remove_from_graph(g, node, _cache=None):

def main():
parser = argparse.ArgumentParser()
parser.add_argument('-p','--prefix', default=sys.prefix)
parser.add_argument('-p','--prefix', default=None)
parser.add_argument('-n','--name', default=None)
subparser = parser.add_subparsers(dest='subcmd')
subparser.add_parser('leafs', help='shows leaf packages')
subparser.add_parser('leaves', help='shows leaf packages')
subparser.add_parser('cycles', help='shows dependency cycles')
p = subparser.add_parser('whoneeds', help='shows packages that depends on this package')
p.add_argument('package', help='the target package')
p = subparser.add_parser('depends', help='shows this package dependencies')
p.add_argument('package', help='the target package')
args = parser.parse_args()

if args.name is not None:
# Allow user to specify name, but check the environment for an
# existing CONDA_EXE command. This allows a different conda
# package to be installed (and imported above) but will
# resolve the name using their expected conda. (The imported
# conda here will find the environments, but might not name
# them as the user expects.)
_conda = os.environ.get('CONDA_EXE', 'conda')
_info = json.loads(subprocess.check_output(
[_conda, 'info', '-e', '--json']))
args.prefix = conda.base.context.locate_prefix_by_name(
name=args.name, envs_dirs=_info['envs_dirs'])

if args.prefix is None:
args.prefix = sys.prefix

l = get_local_cache(args.prefix)
g = make_cache_graph(l)

Expand All @@ -65,7 +86,7 @@ def main():
e = list(map(lambda i: i[0], g.in_edges(args.package)))
print(e)

elif args.subcmd == 'leafs':
elif args.subcmd in set(['leafs', 'leaves']):
e = list(map(lambda i:i[0],(filter(lambda i:i[1]==0,g.in_degree()))))
print(e)
else:
Expand Down
26 changes: 26 additions & 0 deletions meta.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package:
name: conda-tree
version: "0.0.1"

source:
git_url: https://github.com/mforbes/conda-tree.git
git_rev: v0.0.1

requirements:
host:
- python

run:
- networkx
- conda

build:
noarch: python
script: # See https://github.com/conda/conda-build/issues/3166
- mkdir -p "$PREFIX/bin"
- cp conda-tree.py "$PREFIX/bin/"

about:
home: https://github.com/rvalieris/conda-tree
license: MIT
license_file: LICENSE

0 comments on commit a2dcedc

Please sign in to comment.