replace_branch.py 2.43 KB
Newer Older
1
2
3
4
5
6
7
import argparse
import json
import os
import re


def json_minify(string, strip_space=True):
8
    """
9
10
11
12
    Based on JSON.minify.js:
    https://github.com/getify/JSON.minify
    Contributers:
    - Pradyun S. Gedam (conditions and variable names changed)
13
    """
14
15
16
17
18
19
20
21
22
23
    tokenizer = re.compile(r'"|(/\*)|(\*/)|(//)|\n|\r')
    in_string = False
    in_multi = False
    in_single = False

    new_str = []
    index = 0

    for match in re.finditer(tokenizer, string):
        if not (in_multi or in_single):
24
            tmp = string[index : match.start()]
25
26
            if not in_string and strip_space:
                # replace white space as defined in standard
27
                tmp = re.sub("[ \t\n\r]+", "", tmp)
28
29
30
31
32
33
            new_str.append(tmp)

        index = match.end()
        val = match.group()

        if val == '"' and not (in_multi or in_single):
34
            escaped = re.search(r"(\\)*$", string[: match.start()])
35
36

            # start of string or unescaped quote character to end string
37
38
39
            if not in_string or (
                escaped is None or len(escaped.group()) % 2 == 0
            ):
40
41
42
                in_string = not in_string
            index -= 1  # include " character in next catch
        elif not (in_string or in_multi or in_single):
43
            if val == "/*":
44
                in_multi = True
45
            elif val == "//":
46
                in_single = True
47
        elif val == "*/" and in_multi and not (in_string or in_single):
48
            in_multi = False
49
        elif val in "\r\n" and not (in_multi or in_string) and in_single:
50
            in_single = False
51
52
53
        elif not (
            (in_multi or in_single) or (val in " \r\n\t" and strip_space)
        ):
54
55
56
            new_str.append(val)

    new_str.append(string[index:])
57
    content = "".join(new_str)
58
59
60
61
62
63
    content = content.replace(",]", "]")
    content = content.replace(",}", "}")
    return content


def add_prefix(branch_name):
64
65
    if "/" not in branch_name:
        return "origin/" + branch_name
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
    else:
        return branch_name


def change_branch(branch_str: str):
    branches = [add_prefix(b) for b in branch_str.split(",")]
    with open("../asv.conf.json", "r") as f:
        ss = f.read()
        config_json = json.loads(json_minify(ss))
        config_json["branches"] = branches
    with open("../asv.conf.json", "w") as f:
        json.dump(config_json, f)


if __name__ == "__main__":
    if "BRANCH_STR" in os.environ:
        change_branch(os.environ["BRANCH_STR"])