summaryrefslogtreecommitdiffstats
path: root/.github/scripts/graph_compare.py
blob: 5a7c95e23812ec255deb5723de489981dbf11586 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#! /usr/bin/python3 -u

# Compares build graphs for two given refs in the current directory git repo
# Creates ya.make in the current directory listing affected ydb targets
# Parameters: base_commit_sha head_commit_sha

import os
import tempfile
import argparse


def exec(command: str):
    print(f'++ {command}')
    rc = os.system(command)
    if rc != 0:
        print(f'failed, return code {rc}')
        exit(1)


def log(msg: str):
    print(msg)


def main(ya_make_command: str, graph_path: str, context_path: str, base_commit: str, head_commit: str) -> None:
    ya = ya_make_command.split(' ')[0]

    workdir = os.getenv('workdir')
    if not workdir:
        workdir = tempfile.mkdtemp()

    log(f'Workdir: {workdir}')
    log('Checkout base commit...')
    exec(f'git checkout {base_commit}')
    log('Build graph for base commit...')
    exec(f'{ya_make_command} ydb --cache-tests --save-graph-to {workdir}/graph_base.json --save-context-to {workdir}/context_base.json')

    log('Checkout head commit...')
    exec(f'git checkout {head_commit}')
    log('Build graph for head commit...')
    exec(f'{ya_make_command} ydb --cache-tests --save-graph-to {workdir}/graph_head.json --save-context-to {workdir}/context_head.json')

    log('Generate diff graph...')
    exec(f'{ya} tool ygdiff --old {workdir}/graph_base.json --new {workdir}/graph_head.json --cut {graph_path} --dump-uids {workdir}/uids.json')

    log('Generate diff context...')
    exec(f'{ya} tool context_difference {workdir}/context_base.json {workdir}/context_head.json {context_path} {workdir}/uids.json')


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument(
        '--result-graph-path', '-g', type=str, dest='result_graph_path', required=True,
        help='Path to result graph'
    )
    parser.add_argument(
        '--result-context-path', '-c', type=str, dest='result_context_path', required=True,
        help='Path to result context'
    )
    parser.add_argument(
        '--ya-make-command', '-y', type=str, dest='ya_make_command', required=True,
        help='Ya make command'
    )
    parser.add_argument(dest='base_commit', help='Base commit')
    parser.add_argument(dest='head_commit', help='Head commit')
    opts = parser.parse_args()
    main(
        ya_make_command=opts.ya_make_command,
        graph_path=opts.result_graph_path,
        context_path=opts.result_context_path,
        base_commit=opts.base_commit,
        head_commit=opts.head_commit
    )