1
- import glob
2
1
import itertools
3
2
import os
3
+ from pathlib import Path
4
4
5
5
import yaml
6
6
9
9
in the pytests directory to parallelise the CI jobs.
10
10
'''
11
11
12
+
12
13
template = """
13
14
pytest.{}:
14
15
extends: .pytest
19
20
20
21
n_test_files_per_yml = int (os .environ .get ('N_TESTS_PER_YAML' , 4 ))
21
22
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
+
22
31
23
32
def batched (iterable , chunk_size ):
24
33
iterator = iter (iterable )
@@ -32,41 +41,32 @@ def uses_example_model(test_filename):
32
41
return 'example-models' in content
33
42
34
43
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 ))
66
62
if yml is None :
67
- yml = new_yml
63
+ yml = diff_yml
68
64
else :
69
- yml .update (new_yml )
65
+ yml .update (diff_yml )
66
+ return yml
67
+
70
68
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