forked from raimon49/pip-licenses
-
Notifications
You must be signed in to change notification settings - Fork 0
/
piplicenses.py
276 lines (217 loc) · 8.17 KB
/
piplicenses.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8 ff=unix ft=python ts=4 sw=4 sts=4 si et
"""
pip-licenses
MIT License
Copyright (c) 2018 raimon
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
from __future__ import (division, print_function,
absolute_import, unicode_literals)
import sys
import argparse
from email.parser import FeedParser
from email import message_from_string
try:
from pip._internal.utils.misc import get_installed_distributions
except ImportError:
from pip import get_installed_distributions
from prettytable import PrettyTable
from prettytable.prettytable import (FRAME as RULE_FRAME, ALL as RULE_ALL,
HEADER as RULE_HEADER, NONE as RULE_NONE)
__pkgname__ = 'pip-licenses'
__version__ = '1.7.1'
__author__ = 'raimon'
__license__ = 'MIT License'
__summary__ = ('Dump the software license list of '
'Python packages installed with pip.')
__url__ = 'https://github.com/raimon49/pip-licenses'
FIELD_NAMES = (
'Name',
'Version',
'License',
'Author',
'URL',
)
DEFAULT_OUTPUT_FIELDS = (
'Name',
'Version',
'License',
)
METADATA_KEYS = (
'home-page',
'author',
'license',
)
SYSTEM_PACKAGES = (
__pkgname__,
'pip',
'PTable',
'setuptools',
'wheel',
)
LICENSE_UNKNOWN = 'UNKNOWN'
def create_licenses_table(args):
def get_pkg_info(pkg):
pkg_info = {
'name': pkg.project_name,
'version': pkg.version,
'namever': str(pkg),
}
metadata = None
if pkg.has_metadata('METADATA'):
metadata = pkg.get_metadata('METADATA')
if pkg.has_metadata('PKG-INFO') and metadata is None:
metadata = pkg.get_metadata('PKG-INFO')
if metadata is None:
for key in METADATA_KEYS:
pkg_info[key] = LICENSE_UNKNOWN
return pkg_info
feed_parser = FeedParser()
feed_parser.feed(metadata)
parsed_metadata = feed_parser.close()
for key in METADATA_KEYS:
pkg_info[key] = parsed_metadata.get(key, LICENSE_UNKNOWN)
if args.from_classifier and metadata is not None:
message = message_from_string(metadata)
pkg_info['license'] = find_license_from_classifier(message)
return pkg_info
table = factory_styled_table_with_args(args)
pkgs = get_installed_distributions()
ignore_pkgs_as_lower = [pkg.lower() for pkg in args.ignore_packages]
for pkg in pkgs:
pkg_info = get_pkg_info(pkg)
pkg_name = pkg_info['name']
if pkg_name.lower() in ignore_pkgs_as_lower:
continue
if not args.with_system and pkg_name in SYSTEM_PACKAGES:
continue
table.add_row([pkg_info['name'],
pkg_info['version'],
pkg_info['license'],
pkg_info['author'],
pkg_info['home-page'], ])
return table
def factory_styled_table_with_args(args):
table = PrettyTable()
table.field_names = FIELD_NAMES
table.align = 'l'
table.border = (args.format_markdown or args.format_rst or
args.format_confluence)
table.header = True
if args.format_markdown:
table.junction_char = '|'
table.hrules = RULE_HEADER
elif args.format_rst:
table.junction_char = '+'
table.hrules = RULE_ALL
elif args.format_confluence:
table.junction_char = '|'
table.hrules = RULE_NONE
return table
def find_license_from_classifier(message):
license_from_classifier = LICENSE_UNKNOWN
licenses = []
for k, v in message.items():
if k == 'Classifier' and v.startswith('License'):
licenses.append(v.split(' :: ')[-1])
if len(licenses) > 0:
license_from_classifier = ', '.join(licenses)
return license_from_classifier
def get_output_fields(args):
output_fields = list(DEFAULT_OUTPUT_FIELDS)
if args.with_authors:
output_fields.append('Author')
if args.with_urls:
output_fields.append('URL')
return output_fields
def get_sortby(args):
if args.order == 'name':
return 'Name'
elif args.order == 'license':
return 'License'
elif args.order == 'author' and args.with_authors:
return 'Author'
elif args.order == 'url' and args.with_urls:
return 'URL'
return 'Name'
def create_output_string(args):
table = create_licenses_table(args)
output_fields = get_output_fields(args)
sortby = get_sortby(args)
if args.format_html:
return table.get_html_string(fields=output_fields, sortby=sortby)
else:
return table.get_string(fields=output_fields, sortby=sortby)
def create_parser():
parser = argparse.ArgumentParser(
description=__summary__)
parser.add_argument('-v', '--version',
action='version',
version='%(prog)s ' + __version__)
parser.add_argument('-c', '--from-classifier',
action='store_true',
default=False,
help='find license from classifier')
parser.add_argument('-s', '--with-system',
action='store_true',
default=False,
help='dump with system packages')
parser.add_argument('-a', '--with-authors',
action='store_true',
default=False,
help='dump with package authors')
parser.add_argument('-u', '--with-urls',
action='store_true',
default=False,
help='dump with package urls')
parser.add_argument('-i', '--ignore-packages',
action='store', type=str,
nargs='+', metavar='PKG',
default=[],
help='ignore package name in dumped list')
parser.add_argument('-o', '--order',
action='store', type=str,
default='name', metavar='COL',
help=('order by column\n'
'"name", "license", "author", "url"\n'
'default: --order=name'))
parser.add_argument('-m', '--format-markdown',
action='store_true',
default=False,
help='dump as markdown style')
parser.add_argument('-r', '--format-rst',
action='store_true',
default=False,
help='dump as reST style')
parser.add_argument('--format-confluence',
action='store_true',
default=False,
help='dump as confluence wiki style')
parser.add_argument('--format-html',
action='store_true',
default=False,
help='dump as html style')
return parser
def main(): # pragma: no cover
parser = create_parser()
args = parser.parse_args()
output_string = create_output_string(args)
print(output_string)
if __name__ == '__main__': # pragma: no cover
main()