Skip to content

Commit

Permalink
Merge pull request #68 from raimon49/release-2.2.0
Browse files Browse the repository at this point in the history
Release 2.2.0
  • Loading branch information
raimon49 authored Apr 29, 2020
2 parents 7f7555f + e8fe24d commit 05e12f3
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 13 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
## CHANGELOG

### 2.2.0

* Implement new option `--with-notice-file`
* Added to find British style file name `LICENCE` with run '--with-license-file'

### 2.1.1

* Suppress errors when opening license files
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ When executed with the `--with-description` option, output with short descriptio

When executed with the `--with-license-file` option, output the location of the package's license file on disk and the full contents of that file. Due to the length of these fields, this option is best paired with `--format=json`.

If you also want to output the file `NOTICE` distributed under Apache License etc., specify the `--with-notice-file` option additionally.

**Note:** If you want to keep the license file path secret, specify `--no-license-path` option together.

### Option: ignore-packages
Expand Down
44 changes: 32 additions & 12 deletions piplicenses.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
open = open # allow monkey patching

__pkgname__ = 'pip-licenses'
__version__ = '2.1.1'
__version__ = '2.2.0'
__author__ = 'raimon'
__license__ = 'MIT License'
__summary__ = ('Dump the software license list of '
Expand All @@ -74,6 +74,8 @@
'License',
'LicenseFile',
'LicenseText',
'NoticeFile',
'NoticeText',
'Author',
'Description',
'URL',
Expand Down Expand Up @@ -126,37 +128,45 @@

def get_packages(args):

def get_pkg_license_file(pkg):
def get_pkg_included_file(pkg, file_names):
"""
Attempt to find the package's LICENSE file on disk and return the
tuple (license_file_path, license_file_contents).
Attempt to find the package's included file on disk and return the
tuple (included_file_path, included_file_contents).
"""
license_file = LICENSE_UNKNOWN
license_text = LICENSE_UNKNOWN
included_file = LICENSE_UNKNOWN
included_text = LICENSE_UNKNOWN
pkg_dirname = "{}-{}.dist-info".format(
pkg.project_name.replace("-", "_"), pkg.version)
file_names = ('LICENSE*', 'COPYING*')
patterns = []
[patterns.extend(glob.glob(os.path.join(pkg.location,
pkg_dirname,
f))) for f in file_names]
for test_file in patterns:
if os.path.exists(test_file):
license_file = test_file
included_file = test_file
with open(test_file, encoding='utf-8',
errors='backslashreplace') as license_file_handle:
license_text = license_file_handle.read()
errors='backslashreplace') as included_file_handle:
included_text = included_file_handle.read()
break
return (license_file, license_text)
return (included_file, included_text)

def get_pkg_info(pkg):
(license_file, license_text) = get_pkg_license_file(pkg)
(license_file, license_text) = get_pkg_included_file(
pkg,
('LICENSE*', 'LICENCE*', 'COPYING*')
)
(notice_file, notice_text) = get_pkg_included_file(
pkg,
('NOTICE*',)
)
pkg_info = {
'name': pkg.project_name,
'version': pkg.version,
'namever': str(pkg),
'licensefile': license_file,
'licensetext': license_text,
'noticefile': notice_file,
'noticetext': notice_text,
}
metadata = None
if pkg.has_metadata('METADATA'):
Expand Down Expand Up @@ -423,6 +433,11 @@ def get_output_fields(args):

output_fields.append('LicenseText')

if args.with_notice_file:
output_fields.append('NoticeText')
if not args.no_license_path:
output_fields.append('NoticeFile')

return output_fields


Expand Down Expand Up @@ -578,6 +593,11 @@ def create_parser():
default=False,
help='when specified together with option -l, '
'suppress location of license file output')
parser.add_argument('--with-notice-file',
action='store_true',
default=False,
help='when specified together with option -l, '
'dump with location of license file and contents')
parser.add_argument('-i', '--ignore-packages',
action='store', type=str,
nargs='+', metavar='PKG',
Expand Down
28 changes: 27 additions & 1 deletion test_piplicenses.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,23 +203,49 @@ def test_with_license_file(self):
self.assertNotEqual(output_fields, list(DEFAULT_OUTPUT_FIELDS))
self.assertIn('LicenseFile', output_fields)
self.assertIn('LicenseText', output_fields)
self.assertNotIn('NoticeFile', output_fields)
self.assertNotIn('NoticeText', output_fields)

output_string = create_output_string(args)
self.assertIn('LicenseFile', output_string)
self.assertIn('LicenseText', output_string)
self.assertNotIn('NoticeFile', output_string)
self.assertNotIn('NoticeText', output_string)

def test_with_notice_file(self):
with_license_file_args = ['--with-license-file', '--with-notice-file']
args = self.parser.parse_args(with_license_file_args)

output_fields = get_output_fields(args)
self.assertNotEqual(output_fields, list(DEFAULT_OUTPUT_FIELDS))
self.assertIn('LicenseFile', output_fields)
self.assertIn('LicenseText', output_fields)
self.assertIn('NoticeFile', output_fields)
self.assertIn('NoticeText', output_fields)

output_string = create_output_string(args)
self.assertIn('LicenseFile', output_string)
self.assertIn('LicenseText', output_string)
self.assertIn('NoticeFile', output_string)
self.assertIn('NoticeText', output_string)

def test_with_license_file_no_path(self):
with_license_file_args = ['--with-license-file', '--no-license-path']
with_license_file_args = ['--with-license-file', '--with-notice-file',
'--no-license-path']
args = self.parser.parse_args(with_license_file_args)

output_fields = get_output_fields(args)
self.assertNotEqual(output_fields, list(DEFAULT_OUTPUT_FIELDS))
self.assertNotIn('LicenseFile', output_fields)
self.assertIn('LicenseText', output_fields)
self.assertNotIn('NoticeFile', output_fields)
self.assertIn('NoticeText', output_fields)

output_string = create_output_string(args)
self.assertNotIn('LicenseFile', output_string)
self.assertIn('LicenseText', output_string)
self.assertNotIn('NoticeFile', output_string)
self.assertIn('NoticeText', output_string)

def test_with_license_file_warning(self):
with_license_file_args = ['--with-license-file', '--format=markdown']
Expand Down

0 comments on commit 05e12f3

Please sign in to comment.