def initEngine(self, engine, color, lowPriority): name = engine['name'] protocol = engine["protocol"] protover = 2 if engine.get("protover") is None else engine.get( "protover") path = engine['command'] args = [] if engine.get('args') is None else [ a for a in engine['args'] ] if engine.get('vm_command') is not None: vmpath = engine['vm_command'] vmargs = [] if engine.get('vm_args') is None else [ a for a in engine['vm_args'] ] args = vmargs + [path] + args path = vmpath md5_engine = engine['md5'] working_directory = engine.get("workingDirectory") if working_directory: workdir = working_directory else: workdir = getEngineDataPrefix() warnwords = ("illegal", "error", "exception") try: subprocess = SubProcess(path, args=args, warnwords=warnwords, cwd=workdir, lowPriority=lowPriority) yield from subprocess.start() except OSError: raise PlayerIsDead except asyncio.TimeoutError: raise PlayerIsDead except GLib.GError: raise PlayerIsDead except Exception: raise PlayerIsDead engine_proc = attrToProtocol[protocol](subprocess, color, protover, md5_engine) engine_proc.setName(name) # If the user has configured special options for this engine, here is # where they should be set. def optionsCallback(set_option): if engine.get("options"): for option in engine["options"]: key = option["name"] value = option.get("value") if (value is not None) and option["default"] != value: if protocol == "xboard" and option["type"] == "check": value = int(bool(value)) set_option.setOption(key, value) engine_proc.connect("readyForOptions", optionsCallback) return engine_proc
def initEngine(self, engine, color, lowPriority): name = engine['name'] protocol = engine["protocol"] protover = 2 if engine.get("protover") is None else engine.get( "protover") path = engine['command'] args = [] if engine.get('args') is None else [a for a in engine['args']] if engine.get('vm_command') is not None: vmpath = engine['vm_command'] vmargs = [] if engine.get('vm_args') is None else [ a for a in engine['vm_args'] ] args = vmargs + [path] + args path = vmpath md5_engine = engine['md5'] working_directory = engine.get("workingDirectory") if working_directory: workdir = working_directory else: workdir = getEngineDataPrefix() warnwords = ("illegal", "error", "exception") try: subprocess = SubProcess(path, args=args, warnwords=warnwords, cwd=workdir, lowPriority=lowPriority) yield from subprocess.start() except OSError: raise PlayerIsDead except asyncio.TimeoutError: raise PlayerIsDead except GLib.GError: raise PlayerIsDead except Exception: raise PlayerIsDead engine_proc = attrToProtocol[protocol](subprocess, color, protover, md5_engine) engine_proc.setName(name) # If the user has configured special options for this engine, here is # where they should be set. def optionsCallback(set_option): if engine.get("options"): for option in engine["options"]: key = option["name"] value = option.get("value") if (value is not None) and option["default"] != value: if protocol == "xboard" and option["type"] == "check": value = int(bool(value)) set_option.setOption(key, value) engine_proc.connect("readyForOptions", optionsCallback) return engine_proc
class Pinger(GObject.GObject): """ The received signal contains the time it took to get response from the server in millisecconds. -1 means that some error occurred """ __gsignals__ = { "received": (GObject.SignalFlags.RUN_FIRST, None, (float, )), "error": (GObject.SignalFlags.RUN_FIRST, None, (str, )) } def __init__(self, host): GObject.GObject.__init__(self) self.host = host self.subproc = None self.expression = re.compile("=([\d\.]+) (m?s)") # We need untranslated error messages in regexp search # below, so have to use deferred translation here def _(msg): return msg error = _("Destination Host Unreachable") self.errorExprs = (re.compile("(%s)" % error), ) del _ self.restartsOnDead = 3 self.deadCount = 0 def start(self): assert not self.subproc if sys.platform == "win32": args = ["-t", self.host] else: args = ["-i10", self.host] self.subproc = SubProcess(shutil.which("ping"), args, env={"LANG": "en"}) create_task(self.subproc.start()) self.conid1 = self.subproc.connect("line", self.__handleLines) self.conid2 = self.subproc.connect("died", self.__handleDead) def __handleLines(self, subprocess, line): match = self.expression.search(line) if match: time, unit = match.groups() time = float(time) if unit == "s": time *= 1000 self.emit("received", time) else: for expr in self.errorExprs: match = expr.search(line) if match: msg = match.groups()[0] self.emit("error", _(msg)) def __handleDead(self, subprocess): if self.deadCount < self.restartsOnDead: log.warning("Pinger died and restarted (%d/%d)" % (self.deadCount + 1, self.restartsOnDead), extra={"task": self.subproc.defname}) self.stop() self.start() self.deadCount += 1 else: self.emit("error", _("Died")) self.stop() def stop(self): if not self.subproc: return # exitCode = self.subproc.gentleKill() self.subproc.disconnect(self.conid1) self.subproc.disconnect(self.conid2) self.subproc.terminate() self.subproc = None
class Pinger(GObject.GObject): """ The received signal contains the time it took to get response from the server in millisecconds. -1 means that some error occurred """ __gsignals__ = { "received": (GObject.SignalFlags.RUN_FIRST, None, (float, )), "error": (GObject.SignalFlags.RUN_FIRST, None, (str, )) } def __init__(self, host): GObject.GObject.__init__(self) self.host = host self.subproc = None self.expression = re.compile("=([\d\.]+) (m?s)") # We need untranslated error messages in regexp search # below, so have to use deferred translation here def _(msg): return msg error = _("Destination Host Unreachable") self.errorExprs = (re.compile("(%s)" % error), ) del _ self.restartsOnDead = 3 self.deadCount = 0 def start(self): assert not self.subproc if sys.platform == "win32": args = ["-t", self.host] else: args = ["-i10", self.host] self.subproc = SubProcess(shutil.which("ping"), args, env={"LANG": "en"}) asyncio.async(self.subproc.start()) self.conid1 = self.subproc.connect("line", self.__handleLines) self.conid2 = self.subproc.connect("died", self.__handleDead) def __handleLines(self, subprocess, line): match = self.expression.search(line) if match: time, unit = match.groups() time = float(time) if unit == "s": time *= 1000 self.emit("received", time) else: for expr in self.errorExprs: match = expr.search(line) if match: msg = match.groups()[0] self.emit("error", _(msg)) def __handleDead(self, subprocess): if self.deadCount < self.restartsOnDead: log.warning("Pinger died and restarted (%d/%d)" % (self.deadCount + 1, self.restartsOnDead), extra={"task": self.subproc.defname}) self.stop() self.start() self.deadCount += 1 else: self.emit("error", _("Died")) self.stop() def stop(self): if not self.subproc: return # exitCode = self.subproc.gentleKill() self.subproc.disconnect(self.conid1) self.subproc.disconnect(self.conid2) self.subproc.terminate() self.subproc = None