def set_player_subprocess(self, colour, command, check_protocol_version=True, **kwargs): """Specify the a player as a subprocess. command -- list of strings (as for subprocess.Popen) check_protocol_version -- bool (default True) Additional keyword arguments are passed to the Subprocess_gtp_channel constructor. If check_protocol_version is true, rejects an engine that declares a GTP protocol version <> 2. Propagates GtpChannelError if there's an error creating the subprocess or checking the protocol version. """ try: channel = gtp_controller.Subprocess_gtp_channel(command, **kwargs) except GtpChannelError, e: raise GtpChannelError( "error starting subprocess for player %s:\n%s" % (self.players[colour], e))
def check_player(player_check, discard_stderr=False): """Do a test run of a GTP engine. player_check -- Player_check object This starts an engine subprocess, sends it some GTP commands, and ends the process again. Raises CheckFailed if the player doesn't pass the checks. Returns a list of warning messages. Currently checks: - any explicitly specified cwd exists and is a directory - the engine subprocess starts, and replies to GTP commands - the engine reports protocol version 2 (if it supports protocol_version) - the engine accepts any startup_gtp_commands - the engine accepts the specified board size and komi - the engine accepts the 'clear_board' command - the engine accepts 'quit' and closes down cleanly """ player = player_check.player if player.cwd is not None and not os.path.isdir(player.cwd): raise CheckFailed("bad working directory: %s" % player.cwd) if discard_stderr: stderr = open(os.devnull, "w") else: stderr = None try: env = player.make_environ() env['GOMILL_GAME_ID'] = 'startup-check' try: channel = gtp_controller.Subprocess_gtp_channel(player.cmd_args, env=env, cwd=player.cwd, stderr=stderr) except GtpChannelError, e: raise GtpChannelError("error starting subprocess for %s:\n%s" % (player.code, e)) controller = gtp_controller.Gtp_controller(channel, player.code) controller.set_gtp_aliases(player.gtp_aliases) controller.check_protocol_version() for command, arguments in player.startup_gtp_commands: controller.do_command(command, *arguments) controller.do_command("boardsize", str(player_check.board_size)) controller.do_command("clear_board") controller.do_command("komi", str(player_check.komi)) controller.safe_close()
def __init__(self, command, stderr=None, cwd=None, env=None): self.requested_command = command self.requested_stderr = stderr self.requested_cwd = cwd self.requested_env = env self.id = None engine = None callbacks = [] for arg in command[1:]: key, eq, value = arg.partition("=") if not eq: raise SupporterError("Mock_subprocess_gtp_channel: " "bad command-line argument: %s" % arg) if key == 'id': self.id = value self.channels[value] = self elif key == 'engine': try: engine = self.engine_registry[value] except KeyError: raise SupporterError( "Mock_subprocess_gtp_channel: unregistered engine '%s'" % value) elif key == 'init': try: callbacks.append(self.callback_registry[value]) except KeyError: raise SupporterError( "Mock_subprocess_gtp_channel: unregistered init '%s'" % value) elif key == 'fail' and value == 'startup': raise GtpChannelError("exec forced to fail") else: raise SupporterError("Mock_subprocess_gtp_channel: " "bad command-line argument: %s" % arg) if engine is None: engine = get_test_player_engine() gtp_controller_test_support.Testing_gtp_channel.__init__(self, engine) for callback in callbacks: callback(self)