summaryrefslogtreecommitdiffstats
path: root/tools/tryselect/selectors/again.py
blob: 434aed7cc16e29de6c2ebf0a931bcbceea66a4a5 (plain)
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.


import json
import os

from ..cli import BaseTryParser
from ..push import history_path, push_to_try


class AgainParser(BaseTryParser):
    name = "again"
    arguments = [
        [
            ["--index"],
            {
                "default": 0,
                "const": "list",
                "nargs": "?",
                "help": "Index of entry in the history to re-push, "
                "where '0' is the most recent (default 0). "
                "Use --index without a value to display indices.",
            },
        ],
        [
            ["--list"],
            {
                "default": False,
                "action": "store_true",
                "dest": "list_configs",
                "help": "Display history and exit",
            },
        ],
        [
            ["--list-tasks"],
            {
                "default": 0,
                "action": "count",
                "dest": "list_tasks",
                "help": "Like --list, but display selected tasks  "
                "for each history entry, up to 10. Repeat "
                "to display all selected tasks.",
            },
        ],
        [
            ["--purge"],
            {
                "default": False,
                "action": "store_true",
                "help": "Remove all history and exit",
            },
        ],
    ]
    common_groups = ["push"]


def run(
    index=0, purge=False, list_configs=False, list_tasks=0, message="{msg}", **pushargs
):
    if index == "list":
        list_configs = True
    else:
        try:
            index = int(index)
        except ValueError:
            print("error: '--index' must be an integer")
            return 1

    if purge:
        os.remove(history_path)
        return

    if not os.path.isfile(history_path):
        print("error: history file not found: {}".format(history_path))
        return 1

    with open(history_path) as fh:
        history = fh.readlines()

    if list_configs or list_tasks > 0:
        for i, data in enumerate(history):
            msg, config = json.loads(data)
            version = config.get("version", "1")
            settings = {}
            if version == 1:
                tasks = config["tasks"]
                settings = config
            elif version == 2:
                try_config = config.get("parameters", {}).get("try_task_config", {})
                tasks = try_config.get("tasks")
            else:
                tasks = None

            if tasks is not None:
                # Select only the things that are of interest to display.
                settings = settings.copy()
                env = settings.pop("env", {}).copy()
                env.pop("TRY_SELECTOR", None)
                for name in ("tasks", "version"):
                    settings.pop(name, None)

                def pluralize(n, noun):
                    return "{n} {noun}{s}".format(
                        n=n, noun=noun, s="" if n == 1 else "s"
                    )

                out = str(i) + ". (" + pluralize(len(tasks), "task")
                if env:
                    out += ", " + pluralize(len(env), "env var")
                if settings:
                    out += ", " + pluralize(len(settings), "setting")
                out += ") " + msg
                print(out)

                if list_tasks > 0:
                    indent = " " * 4
                    if list_tasks > 1:
                        shown_tasks = tasks
                    else:
                        shown_tasks = tasks[:10]
                    print(indent + ("\n" + indent).join(shown_tasks))

                    num_hidden_tasks = len(tasks) - len(shown_tasks)
                    if num_hidden_tasks > 0:
                        print("{}... and {} more".format(indent, num_hidden_tasks))

                if list_tasks and env:
                    for line in ("env: " + json.dumps(env, indent=2)).splitlines():
                        print("    " + line)

                if list_tasks and settings:
                    for line in (
                        "settings: " + json.dumps(settings, indent=2)
                    ).splitlines():
                        print("    " + line)
            else:
                print(
                    "{index}. {msg}".format(
                        index=i,
                        msg=msg,
                    )
                )

        return

    msg, try_task_config = json.loads(history[index])
    return push_to_try(
        "again", message.format(msg=msg), try_task_config=try_task_config, **pushargs
    )