仕事で使っているOSSのライセンスに問題がないか確認する必要がありました。
Pythonライブラリもその対象だったのですが、数が多くて「pip list」からの「pip show [package名]」ではとてもやっていられません!
そこで、Pythonにインストールされているpackageの「package名、版数、License名、Home Page URL」を一括出力するPythonスクリプトを作成しましたので、記事に残しておきます。
Pythonスクリプト
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
import pkg_resources def get_pkg_license(pkg): ''' pkgで指定するpackageのライセンスを復帰します。 ''' try: lines = pkg.get_metadata_lines('METADATA') except: lines = pkg.get_metadata_lines('PKG-INFO') license = 'UNKNOWN' labels = ['License: ', 'Classifier: License :: OSI Approved :: '] for label in labels: for line in lines: if line.startswith(label): license = line[len(label):] break if license != 'UNKNOWN': return license return '(Licence not found)' def get_pkg_home_page(pkg): ''' pkgで指定するpackageのHome Page URLを取得する。 Home PageがUNKNOWNの場合は、'https://pypi.python.org/pypi/'+package名を取得する。 この場合、実際に有効なリンクとは限らない。 ''' try: lines = pkg.get_metadata_lines('METADATA') except: lines = pkg.get_metadata_lines('PKG-INFO') label = 'Home-page: ' for line in lines: if line.startswith(label): url = line[len(label):] break if url == 'UNKNOWN': url = 'https://pypi.python.org/pypi/'+pkg.key return url def create_packages_and_licenses_text(): ''' pythonにインストールされているライブラリの 「Package名、版数、ライセンス名、Home Page URL」をタブ区切りで出力します。 ''' text = '' for pkg in sorted(pkg_resources.working_set, key=lambda x: str(x).lower()): text += '\t'.join([pkg.key,pkg.version,get_pkg_license(pkg),get_pkg_home_page(pkg)]) + '\n' return text if __name__ == "__main__": text = create_packages_and_licenses_text() print(text) filename = 'packages_and_licenses.txt' with open(filename, 'w', encoding='Shift-JIS') as f: f.write(text) |
仕様
pythonにインストールされているライブラリの 「Package名、版数、ライセンス名、Home Page URL」をタブ区切りで出力します。
Package名
pkg.keyから取得します。
Version
pkg.versionから取得します。
License名
METADATAまたはPKG-INFOから情報を取得します。
「License」がUNKNOWNだった場合は、「Classifier: License :: OSI Approved」から取得し、それもUNKNOWNだった場合は、「(Licence not found)」を復帰します。
Home Page URL
METADATAまたはPKG-INFOから情報を取得します。
「Home-page」がUNKNOWNだった場合は、「https://pypi.python.org/pypi/+package名」を復帰します。この場合、実際に有効なURLとは限りません。
使い方
1 |
python3 output_packages_and_licenses.py |
実行結果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
alabaster 0.7.10 BSD License https://alabaster.readthedocs.io amqp 2.1.4 BSD http://github.com/celery/py-amqp babel 2.4.0 BSD http://babel.pocoo.org/ billiard 3.5.0.2 BSD http://github.com/celery/billiard bkcharts 0.2 New BSD http://github.com/bokeh/bkcharts bleach 2.0.0 Apache Software License http://github.com/mozilla/bleach blinker 1.4 MIT License http://pythonhosted.org/blinker/ bokeh 0.12.6 New BSD http://github.com/bokeh/bokeh cairocffi 0.8.0 BSD https://github.com/SimonSapin/cairocffi cairosvg 2.0.3 GNU LGPL v3+ http://www.cairosvg.org/ celery 4.0.2 BSD http://celeryproject.org cffi 1.10.0 MIT http://cffi.readthedocs.org chardet 2.3.0 LGPL https://github.com/chardet/chardet click 6.7 BSD License http://github.com/mitsuhiko/click colorama 0.3.9 BSD https://github.com/tartley/colorama cssselect 1.0.1 BSD https://github.com/scrapy/cssselect cycler 0.10.0 BSD http://github.com/matplotlib/cycler decorator 4.0.11 new BSD License https://github.com/micheles/decorator docopt 0.6.2 MIT http://docopt.org docutils 0.13.1 public domain, Python, 2-Clause BSD, GPL 3 (see COPYING.txt) http://docutils.sourceforge.net/ elasticsearch 5.4.0 Apache License, Version 2.0 https://github.com/elastic/elasticsearch-py entrypoints 0.2.2 MIT License https://github.com/takluyver/entrypoints et-xmlfile 1.0.1 MIT https://bitbucket.org/openpyxl/et_xmlfile feedparser 5.2.1 (Licence not found) https://github.com/kurtmckee/feedparser flask 0.12.2 BSD http://github.com/pallets/flask/ flask-httpauth 3.2.3 MIT http://github.com/miguelgrinberg/flask-httpauth/ flask-mail 0.9.1 BSD https://github.com/rduplain/flask-mail flask-script 2.0.5 BSD http://github.com/smurfix/flask-script flask-sqlalchemy 2.2 BSD http://github.com/mitsuhiko/flask-sqlalchemy gitsome 0.7.2 Apache License 2.0 https://github.com/donnemartin/gitsome html5lib 0.999999999 MIT License https://github.com/html5lib/html5lib-python imagesize 0.7.1 MIT https://github.com/shibukawa/imagesize_py ipykernel 4.6.1 BSD http://ipython.org ipython 6.1.0 BSD https://ipython.org ipython-genutils 0.2.0 BSD http://ipython.org ipywidgets 6.0.0 BSD http://ipython.org itsdangerous 0.24 BSD License http://github.com/mitsuhiko/itsdangerous jdcal 1.3 BSD http://github.com/phn/jdcal jedi 0.10.2 MIT https://github.com/davidhalter/jedi jinja2 2.9.6 BSD http://jinja.pocoo.org/ jsonschema 2.6.0 MIT http://github.com/Julian/jsonschema jupyter 1.0.0 BSD http://jupyter.org jupyter-client 5.0.1 BSD http://jupyter.org jupyter-console 5.1.0 BSD https://jupyter.org jupyter-core 4.3.0 BSD http://jupyter.org kombu 4.0.2 BSD https://kombu.readthedocs.io lxml 3.8.0 BSD License http://lxml.de/ markupsafe 1.0 BSD http://github.com/pallets/markupsafe matplotlib 2.0.2 BSD http://matplotlib.org mistune 0.7.4 BSD https://github.com/lepture/mistune mpmath 1.0.0 BSD http://mpmath.org nbconvert 5.2.1 BSD http://jupyter.org nbformat 4.3.0 BSD http://jupyter.org notebook 5.0.0 BSD http://jupyter.org numpy 1.13.0 BSD http://www.numpy.org numpydoc 0.7.0 BSD https://github.com/numpy/numpy/blob/master/doc/HOWTO_DOCUMENT.rst.txt olefile 0.44 BSD https://www.decalage.info/python/olefileio openpyxl 2.4.8 MIT/Expat https://openpyxl.readthedocs.io pandas 0.20.2 BSD http://pandas.pydata.org pandocfilters 1.4.1 BSD License http://github.com/jgm/pandocfilters pdfcrowd 2.7 License :: OSI Approved :: MIT License http://pdfcrowd.com/html-to-pdf-api/ pdfkit 0.6.1 MIT https://pypi.python.org/pypi/pdfkit pexpect 4.2.1 ISC license https://pexpect.readthedocs.io/ pickleshare 0.7.4 MIT https://github.com/pickleshare/pickleshare pillow 4.1.1 Standard PIL License https://python-pillow.org pip 9.0.1 MIT https://pip.pypa.io/ plotly 2.0.10 MIT https://plot.ly/python/ ply 3.10 BSD http://www.dabeaz.com/ply/ prompt-toolkit 1.0.14 BSD License https://github.com/jonathanslenders/python-prompt-toolkit ptyprocess 0.5.1 ISC License (ISCL) https://github.com/pexpect/ptyprocess py 1.4.34 MIT license http://py.readthedocs.io/ pycparser 2.17 BSD https://github.com/eliben/pycparser pycurl 7.43.0 LGPL/MIT http://pycurl.io/ pygal 2.4.0 GNU LGPL v3+ http://pygal.org/ pygments 2.2.0 BSD License http://pygments.org/ pygobject 3.20.0 GNU LGPL http://www.pygtk.org/ pyparsing 2.2.0 MIT License http://pyparsing.wikispaces.com/ pypdf2 1.26.0 BSD License http://mstamy2.github.com/PyPDF2 python-apt 1.1.0b1 GNU GPL https://pypi.python.org/pypi/python-apt python-dateutil 2.6.0 Simplified BSD https://dateutil.readthedocs.io python-debian 0.1.27 (Licence not found) http://packages.debian.org/sid/python-debian python-systemd 231 LGPLv2+ https://github.com/systemd/python-systemd pytz 2016.10 MIT http://pythonhosted.org/pytz pyyaml 3.12 MIT http://pyyaml.org/wiki/PyYAML pyzmq 16.0.2 LGPL+BSD https://pyzmq.readthedocs.org qtconsole 4.3.0 BSD http://jupyter.org reportlab 3.4.0 BSD license (see license.txt for details), Copyright (c) 2000-2015, ReportLab Inc. http://www.reportlab.com/ requests 2.9.1 Apache 2.0 http://python-requests.org retry 0.9.2 Apache License 2.0 https://github.com/invl/retry setuptools 36.0.1 MIT License https://github.com/pypa/setuptools simplegeneric 0.8.1 ZPL 2.1 http://cheeseshop.python.org/pypi/simplegeneric six 1.10.0 MIT http://pypi.python.org/pypi/six/ snowballstemmer 1.2.1 BSD https://github.com/shibukawa/snowball_py sphinx 1.6.2 BSD http://sphinx-doc.org/ sphinxcontrib-websupport 1.0.1 BSD http://sphinx-doc.org/ sqlalchemy 1.1.13 MIT License http://www.sqlalchemy.org ssh-import-id 5.5 GPLv3 https://launchpad.net/ssh-import-id sympy 1.1.1 BSD http://sympy.org terminado 0.6 BSD License https://github.com/takluyver/terminado testpath 0.3.1 MIT License https://github.com/jupyter/testpath tinycss 0.4 BSD http://tinycss.readthedocs.io/ tornado 4.5.1 http://www.apache.org/licenses/LICENSE-2.0 http://www.tornadoweb.org/ traitlets 4.3.2 BSD http://ipython.org unattended-upgrades 0.1 (Licence not found) https://pypi.python.org/pypi/unattended-upgrades uritemplate 3.0.0 BSD 3-Clause License or Apache License, Version 2.0 https://uritemplate.readthedocs.org uritemplate.py 3.0.2 BSD 3-Clause License or Apache License, Version 2.0 https://uritemplate.readthedocs.org urllib3 1.13.1 MIT http://urllib3.readthedocs.org/ vine 1.1.3 BSD http://github.com/celery/vine wcwidth 0.1.7 MIT https://github.com/jquast/wcwidth webencodings 0.5.1 BSD https://github.com/SimonSapin/python-webencodings werkzeug 0.12.2 BSD http://werkzeug.pocoo.org/ wheel 0.29.0 MIT https://bitbucket.org/pypa/wheel/ widgetsnbextension 2.0.0 BSD http://ipython.org xhtml2pdf 0.0.6 Apache License 2.0 http://www.xhtml2pdf.com xlrd 1.0.0 BSD http://www.python-excel.org/ |
参考
Can pip (or setuptools, distribute etc…) list the license used by each installed package?