mirror of
https://github.com/bndr/pipreqs.git
synced 2025-06-07 12:05:33 +00:00
fix(pipreqs/pipreqs.py): add python 2.6 and 2.7 support for encoding parameter
This commit is contained in:
parent
2ffa4d2bf0
commit
3acbdaa893
@ -19,6 +19,7 @@ import os
|
|||||||
import sys
|
import sys
|
||||||
import re
|
import re
|
||||||
import logging
|
import logging
|
||||||
|
import codecs
|
||||||
|
|
||||||
from docopt import docopt
|
from docopt import docopt
|
||||||
import requests
|
import requests
|
||||||
@ -32,6 +33,11 @@ REGEXP = [
|
|||||||
re.compile(r'^from ((?!\.+).*?) import (?:.*)$')
|
re.compile(r'^from ((?!\.+).*?) import (?:.*)$')
|
||||||
]
|
]
|
||||||
|
|
||||||
|
if sys.version_info[0] > 2:
|
||||||
|
open_func = open
|
||||||
|
else:
|
||||||
|
open_func = codecs.open
|
||||||
|
|
||||||
|
|
||||||
def get_all_imports(path, encoding=None):
|
def get_all_imports(path, encoding=None):
|
||||||
imports = []
|
imports = []
|
||||||
@ -46,7 +52,7 @@ def get_all_imports(path, encoding=None):
|
|||||||
|
|
||||||
candidates += [os.path.splitext(fn)[0] for fn in files]
|
candidates += [os.path.splitext(fn)[0] for fn in files]
|
||||||
for file_name in files:
|
for file_name in files:
|
||||||
with open(os.path.join(root, file_name), "r", encoding=encoding) as f:
|
with open_func(os.path.join(root, file_name), "r", encoding=encoding) as f:
|
||||||
lines = filter(
|
lines = filter(
|
||||||
filter_line, map(lambda l: l.partition("#")[0].strip(), f))
|
filter_line, map(lambda l: l.partition("#")[0].strip(), f))
|
||||||
for line in lines:
|
for line in lines:
|
||||||
@ -104,14 +110,14 @@ def get_imports_info(imports, pypi_server="https://pypi.python.org/pypi/", proxy
|
|||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
def get_locally_installed_packages():
|
def get_locally_installed_packages(encoding=None):
|
||||||
packages = {}
|
packages = {}
|
||||||
ignore = ["tests", "_tests", "egg", "EGG", "info"]
|
ignore = ["tests", "_tests", "egg", "EGG", "info"]
|
||||||
for path in sys.path:
|
for path in sys.path:
|
||||||
for root, dirs, files in os.walk(path):
|
for root, dirs, files in os.walk(path):
|
||||||
for item in files:
|
for item in files:
|
||||||
if "top_level" in item:
|
if "top_level" in item:
|
||||||
with open(os.path.join(root, item), "r") as f:
|
with open_func(os.path.join(root, item), "r", encoding=encoding) as f:
|
||||||
package = root.split(os.sep)[-1].split("-")
|
package = root.split(os.sep)[-1].split("-")
|
||||||
try:
|
try:
|
||||||
package_import = f.read().strip().split("\n")
|
package_import = f.read().strip().split("\n")
|
||||||
@ -127,7 +133,7 @@ def get_locally_installed_packages():
|
|||||||
return packages
|
return packages
|
||||||
|
|
||||||
|
|
||||||
def get_import_local(imports):
|
def get_import_local(imports, encoding=None):
|
||||||
local = get_locally_installed_packages()
|
local = get_locally_installed_packages()
|
||||||
result = []
|
result = []
|
||||||
for item in imports:
|
for item in imports:
|
||||||
@ -178,10 +184,10 @@ def init(args):
|
|||||||
if args["--use-local"]:
|
if args["--use-local"]:
|
||||||
logging.debug(
|
logging.debug(
|
||||||
"Getting package information ONLY from local installation.")
|
"Getting package information ONLY from local installation.")
|
||||||
imports = get_import_local(candidates)
|
imports = get_import_local(candidates, encoding=encoding)
|
||||||
else:
|
else:
|
||||||
logging.debug("Getting packages information from Local/PyPI")
|
logging.debug("Getting packages information from Local/PyPI")
|
||||||
local = get_import_local(candidates)
|
local = get_import_local(candidates, encoding=encoding)
|
||||||
# Get packages that were not found locally
|
# Get packages that were not found locally
|
||||||
difference = [x for x in candidates
|
difference = [x for x in candidates
|
||||||
if x.lower() not in [z['name'].lower() for z in local]]
|
if x.lower() not in [z['name'].lower() for z in local]]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user