Fix Issue 227

Fixed SSL issues encountered behind enterprise proxies.
This commit is contained in:
Devashish Gaikwad 2021-07-09 09:59:01 +05:30
parent c8ab8cb3d8
commit 9b1f441b14
3 changed files with 18 additions and 3 deletions

1
.gitignore vendored
View File

@ -53,3 +53,4 @@ Session.vim
*~ *~
/pipreqs/*.bak /pipreqs/*.bak
.vscode/

View File

@ -45,6 +45,8 @@ Usage
environments parameter in your terminal: environments parameter in your terminal:
$ export HTTP_PROXY="http://10.10.1.10:3128" $ export HTTP_PROXY="http://10.10.1.10:3128"
$ export HTTPS_PROXY="https://10.10.1.10:1080" $ export HTTPS_PROXY="https://10.10.1.10:1080"
--trusted-host Ignore SSL warnings, recommended using with
enterprise proxy.
--debug Print debug information --debug Print debug information
--ignore <dirs>... Ignore extra directories, each separated by a comma --ignore <dirs>... Ignore extra directories, each separated by a comma
--no-follow-links Do not follow symbolic links in the project --no-follow-links Do not follow symbolic links in the project

View File

@ -18,6 +18,8 @@ Options:
parameter in your terminal: parameter in your terminal:
$ export HTTP_PROXY="http://10.10.1.10:3128" $ export HTTP_PROXY="http://10.10.1.10:3128"
$ export HTTPS_PROXY="https://10.10.1.10:1080" $ export HTTPS_PROXY="https://10.10.1.10:1080"
--trusted-host Ignore SSL warnings, recommended using with
enterprise proxy.
--debug Print debug information --debug Print debug information
--ignore <dirs>... Ignore extra directories, each separated by a comma --ignore <dirs>... Ignore extra directories, each separated by a comma
--no-follow-links Do not follow symbolic links in the project --no-follow-links Do not follow symbolic links in the project
@ -47,6 +49,8 @@ from docopt import docopt
import requests import requests
from yarg import json2package from yarg import json2package
from yarg.exceptions import HTTPError from yarg.exceptions import HTTPError
from urllib3 import disable_warnings
from urllib3.exceptions import InsecureRequestWarning
from pipreqs import __version__ from pipreqs import __version__
@ -171,13 +175,15 @@ def output_requirements(imports, symbol):
def get_imports_info( def get_imports_info(
imports, pypi_server="https://pypi.python.org/pypi/", proxy=None): imports, pypi_server="https://pypi.python.org/pypi/", proxy=None,
verify_ssl=True):
result = [] result = []
for item in imports: for item in imports:
try: try:
response = requests.get( response = requests.get(
"{0}{1}/json".format(pypi_server, item), proxies=proxy) "{0}{1}/json".format(pypi_server, item), proxies=proxy,
verify=verify_ssl)
if response.status_code == 200: if response.status_code == 200:
if hasattr(response.content, 'decode'): if hasattr(response.content, 'decode'):
data = json2package(response.content.decode()) data = json2package(response.content.decode())
@ -405,6 +411,7 @@ def init(args):
encoding = args.get('--encoding') encoding = args.get('--encoding')
extra_ignore_dirs = args.get('--ignore') extra_ignore_dirs = args.get('--ignore')
follow_links = not args.get('--no-follow-links') follow_links = not args.get('--no-follow-links')
verify_ssl = not args.get('--trusted-host')
input_path = args['<path>'] input_path = args['<path>']
if input_path is None: if input_path is None:
input_path = os.path.abspath(os.curdir) input_path = os.path.abspath(os.curdir)
@ -412,6 +419,9 @@ def init(args):
if extra_ignore_dirs: if extra_ignore_dirs:
extra_ignore_dirs = extra_ignore_dirs.split(',') extra_ignore_dirs = extra_ignore_dirs.split(',')
if not verify_ssl:
disable_warnings(InsecureRequestWarning)
candidates = get_all_imports(input_path, candidates = get_all_imports(input_path,
encoding=encoding, encoding=encoding,
extra_ignore_dirs=extra_ignore_dirs, extra_ignore_dirs=extra_ignore_dirs,
@ -438,7 +448,9 @@ def init(args):
if x.lower() not in [z['name'].lower() for z in local]] if x.lower() not in [z['name'].lower() for z in local]]
imports = local + get_imports_info(difference, imports = local + get_imports_info(difference,
proxy=proxy, proxy=proxy,
pypi_server=pypi_server) pypi_server=pypi_server,
verify_ssl=verify_ssl)
# sort imports based on lowercase name of package, similar to `pip freeze`. # sort imports based on lowercase name of package, similar to `pip freeze`.
imports = sorted(imports, key=lambda x: x['name'].lower()) imports = sorted(imports, key=lambda x: x['name'].lower())