Skip to content

Commit

Permalink
Improve python_version parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
etingof committed Aug 7, 2018
1 parent 0f3f80c commit 2231ac4
Showing 1 changed file with 31 additions and 32 deletions.
63 changes: 31 additions & 32 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
# handle unittest discovery feature
try:
import unittest2 as unittest

except ImportError:
import unittest

Expand Down Expand Up @@ -69,6 +70,32 @@ def howto_install_setuptools():
print("ERROR: this package requires Python 2.4 or later!")
sys.exit(1)

requires = [ln.strip() for ln in open('requirements.txt').readlines()]

resolved_requires = []

# NOTE(etingof): older setuptools fail at parsing python_version
for requirement in requires:
match = re.match(
r'(.*?)\s*;\s*python_version\s*([<>=!~]+)\s*\'(.*?)\'', requirement)

if not match:
resolved_requires.append(requirement)
continue

package, condition, expected_py = match.groups()

expected_py = tuple([int(x) for x in expected_py.split('.')])

if py_version == expected_py and condition in ('<=', '==', '>='):
resolved_requires.append(package)

elif py_version < expected_py and condition in ('<=', '<'):
resolved_requires.append(package)

elif py_version > expected_py and condition in ('>=', '>'):
resolved_requires.append(package)

try:
import setuptools

Expand All @@ -87,37 +114,9 @@ def howto_install_setuptools():
'.'.join([str(x) for x in required_version])))
sys.exit(1)

requires = [ln.strip() for ln in open('requirements.txt').readlines()]

# NOTE(etingof): older setuptools fail at parsing python_version

if observed_version < required_version:
resolved_requires = []

for requirement in requires:
match = re.match(
r'(.*?)\s*;\s*python_version\s*([<>=!~]+)\s*\'(.*?)\'', requirement)

if not match:
resolved_requires.append(requirement)
continue

package, condition, expected_py = match.groups()

expected_py = tuple([int(x) for x in expected_py.split('.')])

if py_version == expected_py and condition in ('<=', '==', '>='):
resolved_requires.append(package)

elif py_version < expected_py and condition in ('<=', '<'):
resolved_requires.append(package)

elif py_version > expected_py and condition in ('>=', '>'):
resolved_requires.append(package)

requires = resolved_requires


params = {
'install_requires': requires,
'zip_safe': True
Expand All @@ -130,13 +129,13 @@ def howto_install_setuptools():

from distutils.core import setup, Command

# assume really old Python
requires = ['pycryptodomex']

params = {}

if py_version > (2, 4):
params['requires'] = requires
params['requires'] = [
re.sub(r'(.*?)([<>=!~]+)(.*)', r'\g<1>\g<2>(\g<3>)', r)
for r in resolved_requires
]

doclines = [x.strip() for x in (__doc__ or '').split('\n')]

Expand Down

0 comments on commit 2231ac4

Please sign in to comment.