66
77import os
88import tempfile
9- import argparse
9+ import sys
10+ import json
1011
1112
1213def exec (command : str ):
@@ -21,8 +22,17 @@ def log(msg: str):
2122 print (msg )
2223
2324
24- def main (ya_make_command : str , graph_path : str , context_path : str , base_commit : str , head_commit : str ) -> None :
25- ya = ya_make_command .split (' ' )[0 ]
25+ def do_compare ():
26+ if len (sys .argv ) < 3 :
27+ print ('base or head commit not set' )
28+ exit (1 )
29+ base_commit = sys .argv [1 ]
30+ head_commit = sys .argv [2 ]
31+
32+ ya_make_command = os .getenv ('YA_MAKE_COMMAND' )
33+ if not ya_make_command :
34+ print ('YA_MAKE_COMMAND not set' )
35+ exit (1 )
2636
2737 workdir = os .getenv ('workdir' )
2838 if not workdir :
@@ -32,41 +42,57 @@ def main(ya_make_command: str, graph_path: str, context_path: str, base_commit:
3242 log ('Checkout base commit...' )
3343 exec (f'git checkout { base_commit } ' )
3444 log ('Build graph for base commit...' )
35- exec (f'{ ya_make_command } ydb -k -- cache-tests --save-graph-to { workdir } /graph_base.json --save-context-to { workdir } /context_base .json' )
45+ exec (f'{ ya_make_command } ydb -k -A -- cache-tests -Gj0 > { workdir } /graph_base .json' )
3646
3747 log ('Checkout head commit...' )
3848 exec (f'git checkout { head_commit } ' )
3949 log ('Build graph for head commit...' )
40- exec (f'{ ya_make_command } ydb -k -- cache-tests --save-graph-to { workdir } /graph_head.json --save-context-to { workdir } /context_head .json' )
50+ exec (f'{ ya_make_command } ydb -k -A -- cache-tests -Gj0 > { workdir } /graph_head .json' )
4151
4252 log ('Generate diff graph...' )
43- exec (f'{ ya } tool ygdiff --old { workdir } /graph_base.json --new { workdir } /graph_head.json --cut { graph_path } --dump-uids { workdir } /uids.json --no-cache-for-affected-nodes' )
53+ exec (f'./ya tool ygdiff --old { workdir } /graph_base.json --new { workdir } /graph_head.json --cut { workdir } /graph_diff.json --dump-uids-for-affected-nodes { workdir } /affected_uids.json' )
54+
55+ log ('Read diff graph...' )
56+ with open (f'{ workdir } /graph_diff.json' , 'r' ) as f :
57+ diff_graph = json .load (f )
58+
59+ with open (f'{ workdir } /affected_uids.json' , 'r' ) as f :
60+ uids = set (json .load (f ))
61+
62+ tests = set ()
63+ modules = set ()
64+
65+ log ('Scan diff graph...' )
66+ for target in diff_graph .get ('graph' , []):
67+ if target .get ('uid' ) not in uids :
68+ continue
69+ if target .get ('node-type' ) == 'test' :
70+ path = target .get ('kv' , {}).get ('path' )
71+ if path is not None :
72+ tests .add (os .path .dirname (path ))
73+ tp = target .get ('target_properties' )
74+ if (
75+ tp is not None
76+ and tp .get ('module_type' ) is not None
77+ and tp .get ('module_dir' , '' ).startswith ('ydb' )
78+ and tp .get ('module_tag' , '' ).find ('proto' ) < 0
79+ ):
80+ modules .add (tp .get ('module_dir' ))
81+
82+ log ('Create ya.make' )
4483
45- log ('Generate diff context...' )
46- exec (f'{ ya } tool context_difference { workdir } /context_base.json { workdir } /context_head.json { context_path } { workdir } /uids.json { graph_path } ' )
84+ with open ('ya.make' , 'w' ) as ya_make :
85+ ya_make .write ('RECURSE_FOR_TESTS(\n ' )
86+ for test in sorted (tests ):
87+ ya_make .write (f' { test } \n ' )
88+ ya_make .write (')\n \n RECURSE (\n ' )
89+ for module in sorted (modules ):
90+ ya_make .write (f' { module } \n ' )
91+ ya_make .write (')\n ' )
92+ log ('ya.make content:' )
93+ exec ('cat ya.make' )
94+ exit (0 )
4795
4896
4997if __name__ == '__main__' :
50- parser = argparse .ArgumentParser ()
51- parser .add_argument (
52- '--result-graph-path' , '-g' , type = str , dest = 'result_graph_path' , required = True ,
53- help = 'Path to result graph'
54- )
55- parser .add_argument (
56- '--result-context-path' , '-c' , type = str , dest = 'result_context_path' , required = True ,
57- help = 'Path to result context'
58- )
59- parser .add_argument (
60- '--ya-make-command' , '-y' , type = str , dest = 'ya_make_command' , required = True ,
61- help = 'Ya make command'
62- )
63- parser .add_argument (dest = 'base_commit' , help = 'Base commit' )
64- parser .add_argument (dest = 'head_commit' , help = 'Head commit' )
65- opts = parser .parse_args ()
66- main (
67- ya_make_command = opts .ya_make_command ,
68- graph_path = opts .result_graph_path ,
69- context_path = opts .result_context_path ,
70- base_commit = opts .base_commit ,
71- head_commit = opts .head_commit
72- )
98+ do_compare ()
0 commit comments