diff options
| author | Richard Purdie <richard.purdie@linuxfoundation.org> | 2011-06-08 11:20:08 +0100 |
|---|---|---|
| committer | Richard Purdie <richard.purdie@linuxfoundation.org> | 2011-06-08 11:38:47 +0100 |
| commit | 658ba779ac98803e82fb3f17860a72f178f97d5b (patch) | |
| tree | 983f60549757416ab2fbca9befeaf08c19cffa52 /bitbake/lib/bb/server/process.py | |
| parent | 8aabfed148ddbc42ea8ed28d7ea23193f1cdd87d (diff) | |
| download | poky-658ba779ac98803e82fb3f17860a72f178f97d5b.tar.gz | |
bitbake/server/process: Update to new server API
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.py | 107 |
1 files changed, 75 insertions, 32 deletions
diff --git a/bitbake/lib/bb/server/process.py b/bitbake/lib/bb/server/process.py index 5d7f8aa9de..12f368f80f 100644 --- a/bitbake/lib/bb/server/process.py +++ b/bitbake/lib/bb/server/process.py | |||
| @@ -29,8 +29,8 @@ import os | |||
| 29 | import signal | 29 | import signal |
| 30 | import sys | 30 | import sys |
| 31 | import time | 31 | import time |
| 32 | from bb.cooker import BBCooker | 32 | from Queue import Empty |
| 33 | from multiprocessing import Event, Process, util | 33 | from multiprocessing import Event, Process, util, Queue, Pipe, queues |
| 34 | 34 | ||
| 35 | logger = logging.getLogger('BitBake') | 35 | logger = logging.getLogger('BitBake') |
| 36 | 36 | ||
| @@ -72,13 +72,11 @@ class ProcessServer(Process): | |||
| 72 | profile_filename = "profile.log" | 72 | profile_filename = "profile.log" |
| 73 | profile_processed_filename = "profile.log.processed" | 73 | profile_processed_filename = "profile.log.processed" |
| 74 | 74 | ||
| 75 | def __init__(self, command_channel, event_queue, configuration): | 75 | def __init__(self, command_channel, event_queue): |
| 76 | Process.__init__(self) | 76 | Process.__init__(self) |
| 77 | self.command_channel = command_channel | 77 | self.command_channel = command_channel |
| 78 | self.event_queue = event_queue | 78 | self.event_queue = event_queue |
| 79 | self.event = EventAdapter(event_queue) | 79 | self.event = EventAdapter(event_queue) |
| 80 | self.configuration = configuration | ||
| 81 | self.cooker = BBCooker(configuration, self.register_idle_function) | ||
| 82 | self._idlefunctions = {} | 80 | self._idlefunctions = {} |
| 83 | self.event_handle = bb.event.register_UIHhandler(self) | 81 | self.event_handle = bb.event.register_UIHhandler(self) |
| 84 | self.quit = False | 82 | self.quit = False |
| @@ -95,33 +93,7 @@ class ProcessServer(Process): | |||
| 95 | self._idlefunctions[function] = data | 93 | self._idlefunctions[function] = data |
| 96 | 94 | ||
| 97 | def run(self): | 95 | def run(self): |
| 98 | if self.configuration.profile: | 96 | bb.cooker.server_main(self.cooker, self.main) |
| 99 | return self.profile_main() | ||
| 100 | else: | ||
| 101 | return self.main() | ||
| 102 | |||
| 103 | def profile_main(self): | ||
| 104 | import cProfile | ||
| 105 | profiler = cProfile.Profile() | ||
| 106 | try: | ||
| 107 | return profiler.runcall(self.main) | ||
| 108 | finally: | ||
| 109 | profiler.dump_stats(self.profile_filename) | ||
| 110 | self.write_profile_stats() | ||
| 111 | sys.__stderr__.write("Raw profiling information saved to %s and " | ||
| 112 | "processed statistics to %s\n" % | ||
| 113 | (self.profile_filename, | ||
| 114 | self.profile_processed_filename)) | ||
| 115 | |||
| 116 | def write_profile_stats(self): | ||
| 117 | import pstats | ||
| 118 | with open(self.profile_processed_filename, 'w') as outfile: | ||
| 119 | stats = pstats.Stats(self.profile_filename, stream=outfile) | ||
| 120 | stats.sort_stats('time') | ||
| 121 | stats.print_stats() | ||
| 122 | stats.print_callers() | ||
| 123 | stats.sort_stats('cumulative') | ||
| 124 | stats.print_stats() | ||
| 125 | 97 | ||
| 126 | def main(self): | 98 | def main(self): |
| 127 | # Ignore SIGINT within the server, as all SIGINT handling is done by | 99 | # Ignore SIGINT within the server, as all SIGINT handling is done by |
| @@ -219,3 +191,74 @@ class ProcessServer(Process): | |||
| 219 | # which can result in a bitbake server hang during the parsing process | 191 | # which can result in a bitbake server hang during the parsing process |
| 220 | if (2, 6, 0) <= sys.version_info < (2, 6, 3): | 192 | if (2, 6, 0) <= sys.version_info < (2, 6, 3): |
| 221 | _bootstrap = bootstrap_2_6_6 | 193 | _bootstrap = bootstrap_2_6_6 |
| 194 | |||
| 195 | class BitBakeServerConnection(): | ||
| 196 | def __init__(self, server): | ||
| 197 | self.server = server | ||
| 198 | self.procserver = server.server | ||
| 199 | self.connection = ServerCommunicator(server.ui_channel) | ||
| 200 | self.events = server.event_queue | ||
| 201 | |||
| 202 | def terminate(self, force = False): | ||
| 203 | signal.signal(signal.SIGINT, signal.SIG_IGN) | ||
| 204 | self.procserver.stop() | ||
| 205 | if force: | ||
| 206 | self.procserver.join(0.5) | ||
| 207 | if self.procserver.is_alive(): | ||
| 208 | self.procserver.terminate() | ||
| 209 | self.procserver.join() | ||
| 210 | else: | ||
| 211 | self.procserver.join() | ||
| 212 | while True: | ||
| 213 | try: | ||
| 214 | event = self.server.event_queue.get(block=False) | ||
| 215 | except (Empty, IOError): | ||
| 216 | break | ||
| 217 | if isinstance(event, logging.LogRecord): | ||
| 218 | logger.handle(event) | ||
| 219 | self.server.ui_channel.close() | ||
| 220 | self.server.event_queue.close() | ||
| 221 | if force: | ||
| 222 | sys.exit(1) | ||
| 223 | |||
| 224 | # Wrap Queue to provide API which isn't server implementation specific | ||
| 225 | class ProcessEventQueue(multiprocessing.queues.Queue): | ||
| 226 | def waitEvent(self, timeout): | ||
| 227 | try: | ||
| 228 | return self.get(True, timeout) | ||
| 229 | except Empty: | ||
| 230 | return None | ||
| 231 | |||
| 232 | class BitBakeServer(object): | ||
| 233 | def initServer(self): | ||
| 234 | # establish communication channels. We use bidirectional pipes for | ||
| 235 | # ui <--> server command/response pairs | ||
| 236 | # and a queue for server -> ui event notifications | ||
| 237 | # | ||
| 238 | self.ui_channel, self.server_channel = Pipe() | ||
| 239 | self.event_queue = ProcessEventQueue(0) | ||
| 240 | |||
| 241 | self.server = ProcessServer(self.server_channel, self.event_queue) | ||
| 242 | |||
| 243 | def addcooker(self, cooker): | ||
| 244 | self.cooker = cooker | ||
| 245 | self.server.cooker = cooker | ||
| 246 | |||
| 247 | def getServerIdleCB(self): | ||
| 248 | return self.server.register_idle_function | ||
| 249 | |||
| 250 | def saveConnectionDetails(self): | ||
| 251 | return | ||
| 252 | |||
| 253 | def detach(self, cooker_logfile): | ||
| 254 | self.server.start() | ||
| 255 | return | ||
| 256 | |||
| 257 | def establishConnection(self): | ||
| 258 | self.connection = BitBakeServerConnection(self) | ||
| 259 | signal.signal(signal.SIGTERM, lambda i, s: self.connection.terminate(force=True)) | ||
| 260 | return self.connection | ||
| 261 | |||
| 262 | def launchUI(self, uifunc, *args): | ||
| 263 | return bb.cooker.server_main(self.cooker, uifunc, *args) | ||
| 264 | |||
