Don't show bullets if freeze option is enabled

This makes the output of pipdeptree (with freeze flag) pip friendly
ie. the output can be saved in a requirements.txt file. At the same time
it's human readable due to indentations for sub-dependencies.

There is however one gotcha! In case pip finds a dependency multiple
times in the requirements.txt file, it fails. So the user will need to
take care about this when dumping the output in requirements.txt.

Fixes #31.
This commit is contained in:
Vineet Naik 2015-07-26 23:07:07 +05:30
parent 0f3dd9cf62
commit db33a5d788
2 changed files with 9 additions and 6 deletions

View File

@ -111,7 +111,7 @@ def confusing_deps(req_map):
def render_tree(pkgs, pkg_index, req_map, list_all,
top_pkg_str, non_top_pkg_str):
top_pkg_str, non_top_pkg_str, bullets=True):
"""Renders a package dependency tree as a string
:param list pkgs: pkg_resources.Distribution instances
@ -123,6 +123,8 @@ def render_tree(pkgs, pkg_index, req_map, list_all,
package as string
:param function non_top_pkg_str: function to render a non-top
level package as string
:param bool bullets: whether or not to show bullets for child
dependencies [default: True]
:returns: dependency tree encoded as string
:rtype: str
@ -144,8 +146,8 @@ def render_tree(pkgs, pkg_index, req_map, list_all,
# FixMe! Some dependencies are not present in the result of
# `pip.get_installed_distributions`
# eg. `testresources`. This is a hack around it.
name = pkg.project_name if dist is None else non_top_pkg_str(pkg, dist)
result = [' '*indent+'- '+name]
name = pkg.project_name if dist is None else non_top_pkg_str(pkg, dist)
result = [' '*indent + ('-' if bullets else ' ') + ' ' + name]
else:
result = [top_pkg_str(pkg)]
@ -266,7 +268,8 @@ def main():
req_map=req_map,
list_all=args.all,
top_pkg_str=top_pkg_str,
non_top_pkg_str=non_top_pkg_str)
non_top_pkg_str=non_top_pkg_str,
bullets=not args.freeze)
print(tree)
return 0

View File

@ -76,7 +76,7 @@ def test_render_tree_list_all():
def test_render_tree_freeze():
tree_str = render_tree(pkgs, pkg_index, req_map, False,
top_pkg_src, non_top_pkg_src)
top_pkg_src, non_top_pkg_src, bullets=False)
lines = set()
for line in tree_str.split('\n'):
# Workaround for https://github.com/pypa/pip/issues/1867
@ -86,7 +86,7 @@ def test_render_tree_freeze():
line = line.replace('origin/HEAD', 'master')
lines.add(line)
assert 'Flask-Script==0.6.6' in lines
assert ' - SQLAlchemy==0.9.1' in lines
assert ' SQLAlchemy==0.9.1' in lines
assert '-e git+https://github.com/naiquevin/lookupy.git@cdbe30c160e1c29802df75e145ea4ad903c05386#egg=Lookupy-master' in lines
assert 'itsdangerous==0.23' not in lines