-
Notifications
You must be signed in to change notification settings - Fork 292
/
tasks.py
132 lines (100 loc) · 3.46 KB
/
tasks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import inspect
import operator
import os
import shutil
import stat
import sys
from pathlib import Path
import tomli
from invoke import task
from packaging.requirements import Requirement
from packaging.version import Version
COMPARISONS = {'>=': operator.ge, '>': operator.gt, '<': operator.lt, '<=': operator.le}
if not hasattr(inspect, 'getargspec'):
inspect.getargspec = inspect.getfullargspec
@task
def check_dependencies(c):
c.run('python -m pip check')
@task
def unit(c):
c.run('python -m pytest ./tests/unit --cov=ctgan --cov-report=xml')
@task
def integration(c):
c.run('python -m pytest ./tests/integration --reruns 3')
@task
def readme(c):
test_path = Path('tests/readme_test')
if test_path.exists() and test_path.is_dir():
shutil.rmtree(test_path)
cwd = os.getcwd()
os.makedirs(test_path, exist_ok=True)
shutil.copy('README.md', test_path / 'README.md')
os.chdir(test_path)
c.run('rundoc run --single-session python3 -t python3 README.md')
os.chdir(cwd)
shutil.rmtree(test_path)
def _get_minimum_versions(dependencies, python_version):
min_versions = {}
for dependency in dependencies:
if '@' in dependency:
name, url = dependency.split(' @ ')
min_versions[name] = f'{url}#egg={name}'
continue
req = Requirement(dependency)
if ';' in dependency:
marker = req.marker
if marker and not marker.evaluate({'python_version': python_version}):
continue # Skip this dependency if the marker does not apply to the current Python version
if req.name not in min_versions:
min_version = next(
(spec.version for spec in req.specifier if spec.operator in ('>=', '==')), None
)
if min_version:
min_versions[req.name] = f'{req.name}=={min_version}'
elif '@' not in min_versions[req.name]:
existing_version = Version(min_versions[req.name].split('==')[1])
new_version = next(
(spec.version for spec in req.specifier if spec.operator in ('>=', '==')),
existing_version,
)
if new_version > existing_version:
min_versions[req.name] = (
f'{req.name}=={new_version}' # Change when a valid newer version is found
)
return list(min_versions.values())
@task
def install_minimum(c):
with open('pyproject.toml', 'rb') as pyproject_file:
pyproject_data = tomli.load(pyproject_file)
dependencies = pyproject_data.get('project', {}).get('dependencies', [])
python_version = '.'.join(map(str, sys.version_info[:2]))
minimum_versions = _get_minimum_versions(dependencies, python_version)
if minimum_versions:
install_deps = ' '.join(minimum_versions)
c.run(f'python -m pip install {install_deps}')
@task
def minimum(c):
install_minimum(c)
check_dependencies(c)
unit(c)
integration(c)
@task
def lint(c):
check_dependencies(c)
c.run('ruff check .')
c.run('ruff format --check --diff .')
@task
def fix_lint(c):
check_dependencies(c)
c.run('ruff check --fix .')
c.run('ruff format .')
def remove_readonly(func, path, _):
"""Clear the readonly bit and reattempt the removal"""
os.chmod(path, stat.S_IWRITE)
func(path)
@task
def rmdir(c, path):
try:
shutil.rmtree(path, onerror=remove_readonly)
except PermissionError:
pass