Skip to content

Commit 0954e6a

Browse files
committed
use tomli
1 parent 091d028 commit 0954e6a

File tree

4 files changed

+42
-44
lines changed

4 files changed

+42
-44
lines changed

.github/workflows/readme.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@ jobs:
2222
run: |
2323
python -m pip install --upgrade pip
2424
python -m pip install invoke rundoc .
25+
python -m pip install tomli
2526
- name: Run the README.md
2627
run: invoke readme

CONTRIBUTING.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,9 +219,9 @@ This will perform the following actions:
219219
2. Bump the current version to the next release candidate, ``X.Y.Z.dev(N+1)``
220220

221221
After this is done, the new pre-release can be installed by including the ``dev`` section in the
222-
dependency specification, either in ``setup.py``::
222+
dependency specification, either in ``pyproject.toml``::
223223

224-
install_requires = [
224+
dependencies = [
225225
...
226226
'deepecho>=X.Y.Z.dev',
227227
...

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ test = [
4848
'pytest-rerunfailures>=9.0.0,<10',
4949
'jupyter>=1.0.0,<2',
5050
'rundoc>=0.4.3,<0.5',
51+
'tomli>=2.0.0,<3',
5152
]
5253
dev = [
5354
'deepecho[test]',

tasks.py

Lines changed: 38 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
import inspect
33
import operator
44
import os
5-
import re
6-
import pkg_resources
7-
import platform
85
import shutil
96
import stat
7+
import sys
108
from pathlib import Path
119

10+
import tomli
1211
from invoke import task
13-
12+
from packaging.requirements import Requirement
13+
from packaging.version import Version
1414

1515
COMPARISONS = {
1616
'>=': operator.ge,
@@ -39,49 +39,45 @@ def unit(c):
3939
c.run('python -m pytest ./tests/unit --reruns 3')
4040

4141

42-
def _validate_python_version(line):
43-
is_valid = True
44-
for python_version_match in re.finditer(r"python_version(<=?|>=?|==)\'(\d\.?)+\'", line):
45-
python_version = python_version_match.group(0)
46-
comparison = re.search(r'(>=?|<=?|==)', python_version).group(0)
47-
version_number = python_version.split(comparison)[-1].replace("'", "")
48-
comparison_function = COMPARISONS[comparison]
49-
is_valid = is_valid and comparison_function(
50-
pkg_resources.parse_version(platform.python_version()),
51-
pkg_resources.parse_version(version_number),
52-
)
42+
def _get_minimum_versions(dependencies, python_version):
43+
min_versions = {}
44+
for dependency in dependencies:
45+
if '@' in dependency:
46+
name, url = dependency.split(' @ ')
47+
min_versions[name] = f'{name} @ {url}'
48+
continue
49+
50+
req = Requirement(dependency)
51+
if ';' in dependency:
52+
marker = req.marker
53+
if marker and not marker.evaluate({'python_version': python_version}):
54+
continue # Skip this dependency if the marker does not apply to the current Python version
55+
56+
if req.name not in min_versions:
57+
min_version = next((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}'
5360

54-
return is_valid
61+
elif '@' not in min_versions[req.name]:
62+
existing_version = Version(min_versions[req.name].split('==')[1])
63+
new_version = next((spec.version for spec in req.specifier if spec.operator in ('>=', '==')), existing_version)
64+
if new_version > existing_version:
65+
min_versions[req.name] = f'{req.name}=={new_version}' # Change when a valid newer version is found
66+
67+
return list(min_versions.values())
5568

5669

5770
@task
5871
def install_minimum(c):
59-
with open('pyproject.toml', 'r') as pyproject:
60-
lines = pyproject.read().splitlines()
61-
62-
versions = []
63-
started = False
64-
for line in lines:
65-
if started:
66-
if line == ']':
67-
started = False
68-
continue
69-
70-
line = line.strip()
71-
if _validate_python_version(line):
72-
requirement = re.match(r'[^>]*', line).group(0)
73-
requirement = re.sub(r"""['",]""", '', requirement)
74-
version = re.search(r'>=?(\d\.?)+\w*', line).group(0)
75-
if version:
76-
version = re.sub(r'>=?', '==', version)
77-
version = re.sub(r"""['",]""", '', version)
78-
requirement += version
79-
versions.append(requirement)
80-
81-
elif (line.startswith('dependencies = [')):
82-
started = True
83-
84-
c.run(f'python -m pip install {" ".join(versions)}')
72+
with open('pyproject.toml', 'rb') as pyproject_file:
73+
pyproject_data = tomli.load(pyproject_file)
74+
75+
dependencies = pyproject_data.get('project', {}).get('dependencies', [])
76+
python_version = '.'.join(map(str, sys.version_info[:2]))
77+
minimum_versions = _get_minimum_versions(dependencies, python_version)
78+
79+
if minimum_versions:
80+
c.run(f'python -m pip install {" ".join(minimum_versions)}')
8581

8682

8783
@task

0 commit comments

Comments
 (0)