[pipreqs/pipreqs.py] Use codec to get an open with encoding argument ; fix __version__ read (import cannot work) ; don't crash on UnicodeDecodeError

This commit is contained in:
Samuel Marks 2022-08-05 11:21:48 -04:00
parent a593d27e3d
commit ff846fde1b
No known key found for this signature in database
GPG Key ID: 43FD8EDE42E1A799

View File

@ -43,12 +43,24 @@ import re
import logging
import ast
import traceback
from codecs import open
from docopt import docopt
import requests
from yarg import json2package
from yarg.exceptions import HTTPError
from pipreqs import __version__
if sys.version[0] == "2":
from itertools import imap as map, ifilter as filter
with open(os.path.join(os.path.dirname(__file__), "__init__.py"), "rt") as f:
__version__ = next(map(
lambda buf: next(map(lambda e: e.value.s, ast.parse(buf).body)),
filter(
lambda line: line.startswith("__version__"),
f,
),
))
REGEXP = [
re.compile(r'^import (.+)$'),
@ -190,6 +202,10 @@ def get_imports_info(
logging.debug(
'Package %s does not exist or network problems', item)
continue
except UnicodeDecodeError:
logging.debug(
'Package %s could not be read from server', item)
continue
result.append({'name': item, 'version': data.latest_release_id})
return result