<commit_before>#!/usr/bin/env python
#encoding:utf-8
#author:dbr/Ben
#project:tvnamer
#repository:http://github.com/dbr/tvnamer
#license:Creative Commons GNU GPL v2
# http://creativecommons.org/licenses/GPL/2.0/

"""Ensure that invalid files (non-episodes) are not renamed
"""

from functional_runner import run_tvnamer, verify_out_data
from nose.plugins.attrib import attr

@attr("functional")
def test_simple_single_file():
    """Files without series name should be skipped
    """

    out_data = run_tvnamer(
        with_files = ['S01E02 - Some File.avi'],
        with_flags = ["--batch"])

    expected_files = ['S01E02 - Some File.avi']

    verify_out_data(out_data, expected_files)
<commit_msg>Skip seriesname-less test<commit_after>#!/usr/bin/env python
#encoding:utf-8
#author:dbr/Ben
#project:tvnamer
#repository:http://github.com/dbr/tvnamer
#license:Creative Commons GNU GPL v2
# http://creativecommons.org/licenses/GPL/2.0/

"""Ensure that invalid files (non-episodes) are not renamed
"""

from functional_runner import run_tvnamer, verify_out_data
from nose.plugins.attrib import attr
from nose import SkipTest

@attr("functional")
def test_simple_single_file():
    """Files without series name should be skipped
    """

    raise SkipTest("TODO")

    out_data = run_tvnamer(
        with_files = ['S01E02 - Some File.avi'],
        with_flags = ["--batch"])

    expected_files = ['S01E02 - Some File.avi']

    verify_out_data(out_data, expected_files)
<|endoftext|>


<commit_before>'''
This implementation is based in John Tromp's Java implementation
of Connect 4.

https://github.com/qu1j0t3/fhourstones/blob/master/Connect4.java
'''

from . import Game
from ..util import print_aec, str_aec, ZobristHashing

ROWS = 6
COLS = 7
H1 = ROWS + 1
H2 = ROWS + 2
SIZE = ROWS * COLS
SIZE1 = H1 * COLS
ALL1 = (1 << SIZE1) - 1
COL1 = (1 << H1) - 1
BOTTOM = ALL1 / COL1
TOP = BOTTOM << ROWS

class Connect4(Game):

    def __init__(self):
        self.reset()

    def set_board(self, board):
        self._boards = [0, 0]
        for row in range(ROWS):
            for col in range(COLS):
                if (board[row][col] == 'X'):
                    self._boards[0] |= 1 << ((col * COLS) + (ROWS - row - 1))
                elif (board[row][col] == 'O'):
                    self._boards[1] |= 1 << ((col * COLS) + (ROWS - row - 1))

    def __repr__(self):
        return Connect4View(self).render()

    def print_bitboard(self, board):
        out = '  '
        for col in range(COLS):
            out += ' ' + chr(97 + col)
        out = str_aec(out, 'bold_green') + '\n'
        for row in range(ROWS, -1, -1):
            out += ' ' + str_aec(str(row + 1), 'bold_green')
            for col in range(COLS + 10):
                hello = (col * 7) + row
                if (1 << hello) & board:
                    out += str_aec(' X', 'bold_red')
                else:
                    out += ' .'
            out += '\n'
        out += '\n'
        out += '-' * 80
        out += '\n'
        print(out)

    def __str__(self):
        return Connect4View(self).render()

    ########
    # Game #
    ########

    def copy(self):
        c4 = Connect4()
        c4._cur_player = self._cur_player
        c4._boards = self._boards[:]
        if self._check_win(self._boards[0]) or self._check_win(self._boards[1]):
            return c4
        c4._moves = []
        for m in self._moves:
            c4._moves.append(m)
        for c in range(COLS):
            c4._height[c] = self._height[c]
        return c4

    def cur_player(self):
        return self._cur_player

    def legal_moves(self):
        return [chr(ord('a') + move) for move in self._moves]

    def make_move(self, move):
        move = ord(move) - ord('a')
        if move not in self._moves:
            print(type(move))
            raise Exception('Invalid move: {}'.format(move))
        new_board = self._boards[self.cur_player()] | (1 << self._height[move])
        self._height[move] += 1
        self._boards[self.cur_player()] = new_board
        self._cur_player = (self.cur_player() + 1) % 2
        self._moves = [] if self._check_win(new_board) else self._init_moves()
        return self

    def outcomes(self):
        if self._check_win(self._boards[0]):
            return ['W', 'L']
        elif self._check_win(self._boards[1]):
            return ['L', 'W']
        return ['D', 'D']

    def reset(self):
        self._cur_player = 0
        self._boards = [0, 0]
        self._height = [H1 * i for i in range(COLS)]
        self._moves = self._init_moves()

    #############
    # Connect 4 #
    #############

    def _init_moves(self):
        return [i for i in range(COLS) if ((1 << self._height[i]) & TOP) == 0]

    def _check_win(self, board):
        y = board & (board >> ROWS)
        if y & (y >> 2 * ROWS):
            return True
        y = board & (board >> H1)
        if y & (y >> 2 * H1):
            return True
        y = board & (board >> H2)
        if y & (y >> 2 * H2):
            return True
        y = board & (board >> 1)
        if y & (y >> 2):
            return True
        return False

class Connect4View(object):

    def __init__(self, game):
        self.game = game

    def _info(self):
        if self.game.is_over():
            return self._game_over() + '\n'
        return self._next_player() + self._moves() + '\n'

    def _game_over(self):
        return str_aec('Game Over!', 'bold_green') + '\n'

    def _next_player(self):
        return str_aec('Next player: ', 'bold_green') + str(self.game.cur_player()) + '\n'

    def _moves(self):
        s = '[{}]'.format(', '.join([str(s) for s in self.game.legal_moves()]))
        out = ''
        out += str_aec('Moves: ', 'bold_green') + s + '\n'
        return out

    def _board(self):
        out = '  '
        for col in range(COLS):
            out += ' ' + chr(97 + col)
        out = str_aec(out, 'bold_green') + '\n'
        for row in range(ROWS - 1, -1, -1):
            out += ' ' + str_aec(str(row + 1), 'bold_green')
            for col in range(COLS):
                hello = (col * 7) + row
                if (1 << hello) & self.game._boards[0]:
                    out += str_aec(' X', 'bold_red')
                elif (1 << hello) & self.game._boards[1]:
                    out += str_aec(' O', 'bold_blue')
                else:
                    out += ' .'
            out += '\n'
        out += '\n'
        out += '-' * 80
        out += '\n'
        return out

    def _outcomes(self):
        return '\n' + str(self.game.outcomes()) if self.game.is_over() else ''

    def render(self):
        return '{info}{board}{outcomes}'.format(
            info=self._info(),
            board=self._board(),
            outcomes=self._outcomes()
        )
<commit_msg>Update<commit_after>'''
This implementation is based in John Tromp's Java implementation
of Connect 4.

https://github.com/qu1j0t3/fhourstones/blob/master/Connect4.java
'''

from . import Game
from ..util import print_aec, str_aec, ZobristHashing

ROWS = 6
COLS = 7
H1 = ROWS + 1
H2 = ROWS + 2
SIZE = ROWS * COLS
SIZE1 = H1 * COLS
ALL1 = (1 << SIZE1) - 1
COL1 = (1 << H1) - 1
BOTTOM = ALL1 / COL1
TOP = BOTTOM << ROWS

class Connect4(Game):

    def __init__(self):
        self.reset()

    def set_board(self, board):
        self._boards = [0, 0]
        for row in range(ROWS):
            for col in range(COLS):
                if (board[row][col] == 'X'):
                    self._boards[0] |= 1 << ((col * COLS) + (ROWS - row - 1))
                elif (board[row][col] == 'O'):
                    self._boards[1] |= 1 << ((col * COLS) + (ROWS - row - 1))

    def __repr__(self):
        return Connect4View(self).render()

    def print_bitboard(self, board):
        out = '  '
        for col in range(COLS):
            out += ' ' + chr(97 + col)
        out = str_aec(out, 'bold_green') + '\n'
        for row in range(ROWS, -1, -1):
            out += ' ' + str_aec(str(row + 1), 'bold_green')
            for col in range(COLS + 10):
                hello = (col * 7) + row
                if (1 << hello) & board:
                    out += str_aec(' X', 'bold_red')
                else:
                    out += ' .'
            out += '\n'
        out += '\n'
        out += '-' * 80
        out += '\n'
        print(out)

    def __str__(self):
        return Connect4View(self).render()

    ########
    # Game #
    ########

    def copy(self):
        c4 = Connect4()
        c4._cur_player = self._cur_player
        c4._boards = self._boards[:]
        if self._check_win(self._boards[0]) or self._check_win(self._boards[1]):
            return c4
        c4._moves = []
        for m in self._moves:
            c4._moves.append(m)
        for c in range(COLS):
            c4._height[c] = self._height[c]
        return c4

    def cur_player(self):
        return self._cur_player

    def legal_moves(self):
        return [chr(ord('a') + move) for move in self._moves]

    def make_move(self, move):
        move = ord(move) - ord('a')
        if move not in self._moves:
            print(type(move))
            raise Exception('Invalid move: {}'.format(move))
        new_board = self._boards[self.cur_player()] | (1 << self._height[move])
        self._height[move] += 1
        self._boards[self.cur_player()] = new_board
        self._cur_player = (self.cur_player() + 1) % 2
        self._moves = [] if self._check_win(new_board) else self._generate_moves()
        return self

    def outcomes(self):
        if self._check_win(self._boards[0]):
            return ['W', 'L']
        elif self._check_win(self._boards[1]):
            return ['L', 'W']
        return ['D', 'D']

    def reset(self):
        self._cur_player = 0
        self._boards = [0, 0]
        self._height = [H1 * i for i in range(COLS)]
        self._moves = self._generate_moves()

    #############
    # Connect 4 #
    #############

    def _generate_moves(self):
        return [i for i in range(COLS) if ((1 << self._height[i]) & TOP) == 0]

    def _check_win(self, board):
        y = board & (board >> ROWS)
        if y & (y >> 2 * ROWS):
            return True
        y = board & (board >> H1)
        if y & (y >> 2 * H1):
            return True
        y = board & (board >> H2)
        if y & (y >> 2 * H2):
            return True
        y = board & (board >> 1)
        if y & (y >> 2):
            return True
        return False

class Connect4View(object):

    def __init__(self, game):
        self.game = game

    def _info(self):
        if self.game.is_over():
            return self._game_over() + '\n'
        return self._next_player() + self._moves() + '\n'

    def _game_over(self):
        return str_aec('Game Over!', 'bold_green') + '\n'

    def _next_player(self):
        return str_aec('Next player: ', 'bold_green') + str(self.game.cur_player()) + '\n'

    def _moves(self):
        s = '[{}]'.format(', '.join([str(s) for s in self.game.legal_moves()]))
        out = ''
        out += str_aec('Moves: ', 'bold_green') + s + '\n'
        return out

    def _board(self):
        out = '  '
        for col in range(COLS):
            out += ' ' + chr(97 + col)
        out = str_aec(out, 'bold_green') + '\n'
        for row in range(ROWS - 1, -1, -1):
            out += ' ' + str_aec(str(row + 1), 'bold_green')
            for col in range(COLS):
                hello = (col * 7) + row
                if (1 << hello) & self.game._boards[0]:
                    out += str_aec(' X', 'bold_red')
                elif (1 << hello) & self.game._boards[1]:
                    out += str_aec(' O', 'bold_blue')
                else:
                    out += ' .'
            out += '\n'
        out += '\n'
        out += '-' * 80
        out += '\n'
        return out

    def _outcomes(self):
        return '\n' + str(self.game.outcomes()) if self.game.is_over() else ''

    def render(self):
        return '{info}{board}{outcomes}'.format(
            info=self._info(),
            board=self._board(),
            outcomes=self._outcomes()
        )
<|endoftext|>


<commit_before>from itertools import cycle
from contextlib import ExitStack

from .. import lekvar
from ..errors import InternalError

from .util import *
from .state import State
from . import builtins

#
# class Object
#

@patch
def Object_eval(self):
    raise InternalError("Not Implemented")

@patch
def Object_evalContext(self):
    raise InternalError("Not Implemented")

@patch
def Object_evalCall(self, values):
    raise InternalError("Not Implemented")

@patch
def Object_evalAssign(self, value):
    raise InternalError("Not Implemented")

#
# class Link
#

@patch
def Link_eval(self):
    return self.value.eval()

@patch
def Link_evalContext(self):
    return self.value.evalContext()

@patch
def Link_evalCall(self, values):
    return self.value.evalCall(values)

@patch
def Link_evalAssign(self, value):
    self.value.evalAssign(value)

@patch
def Link_evalSize(self):
    return self.value.evalSize()

#
# class Attribute
#

@patch
def Attribute_eval(self):
    with State.selfScope(self.object.eval()):
        return self.value.eval()

@patch
def Attribute_evalContext(self):
    return self.object.eval()

@patch
def Attribute_evalCall(self, values):
    with State.selfScope(self.object.eval()):
        return self.value.evalCall(values)

@patch
def Attribute_evalAssign(self, value):
    with State.selfScope(self.object.eval()):
        self.value.evalAssign(value)

#
# class ClosedTarget
#

@patch
def ClosedTarget_eval(self):
    with self.target():
        result = self.value.eval()

        if result == self.value:
            return self
        return result

@patch
def ClosedTarget_evalContext(self):
    with self.target():
        return self.value.evalContext()

@patch
def ClosedTarget_evalCall(self, values):
    with self.target():
        return self.value.evalCall(values)

@patch
def ClosedTarget_evalAssign(self, value):
    with self.target():
        self.value.evalAssign(value)

#
# class Reference
#

@patch
def Reference_eval(self):
    return self.value.eval()

@patch
def Reference_evalContext(self):
    return self.value.evalContext()

@patch
def Reference_evalCall(self, values):
    return self.value.evalCall(values)

@patch
def Reference_evalAssign(self, value):
    self.value.evalAssign(value)

@patch
def Reference_evalSize(self):
    return builtins.PTR_SIZE

#
# class Module
#

lekvar.Module.evaled = False

@patch
def Module_eval(self):
    if self.evaled: return self
    self.evaled = True

    for instr in self.main:
        instr.eval()

    for obj in self.context:
        obj.eval()

    return self

#
# class Variable
#

@patch
def Variable_eval(self):
    if isinstance(self.parent, lekvar.Class):
        assert State.self is not None

        if not isinstance(State.self, lekvar.Literal) or not isinstance(State.self.data, dict):
            return State.self
        return State.self.data[self.name]

    if self.value is not None:
        return self.value
    return self

@patch
def Variable_evalContext(self):
    return self.eval()

@patch
def Variable_evalAssign(self, value):
    if isinstance(self.parent, lekvar.Class):
        assert State.self is not None
        assert isinstance(State.self, lekvar.Literal)

        State.self.data[self.name] = value
    else:
        self.value = value

#
# class Assignment
#

@patch
def Assignment_eval(self):
    self.assigned.evalAssign(self.value.eval())

#
# class Method
#

@patch
def Method_eval(self):
    return self

@patch
def Method_evalContext(self):
    return None

@patch
def Method_evalCall(self, values):
    call = lekvar.FunctionType([value.resolveType() for value in values])
    call.verify()
    function = self.resolveCall(call)

    return function.evalCall(values)

#
# class MethodInstance
#

@patch
def MethodInstance_eval(self):
    return State.self

@patch
def MethodInstance_evalCall(self, values):
    method = State.self

    call = lekvar.FunctionType([value.resolveType() for value in values])
    function = method.resolveCall(call)

    return function.evalCall(values)

#
# class Function
#

lekvar.Function.eval_returning = None
lekvar.Function.eval_returned = None

@patch
def Function_eval(self):
    return self

@patch
def Function_evalContext(self):
    #TODO: Implement closures
    assert len(self.closed_context) == 0

    return None

@patch
def Function_evalCall(self, values):
    self.eval_returned = False
    self.eval_returning = None

    previous_values = [arg.resolveValue().value for arg in self.arguments]
    for arg, val in zip(self.arguments, values):
        arg.evalAssign(val)

    for instr in self.instructions:
        instr.eval()

        if self.eval_returned:
            break

    for arg, val in zip(self.arguments, previous_values):
        arg.evalAssign(val)

    returning = self.eval_returning
    self.eval_returned = False
    self.eval_returning = None

    return returning

#
# class FunctionInstance
#

@patch
def FunctionInstance_eval(self):
    return self

@patch
def FunctionInstance_evalCall(self, values):
    return State.self.evalCall(values)

#
# class ExternalFunction
#

@patch
def ExternalFunction_eval(self):
    return self

@patch
def ExternalFunction_evalContext(self):
    return None

#
# class Class
#

@patch
def Class_eval(self):
    targets = [(value, value.eval()) for value in self.closed_context]

    if len(targets) == 0:
        return self
    return lekvar.ClosedTarget(self, targets)

@patch
def Class_evalCall(self, values):
    return self.constructor.evalCall(values)

@patch
def Class_evalContext(self):
    return None

@patch
def Class_evalSize(self):
    variables = [value for value in self.instance_context if isinstance(value, lekvar.Variable)]
    print(self, variables)
    sizes = [value.resolveType().evalSize() for value in variables]

    return sum(sizes)

#
# class Constructor
#

@patch
def Constructor_evalCall(self, values):
    self_value = lekvar.Literal({}, self.constructing)

    with State.selfScope(self_value):
        lekvar.Function.evalCall(self, values)

    return self_value

#
# class ForwardTarget
#

@patch
def ForwardTarget_eval(self):
    with self.target():
        result = self.value.eval()

    if result is self.value:
        return self
    return result

@patch
def ForwardTarget_evalContext(self):
    raise InternalError("Not Implemented")

@patch
def ForwardTarget_evalCall(self, values):
    with self.target():
        return self.value.evalCall(values)

@patch
def ForwardTarget_evalAssign(self, value):
    raise InternalError("Not Implemented")

#
# class ForwardObject
#

@patch
def ForwardObject_eval(self):
    assert self.target is not None
    value = self.target.eval()
    if value is self.target:
        return self
    return value

@patch
def ForwardObject_evalContext(self):
    return self.target.evalContext()

@patch
def ForwardObject_evalCall(self, values):
    return self.target.evalCall(values)

@patch
def ForwardObject_evalAssign(self, value):
    self.target.evalAssign(value)

@patch
def ForwardObject_evalSize(self):
    return self.target.evalSize()

#
# class Literal
#

@patch
def Literal_eval(self):
    return self

#
# class Call
#

@patch
def Call_eval(self):
    with State.selfScope(self.called.eval()):
        called = self.function.eval()

    # Hack, for now
    scope = ExitStack()
    if isinstance(self.function, lekvar.ForwardTarget):
        scope = self.function.target()

    with scope:
        values = [value.eval() for value in self.values]

        context = self.called.evalContext()
        with State.selfScope(context):
            return called.evalCall(values)

@patch
def Call_evalContext(self):
    return self.called.eval()

#
# class Return
#

@patch
def Return_eval(self):
    if self.value is not None:
        self.function.eval_returning = self.value.eval()

    self.function.eval_returned = True
    return None

#
# class Branch
#

@patch
def Branch_eval(self):
    if self.condition is not None:
        value = self.condition.eval()
        assert isinstance(value, lekvar.Literal)

        bool_value = value.data
        if isinstance(value.data, dict):
            bool_value = value.data["value"].data

        if bool_value is False:
            if self.next_branch is not None:
                self.next_branch.eval()
            return

    for instr in self.instructions:
        instr.eval()

#
# class Loop
#

lekvar.Loop.breaking = False

@patch
def Loop_eval(self):
    self.breaking = False

    for instr in cycle(self.instructions):
        instr.eval()

        if self.breaking: break

#
# class Break
#

@patch
def Break_eval(self):
    self.loop.breaking = True

#
# class SizeOf
#

@patch
def SizeOf_evalCall(self, values):
    assert len(values) == 1
    value = values[0]

    return lekvar.Literal(value.evalSize(), self.type.return_type)
<commit_msg>Implemented size evaluation and default initial values in interpreter<commit_after>from itertools import cycle
from contextlib import ExitStack

from .. import lekvar
from ..errors import InternalError

from .util import *
from .state import State
from . import builtins

#
# class Object
#

@patch
def Object_eval(self):
    raise InternalError("Not Implemented")

@patch
def Object_evalContext(self):
    raise InternalError("Not Implemented")

@patch
def Object_evalCall(self, values):
    raise InternalError("Not Implemented")

@patch
def Object_evalAssign(self, value):
    raise InternalError("Not Implemented")

#
# class Type
#

@patch
def Type_evalSize(self):
    raise InternalError("Not Implemented")

@patch
def Type_evalNewValue(self):
    raise InternalError("Not Implemented")

#
# class Link
#

@patch
def Link_eval(self):
    return self.value.eval()

@patch
def Link_evalContext(self):
    return self.value.evalContext()

@patch
def Link_evalCall(self, values):
    return self.value.evalCall(values)

@patch
def Link_evalAssign(self, value):
    self.value.evalAssign(value)

@patch
def Link_evalSize(self):
    return self.value.evalSize()

@patch
def Link_evalNewValue(self):
    return self.value.evalNewValue()

#
# class Attribute
#

@patch
def Attribute_eval(self):
    with State.selfScope(self.object.eval()):
        return self.value.eval()

@patch
def Attribute_evalContext(self):
    return self.object.eval()

@patch
def Attribute_evalCall(self, values):
    with State.selfScope(self.object.eval()):
        return self.value.evalCall(values)

@patch
def Attribute_evalAssign(self, value):
    with State.selfScope(self.object.eval()):
        self.value.evalAssign(value)

#
# class ClosedTarget
#

@patch
def ClosedTarget_eval(self):
    with self.target():
        result = self.value.eval()

        if result == self.value:
            return self
        return result

@patch
def ClosedTarget_evalContext(self):
    with self.target():
        return self.value.evalContext()

@patch
def ClosedTarget_evalCall(self, values):
    with self.target():
        return self.value.evalCall(values)

@patch
def ClosedTarget_evalAssign(self, value):
    with self.target():
        self.value.evalAssign(value)

@patch
def ClosedTarget_evalNewValue(self):
    with self.target():
        return self.value.evalNewValue()

#
# class Reference
#

@patch
def Reference_eval(self):
    return self.value.eval()

@patch
def Reference_evalContext(self):
    return self.value.evalContext()

@patch
def Reference_evalCall(self, values):
    return self.value.evalCall(values)

@patch
def Reference_evalAssign(self, value):
    self.value.evalAssign(value)

@patch
def Reference_evalSize(self):
    return builtins.PTR_SIZE

#
# class Module
#

lekvar.Module.evaled = False

@patch
def Module_eval(self):
    if self.evaled: return self
    self.evaled = True

    for instr in self.main:
        instr.eval()

    for obj in self.context:
        obj.eval()

    return self

#
# class Variable
#

@patch
def Variable_eval(self):
    if isinstance(self.parent, lekvar.Class):
        assert State.self is not None

        if not isinstance(State.self, lekvar.Literal) or not isinstance(State.self.data, dict):
            return State.self
        return State.self.data[self.name]

    if self.value is not None:
        return self.value
    return self

@patch
def Variable_evalContext(self):
    return self.eval()

@patch
def Variable_evalAssign(self, value):
    if isinstance(self.parent, lekvar.Class):
        assert State.self is not None
        assert isinstance(State.self, lekvar.Literal)

        State.self.data[self.name] = value
    else:
        self.value = value

@patch
def Variable_evalSize(self):
    return self.value.evalSize()

@patch
def Variable_evalNewValue(self):
    return self.value.evalNewValue()

#
# class Assignment
#

@patch
def Assignment_eval(self):
    self.assigned.evalAssign(self.value.eval())

#
# class Method
#

@patch
def Method_eval(self):
    return self

@patch
def Method_evalContext(self):
    return None

@patch
def Method_evalCall(self, values):
    call = lekvar.FunctionType([value.resolveType() for value in values])
    call.verify()
    function = self.resolveCall(call)

    return function.evalCall(values)

#
# class MethodType
#

@patch
def MethodType_evalNewValue(self):
    return lekvar.Literal({}, self)

#
# class MethodInstance
#

@patch
def MethodInstance_eval(self):
    return State.self

@patch
def MethodInstance_evalCall(self, values):
    method = State.self

    call = lekvar.FunctionType([value.resolveType() for value in values])
    function = method.resolveCall(call)

    return function.evalCall(values)

#
# class Function
#

lekvar.Function.eval_returning = None
lekvar.Function.eval_returned = None

@patch
def Function_eval(self):
    return self

@patch
def Function_evalContext(self):
    #TODO: Implement closures
    assert len(self.closed_context) == 0

    return None

@patch
def Function_evalCall(self, values):
    self.eval_returned = False
    self.eval_returning = None

    previous_values = [arg.resolveValue().value for arg in self.arguments]
    for arg, val in zip(self.arguments, values):
        arg.evalAssign(val)

    for instr in self.instructions:
        instr.eval()

        if self.eval_returned:
            break

    for arg, val in zip(self.arguments, previous_values):
        arg.evalAssign(val)

    returning = self.eval_returning
    self.eval_returned = False
    self.eval_returning = None

    return returning

#
# class FunctionInstance
#

@patch
def FunctionInstance_eval(self):
    return self

@patch
def FunctionInstance_evalCall(self, values):
    return State.self.evalCall(values)

#
# class ExternalFunction
#

@patch
def ExternalFunction_eval(self):
    return self

@patch
def ExternalFunction_evalContext(self):
    return None

#
# class Class
#

@patch
def Class_eval(self):
    targets = [(value, value.eval()) for value in self.closed_context]

    if len(targets) == 0:
        return self
    return lekvar.ClosedTarget(self, targets)

@patch
def Class_evalCall(self, values):
    return self.constructor.evalCall(values)

@patch
def Class_evalContext(self):
    return None

@patch
def Class_evalSize(self):
    variables = [value for value in self.instance_context if isinstance(value, lekvar.Variable)]
    print(self, variables)
    sizes = [value.resolveType().evalSize() for value in variables]

    return sum(sizes)

@patch
def Class_evalNewValue(self):
    values = {}

    for value in self.instance_context:
        if not isinstance(value, lekvar.Variable):
            continue

        values[value.name] = value.resolveType().evalNewValue()

    return lekvar.Literal(values, self)

#
# class Constructor
#

@patch
def Constructor_evalCall(self, values):
    self_value = self.constructing.evalNewValue()

    with State.selfScope(self_value):
        lekvar.Function.evalCall(self, values)

    return self_value

#
# class ForwardTarget
#

@patch
def ForwardTarget_eval(self):
    with self.target():
        result = self.value.eval()

    if result is self.value:
        return self
    return result

@patch
def ForwardTarget_evalContext(self):
    raise InternalError("Not Implemented")

@patch
def ForwardTarget_evalCall(self, values):
    with self.target():
        return self.value.evalCall(values)

@patch
def ForwardTarget_evalAssign(self, value):
    raise InternalError("Not Implemented")

@patch
def ForwardTarget_evalNewValue(self):
    with self.target():
        return self.value.evalNewValue()

#
# class ForwardObject
#

@patch
def ForwardObject_eval(self):
    assert self.target is not None
    value = self.target.eval()
    if value is self.target:
        return self
    return value

@patch
def ForwardObject_evalContext(self):
    return self.target.evalContext()

@patch
def ForwardObject_evalCall(self, values):
    return self.target.evalCall(values)

@patch
def ForwardObject_evalAssign(self, value):
    self.target.evalAssign(value)

@patch
def ForwardObject_evalSize(self):
    return self.target.evalSize()

@patch
def ForwardObject_evalNewValue(self):
    return self.target.evalNewValue()

#
# class Literal
#

@patch
def Literal_eval(self):
    return self

#
# class Call
#

@patch
def Call_eval(self):
    with State.selfScope(self.called.eval()):
        called = self.function.eval()

    # Hack, for now
    scope = ExitStack()
    if isinstance(self.function, lekvar.ForwardTarget):
        scope = self.function.target()

    with scope:
        values = [value.eval() for value in self.values]

        context = self.called.evalContext()
        with State.selfScope(context):
            return called.evalCall(values)

@patch
def Call_evalContext(self):
    return self.called.eval()

#
# class Return
#

@patch
def Return_eval(self):
    if self.value is not None:
        self.function.eval_returning = self.value.eval()

    self.function.eval_returned = True
    return None

#
# class Branch
#

@patch
def Branch_eval(self):
    if self.condition is not None:
        value = self.condition.eval()
        assert isinstance(value, lekvar.Literal)

        bool_value = value.data
        if isinstance(value.data, dict):
            bool_value = value.data["value"].data

        if bool_value is False:
            if self.next_branch is not None:
                self.next_branch.eval()
            return

    for instr in self.instructions:
        instr.eval()

#
# class Loop
#

lekvar.Loop.breaking = False

@patch
def Loop_eval(self):
    self.breaking = False

    for instr in cycle(self.instructions):
        instr.eval()

        if self.breaking: break

#
# class Break
#

@patch
def Break_eval(self):
    self.loop.breaking = True

#
# class SizeOf
#

@patch
def SizeOf_evalCall(self, values):
    assert len(values) == 1
    value = values[0]

    return lekvar.Literal(value.evalSize(), self.type.return_type)

#
# class VoidType
#

@patch
def VoidType_eval(self):
    return

@patch
def VoidType_evalNewValue(self):
    return lekvar.Literal(0, self)
<|endoftext|>


<commit_before>#!/usr/bin/env python

# -*- coding: utf-8 -*-
#
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
#
#
# Copyright (C) 2003, 2004, 2005 Edgewall Software
# Copyright (C) 2003, 2004, 2005 Jonas Borgström <jonas@edgewall.com>
# Copyright (C) 2005 Christopher Lenz <cmlenz@gmx.de>
#
# This software is licensed as described in the file
# LICENSE_FOR_PYTHON_BINDINGS, which you should have received as part
# of this distribution.  The terms are also available at
# < http://subversion.tigris.org/license-for-python-bindings.html >.
# If newer versions of this license are posted there, you may use a
# newer version instead, at your option.
#
# Author: Jonas Borgström <jonas@edgewall.com>
#         Christopher Lenz <cmlenz@gmx.de>

import unittest

class TestSetup(unittest.TestSuite):
    """
    Test suite decorator that allows a fixture to be setup for a complete
    suite of test cases.
    """
    def setUp(self):
        pass

    def tearDown(self):
        pass

    def __call__(self, result):
        self.setUp()
        unittest.TestSuite.__call__(self, result)
        self.tearDown()
        return result
<commit_msg>Follow-up to r891679, in which I tried to move the "encoding" lines in Python files to the top of the file as required by Python. Unfortunately I failed in this file by inserting a blank line. Now removing the blank line.<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
#
#
# Copyright (C) 2003, 2004, 2005 Edgewall Software
# Copyright (C) 2003, 2004, 2005 Jonas Borgström <jonas@edgewall.com>
# Copyright (C) 2005 Christopher Lenz <cmlenz@gmx.de>
#
# This software is licensed as described in the file
# LICENSE_FOR_PYTHON_BINDINGS, which you should have received as part
# of this distribution.  The terms are also available at
# < http://subversion.tigris.org/license-for-python-bindings.html >.
# If newer versions of this license are posted there, you may use a
# newer version instead, at your option.
#
# Author: Jonas Borgström <jonas@edgewall.com>
#         Christopher Lenz <cmlenz@gmx.de>

import unittest

class TestSetup(unittest.TestSuite):
    """
    Test suite decorator that allows a fixture to be setup for a complete
    suite of test cases.
    """
    def setUp(self):
        pass

    def tearDown(self):
        pass

    def __call__(self, result):
        self.setUp()
        unittest.TestSuite.__call__(self, result)
        self.tearDown()
        return result
<|endoftext|>


<commit_before>import os
import tempfile
import uuid
import logging

from BeautifulSoup import BeautifulSoup
from PIL import Image
import requests
from scrapy import FormRequest, Request

ROOT = os.path.abspath(os.path.dirname(__file__))
ICON_LOC = os.path.join(ROOT, 'iconset')

class CaptchaBuster(object):

    def __init__(self, captcha_loc):
        self.original = Image.open(captcha_loc).convert('P')
        self.temp_file = tempfile.TemporaryFile(suffix='.gif', prefix='CaptchaTemp_', dir=ROOT)
        self.image_segment_files = [tempfile.TemporaryFile(suffix='.gif', prefix='%d_ImageSegment_' % n, dir=ROOT) for n in range(6)]
        self.image_segments = []
        self.processed_captcha = Image.new('P', self.original.size, 255)

    @property
    def guess(self):
        self._pre_process_captcha()
        self._crop_partitions()
        return ''.join([p[0][1] for p in self._guess_characters()])

    @classmethod
    def from_url(cls, url, session=None):
        """
        Create a CaptchaBuster object from the url of a captcha.
        :param url: URL location of the captcha image
        :param session: requests.Session object. Use if you have an
            already generated session that you want to use instead of
            a default session.
        :type url: str
        :type session: requests.Session
        :return:
        :rtype: CaptchaBuster
        """
        if not session:
            session = requests.Session()
        url_tmp_pic = os.path.join(ROOT, 'captcha.jpg')
        with open(url_tmp_pic, 'wb') as f:
            f.write(session.get(url).content)

        return CaptchaBuster(url_tmp_pic)

    def _pre_process_captcha(self):
        """
        Scan the original image from top to bottom moving left to right and
            check if the pixel at location is below color value 10. If the pixel
            at location is within the threshold, write the pixel to the temp file.
        """
        for y in range(self.original.size[1]):
            for x in range(self.original.size[0]):
                pixel = self.original.getpixel((x, y))
                if pixel < 10:
                    self.processed_captcha.putpixel((x, y), 0)
        self.processed_captcha.save(self.temp_file, 'gif')

    def _crop_partitions(self):
        """
        Find discrete partitions within the preprocessed captcha
        to find regions containing individual characters. Returns an accumulated
        list of (start, end) coordinates for this region. Partitions are
        calculated based on the existence of characters within the image, as
        determined by surrounding white space.
        :return:
        """
        letters = []
        in_letter = False
        found_letter = False
        start, end = 0, 0

        for y in range(self.processed_captcha.size[0]):
            for x in range(self.processed_captcha.size[1]):
                pixel = self.processed_captcha.getpixel((y, x))
                if not pixel:
                    in_letter = True

            if not found_letter and in_letter:
                found_letter = True
                start = y

            if found_letter and not in_letter:
                found_letter = False
                end = y
                letters.append((start, end))

            in_letter = False

        count = 0
        for start, end in letters:
            crop_box = (start, 0, end, self.processed_captcha.size[1])
            part = self.processed_captcha.crop(crop_box)

            # If the segment of the image is too small to be a letter, ignore
            # the segment
            if part.size[0] < 15:
                pass
            else:
                part.save(self.image_segment_files[count], 'gif')
                self.image_segments.append(part)
                count += 1

    def _guess_characters(self):
        captcha = []
        for segment in self.image_segments:
            guess = []
            for letter in 'abcdefghijklmnopqrstuvwxyz':
                letter_dir = os.path.join(ICON_LOC, letter)
                for img in os.listdir(letter_dir):
                    if img != 'Thumbs.db' and img != '.gitignore':
                        tmp = Image.open(os.path.join(letter_dir, img))
                        s = segment.resize(tmp.size)
                        guess.append((self.relation(self.build_vector(tmp),
                                                    self.build_vector(s)), letter))
            guess.sort(reverse=True)
            captcha.append(guess)
        return captcha

    @classmethod
    def build_vector(cls, img):
        d1 = {}
        for count, i in enumerate(img.getdata()):
            d1[count] = i
        return d1

    @classmethod
    def relation(cls, concordance1, concordance2):
        """

        """
        relevance = 0
        for word, count in concordance1.iteritems():
            if word in concordance2 and count == concordance2[word]:
                if not count:
                    relevance += 5 if not count else 1
        return float(relevance)/float(len(concordance2))

def test():
    for t in range(5):
        session = requests.Session()
        response = session.get('http://www.amazon.com/errors/validateCaptcha')
        soup = BeautifulSoup(response.content)
        with open('./%d_captcha.jpg' % t, 'wb') as f:
            f.write(session.get(soup.find('img').get('src')).content)

        cb = CaptchaBuster('./%d_captcha.jpg' % t)
        print 'Pass %d:' % t, cb.guess

class RobotMiddleware(object):

    def __init__(self, crawler):
        self.crawler = crawler
        self.logger = logging.getLogger(self.__class__.__name__)

    def process_request(self, request, spider):
        return

    def process_exception(self, request, exception, spider):
        return

    def process_response(self, request, response, spider):
        if not response.url.endswith('.jpg'):
            if response.xpath('//title/text()[contains(., "Robot Check")]'):
                self.crawler.stats.inc_value('robot_check')
                self.logger.warning('Robot Check')
                soup = BeautifulSoup(response.body)

                form = soup.find('form')

                # Generate the captcha buster object
                # cb = CaptchaBuster.from_url(form.find('img').get('src'))

                # Url to send the captcha verification request
                get_url = 'http://www.amazon.com' + form.get('action')

                # Get all input params from the form
                input_params = dict([(x.get('name'), x.get('value')) for x in form.findAll('input', {'type': 'hidden'})])

                meta = {'target_url': get_url,
                        'params': input_params,
                        'decode': True}
                meta = request.meta.update(meta)
                # Set the field keywords param to the captcha buster's guess
                # input_params['field-keywords'] = cb.guess
                # self.logger.info('Captcha Value: %s' % input_params['field-keywords'])

                # return FormRequest(get_url, formdata=input_params, meta=request.meta)
                return Request(form.find('img').get('src'), meta=meta)
        else:
            params = request.meta.get('params')
            target_url = request.meta.get('target_url')
            url_tmp_pic = os.path.join(ROOT, '%s_tmp_captcha.jpg' % uuid.uuid4())
            with open(url_tmp_pic, 'wb') as f:
                f.write(response.body)
            cb = CaptchaBuster(url_tmp_pic)
            params['field-keywords'] = cb.guess
            self.logger.info('Captcha Value: %s' % params['field-keywords'])
            return FormRequest(target_url, formdata=params, meta=request.meta)

        return response

    @classmethod
    def from_crawler(cls, crawler):
        return cls(crawler)

if __name__ == '__main__':
    logging.basicConfig(level=10)
    test()
<commit_msg>Fixing meta in request<commit_after>import os
import tempfile
import uuid
import logging

from BeautifulSoup import BeautifulSoup
from PIL import Image
import requests
from scrapy import FormRequest, Request

ROOT = os.path.abspath(os.path.dirname(__file__))
ICON_LOC = os.path.join(ROOT, 'iconset')

class CaptchaBuster(object):

    def __init__(self, captcha_loc):
        self.original = Image.open(captcha_loc).convert('P')
        self.temp_file = tempfile.TemporaryFile(suffix='.gif', prefix='CaptchaTemp_', dir=ROOT)
        self.image_segment_files = [tempfile.TemporaryFile(suffix='.gif', prefix='%d_ImageSegment_' % n, dir=ROOT) for n in range(6)]
        self.image_segments = []
        self.processed_captcha = Image.new('P', self.original.size, 255)

    @property
    def guess(self):
        self._pre_process_captcha()
        self._crop_partitions()
        return ''.join([p[0][1] for p in self._guess_characters()])

    @classmethod
    def from_url(cls, url, session=None):
        """
        Create a CaptchaBuster object from the url of a captcha.
        :param url: URL location of the captcha image
        :param session: requests.Session object. Use if you have an
            already generated session that you want to use instead of
            a default session.
        :type url: str
        :type session: requests.Session
        :return:
        :rtype: CaptchaBuster
        """
        if not session:
            session = requests.Session()
        url_tmp_pic = os.path.join(ROOT, 'captcha.jpg')
        with open(url_tmp_pic, 'wb') as f:
            f.write(session.get(url).content)

        return CaptchaBuster(url_tmp_pic)

    def _pre_process_captcha(self):
        """
        Scan the original image from top to bottom moving left to right and
            check if the pixel at location is below color value 10. If the pixel
            at location is within the threshold, write the pixel to the temp file.
        """
        for y in range(self.original.size[1]):
            for x in range(self.original.size[0]):
                pixel = self.original.getpixel((x, y))
                if pixel < 10:
                    self.processed_captcha.putpixel((x, y), 0)
        self.processed_captcha.save(self.temp_file, 'gif')

    def _crop_partitions(self):
        """
        Find discrete partitions within the preprocessed captcha
        to find regions containing individual characters. Returns an accumulated
        list of (start, end) coordinates for this region. Partitions are
        calculated based on the existence of characters within the image, as
        determined by surrounding white space.
        :return:
        """
        letters = []
        in_letter = False
        found_letter = False
        start, end = 0, 0

        for y in range(self.processed_captcha.size[0]):
            for x in range(self.processed_captcha.size[1]):
                pixel = self.processed_captcha.getpixel((y, x))
                if not pixel:
                    in_letter = True

            if not found_letter and in_letter:
                found_letter = True
                start = y

            if found_letter and not in_letter:
                found_letter = False
                end = y
                letters.append((start, end))

            in_letter = False

        count = 0
        for start, end in letters:
            crop_box = (start, 0, end, self.processed_captcha.size[1])
            part = self.processed_captcha.crop(crop_box)

            # If the segment of the image is too small to be a letter, ignore
            # the segment
            if part.size[0] < 15:
                pass
            else:
                part.save(self.image_segment_files[count], 'gif')
                self.image_segments.append(part)
                count += 1

    def _guess_characters(self):
        captcha = []
        for segment in self.image_segments:
            guess = []
            for letter in 'abcdefghijklmnopqrstuvwxyz':
                letter_dir = os.path.join(ICON_LOC, letter)
                for img in os.listdir(letter_dir):
                    if img != 'Thumbs.db' and img != '.gitignore':
                        tmp = Image.open(os.path.join(letter_dir, img))
                        s = segment.resize(tmp.size)
                        guess.append((self.relation(self.build_vector(tmp),
                                                    self.build_vector(s)), letter))
            guess.sort(reverse=True)
            captcha.append(guess)
        return captcha

    @classmethod
    def build_vector(cls, img):
        d1 = {}
        for count, i in enumerate(img.getdata()):
            d1[count] = i
        return d1

    @classmethod
    def relation(cls, concordance1, concordance2):
        """

        """
        relevance = 0
        for word, count in concordance1.iteritems():
            if word in concordance2 and count == concordance2[word]:
                if not count:
                    relevance += 5 if not count else 1
        return float(relevance)/float(len(concordance2))

def test():
    for t in range(5):
        session = requests.Session()
        response = session.get('http://www.amazon.com/errors/validateCaptcha')
        soup = BeautifulSoup(response.content)
        with open('./%d_captcha.jpg' % t, 'wb') as f:
            f.write(session.get(soup.find('img').get('src')).content)

        cb = CaptchaBuster('./%d_captcha.jpg' % t)
        print 'Pass %d:' % t, cb.guess

class RobotMiddleware(object):

    def __init__(self, crawler):
        self.crawler = crawler
        self.logger = logging.getLogger(self.__class__.__name__)

    def process_request(self, request, spider):
        return

    def process_exception(self, request, exception, spider):
        return

    def process_response(self, request, response, spider):
        if not response.url.endswith('.jpg'):
            if response.xpath('//title/text()[contains(., "Robot Check")]'):
                self.crawler.stats.inc_value('robot_check')
                self.logger.warning('Robot Check')
                soup = BeautifulSoup(response.body)

                form = soup.find('form')

                # Generate the captcha buster object
                # cb = CaptchaBuster.from_url(form.find('img').get('src'))

                # Url to send the captcha verification request
                get_url = 'http://www.amazon.com' + form.get('action')

                # Get all input params from the form
                input_params = dict([(x.get('name'), x.get('value')) for x in form.findAll('input', {'type': 'hidden'})])

                meta = {'target_url': get_url,
                        'params': input_params,
                        'decode': True}
                meta.update(request.meta)
                # Set the field keywords param to the captcha buster's guess
                # input_params['field-keywords'] = cb.guess
                # self.logger.info('Captcha Value: %s' % input_params['field-keywords'])

                # return FormRequest(get_url, formdata=input_params, meta=request.meta)
                return Request(form.find('img').get('src'), meta=meta)
        else:
            params = request.meta.get('params')
            target_url = request.meta.get('target_url')
            url_tmp_pic = os.path.join(ROOT, '%s_tmp_captcha.jpg' % uuid.uuid4())
            with open(url_tmp_pic, 'wb') as f:
                f.write(response.body)
            cb = CaptchaBuster(url_tmp_pic)
            params['field-keywords'] = cb.guess
            self.logger.info('Captcha Value: %s' % params['field-keywords'])
            return FormRequest(target_url, formdata=params, meta=request.meta)

        return response

    @classmethod
    def from_crawler(cls, crawler):
        return cls(crawler)

if __name__ == '__main__':
    logging.basicConfig(level=10)
    test()
<|endoftext|>


<commit_before>import ConfigParser
import curses
import os
import pexpect
import pytest

from .. import menu_launcher
import test_env

def test_pathdirs():
    """ Gets path directory class from menu_launcher """
    path_dirs = menu_launcher.PathDirs()

def test_get_plugin_status():
    """ Test get_plugin_status function with valid and invalid directories """
    path_dirs = test_env.PathDirs()
    invalid_dirs = test_env.PathDirs(base_dir="/tmp/")
    menu_launcher.get_plugin_status(path_dirs)
    menu_launcher.get_plugin_status(invalid_dirs)

def test_run_plugins():
    """ Test get_run_plugins function with valid and invalid directories """
    # Prep
    os.system("cp modes.backup templates/modes.template")
    os.system("cp core.backup templates/core.template")

    path_dirs = test_env.PathDirs()
    invalid_dirs = test_env.PathDirs(base_dir="/tmp/")
    menu_launcher.run_plugins(path_dirs, "start")
    menu_launcher.run_plugins(invalid_dirs, "start")

    ### Visualization Test ###
    # Find modes.template
    os.system("touch "+path_dirs.template_dir+'modes.template')
    config = ConfigParser.RawConfigParser()
    config.read(path_dirs.template_dir+'modes.template')

    # Check for valid sections/options
    if not config.has_section("plugins"):
        config.add_section("plugins")
    config.set("plugins", "vis_test", "all")

    with open(path_dirs.template_dir+'modes.template', 'w') as f:
        config.write(f)

    # Test with one visualization plugin (filewalk)
    vis_test = "/vis_test"
    if not os.path.exists(path_dirs.vis_dir+vis_test):
        os.system("mkdir "+path_dirs.vis_dir+vis_test)

    menu_launcher.run_plugins(path_dirs, "start")

    # Cleanup
    os.system("rm -rf "+path_dirs.vis_dir+vis_test)
    os.system("cp modes.backup templates/modes.template")

    ### Collectors: Passive/Active Test ###
    # Find core.template
    os.system("touch "+path_dirs.template_dir+'core.template')
    config = ConfigParser.RawConfigParser()
    config.read(path_dirs.template_dir+'core.template')

    # Check for valid sections/options
    config.set("local-collection", "passive", "on")
    config.set("local-collection", "active", "on")

    with open(path_dirs.template_dir+'core.template', 'w') as f:
        config.write(f)

    # Test with one passive/active collector.
    active = "/active-test-collector"
    passive = "/passive-test-collector"

    if not os.path.exists(path_dirs.collectors_dir+active):
        os.system("mkdir "+path_dirs.collectors_dir+active)
    if not os.path.exists(path_dirs.collectors_dir+passive):
        os.system("mkdir "+path_dirs.collectors_dir+passive)

    menu_launcher.run_plugins(path_dirs, "start")

    # Cleanup
    os.system("rm -rf "+path_dirs.collectors_dir+active)
    os.system("rm -rf "+path_dirs.collectors_dir+active)
    os.system("cp core.backup templates/core.template")

    menu_launcher.run_plugins(path_dirs, "start")

def test_get_installed_plugin_repos():
    """ Test get_installed_plugin_repos function with valid and invalid directories """
    path_dirs = test_env.PathDirs()
    invalid_dirs = test_env.PathDirs(base_dir="/tmp/")
    menu_launcher.get_installed_plugin_repos(path_dirs, "INFO", "remove")
    menu_launcher.get_installed_plugin_repos(path_dirs, "INFO", "update")
    menu_launcher.get_installed_plugin_repos(path_dirs, "INFO", "")
    menu_launcher.get_installed_plugin_repos(invalid_dirs, "INFO", "remove")
    menu_launcher.get_installed_plugin_repos(invalid_dirs, "INFO", "update")
    menu_launcher.get_installed_plugin_repos(invalid_dirs, "INFO", "")

    # Test with installed plugins
    url = "https://github.com/CyberReboot/vent-plugins.git"
    env = test_env.TestEnv()
    env.add_plugin(path_dirs, url)
    menu_launcher.get_installed_plugin_repos(path_dirs, "INFO", "remove")
    menu_launcher.get_installed_plugin_repos(path_dirs, "INFO", "update")
    menu_launcher.get_installed_plugin_repos(path_dirs, "INFO", "")
    env.remove_plugin(path_dirs, url)

def test_update_plugins():
    """ Test update_plugins function with valid and invalid directories """
    path_dirs = test_env.PathDirs()
    invalid_dirs = test_env.PathDirs(base_dir="/tmp/")
    menu_launcher.update_plugins(path_dirs)
    menu_launcher.update_plugins(invalid_dirs)

def test_build_menu_dict():
    """ Test build_menu_dict """
    path_dirs = test_env.PathDirs()
    menu_launcher.build_menu_dict(path_dirs)

def test_get_container_menu():
    """test get_container_menu"""
    path_dirs = test_env.PathDirs()
    menu_launcher.get_container_menu(path_dirs)

def test_get_namespace_menu():
    """test get_namespace_menu"""
    path_dirs = test_env.PathDirs()
    menu_launcher.get_namespace_menu(path_dirs)

def test_running_menu():
    """ test running the actual menu """
    cmd_invalid_path = "python2.7 menu_launcher.py "
    child0 = pexpect.spawn(cmd_invalid_path)
    # expect main menu
    child0.expect('Exit')
    # go to mode
    child0.sendline('1')
    child0.expect('Return to Vent menu')
    # go to main menu
    child0.sendline('6')
    child0.expect('Exit')
    # exit
    child0.sendline('7')
    child0.read()
    child0.close()

    path_dirs = test_env.PathDirs()
    cmd = "python2.7 menu_launcher.py "+path_dirs.base_dir+" "+path_dirs.info_dir+" "+path_dirs.data_dir
    invalid_url = "https://thisisinvalid-.git"
    child = pexpect.spawn(cmd)
    # expect main menu
    child.expect('Exit')
    ### Mode Menu ###
    # go to mode
    child.sendline('1')
    child.expect('Return to Vent menu')
    # go to start
    child.sendline('1')
    child.expect('Return to Mode menu')
    # return to mode
    child.sendline('2')
    child.expect('Return to Vent menu')
    # go to stop
    child.sendline('2')
    child.expect('Return to Mode menu')
    # return to mode
    child.sendline('2')
    child.expect('Return to Vent menu')
    # go to clean
    child.sendline('3')
    child.expect('Return to Mode menu')
    # return to mode
    child.sendline('2')
    child.expect('Return to Vent menu')
    # go to status
    child.sendline('4')
    child.expect('Return to Mode menu')
    # return to mode
    child.sendline('7')
    child.expect('Return to Vent menu')
    # go to configure
    child.sendline('5')
    child.expect('Return to Mode menu')
    # return to mode
    child.sendline('5')
    child.expect('Return to Vent menu')
    # return to main menu
    child.sendline('6')
    child.expect('Exit')

    ### Plugins Menu ###
    # go to plugins menu
    child.sendline('2')
    child.expect('Return to Vent menu')
    # add plugin
    child.sendline('1')
    # send url
    child.sendline(invalid_url)
    child.expect('Operation complete. Press any key to continue...')
    # press a key
    # go to plugins menu
    child.send('q')
    child.expect('Exit')
    # go to plugins menu
    child.sendline('2')
    child.expect('Return to Vent menu')
    # go to remove plugin
    child.sendline('2')
    child.expect('Return to Plugins menu')
    # remove plugin
    child.sendline('1')
    child.expect('Return to Vent menu')
    # go to install plugins
    child.sendline('3')
    child.expect('Return to Plugins menu')
    # go to plugins menu
    child.sendline('2')
    child.expect('Return to Vent menu')
    # go to update plugins
    child.sendline('4')
    child.expect('Return to Plugins menu')
    # go to plugins menu
    child.sendline('2')
    child.expect('Return to Vent menu')
    # go to main menu
    child.sendline('5')
    child.expect('Exit')

    ### System Info Menu ###
    # go to System Info Menu
    child.sendline('3')
    child.expect('Return to Vent menu')
    # go to Container Stats
    child.sendline('1')
    child.expect('CONTAINER')
    # return to System Info Menu
    child.sendcontrol('c')
    child.expect('Return to Vent menu')
    # return to Main Menu
    child.sendline('9')
    child.expect('Exit')

    ### Build Menu ###
    # go to Build menu
    child.sendline('4')
    child.expect('Return to Vent menu')
    # Build new plugins and core
    child.sendline('1')
    child.expect('Operation complete. Press any key to continue...')
    # Send confirmation
    child.send('q')
    child.expect('Return to Vent menu')
    # !! TODO - Test Force Rebuild
    # return to Main Menu
    child.sendline('3')
    child.expect('Exit')

    ### System Commands Menu ###
    # go to system commands
    child.sendline('5')
    child.expect('Return to Vent menu')
    # go to logs menu
    child.sendline('1')
    child.expect('Return to System Commands menu')
    # go to containers menu
    child.sendline('1')
    child.expect('Return to Logs menu')
    # return to logs menu
    child.sendline('5')
    child.expect('Return to System Commands menu')
    # go to namespaces menu
    child.sendline('2')
    child.expect('Return to Logs menu')
    # return to logs menu
    child.sendline('3')
    child.expect('Return to System Commands menu')
    # return to system commands menu
    child.sendline('5')
    child.expect('Return to Vent menu')
    # go to main menu
    child.sendline('5')
    child.expect('Exit')

    ### Help ###
    # go to Help
    child.sendline('6')
    child.expect('getting started')
    # return to Main Menu
    child.send('q')
    child.expect('Exit')

    # exit
    child.sendline('7')
    child.read()
    child.close()
    # TODO finish going through menu actions

def test_running_add_plugin():
    """ testing running the menu and adding a plugin """
    path_dirs = test_env.PathDirs()
    cmd = "python2.7 menu_launcher.py "+path_dirs.base_dir+" "+path_dirs.info_dir+" "+path_dirs.data_dir
    child1 = pexpect.spawn(cmd)
    child1.timeout = 600
    ### Plugins Menu ###
    # go to plugins menu
    child1.sendline('2')
    child1.expect('Return to Vent menu')
    # add plugin
    child1.sendline('1')
    # send url
    child1.sendline("https://github.com/CyberReboot/vent-plugins.git")
    child1.expect('Press any key to continue...')
    # press a key
    # go to plugins menu
    child1.send('q')
    child1.expect('Exit')
    child1.sendline('7')
    child1.read()
    child1.close()

def test_running_remove_plugin():
    """ testing running the menu and removing a plugin """
    path_dirs = test_env.PathDirs()
    cmd = "python2.7 menu_launcher.py "+path_dirs.base_dir+" "+path_dirs.info_dir+" "+path_dirs.data_dir
    child1 = pexpect.spawn(cmd)
    ### Plugins Menu ###
    # go to plugins menu
    child1.sendline('2')
    child1.expect('Return to Vent menu')
    # remove plugin menu
    child1.sendline('2')
    child1.expect('Return to Plugins menu')
    # remove plugin
    child1.sendline('1')
    child1.expect('Press any key to continue...')
    # press a key
    # go to plugins menu
    child1.send('q')
    child1.expect('Return to Vent menu')
    # go to main menu
    child1.sendline('5')
    child1.expect('Exit')
    child1.sendline('7')
    child1.read()
    child1.close()

def test_running_configure_template():
    """ testing running the menu and configuring a template file """
    path_dirs = test_env.PathDirs()
    cmd = "python2.7 menu_launcher.py "+path_dirs.base_dir+" "+path_dirs.info_dir+" "+path_dirs.data_dir
    child1 = pexpect.spawn(cmd)
    # go to modes menu
    child1.sendline('1')
    child1.expect('Return to Vent menu')
    # go to configure menu
    child1.sendline('5')
    child1.expect('Return to Mode menu')
    # configure a template
    child1.sendline('1')
    child1.expect('Suplemon')
    # quit out of editing the template
    child1.sendcontrol('q')
    child1.expect('Return to Mode menu')
    # return to modes menu
    child1.sendline('6')
    child1.expect('Return to Vent menu')
    # go to main menu
    child1.sendline('6')
    child1.expect('Exit')
    child1.sendline('7')
    child1.read()
    child1.close()
<commit_msg>pexpect is being cagey considering lag in container creation. Trying to use up arrow.<commit_after>import ConfigParser
import curses
import os
import pexpect
import pytest

from .. import menu_launcher
import test_env

def test_pathdirs():
    """ Gets path directory class from menu_launcher """
    path_dirs = menu_launcher.PathDirs()

def test_get_plugin_status():
    """ Test get_plugin_status function with valid and invalid directories """
    path_dirs = test_env.PathDirs()
    invalid_dirs = test_env.PathDirs(base_dir="/tmp/")
    menu_launcher.get_plugin_status(path_dirs)
    menu_launcher.get_plugin_status(invalid_dirs)

def test_run_plugins():
    """ Test get_run_plugins function with valid and invalid directories """
    # Prep
    os.system("cp modes.backup templates/modes.template")
    os.system("cp core.backup templates/core.template")

    path_dirs = test_env.PathDirs()
    invalid_dirs = test_env.PathDirs(base_dir="/tmp/")
    menu_launcher.run_plugins(path_dirs, "start")
    menu_launcher.run_plugins(invalid_dirs, "start")

    ### Visualization Test ###
    # Find modes.template
    os.system("touch "+path_dirs.template_dir+'modes.template')
    config = ConfigParser.RawConfigParser()
    config.read(path_dirs.template_dir+'modes.template')

    # Check for valid sections/options
    if not config.has_section("plugins"):
        config.add_section("plugins")
    config.set("plugins", "vis_test", "all")

    with open(path_dirs.template_dir+'modes.template', 'w') as f:
        config.write(f)

    # Test with one visualization plugin (filewalk)
    vis_test = "/vis_test"
    if not os.path.exists(path_dirs.vis_dir+vis_test):
        os.system("mkdir "+path_dirs.vis_dir+vis_test)

    menu_launcher.run_plugins(path_dirs, "start")

    # Cleanup
    os.system("rm -rf "+path_dirs.vis_dir+vis_test)
    os.system("cp modes.backup templates/modes.template")

    ### Collectors: Passive/Active Test ###
    # Find core.template
    os.system("touch "+path_dirs.template_dir+'core.template')
    config = ConfigParser.RawConfigParser()
    config.read(path_dirs.template_dir+'core.template')

    # Check for valid sections/options
    config.set("local-collection", "passive", "on")
    config.set("local-collection", "active", "on")

    with open(path_dirs.template_dir+'core.template', 'w') as f:
        config.write(f)

    # Test with one passive/active collector.
    active = "/active-test-collector"
    passive = "/passive-test-collector"

    if not os.path.exists(path_dirs.collectors_dir+active):
        os.system("mkdir "+path_dirs.collectors_dir+active)
    if not os.path.exists(path_dirs.collectors_dir+passive):
        os.system("mkdir "+path_dirs.collectors_dir+passive)

    menu_launcher.run_plugins(path_dirs, "start")

    # Cleanup
    os.system("rm -rf "+path_dirs.collectors_dir+active)
    os.system("rm -rf "+path_dirs.collectors_dir+active)
    os.system("cp core.backup templates/core.template")

    menu_launcher.run_plugins(path_dirs, "start")

def test_get_installed_plugin_repos():
    """ Test get_installed_plugin_repos function with valid and invalid directories """
    path_dirs = test_env.PathDirs()
    invalid_dirs = test_env.PathDirs(base_dir="/tmp/")
    menu_launcher.get_installed_plugin_repos(path_dirs, "INFO", "remove")
    menu_launcher.get_installed_plugin_repos(path_dirs, "INFO", "update")
    menu_launcher.get_installed_plugin_repos(path_dirs, "INFO", "")
    menu_launcher.get_installed_plugin_repos(invalid_dirs, "INFO", "remove")
    menu_launcher.get_installed_plugin_repos(invalid_dirs, "INFO", "update")
    menu_launcher.get_installed_plugin_repos(invalid_dirs, "INFO", "")

    # Test with installed plugins
    url = "https://github.com/CyberReboot/vent-plugins.git"
    env = test_env.TestEnv()
    env.add_plugin(path_dirs, url)
    menu_launcher.get_installed_plugin_repos(path_dirs, "INFO", "remove")
    menu_launcher.get_installed_plugin_repos(path_dirs, "INFO", "update")
    menu_launcher.get_installed_plugin_repos(path_dirs, "INFO", "")
    env.remove_plugin(path_dirs, url)

def test_update_plugins():
    """ Test update_plugins function with valid and invalid directories """
    path_dirs = test_env.PathDirs()
    invalid_dirs = test_env.PathDirs(base_dir="/tmp/")
    menu_launcher.update_plugins(path_dirs)
    menu_launcher.update_plugins(invalid_dirs)

def test_build_menu_dict():
    """ Test build_menu_dict """
    path_dirs = test_env.PathDirs()
    menu_launcher.build_menu_dict(path_dirs)

def test_get_container_menu():
    """test get_container_menu"""
    path_dirs = test_env.PathDirs()
    menu_launcher.get_container_menu(path_dirs)

def test_get_namespace_menu():
    """test get_namespace_menu"""
    path_dirs = test_env.PathDirs()
    menu_launcher.get_namespace_menu(path_dirs)

def test_running_menu():
    """ test running the actual menu """
    cmd_invalid_path = "python2.7 menu_launcher.py "
    child0 = pexpect.spawn(cmd_invalid_path)
    # expect main menu
    child0.expect('Exit')
    # go to mode
    child0.sendline('1')
    child0.expect('Return to Vent menu')
    # go to main menu
    child0.sendline('6')
    child0.expect('Exit')
    # exit
    child0.sendline('7')
    child0.read()
    child0.close()

    path_dirs = test_env.PathDirs()
    cmd = "python2.7 menu_launcher.py "+path_dirs.base_dir+" "+path_dirs.info_dir+" "+path_dirs.data_dir
    invalid_url = "https://thisisinvalid-.git"
    child = pexpect.spawn(cmd)
    # expect main menu
    child.expect('Exit')
    ### Mode Menu ###
    # go to mode
    child.sendline('1')
    child.expect('Return to Vent menu')
    # go to start
    child.sendline('1')
    child.expect('Return to Mode menu')
    # return to mode
    child.sendline('2')
    child.expect('Return to Vent menu')
    # go to stop
    child.sendline('2')
    child.expect('Return to Mode menu')
    # return to mode
    child.sendline('2')
    child.expect('Return to Vent menu')
    # go to clean
    child.sendline('3')
    child.expect('Return to Mode menu')
    # return to mode
    child.sendline('2')
    child.expect('Return to Vent menu')
    # go to status
    child.sendline('4')
    child.expect('Return to Mode menu')
    # return to mode
    child.sendline('7')
    child.expect('Return to Vent menu')
    # go to configure
    child.sendline('5')
    child.expect('Return to Mode menu')
    # return to mode
    child.sendline('5')
    child.expect('Return to Vent menu')
    # return to main menu
    child.sendline('6')
    child.expect('Exit')

    ### Plugins Menu ###
    # go to plugins menu
    child.sendline('2')
    child.expect('Return to Vent menu')
    # add plugin
    child.sendline('1')
    # send url
    child.sendline(invalid_url)
    child.expect('Operation complete. Press any key to continue...')
    # press a key
    # go to plugins menu
    child.send('q')
    child.expect('Exit')
    # go to plugins menu
    child.sendline('2')
    child.expect('Return to Vent menu')
    # go to remove plugin
    child.sendline('2')
    child.expect('Return to Plugins menu')
    # remove plugin
    child.sendline('1')
    child.expect('Return to Vent menu')
    # go to install plugins
    child.sendline('3')
    child.expect('Return to Plugins menu')
    # go to plugins menu
    child.sendline('2')
    child.expect('Return to Vent menu')
    # go to update plugins
    child.sendline('4')
    child.expect('Return to Plugins menu')
    # go to plugins menu
    child.sendline('2')
    child.expect('Return to Vent menu')
    # go to main menu
    child.sendline('5')
    child.expect('Exit')

    ### System Info Menu ###
    # go to System Info Menu
    child.sendline('3')
    child.expect('Return to Vent menu')
    # go to Container Stats
    child.sendline('1')
    child.expect('CONTAINER')
    # return to System Info Menu
    child.sendcontrol('c')
    child.expect('Return to Vent menu')
    # return to Main Menu
    child.sendline('9')
    child.expect('Exit')

    ### Build Menu ###
    # go to Build menu
    child.sendline('4')
    child.expect('Return to Vent menu')
    # Build new plugins and core
    child.sendline('1')
    child.expect('Operation complete. Press any key to continue...')
    # Send confirmation
    child.send('q')
    child.expect('Return to Vent menu')
    # !! TODO - Test Force Rebuild
    # return to Main Menu
    child.sendline('3')
    child.expect('Exit')

    ### System Commands Menu ###
    # go to system commands
    child.sendline('5')
    child.expect('Return to Vent menu')
    # go to logs menu
    child.sendline('1')
    child.expect('Return to System Commands menu')
    # go to containers menu
    child.sendline('1')
    child.expect('Return to Logs menu')
    # return to logs menu
    child.send('\x1b[A')
    child.sendline()
    child.expect('Return to System Commands menu')
    # go to namespaces menu
    child.sendline('2')
    child.expect('Return to Logs menu')
    # return to logs menu
    child.sendline('3')
    child.expect('Return to System Commands menu')
    # return to system commands menu
    child.sendline('5')
    child.expect('Return to Vent menu')
    # go to main menu
    child.sendline('5')
    child.expect('Exit')

    ### Help ###
    # go to Help
    child.sendline('6')
    child.expect('getting started')
    # return to Main Menu
    child.send('q')
    child.expect('Exit')

    # exit
    child.sendline('7')
    child.read()
    child.close()
    # TODO finish going through menu actions

def test_running_add_plugin():
    """ testing running the menu and adding a plugin """
    path_dirs = test_env.PathDirs()
    cmd = "python2.7 menu_launcher.py "+path_dirs.base_dir+" "+path_dirs.info_dir+" "+path_dirs.data_dir
    child1 = pexpect.spawn(cmd)
    child1.timeout = 600
    ### Plugins Menu ###
    # go to plugins menu
    child1.sendline('2')
    child1.expect('Return to Vent menu')
    # add plugin
    child1.sendline('1')
    # send url
    child1.sendline("https://github.com/CyberReboot/vent-plugins.git")
    child1.expect('Press any key to continue...')
    # press a key
    # go to plugins menu
    child1.send('q')
    child1.expect('Exit')
    child1.sendline('7')
    child1.read()
    child1.close()

def test_running_remove_plugin():
    """ testing running the menu and removing a plugin """
    path_dirs = test_env.PathDirs()
    cmd = "python2.7 menu_launcher.py "+path_dirs.base_dir+" "+path_dirs.info_dir+" "+path_dirs.data_dir
    child1 = pexpect.spawn(cmd)
    ### Plugins Menu ###
    # go to plugins menu
    child1.sendline('2')
    child1.expect('Return to Vent menu')
    # remove plugin menu
    child1.sendline('2')
    child1.expect('Return to Plugins menu')
    # remove plugin
    child1.sendline('1')
    child1.expect('Press any key to continue...')
    # press a key
    # go to plugins menu
    child1.send('q')
    child1.expect('Return to Vent menu')
    # go to main menu
    child1.sendline('5')
    child1.expect('Exit')
    child1.sendline('7')
    child1.read()
    child1.close()

def test_running_configure_template():
    """ testing running the menu and configuring a template file """
    path_dirs = test_env.PathDirs()
    cmd = "python2.7 menu_launcher.py "+path_dirs.base_dir+" "+path_dirs.info_dir+" "+path_dirs.data_dir
    child1 = pexpect.spawn(cmd)
    # go to modes menu
    child1.sendline('1')
    child1.expect('Return to Vent menu')
    # go to configure menu
    child1.sendline('5')
    child1.expect('Return to Mode menu')
    # configure a template
    child1.sendline('1')
    child1.expect('Suplemon')
    # quit out of editing the template
    child1.sendcontrol('q')
    child1.expect('Return to Mode menu')
    # return to modes menu
    child1.sendline('6')
    child1.expect('Return to Vent menu')
    # go to main menu
    child1.sendline('6')
    child1.expect('Exit')
    child1.sendline('7')
    child1.read()
    child1.close()
<|endoftext|>


<commit_before>#!/usr/bin/env python
#
#  svnadmin_tests.py:  testing the 'svnadmin' tool.
#
#  Subversion is a tool for revision control.
#  See http://subversion.tigris.org for more information.
#
# ====================================================================
# Copyright (c) 2000-2001 CollabNet.  All rights reserved.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution.  The terms
# are also available at http://subversion.tigris.org/license-1.html.
# If newer versions of this license are posted there, you may use a
# newer version instead, at your option.
#
######################################################################

# General modules
import shutil, string, sys, re, os.path, traceback

# The `svntest' module
try:
  import svntest
except SyntaxError:
  sys.stderr.write('[SKIPPED] ')
  print "<<< Please make sure you have Python 2 or better! >>>"
  traceback.print_exc(None,sys.stdout)
  raise SystemExit

# (abbreviation)
path_index = svntest.actions.path_index

#----------------------------------------------------------------------

# How we currently test 'svnadmin' --
#
#   'svnadmin create':   Create an empty repository, test that the
#                        root node has a proper created-revision,
#                        because there was once a bug where it
#                        didn't.
#
#                        Note also that "svnadmin create" is tested
#                        implicitly every time we run a python test
#                        script.  (An empty repository is always
#                        created and then imported into;  if this
#                        subcommand failed catastrophically, every
#                        test would fail and we would know instantly.)
#
#   'svnadmin youngest': Just commit a couple of times and directly parse
#                        the printed number.
#
#   'svnadmin createtxn'
#   'svnadmin rmtxn':    See below.
#
#   'svnadmin lstxns':   We don't care about the contents of transactions;
#                        we only care that they exist or not.
#                        Therefore, we can simply parse transaction headers.
#
#   'svnadmin lsrevs':   Parse headers as above.
#
#  ### TODO:  someday maybe we could parse the contents of trees too.
#
######################################################################
# Helper routines

def get_trees(repo_dir, revision_p = 0):
  """Run 'svnadmin' on REPO_DIR, with a subcommand of either 'lstxns'
  or 'lsrevs', depending on the state of REVISION_P.  Return a list of
  strings that are either transaction names or revision numbers."""

  tree_names = []

  if revision_p:
    output_lines, errput_lines = svntest.main.run_svnadmin("lsrevs", repo_dir)
    rm = re.compile("^Revision\s+(.+)")
  else:
    output_lines, errput_lines = svntest.main.run_svnadmin("lstxns", repo_dir)
    rm = re.compile("^Txn\s+(.+):")

  for line in output_lines:
    match = rm.search(line)
    if match and match.groups():
      tree_names.append(match.group(1))

  return tree_names

######################################################################
# Tests
#
#   Each test must return 0 on success or non-zero on failure.

#----------------------------------------------------------------------

def test_create(sbox):
  "test 'svnadmin create' subcommand"

  if sbox.build():
    return 1

  wc_dir = sbox.wc_dir
  repo_dir = sbox.repo_dir

  url = svntest.main.test_area_url + '/' + repo_dir

  if os.path.exists(repo_dir):
    shutil.rmtree(repo_dir)

  if os.path.exists(wc_dir):
    shutil.rmtree(wc_dir)

  svntest.main.run_svnadmin ("create", repo_dir)

  stdout_lines, stderr_lines = \
                svntest.main.run_svn (None, "checkout", url, "-d", wc_dir)

  if len (stderr_lines) > 0:
    return 1

  return 0  # success

def test_youngest(sbox):
  "test 'svnadmin youngest' subcommand"

  if sbox.build():
    return 1

  wc_dir = sbox.wc_dir
  repo_dir = sbox.repo_dir

  # Make a couple of local mods to files
  mu_path = os.path.join(wc_dir, 'A', 'mu')
  rho_path = os.path.join(wc_dir, 'A', 'D', 'G', 'rho')
  svntest.main.file_append (mu_path, 'appended mu text')
  svntest.main.file_append (rho_path, 'new appended text for rho')

  # Created expected output tree for 'svn ci'
  output_list = [ [mu_path, None, {}, {'verb' : 'Sending' }],
                  [rho_path, None, {}, {'verb' : 'Sending' }] ]
  expected_output_tree = svntest.tree.build_generic_tree(output_list)

  # Create expected status tree; all local revisions should be at 1,
  # but mu and rho should be at revision 2.
  status_list = svntest.actions.get_virginal_status_list(wc_dir, '2')
  for item in status_list:
    if (item[0] != mu_path) and (item[0] != rho_path):
      item[3]['wc_rev'] = '1'
  expected_status_tree = svntest.tree.build_generic_tree(status_list)

  if svntest.actions.run_and_verify_commit (wc_dir,
                                            expected_output_tree,
                                            expected_status_tree,
                                            None,
                                            None, None,
                                            None, None,
                                            wc_dir):
    return 1

  # Youngest revision should now be 2.  Let's verify that.
  output, errput = svntest.main.run_svnadmin("youngest", repo_dir)

  if output[0] != "2\n":
    return 1

  return 0  # success

#----------------------------------------------------------------------

def create_txn(sbox):
  "test 'svnadmin createtxn' subcommand"

  if sbox.build():
    return 1

  wc_dir = sbox.wc_dir
  repo_dir = sbox.repo_dir

  # Make a transaction based on revision 1.
  output, errput = svntest.main.run_svnadmin("createtxn", repo_dir, "1")

  # Look for it by running 'lstxn'.
  tree_list = get_trees(repo_dir)
  if tree_list != ['1']:
    return 1

  return 0

#----------------------------------------------------------------------

def remove_txn(sbox):
  "test 'svnadmin rmtxns' subcommand"

  if sbox.build():
    return 1

  wc_dir = sbox.wc_dir
  repo_dir = sbox.repo_dir

  # Make three transactions based on revision 1.
  svntest.main.run_svnadmin("createtxn", repo_dir, "1")
  svntest.main.run_svnadmin("createtxn", repo_dir, "1")
  svntest.main.run_svnadmin("createtxn", repo_dir, "1")
  svntest.main.run_svnadmin("createtxn", repo_dir, "1")
  svntest.main.run_svnadmin("createtxn", repo_dir, "1")

  # Look for them by running 'lstxn'.
  tree_list = get_trees(repo_dir)
  if tree_list != ['1', '2', '3', '4', '5']:
    return 1

  # Remove the 2nd and 4th transactions.
  svntest.main.run_svnadmin("rmtxns", repo_dir, "2", "4")

  # Examine the list of transactions again.
  tree_list = get_trees(repo_dir)
  if tree_list != ['1', '3', '5']:
    return 1

  return 0

#----------------------------------------------------------------------

def list_revs(sbox):
  "test 'svnadmin lsrevs' subcommand"

  if sbox.build():
    return 1

  wc_dir = sbox.wc_dir
  repo_dir = sbox.repo_dir

  # Make a couple of local mods to files
  mu_path = os.path.join(wc_dir, 'A', 'mu')
  rho_path = os.path.join(wc_dir, 'A', 'D', 'G', 'rho')
  svntest.main.file_append (mu_path, 'appended mu text')
  svntest.main.file_append (rho_path, 'new appended text for rho')

  # Created expected output tree for 'svn ci'
  output_list = [ [mu_path, None, {}, {'verb' : 'Sending' }],
                  [rho_path, None, {}, {'verb' : 'Sending' }] ]
  expected_output_tree = svntest.tree.build_generic_tree(output_list)

  # Create expected status tree; all local revisions should be at 1,
  # but mu and rho should be at revision 2.
  status_list = svntest.actions.get_virginal_status_list(wc_dir, '2')
  for item in status_list:
    if (item[0] != mu_path) and (item[0] != rho_path):
      item[3]['wc_rev'] = '1'
  expected_status_tree = svntest.tree.build_generic_tree(status_list)

  # Commit, and create revision 2.
  if svntest.actions.run_and_verify_commit (wc_dir,
                                            expected_output_tree,
                                            expected_status_tree,
                                            None,
                                            None, None,
                                            None, None,
                                            wc_dir):
    return 1

  # Remove gamma
  gamma_path = os.path.join(wc_dir, 'A', 'D', 'gamma')
  svntest.main.run_svn(None, 'rm', gamma_path)

  # Created expected output tree for 'svn ci'
  output_list = [ [gamma_path, None, {}, {'verb' : 'Deleting' }] ]
  expected_output_tree = svntest.tree.build_generic_tree(output_list)

  # Create expected status tree
  status_list = svntest.actions.get_virginal_status_list(wc_dir, '3')
  for item in status_list:
    item[3]['wc_rev'] = '1'
    if (item[0] == mu_path) or (item[0] == rho_path):
      item[3]['wc_rev'] = '2'
  status_list.pop(path_index(status_list, gamma_path))
  expected_status_tree = svntest.tree.build_generic_tree(status_list)

  # Commit, and create revision 3.
  if svntest.actions.run_and_verify_commit (wc_dir,
                                            expected_output_tree,
                                            expected_status_tree,
                                            None,
                                            None, None,
                                            None, None,
                                            wc_dir):
    return 1

  # Now fetch all revisions from the repository.
  tree_list = get_trees(repo_dir, 1)
  if tree_list != ['0', '1', '2', '3']:
    return 1

  return 0

########################################################################
# Run the tests

# list all tests here, starting with None:
test_list = [ None,
              test_create,
              test_youngest,
              create_txn,
              remove_txn,
              list_revs
             ]

if __name__ == '__main__':
  svntest.main.run_tests(test_list)
  # NOTREACHED

### End of file.
# local variables:
# eval: (load-file "../../../svn-dev.el")
# end:
<commit_msg><commit_after>#!/usr/bin/env python
#
#  svnadmin_tests.py:  testing the 'svnadmin' tool.
#
#  Subversion is a tool for revision control.
#  See http://subversion.tigris.org for more information.
#
# ====================================================================
# Copyright (c) 2000-2001 CollabNet.  All rights reserved.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution.  The terms
# are also available at http://subversion.tigris.org/license-1.html.
# If newer versions of this license are posted there, you may use a
# newer version instead, at your option.
#
######################################################################

# General modules
import shutil, string, sys, re, os.path, traceback

# The `svntest' module
try:
  import svntest
except SyntaxError:
  sys.stderr.write('[SKIPPED] ')
  print "<<< Please make sure you have Python 2 or better! >>>"
  traceback.print_exc(None,sys.stdout)
  raise SystemExit

# (abbreviation)
path_index = svntest.actions.path_index

#----------------------------------------------------------------------

# How we currently test 'svnadmin' --
#
#   'svnadmin create':   Create an empty repository, test that the
#                        root node has a proper created-revision,
#                        because there was once a bug where it
#                        didn't.
#
#                        Note also that "svnadmin create" is tested
#                        implicitly every time we run a python test
#                        script.  (An empty repository is always
#                        created and then imported into;  if this
#                        subcommand failed catastrophically, every
#                        test would fail and we would know instantly.)
#
#   'svnadmin youngest': Just commit a couple of times and directly parse
#                        the printed number.
#
#   'svnadmin createtxn'
#   'svnadmin rmtxn':    See below.
#
#   'svnadmin lstxns':   We don't care about the contents of transactions;
#                        we only care that they exist or not.
#                        Therefore, we can simply parse transaction headers.
#
#   'svnadmin lsrevs':   Parse headers as above.
#
#  ### TODO:  someday maybe we could parse the contents of trees too.
#
######################################################################
# Helper routines

def get_trees(repo_dir, revision_p = 0):
  """Run 'svnadmin' on REPO_DIR, with a subcommand of either 'lstxns'
  or 'lsrevs', depending on the state of REVISION_P.  Return a list of
  strings that are either transaction names or revision numbers."""

  tree_names = []

  if revision_p:
    output_lines, errput_lines = svntest.main.run_svnadmin("lsrevs", repo_dir)
    rm = re.compile("^Revision\s+(.+)")
  else:
    output_lines, errput_lines = svntest.main.run_svnadmin("lstxns", repo_dir)
    rm = re.compile("^Txn\s+(.+):")

  for line in output_lines:
    match = rm.search(line)
    if match and match.groups():
      tree_names.append(match.group(1))

  return tree_names

######################################################################
# Tests
#
#   Each test must return 0 on success or non-zero on failure.

#----------------------------------------------------------------------

def test_create(sbox):
  "test 'svnadmin create' subcommand"

  # This call tests the creation of repository.
  # It also imports to the repository, and checks out from the repository.
  if sbox.build():
    return 1

  # ### TODO: someday this test should create an empty repo, checkout,
  # ### and then look at the CR value using 'svn st -v'.  We're not
  # ### parsing that value in our status regexp yet anyway.

  return 0  # success

def test_youngest(sbox):
  "test 'svnadmin youngest' subcommand"

  if sbox.build():
    return 1

  wc_dir = sbox.wc_dir
  repo_dir = sbox.repo_dir

  # Make a couple of local mods to files
  mu_path = os.path.join(wc_dir, 'A', 'mu')
  rho_path = os.path.join(wc_dir, 'A', 'D', 'G', 'rho')
  svntest.main.file_append (mu_path, 'appended mu text')
  svntest.main.file_append (rho_path, 'new appended text for rho')

  # Created expected output tree for 'svn ci'
  output_list = [ [mu_path, None, {}, {'verb' : 'Sending' }],
                  [rho_path, None, {}, {'verb' : 'Sending' }] ]
  expected_output_tree = svntest.tree.build_generic_tree(output_list)

  # Create expected status tree; all local revisions should be at 1,
  # but mu and rho should be at revision 2.
  status_list = svntest.actions.get_virginal_status_list(wc_dir, '2')
  for item in status_list:
    if (item[0] != mu_path) and (item[0] != rho_path):
      item[3]['wc_rev'] = '1'
  expected_status_tree = svntest.tree.build_generic_tree(status_list)

  if svntest.actions.run_and_verify_commit (wc_dir,
                                            expected_output_tree,
                                            expected_status_tree,
                                            None,
                                            None, None,
                                            None, None,
                                            wc_dir):
    return 1

  # Youngest revision should now be 2.  Let's verify that.
  output, errput = svntest.main.run_svnadmin("youngest", repo_dir)

  if output[0] != "2\n":
    return 1

  return 0  # success

#----------------------------------------------------------------------

def create_txn(sbox):
  "test 'svnadmin createtxn' subcommand"

  if sbox.build():
    return 1

  wc_dir = sbox.wc_dir
  repo_dir = sbox.repo_dir

  # Make a transaction based on revision 1.
  output, errput = svntest.main.run_svnadmin("createtxn", repo_dir, "1")

  # Look for it by running 'lstxn'.
  tree_list = get_trees(repo_dir)
  if tree_list != ['1']:
    return 1

  return 0

#----------------------------------------------------------------------

def remove_txn(sbox):
  "test 'svnadmin rmtxns' subcommand"

  if sbox.build():
    return 1

  wc_dir = sbox.wc_dir
  repo_dir = sbox.repo_dir

  # Make three transactions based on revision 1.
  svntest.main.run_svnadmin("createtxn", repo_dir, "1")
  svntest.main.run_svnadmin("createtxn", repo_dir, "1")
  svntest.main.run_svnadmin("createtxn", repo_dir, "1")
  svntest.main.run_svnadmin("createtxn", repo_dir, "1")
  svntest.main.run_svnadmin("createtxn", repo_dir, "1")

  # Look for them by running 'lstxn'.
  tree_list = get_trees(repo_dir)
  if tree_list != ['1', '2', '3', '4', '5']:
    return 1

  # Remove the 2nd and 4th transactions.
  svntest.main.run_svnadmin("rmtxns", repo_dir, "2", "4")

  # Examine the list of transactions again.
  tree_list = get_trees(repo_dir)
  if tree_list != ['1', '3', '5']:
    return 1

  return 0

#----------------------------------------------------------------------

def list_revs(sbox):
  "test 'svnadmin lsrevs' subcommand"

  if sbox.build():
    return 1

  wc_dir = sbox.wc_dir
  repo_dir = sbox.repo_dir

  # Make a couple of local mods to files
  mu_path = os.path.join(wc_dir, 'A', 'mu')
  rho_path = os.path.join(wc_dir, 'A', 'D', 'G', 'rho')
  svntest.main.file_append (mu_path, 'appended mu text')
  svntest.main.file_append (rho_path, 'new appended text for rho')

  # Created expected output tree for 'svn ci'
  output_list = [ [mu_path, None, {}, {'verb' : 'Sending' }],
                  [rho_path, None, {}, {'verb' : 'Sending' }] ]
  expected_output_tree = svntest.tree.build_generic_tree(output_list)

  # Create expected status tree; all local revisions should be at 1,
  # but mu and rho should be at revision 2.
  status_list = svntest.actions.get_virginal_status_list(wc_dir, '2')
  for item in status_list:
    if (item[0] != mu_path) and (item[0] != rho_path):
      item[3]['wc_rev'] = '1'
  expected_status_tree = svntest.tree.build_generic_tree(status_list)

  # Commit, and create revision 2.
  if svntest.actions.run_and_verify_commit (wc_dir,
                                            expected_output_tree,
                                            expected_status_tree,
                                            None,
                                            None, None,
                                            None, None,
                                            wc_dir):
    return 1

  # Remove gamma
  gamma_path = os.path.join(wc_dir, 'A', 'D', 'gamma')
  svntest.main.run_svn(None, 'rm', gamma_path)

  # Created expected output tree for 'svn ci'
  output_list = [ [gamma_path, None, {}, {'verb' : 'Deleting' }] ]
  expected_output_tree = svntest.tree.build_generic_tree(output_list)

  # Create expected status tree
  status_list = svntest.actions.get_virginal_status_list(wc_dir, '3')
  for item in status_list:
    item[3]['wc_rev'] = '1'
    if (item[0] == mu_path) or (item[0] == rho_path):
      item[3]['wc_rev'] = '2'
  status_list.pop(path_index(status_list, gamma_path))
  expected_status_tree = svntest.tree.build_generic_tree(status_list)

  # Commit, and create revision 3.
  if svntest.actions.run_and_verify_commit (wc_dir,
                                            expected_output_tree,
                                            expected_status_tree,
                                            None,
                                            None, None,
                                            None, None,
                                            wc_dir):
    return 1

  # Now fetch all revisions from the repository.
  tree_list = get_trees(repo_dir, 1)
  if tree_list != ['0', '1', '2', '3']:
    return 1

  return 0

########################################################################
# Run the tests

# list all tests here, starting with None:
test_list = [ None,
              test_create,
              test_youngest,
              create_txn,
              remove_txn,
              list_revs
             ]

if __name__ == '__main__':
  svntest.main.run_tests(test_list)
  # NOTREACHED

### End of file.
# local variables:
# eval: (load-file "../../../svn-dev.el")
# end:
<|endoftext|>