Skip to content

Commit

Permalink
Merge pull request #32 from raimon49/implement-mixed-mode
Browse files Browse the repository at this point in the history
Release 1.14.0
  • Loading branch information
raimon49 authored May 10, 2019
2 parents cbbf673 + 6689521 commit 49daf63
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## CHANGELOG

### 1.14.0

* Implement new option `--from=mixed` as a mixed mode

### 1.13.0

* Implement new option `--from=meta`, `from=classifier`
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,15 @@ If you want to refer to the license declared in [the Classifiers](https://pypi.p
setuptools 38.5.0 MIT License
```

If you want to find a license from whichever, mixed mode (`--from=mixed`) is available in `pip-licenses` version 1.14.0 or later.

In mixed mode, it first tries to look for licenses in the Classifiers. When not found in the Classifiers, the license declared in Metadata is displayed.

**Note:** If neither can find license information, please check with the `with-authors` and `with-urls` options and contact the software author.

* The `m` keyword is prepared as alias of `meta`.
* The `c` keyword is prepared as alias of `classifier`.
* The `mix` keyword is prepared as alias of `mixed`.

#### Deprecated from-classifier

Expand Down
10 changes: 5 additions & 5 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@ funcsigs==1.0.2 # via pytest
idna==2.8 # via requests
more-itertools==5.0.0 # via pytest
pathlib2==2.3.3 # via pytest
pip-tools==3.6.1
pip-tools==3.7.0
pkginfo==1.5.0.1 # via twine
pluggy==0.9.0 # via pytest
pluggy==0.11.0 # via pytest
ptable==0.9.2
py==1.8.0 # via pytest
pycodestyle==2.5.0 # via autopep8, pytest-pycodestyle
pygments==2.3.1 # via readme-renderer
pygments==2.4.0 # via readme-renderer
pypandoc==1.4
pytest-cache==1.0 # via pytest-pycodestyle
pytest-cov==2.7.1
pytest-pycodestyle==1.0.6
pytest-runner==4.4
pytest==4.4.1 # via pytest-cache, pytest-cov, pytest-pycodestyle
pytest==4.4.2 # via pytest-cache, pytest-cov, pytest-pycodestyle
readme-renderer==24.0 # via twine
requests-toolbelt==0.9.1 # via twine
requests==2.21.0 # via codecov, requests-toolbelt, twine
Expand All @@ -42,4 +42,4 @@ tqdm==4.31.1 # via twine
twine==1.13.0
urllib3==1.24.3 # via requests
webencodings==0.5.1 # via bleach
wheel==0.33.1
wheel==0.33.2
28 changes: 24 additions & 4 deletions piplicenses.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
HEADER as RULE_HEADER, NONE as RULE_NONE)

__pkgname__ = 'pip-licenses'
__version__ = '1.13.0'
__version__ = '1.14.0'
__author__ = 'raimon'
__license__ = 'MIT License'
__summary__ = ('Dump the software license list of '
Expand Down Expand Up @@ -165,9 +165,16 @@ def get_pkg_info(pkg):
for key in METADATA_KEYS:
pkg_info[key] = parsed_metadata.get(key, LICENSE_UNKNOWN)

if getattr(args, 'from') == 'classifier' and metadata is not None:
from_source = getattr(args, 'from')
need_classifier = from_source == 'classifier' or from_source == 'mixed'
if need_classifier and metadata is not None:
message = message_from_string(metadata)
pkg_info['license'] = find_license_from_classifier(message)
license_classifier = find_license_from_classifier(message)
license_meta = pkg_info['license']
# Overwrite license by condition
pkg_info['license'] = select_license_by_source(from_source,
license_classifier,
license_meta)

return pkg_info

Expand Down Expand Up @@ -287,6 +294,16 @@ def find_license_from_classifier(message):
return license_from_classifier


def select_license_by_source(from_source, license_classifier, license_meta):
if from_source == 'classifier':
return license_classifier
elif from_source == 'mixed':
if license_classifier != LICENSE_UNKNOWN:
return license_classifier
else:
return license_meta


def get_output_fields(args):
if args.summary:
return list(SUMMARY_OUTPUT_FIELDS)
Expand Down Expand Up @@ -391,6 +408,9 @@ def _compatible_format_args(self, args):
if from_input in ('classifier', 'c'):
setattr(args, 'from', 'classifier')

if from_input in ('mixed', 'mix'):
setattr(args, 'from', 'mixed')

if order_input in ('count', 'c'):
args.order = 'count'

Expand Down Expand Up @@ -449,7 +469,7 @@ def create_parser():
action='store', type=str,
default='meta', metavar='SOURCE',
help=('where to find license information\n'
'"meta", "classifier"\n'
'"meta", "classifier, "mixed"\n'
'default: --from=meta'))
parser.add_argument('-c', '--from-classifier',
action='store_true',
Expand Down
35 changes: 35 additions & 0 deletions test_piplicenses.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
create_licenses_table, get_output_fields, get_sortby,
factory_styled_table_with_args, create_warn_string,
find_license_from_classifier, create_output_string,
select_license_by_source,
DEFAULT_OUTPUT_FIELDS, SYSTEM_PACKAGES,
LICENSE_UNKNOWN)

Expand Down Expand Up @@ -85,6 +86,19 @@ def test_from_classifier(self):
license_notation_as_classifier = 'MIT License'
self.assertIn(license_notation_as_classifier, license_columns)

def test_from_mixed(self):
from_classifier_args = ['--from=mixed']
args = self.parser.parse_args(from_classifier_args)
table = create_licenses_table(args)

output_fields = get_output_fields(args)
self.assertIn('License', output_fields)

license_columns = self._create_license_columns(table)
# Depending on the condition "MIT" or "BSD" etc.
license_notation_as_classifier = 'MIT License'
self.assertIn(license_notation_as_classifier, license_columns)

def test_find_license_from_classifier(self):
metadata = ('Metadata-Version: 2.0\r\n'
'Name: pip-licenses\r\n'
Expand Down Expand Up @@ -117,6 +131,27 @@ def test_not_found_license_from_classifier(self):
self.assertEqual(LICENSE_UNKNOWN,
find_license_from_classifier(message))

def test_select_license_by_source(self):
self.assertEqual('MIT License',
select_license_by_source('classifier',
'MIT License',
'MIT'))

self.assertEqual(LICENSE_UNKNOWN,
select_license_by_source('classifier',
LICENSE_UNKNOWN,
'MIT'))

self.assertEqual('MIT License',
select_license_by_source('mixed',
'MIT License',
'MIT'))

self.assertEqual('MIT',
select_license_by_source('mixed',
LICENSE_UNKNOWN,
'MIT'))

def test_with_system(self):
with_system_args = ['--with-system']
args = self.parser.parse_args(with_system_args)
Expand Down

0 comments on commit 49daf63

Please sign in to comment.