summaryrefslogtreecommitdiffstats
path: root/bitbake/lib/bb/server/process.py
diff options
context:
space:
mode:
authorAlexandru DAMIAN <alexandru.damian@intel.com>2013-05-31 12:06:46 +0100
committerRichard Purdie <richard.purdie@linuxfoundation.org>2013-06-07 14:13:18 +0100
commit748e3c13c80f698f8396bbc55e42ac3ee6a2a81e (patch)
tree23152dd616209720267647d8a5f36730896983f9 /bitbake/lib/bb/server/process.py
parenta62aed41f2d8f874f7ae24d0e5be5dbc66ea2199 (diff)
downloadpoky-748e3c13c80f698f8396bbc55e42ac3ee6a2a81e.tar.gz
bitbake: bitbake server: create common server infrastructure
In an attempt to minimize code duplication, create clear interfaces, and maximize code reuse through OOP, bb.server adds base classes for the BitBakeServer, BitBakeServerConnection and actual server implementations instructed in particular server types. These classes document the minimum interfaces that the derived classes must implement, and provide boilerplate code. Changes to None, Process and XMLRPC servers as to use the common server infrastructure. (Bitbake rev: 6db4a64cef20f8d0aba804db4c4e1eec7b112b46) Signed-off-by: Alexandru DAMIAN <alexandru.damian@intel.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'bitbake/lib/bb/server/process.py')
-rw-r--r--bitbake/lib/bb/server/process.py53
1 files changed, 20 insertions, 33 deletions
diff --git a/bitbake/lib/bb/server/process.py b/bitbake/lib/bb/server/process.py
index 163dbbb997..d73fe827e4 100644
--- a/bitbake/lib/bb/server/process.py
+++ b/bitbake/lib/bb/server/process.py
@@ -32,6 +32,8 @@ import time
32from Queue import Empty 32from Queue import Empty
33from multiprocessing import Event, Process, util, Queue, Pipe, queues 33from multiprocessing import Event, Process, util, Queue, Pipe, queues
34 34
35from . import BitBakeBaseServer, BitBakeBaseServerConnection, BaseImplServer
36
35logger = logging.getLogger('BitBake') 37logger = logging.getLogger('BitBake')
36 38
37class ServerCommunicator(): 39class ServerCommunicator():
@@ -68,26 +70,21 @@ class EventAdapter():
68 print("EventAdapter puked: %s" % str(err)) 70 print("EventAdapter puked: %s" % str(err))
69 71
70 72
71class ProcessServer(Process): 73class ProcessServer(Process, BaseImplServer):
72 profile_filename = "profile.log" 74 profile_filename = "profile.log"
73 profile_processed_filename = "profile.log.processed" 75 profile_processed_filename = "profile.log.processed"
74 76
75 def __init__(self, command_channel, event_queue): 77 def __init__(self, command_channel, event_queue):
78 BaseImplServer.__init__(self)
76 Process.__init__(self) 79 Process.__init__(self)
77 self.command_channel = command_channel 80 self.command_channel = command_channel
78 self.event_queue = event_queue 81 self.event_queue = event_queue
79 self.event = EventAdapter(event_queue) 82 self.event = EventAdapter(event_queue)
80 self._idlefunctions = {}
81 self.quit = False 83 self.quit = False
82 84
83 self.keep_running = Event() 85 self.keep_running = Event()
84 self.keep_running.set() 86 self.keep_running.set()
85 87
86 def register_idle_function(self, function, data):
87 """Register a function to be called while the server is idle"""
88 assert hasattr(function, '__call__')
89 self._idlefunctions[function] = data
90
91 def run(self): 88 def run(self):
92 for event in bb.event.ui_queue: 89 for event in bb.event.ui_queue:
93 self.event_queue.put(event) 90 self.event_queue.put(event)
@@ -117,11 +114,11 @@ class ProcessServer(Process):
117 def idle_commands(self, delay): 114 def idle_commands(self, delay):
118 nextsleep = delay 115 nextsleep = delay
119 116
120 for function, data in self._idlefunctions.items(): 117 for function, data in self._idlefuns.items():
121 try: 118 try:
122 retval = function(self, data, False) 119 retval = function(self, data, False)
123 if retval is False: 120 if retval is False:
124 del self._idlefunctions[function] 121 del self._idlefuns[function]
125 elif retval is True: 122 elif retval is True:
126 nextsleep = None 123 nextsleep = None
127 elif nextsleep is None: 124 elif nextsleep is None:
@@ -191,12 +188,13 @@ class ProcessServer(Process):
191 if (2, 6, 0) <= sys.version_info < (2, 6, 3): 188 if (2, 6, 0) <= sys.version_info < (2, 6, 3):
192 _bootstrap = bootstrap_2_6_6 189 _bootstrap = bootstrap_2_6_6
193 190
194class BitBakeServerConnection(): 191class BitBakeProcessServerConnection(BitBakeBaseServerConnection):
195 def __init__(self, server): 192 def __init__(self, serverImpl, ui_channel, event_queue):
196 self.server = server 193 self.procserver = serverImpl
197 self.procserver = server.server 194 self.ui_channel = ui_channel
198 self.connection = ServerCommunicator(server.ui_channel) 195 self.event_queue = event_queue
199 self.events = server.event_queue 196 self.connection = ServerCommunicator(self.ui_channel)
197 self.events = self.event_queue
200 198
201 def terminate(self, force = False): 199 def terminate(self, force = False):
202 signal.signal(signal.SIGINT, signal.SIG_IGN) 200 signal.signal(signal.SIGINT, signal.SIG_IGN)
@@ -210,13 +208,13 @@ class BitBakeServerConnection():
210 self.procserver.join() 208 self.procserver.join()
211 while True: 209 while True:
212 try: 210 try:
213 event = self.server.event_queue.get(block=False) 211 event = self.event_queue.get(block=False)
214 except (Empty, IOError): 212 except (Empty, IOError):
215 break 213 break
216 if isinstance(event, logging.LogRecord): 214 if isinstance(event, logging.LogRecord):
217 logger.handle(event) 215 logger.handle(event)
218 self.server.ui_channel.close() 216 self.ui_channel.close()
219 self.server.event_queue.close() 217 self.event_queue.close()
220 if force: 218 if force:
221 sys.exit(1) 219 sys.exit(1)
222 220
@@ -235,7 +233,7 @@ class ProcessEventQueue(multiprocessing.queues.Queue):
235 return None 233 return None
236 234
237 235
238class BitBakeServer(object): 236class BitBakeServer(BitBakeBaseServer):
239 def initServer(self): 237 def initServer(self):
240 # establish communication channels. We use bidirectional pipes for 238 # establish communication channels. We use bidirectional pipes for
241 # ui <--> server command/response pairs 239 # ui <--> server command/response pairs
@@ -243,24 +241,13 @@ class BitBakeServer(object):
243 # 241 #
244 self.ui_channel, self.server_channel = Pipe() 242 self.ui_channel, self.server_channel = Pipe()
245 self.event_queue = ProcessEventQueue(0) 243 self.event_queue = ProcessEventQueue(0)
246 244 self.serverImpl = ProcessServer(self.server_channel, self.event_queue)
247 self.server = ProcessServer(self.server_channel, self.event_queue)
248
249 def addcooker(self, cooker):
250 self.cooker = cooker
251 self.server.cooker = cooker
252
253 def getServerIdleCB(self):
254 return self.server.register_idle_function
255
256 def saveConnectionDetails(self):
257 return
258 245
259 def detach(self): 246 def detach(self):
260 self.server.start() 247 self.serverImpl.start()
261 return 248 return
262 249
263 def establishConnection(self): 250 def establishConnection(self):
264 self.connection = BitBakeServerConnection(self) 251 self.connection = BitBakeProcessServerConnection(self.serverImpl, self.ui_channel, self.event_queue)
265 signal.signal(signal.SIGTERM, lambda i, s: self.connection.terminate(force=True)) 252 signal.signal(signal.SIGTERM, lambda i, s: self.connection.terminate(force=True))
266 return self.connection 253 return self.connection