2
2
import inspect
3
3
import operator
4
4
import os
5
- import re
6
- import pkg_resources
7
- import platform
5
+ import toml
6
+ from packaging . requirements import Requirement
7
+ from packaging . version import Version
8
8
import shutil
9
9
import stat
10
10
from pathlib import Path
20
20
21
21
22
22
if not hasattr (inspect , 'getargspec' ):
23
- inspect .getargspec = inspect .getfullargspec
23
+ inspect .getargspec = inspect .getfullargspec
24
24
25
25
26
26
@task
@@ -38,51 +38,48 @@ def integration(c):
38
38
c .run ('python -m pytest ./tests/integration --reruns 5 --disable-warnings' )
39
39
40
40
41
- def _validate_python_version (line ):
42
- is_valid = True
43
- for python_version_match in re .finditer (r"python_version(<=?|>=?|==)\'(\d\.?)+\'" , line ):
44
- python_version = python_version_match .group (0 )
45
- comparison = re .search (r'(>=?|<=?|==)' , python_version ).group (0 )
46
- version_number = python_version .split (comparison )[- 1 ].replace ("'" , "" )
47
- comparison_function = COMPARISONS [comparison ]
48
- is_valid = is_valid and comparison_function (
49
- pkg_resources .parse_version (platform .python_version ()),
50
- pkg_resources .parse_version (version_number ),
51
- )
41
+ def _get_minimum_versions (dependencies , python_version ):
42
+ min_versions = {}
43
+ for dependency in dependencies :
44
+ if '@' in dependency :
45
+ name , url = dependency .split (' @ ' )
46
+ min_versions [name ] = f'{ name } @ { url } '
47
+ continue
52
48
53
- return is_valid
49
+ req = Requirement (dependency )
50
+ if ';' in dependency :
51
+ marker = req .marker
52
+ if marker and not marker .evaluate ({'python_version' : python_version }):
53
+ continue # Skip this dependency if the marker does not apply to the current Python version
54
+
55
+ if req .name not in min_versions :
56
+ min_version = next (
57
+ (spec .version for spec in req .specifier if spec .operator in ('>=' , '==' )), None )
58
+ if min_version :
59
+ min_versions [req .name ] = f'{ req .name } =={ min_version } '
60
+
61
+ elif '@' not in min_versions [req .name ]:
62
+ existing_version = Version (min_versions [req .name ].split ('==' )[1 ])
63
+ new_version = next (
64
+ (spec .version for spec in req .specifier if spec .operator in ('>=' , '==' )), existing_version )
65
+ if new_version > existing_version :
66
+ # Change when a valid newer version is found
67
+ min_versions [req .name ] = f'{ req .name } =={ new_version } '
68
+
69
+ return list (min_versions .values ())
54
70
55
71
56
72
@task
57
73
def install_minimum (c ):
58
- with open ('setup.py' , 'r' ) as setup_py :
59
- lines = setup_py .read ().splitlines ()
60
-
61
- versions = []
62
- started = False
63
- for line in lines :
64
- if started :
65
- if line == ']' :
66
- break
67
-
68
- line = line .strip ()
69
- if _validate_python_version (line ):
70
- requirement = re .match (r'[^>]*' , line ).group (0 )
71
- requirement = re .sub (r"""['",]""" , '' , requirement )
72
- version = re .search (r'>=?(\d\.?)+' , line ).group (0 )
73
- if version :
74
- version = re .sub (r'>=?' , '==' , version )
75
- version = re .sub (r"""['",]""" , '' , version )
76
- requirement += version
77
-
78
- versions .append (requirement )
79
-
80
- elif (line .startswith ('install_requires = [' ) or
81
- line .startswith ('pomegranate_requires = [' ) or
82
- line .startswith ('torch_requires = [' )):
83
- started = True
84
-
85
- c .run (f'python -m pip install { " " .join (versions )} ' )
74
+ with open ('pyproject.toml' , 'r' , encoding = 'utf-8' ) as pyproject_file :
75
+ pyproject_data = toml .load (pyproject_file )
76
+
77
+ dependencies = pyproject_data .get ('project' , {}).get ('dependencies' , [])
78
+ python_version = '.' .join (map (str , sys .version_info [:2 ]))
79
+ minimum_versions = _get_minimum_versions (dependencies , python_version )
80
+
81
+ if minimum_versions :
82
+ c .run (f'python -m pip install { " " .join (minimum_versions )} ' )
86
83
87
84
88
85
@task
0 commit comments