tox-dev / pipdeptree

A command line utility to display dependency tree of the installed Python packages

Home Page:https://pypi.python.org/pypi/pipdeptree

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add an option to show the license

anki-code opened this issue · comments

Hi! Great tool!

It will be cool to have an option to show license in the tree e.g.:

pipdeptree -w silence -p pandas # --license
# pandas==1.5.3 ('BSD-3-Clause', 'BSD License')
# ├── numpy [required: >=1.21.0, installed: 1.24.3]  ('BSD-3-Clause', 'BSD License') 
# ├── python-dateutil [required: >=2.8.1, installed: 2.8.2] ('Dual License', 'BSD License', 'Apache Software License')
# │   └── six [required: >=1.5, installed: 1.16.0] ('MIT', 'MIT License')
# └── pytz [required: >=2020.1, installed: 2022.7] ('MIT', 'MIT License')

To create this example I used this code:

from itertools import chain, compress
from pkg_resources import get_distribution


def filters(line):
    return compress(
        (line[9:], line[39:]),
        (line.startswith('License:'), line.startswith('Classifier: License')),
    )


def get_pkg_license(pkg):
    distribution = get_distribution(pkg)
    try:
        lines = distribution.get_metadata_lines('METADATA')
    except OSError:
        lines = distribution.get_metadata_lines('PKG-INFO')
    return tuple(chain.from_iterable(map(filters, lines)))

for p in ['pandas', 'numpy', 'six', 'pytz']:
    print(p, get_pkg_license(p))

Thanks!

For community

⬇️ Please click the 👍 reaction instead of leaving a +1 or 👍 comment

PR welcome.

This seems like it would also resolve #79 from reading its description