1- import glob
21import itertools
32import os
3+ from pathlib import Path
44
55import yaml
66
99in the pytests directory to parallelise the CI jobs.
1010'''
1111
12+
1213template = """
1314pytest.{}:
1415 extends: .pytest
1920
2021n_test_files_per_yml = int (os .environ .get ('N_TESTS_PER_YAML' , 4 ))
2122
23+ BLACKLIST = {'test_reduction' }
24+
25+
26+ def path_to_name (test_path ):
27+ path = Path (test_path )
28+ name = path .stem .replace ('test_' , '' )
29+ return name
30+
2231
2332def batched (iterable , chunk_size ):
2433 iterator = iter (iterable )
@@ -32,41 +41,32 @@ def uses_example_model(test_filename):
3241 return 'example-models' in content
3342
3443
35- yml = None
36- tests = glob .glob ('test_*.py' )
37- for test_batch in batched (tests , n_test_files_per_yml ):
38- name = '+' .join ([test .replace ('test_' , '' ).replace ('.py' , '' ) for test in test_batch ])
39- test_files = ' ' .join (list (test_batch ))
40- uses_example_models = int (any ([uses_example_model (test ) for test in test_batch ]))
41-
42- new_yml = yaml .safe_load (template .format (name , test_files , uses_example_models ))
43- if yml is None :
44- yml = new_yml
45- else :
46- yml .update (new_yml )
47-
48- # hls4ml Optimization API
49- tests = glob .glob ('test_optimization/test_*.py' )
50- for test in tests :
51- name = test .replace ('test_optimization/' , '' ).replace ('test_' , '' ).replace ('.py' , '' )
52- new_yml = yaml .safe_load (template .format (name , f'test_optimization/test_{ name } .py' , int (uses_example_model (test ))))
53- if yml is None :
54- yml = new_yml
55- else :
56- yml .update (new_yml )
57-
58- tests = glob .glob ('test_optimization/test_keras/test_*.py' )
59- for test in tests :
60- # For now, skip Keras Surgeon [conflicting versions]
61- if 'test_reduction' not in test :
62- name = test .replace ('test_optimization/test_keras/' , '' ).replace ('test_' , '' ).replace ('.py' , '' )
63- new_yml = yaml .safe_load (
64- template .format (name , f'test_optimization/test_keras/test_{ name } .py' , int (uses_example_model (test )))
65- )
44+ def generate_test_yaml (test_root = '.' ):
45+ test_root = Path (test_root )
46+ test_paths = [path for path in test_root .glob ('**/test_*.py' ) if path .stem not in BLACKLIST ]
47+ for path in test_paths :
48+ print (path .name )
49+ need_example_models = [uses_example_model (path ) for path in test_paths ]
50+
51+ idxs = list (range (len (need_example_models )))
52+ idxs = sorted (idxs , key = lambda i : f'{ need_example_models [i ]} _{ path_to_name (test_paths [i ])} ' )
53+
54+ yml = None
55+ for batch_idxs in batched (idxs , n_test_files_per_yml ):
56+ batch_paths : list [Path ] = [test_paths [i ] for i in batch_idxs ]
57+ names = [path_to_name (path ) for path in batch_paths ]
58+ name = '+' .join (names )
59+ test_files = ' ' .join ([str (path .relative_to (test_root )) for path in batch_paths ])
60+ batch_need_example_model = int (any ([need_example_models [i ] for i in batch_idxs ]))
61+ diff_yml = yaml .safe_load (template .format (name , test_files , batch_need_example_model ))
6662 if yml is None :
67- yml = new_yml
63+ yml = diff_yml
6864 else :
69- yml .update (new_yml )
65+ yml .update (diff_yml )
66+ return yml
67+
7068
71- yamlfile = open ('pytests.yml' , 'w' )
72- yaml .safe_dump (yml , yamlfile )
69+ if __name__ == '__main__' :
70+ yml = generate_test_yaml (Path (__file__ ).parent )
71+ with open ('pytests.yml' , 'w' ) as yamlfile :
72+ yaml .safe_dump (yml , yamlfile )
0 commit comments