Example #1
0
def run_interactive(query, editor=None, just_count=False, default_no=False):
    """
    Asks the user about each patch suggested by the result of the query.

    @param query        An instance of the Query class.
    @param editor       Name of editor to use for manual intervention, e.g.
                        'vim'
                        or 'emacs'.  If omitted/None, defaults to $EDITOR
                        environment variable.
    @param just_count   If true: don't run normally.  Just print out number of
                        places in the codebase where the query matches.
    """
    global yes_to_all

    # Load start from bookmark, if appropriate.
    bookmark = _load_bookmark()
    if bookmark:
        print('Resume where you left off, at %s (y/n)? '
              % str(bookmark), end=' ')
        sys.stdout.flush()
        if (_prompt(default='y') == 'y'):
            query.start_position = bookmark

    # Okay, enough of this foolishness of computing start and end.
    # Let's ask the user about some one line diffs!
    print('Searching for first instance...')
    suggestions = query.generate_patches()

    if just_count:
        for count, _ in enumerate(suggestions):
            terminal.terminal_move_to_beginning_of_line()
            print(count, end=" ")
            sys.stdout.flush()  # since print statement ends in comma
        print()
        return

    for patch in suggestions:
        _save_bookmark(patch.start_position)
        _ask_about_patch(patch, editor, default_no)
        print('Searching...')
    _delete_bookmark()
    if yes_to_all:
        terminal.terminal_clear()
        print(
            "You MUST indicate in your code review:"
            " \"codemod with 'Yes to all'\"."
            "Make sure you and other people review the changes.\n\n"
            "With great power, comes great responsibility."
        )
Example #2
0
def run_interactive(query, editor=None, just_count=False, default_no=False):
    """
    Asks the user about each patch suggested by the result of the query.

    @param query        An instance of the Query class.
    @param editor       Name of editor to use for manual intervention, e.g.
                        'vim'
                        or 'emacs'.  If omitted/None, defaults to $EDITOR
                        environment variable.
    @param just_count   If true: don't run normally.  Just print out number of
                        places in the codebase where the query matches.
    """
    global yes_to_all

    # Load start from bookmark, if appropriate.
    bookmark = _load_bookmark()
    if bookmark:
        print('Resume where you left off, at %s (y/n)? ' % str(bookmark),
              end=' ')
        sys.stdout.flush()
        if (_prompt(default='y') == 'y'):
            query.start_position = bookmark

    # Okay, enough of this foolishness of computing start and end.
    # Let's ask the user about some one line diffs!
    print('Searching for first instance...')
    suggestions = query.generate_patches()

    if just_count:
        for count, _ in enumerate(suggestions):
            terminal.terminal_move_to_beginning_of_line()
            print(count, end=" ")
            sys.stdout.flush()  # since print statement ends in comma
        print()
        return

    for patch in suggestions:
        _save_bookmark(patch.start_position)
        _ask_about_patch(patch, editor, default_no)
        print('Searching...')
    _delete_bookmark()
    if yes_to_all:
        terminal.terminal_clear()
        print("You MUST indicate in your code review:"
              " \"codemod with 'Yes to all'\"."
              "Make sure you and other people review the changes.\n\n"
              "With great power, comes great responsibility.")
Example #3
0
def _ask_about_patch(patch, editor, default_no):
    global yes_to_all

    default_action = 'n' if default_no else 'y'
    terminal.terminal_clear()
    terminal.terminal_print('%s\n' % patch.render_range(), color='WHITE')
    print()

    lines = list(open(patch.path))
    size = list(terminal.terminal_get_size())
    print_patch(patch, size[0] - 20, lines)

    print()

    if patch.new_lines is not None:
        if not yes_to_all:
            if default_no:
                print('Accept change (y = yes, n = no [default], e = edit, ' +
                      'A = yes to all, E = yes+edit, q = quit)? '),
            else:
                print('Accept change (y = yes [default], n = no, e = edit, ' +
                      'A = yes to all, E = yes+edit, q = quit)? '),
            p = _prompt('yneEAq', default=default_action)
        else:
            p = 'y'
    else:
        print('(e = edit [default], n = skip line, q = quit)? ', end=" ")
        p = _prompt('enq', default='e')

    if p in 'A':
        yes_to_all = True
        p = 'y'
    if p in 'yE':
        patch.apply_to(lines)
        _save(patch.path, lines)
    if p in 'eE':
        run_editor(patch.start_position, editor)
    if p in 'q':
        sys.exit(0)
Example #4
0
def _ask_about_patch(patch, editor, default_no):
    global yes_to_all

    default_action = 'n' if default_no else 'y'
    terminal.terminal_clear()
    terminal.terminal_print('%s\n' % patch.render_range(), color='WHITE')
    print()

    lines = list(open(patch.path))
    size = list(terminal.terminal_get_size())
    print_patch(patch, size[0] - 20, lines)

    print()

    if patch.new_lines is not None:
        if not yes_to_all:
            if default_no:
                print('Accept change (y = yes, n = no [default], e = edit, ' +
                      'A = yes to all, E = yes+edit, q = quit)? '),
            else:
                print('Accept change (y = yes [default], n = no, e = edit, ' +
                      'A = yes to all, E = yes+edit, q = quit)? '),
            p = _prompt('yneEAq', default=default_action)
        else:
            p = 'y'
    else:
        print('(e = edit [default], n = skip line, q = quit)? ', end=" ")
        p = _prompt('enq', default='e')

    if p in 'A':
        yes_to_all = True
        p = 'y'
    if p in 'yE':
        patch.apply_to(lines)
        _save(patch.path, lines)
    if p in 'eE':
        run_editor(patch.start_position, editor)
    if p in 'q':
        sys.exit(0)