def testFileWrite(self): tmp_dir = tempfile.mktemp('-roxlib-test') os.mkdir(tmp_dir) root = self.master.root tmp_file = join(tmp_dir, 'new') def run(): response = root.open(tmp_file, 'w') yield response stream = response.result assert os.path.isfile(tmp_file) response = root.write(stream, 'Hello\n') yield response assert response.result == None response = root.close(stream) yield response assert response.result is None assert file(tmp_file).read() == 'Hello\n' response = root.close(stream) yield response try: response.result assert 0, 'Expected an exception!' except KeyError: pass g.main_quit() tasks.Task(run()) g.main() shutil.rmtree(tmp_dir)
def testOutputBlocker(self): readable, writeable = os.pipe() def run(): # Fill the input buffer... sent = 0 while True: ob = tasks.OutputBlocker(writeable) tb = tasks.TimeoutBlocker(0.2) yield ob, tb if ob.happened: sent += os.write(writeable, 'Hello\n') else: assert tb.happened break assert sent > 0 #print "send %d bytes" % sent # Read it all back... got = 0 while got < sent: got += len(os.read(readable, sent - got)) ob = tasks.OutputBlocker(writeable) tb = tasks.TimeoutBlocker(0.2) yield ob, tb assert ob.happened assert not tb.happened g.main_quit() tasks.Task(run()) g.main()
def testIdleBlocker(self): def run(): yield None g.main_quit() tasks.Task(run()) g.main()
def testFinished(self): readable, writeable = os.pipe() got = [] def run(fail = False): for x in range(3): got.append(x) yield None if fail: raise Exception("Fail") def wait_for(t1, expected): yield t1.finished assert got == expected g.main_quit() t1 = tasks.Task(run()) tasks.Task(wait_for(t1, [0, 1, 2])) assert not t1.finished.happened g.main() assert t1.finished.happened old = rox.report_exception try: rox.report_exception = lambda: (got.append(False), g.main_quit()) got = [] t2 = tasks.Task(run(fail = True)) tasks.Task(wait_for(t2, [0, 1, 2, False])) g.main() finally: rox.report_exception = old
def testFileRead(self): tmp_file = tempfile.mktemp(suffix='-roxlib-test') s = file(tmp_file, 'w') s.write('Hello\n') s.close() root = self.master.root def run(): response = root.open(tmp_file) yield response stream = response.result response = root.read(stream, 5) yield response assert "Hello" == response.result response = root.close(stream) yield response assert response.result is None g.main_quit() tasks.Task(run()) g.main() os.unlink(tmp_file)
def testInputBlocker(self): readable, writeable = os.pipe() def run(): ib = tasks.InputBlocker(readable) tb = tasks.TimeoutBlocker(0.2) yield ib, tb assert not ib.happened assert tb.happened os.write(writeable, "!") tb = tasks.TimeoutBlocker(0.2) yield ib, tb assert ib.happened assert not tb.happened assert os.read(readable, 1) == '!' os.close(writeable) ib = tasks.InputBlocker(readable) yield ib assert ib.happened g.main_quit() tasks.Task(run()) g.main()
def testFinished(self): readable, writeable = os.pipe() got = [] def run(fail=False): for x in range(3): got.append(x) yield None if fail: raise Exception("Fail") def wait_for(t1, expected): yield t1.finished assert got == expected g.main_quit() t1 = tasks.Task(run()) tasks.Task(wait_for(t1, [0, 1, 2])) assert not t1.finished.happened g.main() assert t1.finished.happened old = rox.report_exception try: rox.report_exception = lambda: (got.append(False), g.main_quit()) got = [] t2 = tasks.Task(run(fail=True)) tasks.Task(wait_for(t2, [0, 1, 2, False])) g.main() finally: rox.report_exception = old
def testTimeoutBlocker(self): def run(): start = time.time() yield tasks.TimeoutBlocker(0.6) end = time.time() assert end > start + 0.5 g.main_quit() tasks.Task(run()) g.main()
def wait(self): """Run a recursive mainloop until the command terminates. Raises an exception on error.""" if self.child is None: self.start() self.waiting = True while not self.done: g.main() if self.done is not True: raise self.done
def get_response(self): if self.invisible.xmlrpc_response is None: self.waiting = True try: g.main() finally: self.waiting = False assert self.invisible.xmlrpc_response is not None retval, method = xmlrpclib.loads(self.invisible.xmlrpc_response) assert len(retval) == 1 return retval[0]
def testMissing(self): def run(): response = self.master.root.missing('foo') yield response try: response.result assert 0, 'Expected an exception!' except AttributeError: pass g.main_quit() tasks.Task(run()) g.main()
def testUnlink(self): tmp = tempfile.mktemp('-roxlib-test') file(tmp, 'w').close() def run(): assert os.path.isfile(tmp) response = self.master.root.unlink(tmp) yield response assert response.result is None assert not os.path.exists(tmp) g.main_quit() tasks.Task(run()) g.main()
def testRmTree(self): tmp_dir = tempfile.mktemp('-roxlib-test') os.mkdir(tmp_dir) def run(): assert os.path.isdir(tmp_dir) response = self.master.root.rmtree(tmp_dir) yield response assert response.result is None assert not os.path.exists(tmp_dir) g.main_quit() tasks.Task(run()) g.main()
def testTooSoon(self): def run(): response = self.master.root.invoke() try: response.result assert 0, 'Expected an exception!' except Exception: pass yield response response.result g.main_quit() tasks.Task(run()) g.main()
def get_response(self): if self.response is None: self.waiting = True tag = gobject.timeout_add(60 * 1000, self.timed_out) try: g.main() finally: self.waiting = False gobject.source_remove(tag) assert self.response is not None retval, method = xmlrpclib.loads(self.response) assert len(retval) == 1 return retval[0]
def testChmod(self): tmp_file = tempfile.mktemp(suffix = '-roxlib-test') s = file(tmp_file, 'w') s.close() root = self.master.root os.chmod(tmp_file, 0700) def run(): assert os.stat(tmp_file).st_mode & 0777 == 0700 response = root.chmod(tmp_file, 0655) yield response response.result assert os.stat(tmp_file).st_mode & 0777 == 0655 g.main_quit() tasks.Task(run()) g.main() os.unlink(tmp_file)
def testChmod(self): tmp_file = tempfile.mktemp(suffix='-roxlib-test') s = file(tmp_file, 'w') s.close() root = self.master.root os.chmod(tmp_file, 0700) def run(): assert os.stat(tmp_file).st_mode & 0777 == 0700 response = root.chmod(tmp_file, 0655) yield response response.result assert os.stat(tmp_file).st_mode & 0777 == 0655 g.main_quit() tasks.Task(run()) g.main() os.unlink(tmp_file)
def testRename(self): tmp_dir = tempfile.mktemp('-roxlib-test') os.mkdir(tmp_dir) root = self.master.root f = file(join(tmp_dir, 'old'), 'w') f.write('Hello\n') f.close() def run(): response = root.rename(join(tmp_dir, 'old'), join(tmp_dir, 'new')) yield response assert response.result == None assert file(join(tmp_dir, 'new')).read() == 'Hello\n' g.main_quit() tasks.Task(run()) g.main() shutil.rmtree(tmp_dir)
def testSu(self): def run(): maker = su.SuProxyMaker( 'Need to become root to test this module.') yield maker.blocker root = maker.get_root() response = root.spawnvpe(os.P_NOWAIT, 'false', ['false']) yield response pid = response.result assert pid response = root.waitpid(pid, 0) yield response (pid, status) = response.result exitstatus = os.WEXITSTATUS(status) assert exitstatus != 0 response = root.spawnvpe(os.P_WAIT, 'true', ['true']) yield response assert response.result == 0 response = root.getuid() yield response assert response.result == 0 response = root.setuid(os.getuid()) yield response assert response.result is None response = root.getuid() yield response assert response.result == os.getuid() root.finish() g.main_quit() tasks.Task(run()) g.main()
def testFileRead(self): tmp_file = tempfile.mktemp(suffix = '-roxlib-test') s = file(tmp_file, 'w') s.write('Hello\n') s.close() root = self.master.root def run(): response = root.open(tmp_file) yield response stream = response.result response = root.read(stream, 5) yield response assert "Hello" == response.result response = root.close(stream) yield response assert response.result is None g.main_quit() tasks.Task(run()) g.main() os.unlink(tmp_file)
def testSu(self): def run(): maker = su.SuProxyMaker('Need to become root to test this module.') yield maker.blocker root = maker.get_root() response = root.spawnvpe(os.P_NOWAIT, 'false', ['false']) yield response pid = response.result assert pid response = root.waitpid(pid, 0) yield response (pid, status) = response.result exitstatus = os.WEXITSTATUS(status) assert exitstatus != 0 response = root.spawnvpe(os.P_WAIT, 'true', ['true']) yield response assert response.result == 0 response = root.getuid() yield response assert response.result == 0 response = root.setuid(os.getuid()) yield response assert response.result is None response = root.getuid() yield response assert response.result == os.getuid() root.finish() g.main_quit() tasks.Task(run()) g.main()
def manage_session(test_mode): log.init() if not mydbus.dbus_version_ok and not xxmlrpc.working: rox.alert(problem_msg) set_up_environment() session.init() children.init() session_dbus.init() # Start even if DBus is too old, for session bus xml_settings = settings.init() if mydbus.dbus_version_ok: service = dbus.service.BusName(constants.session_service, bus=session_dbus.get_session_bus()) SessionObject3x(service) # This is like the D-BUS service, except using XML-RPC-over-X xml_service = xxmlrpc.XXMLRPCServer(constants.session_service) xml_service.add_object('/Session', XMLSessionObject()) xml_service.add_object('/Settings', xml_settings) try: if test_mode: print "Test mode!" print "Started", os.system( "(/bin/echo hi >&2; sleep 4; date >&2)&") print "OK" else: try: wm.start() except: rox.report_exception() g.main() finally: session_dbus.destroy()
from rox import xxmlrpc, g, tasks try: proxy = xxmlrpc.XXMLProxy(memo_service) # Check to make sure it really is running... def check(): call = proxy.get_object('/').get_pid() yield call, tasks.TimeoutBlocker(2) if call.happened: pid = call.get_response() rox.alert('Memo is already running (PID = %d)!' % pid) os._exit(1) g.main_quit() tasks.Task(check()) g.main() print "Possible existing copy of Memo is not responding" except xxmlrpc.NoSuchService: pass # Good server = xxmlrpc.XXMLRPCServer(memo_service) class MemoObject: allowed_methods = ['get_pid'] def get_pid(self): return os.getpid() server.add_object('/', MemoObject())
def show_exception(type, value, tb, auto_details = False): """Display this exception in an error box. The user has the options of ignoring the error, quitting the application and examining the exception in more detail. See also rox.report_exception().""" QUIT = 1 DETAILS = 2 SAVE = 3 brief = ''.join(traceback.format_exception_only(type, value)) toplevel_ref() box = g.MessageDialog(None, 0, g.MESSAGE_ERROR, g.BUTTONS_NONE, brief) if not auto_details: button = ButtonMixed(g.STOCK_ZOOM_IN, _('_Details')) button.set_flags(g.CAN_DEFAULT) button.show() box.add_action_widget(button, DETAILS) box.add_button(g.STOCK_HELP, g.RESPONSE_HELP) box.add_button(g.STOCK_OK, g.RESPONSE_OK) box.set_default_response(g.RESPONSE_OK) box.set_position(g.WIN_POS_CENTER) box.set_title(_('Error')) reply = [] def response(box, resp): reply.append(resp) g.main_quit() box.connect('response', response) box.show() if tb: bug_report = 'Traceback (most recent call last):\n' + \ ''.join(traceback.format_stack(tb.tb_frame.f_back) + traceback.format_tb(tb) + traceback.format_exception_only(type, value)) else: bug_report = 'No stack trace.' while 1: if auto_details: resp = DETAILS auto_details = False else: g.main() resp = reply.pop() if resp == int(g.RESPONSE_OK) or resp == int(g.RESPONSE_DELETE_EVENT): break if resp == SAVE: global savebox if savebox: savebox.destroy() def destroy(box): global savebox # For pychecker savebox = None from saving import StringSaver savebox = StringSaver(bug_report, 'BugReport') savebox.connect('destroy', destroy) savebox.show() continue if resp == QUIT: sys.exit(1) elif resp == int(g.RESPONSE_HELP): _show_debug_help() continue assert resp == DETAILS box.set_response_sensitive(DETAILS, False) button = ButtonMixed(g.STOCK_SAVE, _('_Bug Report')) button.set_flags(g.CAN_DEFAULT) button.show() box.add_action_widget(button, SAVE) box.action_area.set_child_secondary(button, True) button = ButtonMixed(g.STOCK_QUIT, _('Forced Quit')) button.set_flags(g.CAN_DEFAULT) button.show() box.add_action_widget(button, QUIT) box.action_area.set_child_secondary(button, True) if tb: ee = ExceptionExplorer(tb) box.vbox.pack_start(ee) ee.show() else: no_trace = g.Label('No traceback object!') box.vbox.pack_start(no_trace) no_trace.show() box.destroy() toplevel_unref()