Skip to content

Commit

Permalink
Fix reading license files with windows newlines
Browse files Browse the repository at this point in the history
Fixes raimon49#55.

By using `open()` rather than `codecs.open()` the newline-fixing behaviour from Python 3 can be used.

Presumably `codecs.open()` was used previously to allow proper UTF-8 handling in Python 2, but since that has been dropped the normal `open()` can be used.

Also drop some inner imports in tests and empty `setUp()` and `tearDown()` methods.

Tested with:

```
$ pip install django-cors-headers==3.2.0
$ python piplicenses.py --with-license-file --no-license-path | grep -C 2 django-cors-headers
                                                                                                   END OF TERMS AND CONDITIONS

 django-cors-headers  3.2.0       MIT License                                                   Copyright 2017 Otto Yiu and other contributors
                                                                                                http://ottoyiu.com
```

This showed the same breakage as in the issue before the fix.
  • Loading branch information
adamchainz committed Jan 4, 2020
1 parent caca390 commit 6351666
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 28 deletions.
18 changes: 5 additions & 13 deletions piplicenses.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import sys
import glob
import os
import codecs
import argparse
from functools import partial
from email.parser import FeedParser
Expand Down Expand Up @@ -57,6 +56,8 @@
)
PTABLE = False

open = open # allow monkey patching

__pkgname__ = 'pip-licenses'
__version__ = '2.0.0'
__author__ = 'raimon'
Expand Down Expand Up @@ -137,17 +138,8 @@ def get_pkg_license_file(pkg):
for test_file in glob.glob(license_file_base):
if os.path.exists(test_file):
license_file = test_file
with codecs.open(test_file,
encoding='utf-8',
errors='replace') as license_file_handle:
file_lines = license_file_handle.readlines()
try:
# python 3 is happy with maybe-Unicode files
license_text = "".join(file_lines)
except UnicodeDecodeError: # pragma: no cover
# python 2 not so much
license_text = "".join([line.decode('utf-8', 'replace')
for line in file_lines])
with open(test_file) as license_file_handle:
license_text = license_file_handle.read()
break
return (license_file, license_text)

Expand Down Expand Up @@ -611,7 +603,7 @@ def save_if_needs(output_file, output_string):
return

try:
with codecs.open(output_file, 'w', 'utf-8',) as f:
with open(output_file, 'w') as f:
f.write(output_string)
sys.stdout.write('created path: ' + output_file + '\n')
sys.exit(0)
Expand Down
20 changes: 5 additions & 15 deletions test_piplicenses.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# -*- coding: utf-8 -*-
# vim:fenc=utf-8 ff=unix ft=python ts=4 sw=4 sts=4 si et
import copy
import sys
import unittest
from email import message_from_string

import piplicenses
from piplicenses import (__pkgname__, create_parser, output_colored,
create_licenses_table, get_output_fields, get_sortby,
factory_styled_table_with_args, create_warn_string,
Expand All @@ -20,11 +23,8 @@ def setUpClass(cls):


class TestGetLicenses(CommandLineTestCase):
def setUp(self):
pass

def _create_pkg_name_columns(self, table):
import copy
index = DEFAULT_OUTPUT_FIELDS.index('Name')

# XXX: access to private API
Expand All @@ -36,7 +36,6 @@ def _create_pkg_name_columns(self, table):
return pkg_name_columns

def _create_license_columns(self, table):
import copy
index = DEFAULT_OUTPUT_FIELDS.index('License')

# XXX: access to private API
Expand Down Expand Up @@ -411,9 +410,6 @@ def test_output_colored_bold(self):
self.assertIn(text, actual)
self.assertTrue(actual.endswith('\033[0m'))

def tearDown(self):
pass


class MockStdStream(object):

Expand All @@ -431,9 +427,7 @@ def mocked_open(*args, **kwargs):

mocked_stdout = MockStdStream()
mocked_stderr = MockStdStream()
import codecs
import sys
monkeypatch.setattr(codecs, 'open', mocked_open)
monkeypatch.setattr(piplicenses, 'open', mocked_open)
monkeypatch.setattr(sys.stdout, 'write', mocked_stdout.write)
monkeypatch.setattr(sys.stderr, 'write', mocked_stderr.write)
monkeypatch.setattr(sys, 'exit', lambda n: None)
Expand All @@ -449,9 +443,7 @@ def mocked_open(*args, **kwargs):

mocked_stdout = MockStdStream()
mocked_stderr = MockStdStream()
import codecs
import sys
monkeypatch.setattr(codecs, 'open', mocked_open)
monkeypatch.setattr(piplicenses, 'open', mocked_open)
monkeypatch.setattr(sys.stdout, 'write', mocked_stdout.write)
monkeypatch.setattr(sys.stderr, 'write', mocked_stderr.write)
monkeypatch.setattr(sys, 'exit', lambda n: None)
Expand All @@ -464,8 +456,6 @@ def mocked_open(*args, **kwargs):
def test_output_file_none(monkeypatch):
mocked_stdout = MockStdStream()
mocked_stderr = MockStdStream()
import codecs
import sys
monkeypatch.setattr(sys.stdout, 'write', mocked_stdout.write)
monkeypatch.setattr(sys.stderr, 'write', mocked_stderr.write)

Expand Down

0 comments on commit 6351666

Please sign in to comment.