diff --git a/setup.py b/setup.py --- a/setup.py +++ b/setup.py @@ -22,20 +22,47 @@ from setuptools import setup, find_packages import os +import re import sys import pkgutil import platform import codecs -try: # for pip >= 10 +import pip + +pip_major_version = int(pip.__version__.split(".")[0]) +if pip_major_version >= 20: from pip._internal.req import parse_requirements -except ImportError: # for pip <= 9.0.3 + from pip._internal.network.session import PipSession +elif pip_major_version >= 10: + from pip._internal.req import parse_requirements + from pip._internal.download import PipSession +else: from pip.req import parse_requirements + from pip.download import PipSession + -try: # for pip >= 10 - from pip._internal.download import PipSession -except ImportError: # for pip <= 9.0.3 - from pip.download import PipSession +def get_package_name(req_object): + package_name = None + try: + from pip._internal.req.constructors import install_req_from_parsed_requirement + except ImportError: + install_req_from_parsed_requirement = None + + # In 20.1 of pip, the requirements object changed + if hasattr(req_object, 'req'): + package_name = req_object.req.name + + if package_name is None: + if install_req_from_parsed_requirement: + package = install_req_from_parsed_requirement(req_object) + package_name = package.req.name + + if package_name is None: + # fallback for older pip + package_name = re.split('===|<=|!=|==|>=|~=|<|>', req_object.requirement)[0] + + return package_name if sys.version_info < (2, 7): @@ -63,14 +90,15 @@ def _get_requirements(req_filename, excl parsed = parse_requirements(os.path.join(here, req_filename)) requirements = [] - for ir in parsed: - if ir.req and ir.name not in exclude: - requirements.append(str(ir.req)) + for int_req in parsed: + req_name = get_package_name(int_req) + if req_name not in exclude: + requirements.append(req_name) return requirements + extras # requirements extract -setup_requirements = ['PasteScript', 'pytest-runner'] +setup_requirements = ['PasteScript'] install_requirements = _get_requirements( 'requirements.txt', exclude=['setuptools', 'entrypoints']) test_requirements = _get_requirements(