1
1
import glob
2
+ import operator
2
3
import os
4
+ import pkg_resources
5
+ import platform
6
+ import re
3
7
import shutil
4
8
import stat
5
- import sys
6
9
from pathlib import Path
7
10
8
- import tomli
9
11
from invoke import task
10
- from packaging .requirements import Requirement
11
- from packaging .version import Version
12
12
13
- from mlblocks .discovery import add_primitives_path , add_pipelines_path
14
13
15
- def _get_minimum_versions (dependencies , python_version ):
16
- min_versions = {}
17
- for dependency in dependencies :
18
- if '@' in dependency :
19
- name , url = dependency .split (' @ ' )
20
- min_versions [name ] = f'{ url } #egg={ name } '
21
- continue
14
+ COMPARISONS = {
15
+ '>=' : operator .ge ,
16
+ '>' : operator .gt ,
17
+ '<' : operator .lt ,
18
+ '<=' : operator .le
19
+ }
22
20
23
- req = Requirement (dependency )
24
- if ';' in dependency :
25
- marker = req .marker
26
- if marker and not marker .evaluate ({'python_version' : python_version }):
27
- continue # python version does not match
28
21
29
- if req .name not in min_versions :
30
- min_version = next ((spec .version for spec in req .specifier if spec .operator in ('>=' , '==' )), None )
31
- if min_version :
32
- min_versions [req .name ] = f'{ req .name } =={ min_version } '
33
-
34
- elif '@' not in min_versions [req .name ]:
35
- existing_version = Version (min_versions [req .name ].split ('==' )[1 ])
36
- new_version = next ((spec .version for spec in req .specifier if spec .operator in ('>=' , '==' )), existing_version )
37
- if new_version > existing_version :
38
- min_versions [req .name ] = f'{ req .name } =={ new_version } '
39
-
40
- return list (min_versions .values ())
22
+ @task
23
+ def check_dependencies (c ):
24
+ c .run ('python -m pip check' )
41
25
42
26
43
27
@task
44
- def install_minimum (c ):
45
- with open ('pyproject.toml' , 'rb' ) as pyproject_file :
46
- pyproject_data = tomli .load (pyproject_file )
28
+ def unit (c ):
29
+ c .run ('python -m pytest --cov=sigllm --cov-report=xml' )
47
30
48
- dependencies = pyproject_data .get ('project' , {}).get ('dependencies' , [])
49
- python_version = '.' .join (map (str , sys .version_info [:2 ]))
50
- minimum_versions = _get_minimum_versions (dependencies , python_version )
51
31
52
- if minimum_versions :
53
- c .run (f'python -m pip install { " " .join (minimum_versions )} ' )
32
+ def _validate_python_version (line ):
33
+ is_valid = True
34
+ for python_version_match in re .finditer (r"python_version(<=?|>=?|==)\'(\d\.?)+\'" , line ):
35
+ python_version = python_version_match .group (0 )
36
+ comparison = re .search (r'(>=?|<=?|==)' , python_version ).group (0 )
37
+ version_number = python_version .split (comparison )[- 1 ].replace ("'" , "" )
38
+ comparison_function = COMPARISONS [comparison ]
39
+ is_valid = is_valid and comparison_function (
40
+ pkg_resources .parse_version (platform .python_version ()),
41
+ pkg_resources .parse_version (version_number ),
42
+ )
54
43
55
-
56
- @task
57
- def check_dependencies (c ):
58
- c .run ('python -m pip check' )
44
+ return is_valid
59
45
60
46
61
47
@task
62
- def unit (c ):
63
- c .run ('python -m pytest --cov=sigllm --cov-report=xml' )
48
+ def install_minimum (c ):
49
+ with open ('setup.py' , 'r' ) as setup_py :
50
+ lines = setup_py .read ().splitlines ()
51
+
52
+ versions = []
53
+ started = False
54
+ for line in lines :
55
+ if started :
56
+ if line == ']' :
57
+ started = False
58
+ continue
59
+
60
+ line = line .strip ()
61
+ if _validate_python_version (line ):
62
+ requirement = re .match (r'[^>]*' , line ).group (0 )
63
+ requirement = re .sub (r"""['",]""" , '' , requirement )
64
+ version = re .search (r'>=?(\d\.?)+\w*' , line ).group (0 )
65
+ if version :
66
+ version = re .sub (r'>=?' , '==' , version )
67
+ version = re .sub (r"""['",]""" , '' , version )
68
+ requirement += version
69
+ versions .append (requirement )
70
+
71
+ elif (line .startswith ('install_requires = [' )):
72
+ started = True
73
+
74
+ c .run (f'python -m pip install { " " .join (versions )} ' )
64
75
65
76
66
77
@task
@@ -72,17 +83,14 @@ def minimum(c):
72
83
73
84
@task
74
85
def readme (c ):
75
- pipeline_path = 'sigllm/pipelines/detector/gpt_detector.json'
76
86
test_path = Path ('tests/readme_test' )
77
87
if test_path .exists () and test_path .is_dir ():
78
88
shutil .rmtree (test_path )
79
89
80
90
cwd = os .getcwd ()
81
91
os .makedirs (test_path , exist_ok = True )
82
- os .makedirs (test_path / 'mlpipelines' , exist_ok = True )
83
92
shutil .copy ('README.md' , test_path / 'README.md' )
84
93
shutil .copy ('tutorials/data.csv' , test_path / 'data.csv' )
85
- shutil .copy (pipeline_path , test_path / 'mlpipelines' / 'gpt_detector.json' )
86
94
os .chdir (test_path )
87
95
c .run ('rundoc run --single-session python3 -t python3 README.md' )
88
96
os .chdir (cwd )
@@ -102,15 +110,8 @@ def tutorials(c):
102
110
@task
103
111
def lint (c ):
104
112
check_dependencies (c )
105
- c .run ('ruff check .' )
106
- c .run ('ruff format --check --diff .' )
107
-
108
-
109
- @task
110
- def fix_lint (c ):
111
- check_dependencies (c )
112
- c .run ('ruff check --fix .' )
113
- c .run ('ruff format .' )
113
+ c .run ('flake8 sigllm tests' )
114
+ c .run ('isort -c --recursive sigllm tests' )
114
115
115
116
116
117
def remove_readonly (func , path , _ ):
0 commit comments