tools/tryselect/selectors/again.py
author Tudor-Gabriel Vîjială <tvijiala@mozilla.com>
Mon, 06 Aug 2018 10:44:29 +0100
changeset 431392 5312e71473c402f520db9e24f5c7e5eb6795cbcf
parent 426739 8c423f75eb7723a68179dfe3ba1602b5c4c637d9
child 456680 de78b968e1786d92e7c96f49de0921c04087b3f5
permissions -rw-r--r--
Bug 1480187 - Fix mach try again when using multiple source checkouts. r=ahal MozReview-Commit-ID: AV7ynmTSY1x

# 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/.

from __future__ import absolute_import, print_function, unicode_literals

import json
import os
import shutil

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


class AgainParser(BaseTryParser):
    name = 'again'
    arguments = [
        [['--index'],
         {'default': 0,
          'type': int,
          'help': "Index of entry in the history to re-push, "
                  "where '0' is the most recent (default 0). "
                  "Use --list to display indices.",
          }],
        [['--list'],
         {'default': False,
          'action': 'store_true',
          'dest': 'list_configs',
          'help': "Display history and exit",
          }],
        [['--purge'],
         {'default': False,
          'action': 'store_true',
          'help': "Remove all history and exit",
          }],
    ]
    common_groups = ['push']


def run_try_again(index=0, purge=False, list_configs=False, message='{msg}', **pushargs):
    # Try to move existing history file from the old location to the new one.
    if os.path.isfile(old_history_path) and not os.path.isfile(history_path):
        if not os.path.isdir(os.path.dirname(history_path)):
            os.makedirs(os.path.dirname(history_path))
        shutil.move(old_history_path, history_path)

    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, 'r') as fh:
        history = fh.readlines()

    if list_configs:
        for i, data in enumerate(history):
            msg, config = json.loads(data)
            print('{}. {}'.format(i, 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)