Skip to content

Commit

Permalink
ENH: Preliminary support for module runner specification
Browse files Browse the repository at this point in the history
This commit implements a prefix syntax in the module specification which
allows pelitagame to automatically start a player with a specific
executable. Currently, only Python executables are hard-coded but at
some point this may be made more flexible.

Example:

  $ python3 pelitagame py2@old_school_player py3@modern_player

This starts pelitagame in Python 3 mode which in turn runs subprocesses
of Python 2 and Python 3 for the individual modules.

If no prefix is given, this defaults to py@, which specifies the same
Python interpreter that was used to run pelitagame itself.
  • Loading branch information
Debilski committed Aug 31, 2015
1 parent c74e3f3 commit 18a74ac
Showing 1 changed file with 49 additions and 7 deletions.
56 changes: 49 additions & 7 deletions pelitagame
Expand Up @@ -125,20 +125,62 @@ def run_external_viewer(subscribe_sock, controller, geometry, delay):
tkviewer = os.path.join(os.path.dirname(sys.argv[0]), "tkviewer.py")
return subprocess.Popen([get_python_process(), tkviewer] + viewer_args)

class ModuleRunner(object):
def __init__(self, team_spec):
self.team_spec = team_spec

class DefaultRunner(ModuleRunner):
def run(self, addr):
player_path = os.path.dirname(sys.argv[0])
player = os.path.join(player_path, "module_player.py")
return subprocess.Popen([get_python_process(),
player,
self.team_spec,
addr])

class Py2Runner(ModuleRunner):
def run(self, addr):
player_path = os.path.dirname(sys.argv[0])
player = os.path.join(player_path, "module_player.py")
return subprocess.Popen(["python2",
player,
self.team_spec,
addr])

class Py3Runner(ModuleRunner):
def run(self, addr):
player_path = os.path.dirname(sys.argv[0])
player = os.path.join(player_path, "module_player.py")
return subprocess.Popen(["python3",
player,
self.team_spec,
addr])


def call_standalone_pelitagame(team_spec, addr):
""" Starts another process with the same Python executable,
the same start script (pelitagame) and runs `team_spec`
as a standalone client on URL `addr`.
"""
player_path = os.path.dirname(sys.argv[0])
player = os.path.join(player_path, "module_player.py")
return subprocess.Popen([get_python_process(),
player,
team_spec,
addr])

defined_runners = {
"py": DefaultRunner,
"py2": Py2Runner,
"py3": Py3Runner
}

if "@" in team_spec:
try:
runner_, spec = team_spec.split("@")
runner = defined_runners[runner_]
except ValueError:
raise ValueError("Bad spec: {}.".format(team_spec))
except KeyError:
raise ValueError("Unknown runner: {}:".format(runner))
else:
runner = DefaultRunner
spec = team_spec

return runner(spec).run(addr)

def prepare_team(team_spec):
# check if we've been given an address which a remote
Expand Down

0 comments on commit 18a74ac

Please sign in to comment.