def exit(self): # Grab the lock. lock = koWndWrapper.create_mutex(self._commandmentsLockName) koWndWrapper.wait_for_single_object(lock) # Send __exit__ commandment. f = open(self._commandmentsFileName, 'a') f.write("__exit__\n") f.close() # Signal that there are new commandments: to ensure worker # thread doesn't wedge. newCommandments = koWndWrapper.create_event(self._commandmentsEventName) koWndWrapper.set_event(newCommandments) koWndWrapper.close_handle(newCommandments) # Release the lock. koWndWrapper.release_mutex(lock) koWndWrapper.close_handle(lock) self.join() try: os.remove(self._commandmentsFileName) except EnvironmentError: pass
def exit(self): # Grab the lock. lock = koWndWrapper.create_mutex(self._commandmentsLockName) koWndWrapper.wait_for_single_object(lock) # Send __exit__ commandment. f = open(self._commandmentsFileName, 'a') f.write("__exit__\n") f.close() # Signal that there are new commandments: to ensure worker # thread doesn't wedge. newCommandments = koWndWrapper.create_event(self._commandmentsEventName) koWndWrapper.set_event(newCommandments) koWndWrapper.close_handle(newCommandments) # Release the lock. koWndWrapper.release_mutex(lock) koWndWrapper.close_handle(lock) self.join() try: os.remove(self._commandmentsFileName) except EnvironmentError: pass
class _CommandmentsReader(threading.Thread): """Consume commandments from the commandments.txt file. The .txt file must by guarded by the appropriate mutex and an event is signalled when new commandments are added. """ _product_type = "PRODUCT_TYPE" def __init__(self, dname, ver): """Create the commandments reader thread. "dname" is the directory in which to place files for the commandment system mechanics. "ver" is a Komodo version string on which to key commandment system resources (locks, event names, etc.) """ self._commandmentsLockName \ = "komodo-%s-%s-commandments-lock" % (self._product_type, ver) self._commandmentsEventName \ = "komodo-%s-%s-new-commandments" % (self._product_type, ver) self._commandmentsFileName = os.path.join(dname, "commandments.txt") threading.Thread.__init__(self, name="CommandmentsReader") self.setDaemon(True) def run(self): lock = koWndWrapper.create_mutex(self._commandmentsLockName) # existing: are there commandments from the invoking command-line? existing = os.path.exists(self._commandmentsFileName) newCommandments = koWndWrapper.create_event( self._commandmentsEventName, None, 1, existing) while 1: # Wait for new commandments. rv = koWndWrapper.wait_for_single_object(newCommandments) if rv == koWndWrapper.WAIT_OBJECT_0: retval = 1 else: raise CommandmentError("Error waiting for new " "commandments: %r" % rv) # Grab the lock. koWndWrapper.wait_for_single_object(lock) # Consume the commandments. f = open(self._commandmentsFileName, 'r') cmds = [] for line in f.readlines(): if line[-1] == '\n': line = line[:-1] if line.strip(): # skip empty lines cmds.append(line) f.close() os.unlink(self._commandmentsFileName) # Reset the "new commandments" event. koWndWrapper.reset_event(newCommandments) # Release the lock. koWndWrapper.release_mutex(lock) # Handle the commandments. exit = 0 for cmd in cmds: if cmd == "__exit__": exit = 1 break else: try: _handleCommandment(cmd) except Exception, ex: tb = ''.join( traceback.format_exception(*sys.exc_info())) log.error("'%s': %s:\n%s", cmd, str(ex), tb) #XXX Dialog proxy's alert() is screwed up. # It misbehaves and leads to Jaguar # crash. #dialogProxy = components\ # .classes["@activestate.com/asDialogProxy;1"]\ # .getService(components.interfaces.asIDialogProxy) #msg = "Error handling commandment: '%s'\n\n%s"\ # % (cmd, tb) #dialogProxy.alert(msg) if exit: break koWndWrapper.close_handle(newCommandments) koWndWrapper.close_handle(lock)