def test_simpleStoreAndLoad(self): a = service.Application("hello") p = sob.IPersistable(a) for style in "source pickle".split(): p.setStyle(style) p.save() a1 = service.loadApplication("hello.ta" + style[0], style) self.assertEqual(service.IService(a1).name, "hello") f = open("hello.tac", "w") f.writelines(["from twisted.application import service\n", "application = service.Application('hello')\n"]) f.close() a1 = service.loadApplication("hello.tac", "python") self.assertEqual(service.IService(a1).name, "hello")
def test_simpleStoreAndLoad(self): a = service.Application("hello") p = sob.IPersistable(a) for style in "xml source pickle".split(): if style == "xml" and not gotMicrodom: continue p.setStyle(style) p.save() a1 = service.loadApplication("hello.ta" + style[0], style) self.assertEqual(service.IService(a1).name, "hello") open("hello.tac", "w").writelines( ["from twisted.application import service\n", "application = service.Application('hello')\n"] ) a1 = service.loadApplication("hello.tac", "python") self.assertEqual(service.IService(a1).name, "hello")
def test_simpleStoreAndLoad(self): a = service.Application("hello") p = sob.IPersistable(a) for style in "source pickle".split(): p.setStyle(style) p.save() a1 = service.loadApplication("hello.ta" + style[0], style) self.assertEqual(service.IService(a1).name, "hello") with open("hello.tac", "w") as f: f.writelines([ "from twisted.application import service\n", "application = service.Application('hello')\n", ]) a1 = service.loadApplication("hello.tac", "python") self.assertEqual(service.IService(a1).name, "hello")
def convertStyle(filein, typein, passphrase, fileout, typeout, encrypt): application = service.loadApplication(filein, typein, passphrase) sob.IPersistable(application).setStyle(typeout) passphrase = getSavePassphrase(encrypt) if passphrase: fileout = None sob.IPersistable(application).save(filename=fileout, passphrase=passphrase)
def test_simpleStoreAndLoad(self): a = service.Application("hello") p = sob.IPersistable(a) for style in 'xml source pickle'.split(): if style == 'xml' and not gotMicrodom: continue p.setStyle(style) p.save() a1 = service.loadApplication("hello.ta" + style[0], style) self.assertEqual(service.IService(a1).name, "hello") open("hello.tac", 'w').writelines([ "from twisted.application import service\n", "application = service.Application('hello')\n", ]) a1 = service.loadApplication("hello.tac", 'python') self.assertEqual(service.IService(a1).name, "hello")
def test_convertStyle(self): appl = service.Application("lala") for instyle in "source pickle".split(): for outstyle in "source pickle".split(): sob.IPersistable(appl).setStyle(instyle) sob.IPersistable(appl).save(filename="converttest") app.convertStyle("converttest", instyle, None, "converttest.out", outstyle, 0) appl2 = service.loadApplication("converttest.out", outstyle) self.assertEqual(service.IService(appl2).name, "lala")
def test_convertStyle(self): appl = service.Application("lala") for instyle in 'source pickle'.split(): for outstyle in 'source pickle'.split(): sob.IPersistable(appl).setStyle(instyle) sob.IPersistable(appl).save(filename="converttest") app.convertStyle("converttest", instyle, None, "converttest.out", outstyle, 0) appl2 = service.loadApplication("converttest.out", outstyle) self.assertEqual(service.IService(appl2).name, "lala")
def convertStyle(filein, typein, passphrase, fileout, typeout, encrypt): # FIXME: https://twistedmatrix.com/trac/ticket/7827 # twisted.persisted is not yet ported to Python 3, so import it here. from twisted.persisted import sob application = service.loadApplication(filein, typein, passphrase) sob.IPersistable(application).setStyle(typeout) passphrase = getSavePassphrase(encrypt) if passphrase: fileout = None sob.IPersistable(application).save(filename=fileout, passphrase=passphrase)
def addToApplication(ser, name, append, procname, type, encrypted, uid, gid): if append and os.path.exists(append): a = service.loadApplication(append, 'pickle', None) else: a = service.Application(name, uid, gid) if procname: service.IProcess(a).processName = procname ser.setServiceParent(service.IServiceCollection(a)) sob.IPersistable(a).setStyle(type) passphrase = app.getSavePassphrase(encrypted) if passphrase: append = None sob.IPersistable(a).save(filename=append, passphrase=passphrase)
def test_implicitConversion(self): a = Dummy() a.__dict__ = {'udpConnectors': [], 'unixConnectors': [], '_listenerDict': {}, 'name': 'dummy', 'sslConnectors': [], 'unixPorts': [], '_extraListeners': {}, 'sslPorts': [], 'tcpPorts': [], 'services': {}, 'gid': 0, 'tcpConnectors': [], 'extraConnectors': [], 'udpPorts': [], 'extraPorts': [], 'uid': 0} pickle.dump(a, open("file.tap", 'wb')) a1 = service.loadApplication("file.tap", "pickle", None) self.assertEqual(service.IService(a1).name, "dummy") self.assertEqual(list(service.IServiceCollection(a1)), [])
def test_convertStyle(self): appl = service.Application("lala") for instyle in "xml source pickle".split(): if instyle == "xml" and not gotMicrodom: continue for outstyle in "xml source pickle".split(): if outstyle == "xml" and not gotMicrodom: continue sob.IPersistable(appl).setStyle(instyle) sob.IPersistable(appl).save(filename="converttest") app.convertStyle("converttest", instyle, None, "converttest.out", outstyle, 0) appl2 = service.loadApplication("converttest.out", outstyle) self.assertEqual(service.IService(appl2).name, "lala")
def test_convertStyle(self): appl = service.Application("lala") for instyle in 'xml source pickle'.split(): if instyle == 'xml' and not gotMicrodom: continue for outstyle in 'xml source pickle'.split(): if outstyle == 'xml' and not gotMicrodom: continue sob.IPersistable(appl).setStyle(instyle) sob.IPersistable(appl).save(filename="converttest") app.convertStyle("converttest", instyle, None, "converttest.out", outstyle, 0) appl2 = service.loadApplication("converttest.out", outstyle) self.assertEqual(service.IService(appl2).name, "lala")
def getApplication(config, passphrase): s = [(config[t], t) for t in ["python", "xml", "source", "file"] if config[t]][0] filename, style = s[0], {"file": "pickle"}.get(s[1], s[1]) try: log.msg("Loading %s..." % filename) application = service.loadApplication(filename, style, passphrase) log.msg("Loaded.") except Exception, e: s = "Failed to load application: %s" % e if isinstance(e, KeyError) and e.args[0] == "application": s += """ Could not find 'application' in the file. To use 'twistd -y', your .tac file must create a suitable object (e.g., by calling service.Application()) and store it in a variable named 'application'. twistd loads your .tac file and scans the global variables for one of this name. Please read the 'Using Application' HOWTO for details. """ traceback.print_exc(file=log.logfile) log.msg(s) log.deferr() sys.exit("\n" + s + "\n")
def _saveConfiguredIDTest(self, argv, uid, gid): """ Test that when L{run} is invoked and L{sys.argv} has the given value, the resulting application has the specified UID and GID. @type argv: C{list} of C{str} @param argv: The value to which to set L{sys.argv} before calling L{run}. @type uid: C{int} @param uid: The expected value for the resulting application's L{IProcess.uid}. @type gid: C{int} @param gid: The expected value for the resulting application's L{IProcess.gid}. """ sys.argv = argv run() app = loadApplication("ftp.tap", "pickle", None) process = IProcess(app) self.assertEqual(process.uid, uid) self.assertEqual(process.gid, gid)
def getApplication(config, passphrase): s = [(config[t], t) for t in ['python', 'source', 'file'] if config[t]][0] filename, style = s[0], {'file': 'pickle'}.get(s[1], s[1]) try: log.msg("Loading %s..." % filename) application = service.loadApplication(filename, style, passphrase) log.msg("Loaded.") except Exception, e: s = "Failed to load application: %s" % e if isinstance(e, KeyError) and e.args[0] == "application": s += """ Could not find 'application' in the file. To use 'twistd -y', your .tac file must create a suitable object (e.g., by calling service.Application()) and store it in a variable named 'application'. twistd loads your .tac file and scans the global variables for one of this name. Please read the 'Using Application' HOWTO for details. """ traceback.print_exc(file=log.logfile) log.msg(s) log.deferr() sys.exit('\n' + s + '\n')
def test_implicitConversion(self): a = Dummy() a.__dict__ = { 'udpConnectors': [], 'unixConnectors': [], '_listenerDict': {}, 'name': 'dummy', 'sslConnectors': [], 'unixPorts': [], '_extraListeners': {}, 'sslPorts': [], 'tcpPorts': [], 'services': {}, 'gid': 0, 'tcpConnectors': [], 'extraConnectors': [], 'udpPorts': [], 'extraPorts': [], 'uid': 0 } pickle.dump(a, open("file.tap", 'wb')) a1 = service.loadApplication("file.tap", "pickle", None) self.assertEqual(service.IService(a1).name, "dummy") self.assertEqual(list(service.IServiceCollection(a1)), [])
def test_implicitConversion(self): a = Dummy() a.__dict__ = { "udpConnectors": [], "unixConnectors": [], "_listenerDict": {}, "name": "dummy", "sslConnectors": [], "unixPorts": [], "_extraListeners": {}, "sslPorts": [], "tcpPorts": [], "services": {}, "gid": 0, "tcpConnectors": [], "extraConnectors": [], "udpPorts": [], "extraPorts": [], "uid": 0, } pickle.dump(a, open("file.tap", "wb")) a1 = service.loadApplication("file.tap", "pickle", None) self.assertEqual(service.IService(a1).name, "dummy") self.assertEqual(list(service.IServiceCollection(a1)), [])
class TkMkAppFrame(Tkinter.Frame): """ A frame with all the necessary widgets to configure a Twisted Application. """ # Plugin currently selected coil = None # Options instance currently displayed options = None # Frame options are displayed in optFrame = None def __init__(self, master, coil): Tkinter.Frame.__init__(self, master) self.setupMkTap() self.reset(coil) def setupMkTap(self): # Create all of the "mktap" option widgets appFrame = Tkinter.Frame(self) f = Tkinter.Frame(appFrame) listLabel = Tkinter.Label(f, text='TAp Format') self.typeList = Tkinter.Listbox(f, background='white') self.typeList['height'] = 3 for t in ('pickle', 'xml', 'source'): self.typeList.insert(Tkinter.END, t) self.typeList.selection_set(0) listLabel.pack(side=Tkinter.TOP) self.typeList.pack(side=Tkinter.TOP) f.pack(side=Tkinter.LEFT, anchor=Tkinter.N) f = Tkinter.Frame(appFrame) tapLabel = Tkinter.Label(f, text='TAp Filename') tapButton = Tkinter.Button(f, text="Choose", command=self.pickTapFile) self.tapfile = Tkinter.Entry(f, background='white') tapLabel.pack(side=Tkinter.LEFT) self.tapfile.pack(side=Tkinter.LEFT) tapButton.pack(side=Tkinter.LEFT) f.pack(side=Tkinter.TOP, anchor=Tkinter.E) f = Tkinter.Frame(appFrame) nameLabel = Tkinter.Label(f, text='Application Process Name') self.appname = Tkinter.Entry(f, background='white') nameLabel.pack(side=Tkinter.LEFT) self.appname.pack(side=Tkinter.LEFT) f.pack(side=Tkinter.TOP, anchor=Tkinter.E) f = Tkinter.Frame(appFrame) encLabel = Tkinter.Label(f, text='Passphrase') self.passphrase = Tkinter.Entry(f, background='white') encLabel.pack(side=Tkinter.LEFT) self.passphrase.pack(side=Tkinter.LEFT) f.pack(side=Tkinter.TOP, anchor=Tkinter.E) f = Tkinter.Frame(appFrame) self.append = Tkinter.BooleanVar() appLabel = Tkinter.Label(f, text='Append') appButton = Tkinter.Checkbutton(f, variable=self.append) appLabel.pack(side=Tkinter.LEFT) appButton.pack(side=Tkinter.LEFT) f.pack(side=Tkinter.LEFT, anchor=Tkinter.E) f = Tkinter.Frame(appFrame) s = Tkinter.StringVar() s.set(not hasattr(os, 'getuid') and '0' or str(os.getuid())) uidLabel = Tkinter.Label(f, text='UID') self.uid = Tkinter.Entry(f, text=s, background='white') uidLabel.pack(side=Tkinter.LEFT) self.uid.pack(side=Tkinter.LEFT) f.pack(side=Tkinter.BOTTOM) f = Tkinter.Frame(appFrame) s = Tkinter.StringVar() s.set(not hasattr(os, 'getgid') and '0' or str(os.getgid())) gidLabel = Tkinter.Label(f, text='GID') self.gid = Tkinter.Entry(f, text=s, background='white') gidLabel.pack(side=Tkinter.LEFT) self.gid.pack(side=Tkinter.LEFT) f.pack(side=Tkinter.BOTTOM) appFrame.grid(row=0, column=0, columnspan=3, sticky=Tkinter.N + Tkinter.S) def pickTapFile(self): r = tkFileDialog.askopenfilename() if r: self.tapfile.delete(0, Tkinter.END) self.tapfile.insert(Tkinter.END, r) def reset(self, coil): """ Remove the existing coil-specific widgets and then create and add new ones based on the given plugin object. """ if coil is self.coil: return try: opt = coil.load().Options() except: f = StringIO.StringIO() traceback.print_exc(file=f) # XXX - Why is this so narrow? tkMessageBox.showerror(title="Options Error", message=f.getvalue(), parent=self) return if self.optFrame: self.optFrame.forget() self.optFrame.destroy() self.optFrame = None self.coil = coil self.options = opt self.optFrame = TkConfigFrame(self, self.options) self.optFrame.grid(row=1, column=0) # self.tapfile.delete(0, Tkinter.END) # try: # self.tapfile.insert(Tkinter.END, self.coil.tapname) # except AttributeError: # self.tapfile.insert(Tkinter.END, self.coil.name) def copyOptions(self): # Snarf the data out of the widgets and place them into the Options # instance. extra = self.optFrame.updateConfig(self.options) self.options['filename'] = self.tapfile.get() self.options['appname'] = self.appname.get() self.options['passphrase'] = self.passphrase.get() self.options['append'] = self.append.get() self.options['encrypted'] = len(self.options['passphrase']) self.options['uid'] = int(self.uid.get()) self.options['gid'] = int(self.gid.get()) try: self.options['type'] = self.typeList.curselection()[0] except IndexError: raise usage.UsageError("Select a TAp Format") self.options['help'] = 0 if extra: try: # XXX - this is wrong. It needs to respect quotes, etc. self.options.parseArgs(extra.split()) except TypeError: raise usage.UsageError("Wrong number of extra arguments") self.options.postOptions() def createApplication(self): if not self.options: tkMessageBox.showerror(message="Select an Application first") return try: self.copyOptions() except usage.UsageError, e: tkMessageBox.showerror(message=str(e)) return exists = os.path.exists(self.options['filename']) if self.options['append'] and exists: a = service.loadApplication(self.options['filename'], self.options['type'], self.options['passphrase']) else: if exists: overwrite = tkMessageBox.askyesno(title='File Exists', message='Overwrite?') if not overwrite: return a = service.Application(self.coil.name, self.options['uid'], self.options['gid']) try: s = mktap.makeService(self.coil.load(), self.options['appname'], self.options) except usage.UsageError: f = StringIO.StringIO() traceback.print_stack(file=f) tkMessageBox.showerror(title="Usage Error", message=f.getvalue(), parent=self) else: try: mktap.addToApplication( s, self.coil.name, self.options['append'], self.options['appname'], self.options['type'], self.options['encrypted'], self.options['uid'], self.options['gid'], ) except: f = StringIO.StringIO() traceback.print_exc(file=f) print f.getvalue() tkMessageBox.showerror(title="Usage Error", message=f.getvalue(), parent=self) else: filename = self.options['filename'] if not filename: filename = self.coil.name tkMessageBox.showinfo(message="Wrote " + filename)