11import glob
2+ import operator
23import os
4+ import pkg_resources
5+ import platform
6+ import re
37import shutil
48import stat
5- import sys
69from pathlib import Path
710
8- import tomli
911from invoke import task
10- from packaging .requirements import Requirement
11- from packaging .version import Version
1212
13- from mlblocks .discovery import add_primitives_path , add_pipelines_path
1413
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+ }
2220
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
2821
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' )
4125
4226
4327@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' )
4730
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 )
5131
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+ )
5443
55-
56- @task
57- def check_dependencies (c ):
58- c .run ('python -m pip check' )
44+ return is_valid
5945
6046
6147@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 )} ' )
6475
6576
6677@task
@@ -72,17 +83,14 @@ def minimum(c):
7283
7384@task
7485def readme (c ):
75- pipeline_path = 'sigllm/pipelines/detector/gpt_detector.json'
7686 test_path = Path ('tests/readme_test' )
7787 if test_path .exists () and test_path .is_dir ():
7888 shutil .rmtree (test_path )
7989
8090 cwd = os .getcwd ()
8191 os .makedirs (test_path , exist_ok = True )
82- os .makedirs (test_path / 'mlpipelines' , exist_ok = True )
8392 shutil .copy ('README.md' , test_path / 'README.md' )
8493 shutil .copy ('tutorials/data.csv' , test_path / 'data.csv' )
85- shutil .copy (pipeline_path , test_path / 'mlpipelines' / 'gpt_detector.json' )
8694 os .chdir (test_path )
8795 c .run ('rundoc run --single-session python3 -t python3 README.md' )
8896 os .chdir (cwd )
@@ -102,15 +110,8 @@ def tutorials(c):
102110@task
103111def lint (c ):
104112 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' )
114115
115116
116117def remove_readonly (func , path , _ ):
0 commit comments