This file is indexed.

/usr/share/pyshared/pychess/Players/engineNest.py is in pychess 0.12~beta3-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
from __future__ import with_statement 

import os
import sys
import json
from hashlib import md5
from threading import Thread
from os.path import join, dirname, abspath
from copy import deepcopy

from gobject import GObject, SIGNAL_RUN_FIRST, TYPE_NONE

from pychess.System import conf
from pychess.System.Log import log
from pychess.System.command import Command
from pychess.System.SubProcess import SubProcess, searchPath, SubProcessError
from pychess.System.prefix import addUserConfigPrefix, getEngineDataPrefix
from pychess.System.ThreadPool import pool, PooledThread
from pychess.Players.Player import PlayerIsDead
from pychess.Utils.const import *
from CECPEngine import CECPEngine
from UCIEngine import UCIEngine
from pychess.Variants import variants

attrToProtocol = {"uci": UCIEngine, "xboard": CECPEngine}

PYTHONBIN = sys.executable.split("/")[-1]

backup = [
    {"protocol": "xboard", "name": "PyChess.py", "country": "dk",
        "vm_name": PYTHONBIN, "vm_args": ["-u"]},
    {"protocol": "xboard", "name": "shatranj.py", "country": "us",
        "vm_name": PYTHONBIN, "vm_args": ["-u"], "args": ["-xboard"]},
    {"protocol": "xboard", "name": "gnuchess", "country": "us"},
    {"protocol": "xboard", "name": "gnome-gnuchess", "country": "us"},
    {"protocol": "xboard", "name": "crafty", "country": "us"},
    {"protocol": "xboard", "name": "faile", "country": "ca", "protover": 1},
    {"protocol": "xboard", "name": "phalanx", "country": "cz", "protover": 1},
    {"protocol": "xboard", "name": "sjeng", "country": "be"},
    {"protocol": "xboard", "name": "hoichess", "country": "de"},
    {"protocol": "xboard", "name": "boochess", "country": "de", "protover": 1},
    {"protocol": "xboard", "name": "amy", "country": "de"},
    {"protocol": "xboard", "name": "amundsen", "country": "sw"},
    {"protocol": "uci", "name": "gnuchessu", "country": "us"},
    {"protocol": "uci", "name": "robbolito", "country": "ru"},
    {"protocol": "uci", "name": "glaurung", "country": "no"},
    {"protocol": "uci", "name": "stockfish", "country": "no"},
    {"protocol": "uci", "name": "ShredderClassicLinux", "country": "de"},
    {"protocol": "uci", "name": "fruit_21_static", "country": "fr"},
    {"protocol": "uci", "name": "fruit", "country": "fr"},
    {"protocol": "uci", "name": "toga2", "country": "de"},
    {"protocol": "uci", "name": "hiarcs", "country": "gb"},
    {"protocol": "uci", "name": "diablo", "country": "us"},
    {"protocol": "uci", "name": "Houdini.exe", "country": "be", "vm_name": "wine"},
    {"protocol": "uci", "name": "Rybka.exe", "country": "ru", "vm_name": "wine"},
]


class EngineDiscoverer (GObject, PooledThread):
    
    __gsignals__ = {
        "discovering_started": (SIGNAL_RUN_FIRST, TYPE_NONE, (object,)),
        "engine_discovered": (SIGNAL_RUN_FIRST, TYPE_NONE, (str, object)),
        "engine_failed": (SIGNAL_RUN_FIRST, TYPE_NONE, (str, object)),
        "all_engines_discovered": (SIGNAL_RUN_FIRST, TYPE_NONE, ()),
    }
    
    def __init__ (self):
        GObject.__init__(self)
        
        self.engines = []
        
        self.jsonpath = addUserConfigPrefix("engines.json")
        try:
            self._engines = json.load(open(self.jsonpath))
        except ValueError, e:
            log.warn("engineNest: Couldn\'t read engines.json, renamed it to .bak\n%s\n" % (self.jsonpath,e))
            os.rename(self.jsonpath, self.jsonpath+".bak")
            self._engines = deepcopy(backup)
        except IOError, e:
            log.info("engineNest: Couldn\'t open engines.json, creating a new.\n%s\n" % e)
            self._engines = deepcopy(backup)
    
    ############################################################################
    # Discover methods                                                         #
    ############################################################################
    
    def __findRundata (self, engine):
        """ Searches for a readable, executable named 'name' in the PATH.
            For the PyChess engine, special handling is taken, and we search
            PYTHONPATH as well as the directory from where the 'os' module is
            imported """
        
        if engine.get("vm_name") is not None:
            altpath = engine.get("vm_command")
            vmpath = searchPath(engine["vm_name"], access=os.R_OK|os.X_OK, altpath = altpath)
            
            if engine["name"] == "PyChess.py":
                path = join(abspath(dirname(__file__)), "PyChess.py")
                if not os.access(path, os.R_OK):
                    path = None
            else:
                altpath = engine.get("command")
                path = searchPath(engine["name"], access=os.R_OK, altpath=altpath)
            
            if vmpath and path:
                return vmpath, path
        else:
            altpath = engine.get("command")
            path = searchPath(engine["name"], access=os.R_OK|os.X_OK, altpath=altpath)
            if path:
                return None, path
        
        return False
    
    def __fromUCIProcess (self, subprocess):
        ids = subprocess.ids
        options = subprocess.options
        engine = {}
        if 'author' in ids:
            engine['author'] = ids['author']
        if options:
            engine["options"] = options.values()
        return engine
    
    def __fromCECPProcess (self, subprocess):
        features = subprocess.features
        options = subprocess.options
        engine = {}
        if features['variants'] is not None:
            engine['variants'] = features['variants'].split(",")
        if features['analyze'] == 1:
            engine["analyze"] = True
        if options:
            engine["options"] = options.values()
        
        return engine
    
    def __discoverE (self, engine):
        subproc = self.initEngine (engine, BLACK)
        try:
            subproc.connect('readyForOptions', self.__discoverE2, engine)
            subproc.prestart() # Sends the 'start line'
            subproc.start()
        except SubProcessError, e:
            log.warn("Engine %s failed discovery: %s" % (engine["name"],e))
            self.emit("engine_failed", engine["name"], engine)
        except PlayerIsDead, e:
            # Check if the player died after engine_discovered by our own hands
            if not self.toBeRechecked[engine["name"]][1]:
                log.warn("Engine %s failed discovery: %s" % (engine["name"],e))
                self.emit("engine_failed", engine["name"], engine)
    
    def __discoverE2 (self, subproc, engine):
        if engine.get("protocol") == "uci":
            fresh = self.__fromUCIProcess(subproc)
        elif engine.get("protocol") == "xboard":
            fresh = self.__fromCECPProcess(subproc)
        engine.update(fresh)

        exitcode = subproc.kill(UNKNOWN_REASON)
        if exitcode:
            log.debug("Engine failed %s\n" % engine["name"])
            self.emit("engine_failed", engine['name'], engine)
            return
        
        engine['recheck'] = False
        log.debug("Engine finished %s\n" % engine["name"])
        self.emit ("engine_discovered", engine['name'], engine)
    
    
    ############################################################################
    # Main loop                                                                #
    ############################################################################
    
    def __needClean(self, rundata, engine):
        """ Check if the filename or md5sum of the engine has changed.
            In that case we need to clean the engine """
        
        vmpath, path = rundata
        
        # Check if filename is not set, or if it has changed
        if engine.get("command") is None or engine.get("command") != path:
            return True
        # If the engine failed last time, we'll recheck it as well
        if engine.get('recheck'):
            return True
        
        # Check if md5sum is not set, or if it has changed
        if engine.get("md5") is None:
            return True
        with open(path) as f:
            md5sum = md5(f.read()).hexdigest()
        if engine.get("md5") != md5sum:
            return True
        
        return False
    
    def __clean(self, rundata, engine):
        """ Grab the engine from the backup and attach the attributes
            from rundata. The update engine is ready for discovering.
        """
        
        vmpath, path = rundata
        
        with open(path) as f:
            md5sum = md5(f.read()).hexdigest()
        
        ######
        # Find the backup engine
        ######
        try:
            backup_engine = (c for c in backup if c["name"] == engine["name"]).next()
            engine["country"] = backup_engine["country"]
        except StopIteration:
            log.warn("Engine '%s' has not been tested and verified to work with PyChess\n" % \
                engine.get('name'))
            engine['recheck'] = True

        ######
        # Clean it
        ######
        engine['command'] = path
        engine['md5'] = md5sum
        if vmpath is not None:
            engine['vm_command'] = vmpath
        
    ######
    # Save the xml
    ######
    def save(self, *args):
        try:
            with open(self.jsonpath, "w") as f:
                json.dump(self._engines, f, indent=1, sort_keys=True)
        except IOError, e:
            log.error("Saving engines.json raised exception: %s\n" % \
                      ", ".join(str(a) for a in e.args))
    
    def run (self):
        self.engines = []
        # List available engines
        for engine in self._engines:
            # Find the known and installed engines on the system
         
            # Look up
            rundata = self.__findRundata(engine)
            if not rundata:
                # Engine is not available on the system
                continue
            
            if self.__needClean(rundata, engine):
                self.__clean(rundata, engine)
                engine['recheck'] = True
            
            self.engines.append(engine)
            
        ######
        # Runs all the engines in toBeRechecked, in order to gather information
        ######
        self.toBeRechecked = dict((c["name"],[c,False]) for c in self._engines if c.get('recheck'))
        
        def count(self_, name, engine, wentwell):
            if wentwell:
                self.toBeRechecked[name][1] = True
            if all([elem[1] for elem in self.toBeRechecked.values()]):
                self.engines.sort(key=lambda x: x["name"])
                self.emit("all_engines_discovered")
        self.connect("engine_discovered", count, True)
        self.connect("engine_failed", count, False)
        
        if self.toBeRechecked:
            self.emit("discovering_started", self.toBeRechecked.keys())
            self.connect("all_engines_discovered", self.save)
            for engine, need in self.toBeRechecked.values():
                self.__discoverE(engine)
        else:
            self.emit("all_engines_discovered")
            
    ############################################################################
    # Interaction                                                              #
    ############################################################################
    
    def is_analyzer(self, engine):
        protocol = engine.get("protocol")
        if protocol == "uci":
            return True
        elif protocol == "xboard":
            return engine.get("analyze") is not None
        
    def getAnalyzers (self):
        return [engine for engine in self.getEngines() if self.is_analyzer(engine)]
    
    def getEngines (self):
        """ Returns list of engine dicts """
        return self.engines
    
    def getEngineN (self, index):
        return self.getEngines()[index]

    def getEngineByName (self, name):
        names = [engine["name"] for engine in self.getEngines()]
        return self.getEngines()[names.index(name)]
    
    def getEngineByMd5 (self, md5sum, list=[]):
        if not list:
            list = self.getEngines()
        for engine in list:
            md5 = engine.get('md5')
            if md5 is None: continue
            if md5 == md5sum:
                return engine
    
    def getEngineVariants (self, engine):
        for variantClass in variants.values():
            if variantClass.standard_rules:
                yield variantClass.board.variant
            else:
                if engine.get("variants"):
                    if variantClass.cecp_name in engine.get("variants"):
                        yield variantClass.board.variant
                # UCI knows Chess960 only
                if variantClass.cecp_name == "fischerandom" and engine.get("options"):
                    for option in engine["options"]:
                        if option["name"] == "UCI_Chess960":
                            yield variantClass.board.variant
    
    def getName (self, engine=None):
        # Test if the call was to get the name of the thread
        if engine is None:
            return Thread.getName(self)
        return engine["name"]
    
    def getCountry (self, engine):
        return engine.get("country")

    def initEngine (self, engine, color):
        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['md5']
        
        working_directory = engine.get("workingDirectory")
        if working_directory:
            workdir = working_directory
        else:
            workdir = getEngineDataPrefix()
        warnwords = ("illegal", "error", "exception")
        subprocess = SubProcess(path, args, warnwords, SUBPROCESS_SUBPROCESS, workdir)
        engine_proc = attrToProtocol[protocol](subprocess, color, protover, md5)
        
        engine_proc.setName(name)
        
        # If the user has configured special options for this engine, here is
        # where they should be set.
        def optionsCallback(e):
            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))
                        e.setOption(key, value)
        engine_proc.connect("readyForOptions", optionsCallback)
        
        return engine_proc
    
    def initPlayerEngine (self, engine, color, diffi, variant, secs=0, incr=0, forcePonderOff=False):
        engine = self.initEngine (engine, color)
        def optionsCallback (engine):
            engine.setOptionStrength(diffi, forcePonderOff)
            engine.setOptionVariant(variant)
            if secs > 0:
                engine.setOptionTime(secs, incr)
        engine.connect("readyForOptions", optionsCallback)
        engine.prestart()
        return engine
    
    def initAnalyzerEngine (self, engine, mode, variant):
        engine = self.initEngine (engine, WHITE)
        def optionsCallback (engine):
            engine.setOptionAnalyzing(mode)
            engine.setOptionVariant(variant)
        engine.connect("readyForOptions", optionsCallback)
        engine.prestart()
        return engine

    def addEngine(self, name, new_engine, protocol):
        engine = {"name": name,
                  "protocol": protocol,
                  "command": new_engine,
                  "recheck": True}
        self._engines.append(engine)

    def removeEngine(self, name):
        names = [engine["name"] for engine in self.getEngines()]
        index = names.index(name)
        del self.engines[index]
        
        names = [engine["name"] for engine in self._engines]
        index = names.index(name)
        del self._engines[index]

discoverer = EngineDiscoverer()

def init_engine (analyzer_type, gamemodel, force=False):
    """
    Initializes and starts the engine analyzer of analyzer_type the user has
    configured in the Engines tab of the preferencesDialog, for gamemodel. If no
    such engine is set in the preferences, or if the configured engine doesn't
    support the chess variant being played in gamemodel, then no analyzer is
    started and None is returned.
    """
    if analyzer_type == HINT:
        combo_name = "ana_combobox"
        check_name = "analyzer_check"
        mode = ANALYZING
    else:
        combo_name = "inv_ana_combobox"
        check_name = "inv_analyzer_check"
        mode = INVERSE_ANALYZING
    
    analyzer = None

    if conf.get(check_name, True):
        anaengines = list(discoverer.getAnalyzers())
        engine = discoverer.getEngineByMd5(conf.get(combo_name, 0))
        if engine is None:
            engine = anaengines[0]
        
        if gamemodel.variant.board.variant in discoverer.getEngineVariants(engine):
            analyzer = discoverer.initAnalyzerEngine(engine, mode, gamemodel.variant)
            log.debug("%s analyzer: %s\n" % (analyzer_type, repr(analyzer)))
        
    return analyzer

def is_uci(engine_command):
    command = Command(engine_command, "uci\n")
    output = command.run(timeout=3)[1]
    uci = False
    for line in output.split("\n"):
        line = line.rstrip()
        if line == "uciok":
            uci = True
            break
        elif "Error" in line or "Illegal" in line or "Invalid" in line:
            break
    return uci

def is_cecp(engine_command):
    command = Command(engine_command, "xboard\nprotover 2\n")
    output = command.run(timeout=3)[1]
    cecp = False
    for line in output.split("\n"):
        line = line.rstrip()
        if "feature" in line and "done" in line:
            cecp = True
            break
        elif "Error" in line or "Illegal" in line or "Invalid" in line:
            break
    return cecp


if __name__ == "__main__":
    import glib, gobject
    gobject.threads_init()
    mainloop = glib.MainLoop()

#    discoverer = EngineDiscoverer()

    def discovering_started (discoverer, names):
        print "discovering_started", names
    discoverer.connect("discovering_started", discovering_started)

    def engine_discovered (discoverer, name, engine):
        sys.stdout.write(".")
    discoverer.connect("engine_discovered", engine_discovered)

    def all_engines_discovered (discoverer):
        print "all_engines_discovered"
        print [engine["name"] for engine in discoverer.getEngines()]
        mainloop.quit()
    discoverer.connect("all_engines_discovered", all_engines_discovered)
    
    discoverer.start()

    mainloop.run()