File: server.py

package info (click to toggle)
atheist 0.20110402-3
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 1,348 kB
  • sloc: python: 4,764; xml: 626; makefile: 113; cpp: 54; ansic: 5; sh: 5
file content (147 lines) | stat: -rwxr-xr-x 4,023 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/python -u
# -*- mode: python; coding: utf-8 -*-

from __future__ import with_statement

import sys
import threading
import os
import logging

import Ice
cwd = os.path.dirname(__file__)
Ice.loadSlice('-I%s --all %s/atheist.ice' % (Ice.getSliceDir(), cwd))

import Atheist as rath
import atheist
from atheist.manager import Manager

log = logging.getLogger('atheist.server')
log.setLevel(logging.DEBUG)
log.debug('log init')


def get_proxies(adapter, cast, servants):
    proxies = [adapter.addWithUUID(x) for x in servants]
    return [cast.uncheckedCast(x) for x in proxies]


class TaskCaseI(rath.TaskCase):

    def __init__(self, mng, tc):
        self.mng = mng
        self.tc = tc
        self.lock = threading.Lock()

    # from TaskCase interface
    def run(self, current=None):
        self.mng.statusOb.updateTaskCases({self.tc.fname: rath.Status.RUNNING})

        if self.lock.locked(): return

        with self.lock:
            self.tc.run()
            self.mng.statusOb.updateTasks(
                dict([("%s:%s" % (t.fname, t.name),
                       rath.Status(t.result)) for t in self.tc.tasks]))

            self.mng.statusOb.updateTaskCases(
                {self.tc.fname: rath.Status(self.tc.result)})


    # from TaskCase interface
    def getConfig(self, current=None):
        retval = {}
        for task in self.tc.tasks:
            config = rath.TaskConfig(indx=task.indx)
            for name in [x for x in dir(config)
                     if x in atheist.task_attrs.keys()]:
                setattr(config, name, getattr(task, name))
            retval[str(task.indx)] = config

        return retval

    # from TaskCase interface
    def getStatus(self, current=None):
        raise NotImplementedError


class ManagerI(rath.Manager):

    def __init__(self, argv, adap):
        self.mng = Manager(argv)
        self.adap = adap
        self.statusOb = None
        self.outOb = None
        self.logOb = None
        self.cases = []
        self.proxies = []
        self.lock = threading.Lock()

    def refresh(self):
        # FIXME: Se deben eliminar los proxies de los cases que ya no
        # existen y aƱadir solo los nuevos

        for prx in self.proxies:
            self.adap.remove(prx.ice_getIdentity())

        self.mng.reload()
        self.cases = [TaskCaseI(self,x) for x in self.mng.itercases()]
        self.proxies = get_proxies(self.adap, rath.TaskCasePrx,
                                   self.cases)

        for c in self.mng.itercases():
            print c

        log.info("cases: %s" % len(self.cases))

    # from Manager interface
    def getTaskCases(self, current=None):
        return self.proxies

    # from Manager interface
    def runAll(self, current=None):
        if self.lock.locked():
            log.warning('runAll: Manager is already running .............')
            return

        with self.lock:
            self.refresh()
            self.statusOb.updateManager(rath.Status.RUNNING)
            for tc in self.cases:
                tc.run()
            self.statusOb.updateManager(rath.Status.UNKNOWN)

    # from Manager interface
    def attachStatusOb(self, ob, current=None):
        self.statusOb = ob

    # from Manager interface
    def attachOutOb(self, ob, taskIds, current=None):
        self.outOb = ob

    # from Manager interface
    def attachLogOb(self, ob, taskIds, current=None):
        self.logOb = ob


class server(Ice.Application):

    def run(self, argv):
        self.shutdownOnInterrupt()
        ic = self.communicator()

        adapter = self.communicator().createObjectAdapter("Manager.Adapter")
        oid = ic.getProperties().getProperty("Manager.InstanceName")
        oid = ic.stringToIdentity(oid)
        proxy = adapter.add(ManagerI(argv, adapter), oid)
        adapter.activate()

        log.debug("Manager at '%s'" % proxy)

        self.communicator().waitForShutdown()
        return 0


if __name__ == "__main__":
    sys.exit(server().main(sys.argv[1:]))