[Twisted-Python] websetroot

Moshe Zadka twisted at moshez.org
Thu Oct 17 05:22:17 EDT 2002


I don't want to check in a new utility without some discussion. Here is the
patch:

diff -urN -x *.pyc -x CVS -x static -x howto Twisted.clean/ChangeLog Twisted/ChangeLog
--- Twisted.clean/ChangeLog	Tue Oct 15 07:04:57 2002
+++ Twisted/ChangeLog	Thu Oct 17 04:18:18 2002
@@ -1,3 +1,8 @@
+2002-10-17  Moshe Zadka <moshez at twistedmatrix.com>
+
+	* bin/websetroot twisted/scripts/websetroot.py: Added a program to set
+	the root of a web server after the tap exists
+
 2002-10-14  Moshe Zadka <moshez at twistedmatrix.com>
 
 	* twisted/web/vhost.py: add a virtual host monster to support twisted
diff -urN -x *.pyc -x CVS -x static -x howto Twisted.clean/bin/websetroot Twisted/bin/websetroot
--- Twisted.clean/bin/websetroot	Wed Dec 31 18:00:00 1969
+++ Twisted/bin/websetroot	Thu Oct 17 03:22:21 2002
@@ -0,0 +1,29 @@
+#!/usr/bin/env python
+
+# Twisted, the Framework of Your Internet
+# Copyright (C) 2001 Matthew W. Lefkowitz
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of version 2.1 of the GNU Lesser General Public
+# License as published by the Free Software Foundation.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+
+### Twisted Preamble
+# This makes sure that users don't have to set up their environment
+# specially in order to run these programs from bin/.
+import sys, os, string
+if string.find(os.path.abspath(sys.argv[0]), os.sep+'Twisted') != -1:
+    sys.path.insert(0, os.path.normpath(os.path.join(os.path.abspath(sys.argv[0]), os.pardir, os.pardir)))
+sys.path.insert(0, os.curdir)
+### end of preamble
+
+from twisted.scripts.websetroot import run
+run()
diff -urN -x *.pyc -x CVS -x static -x howto Twisted.clean/doc/examples/index.html Twisted/doc/examples/index.html
--- Twisted.clean/doc/examples/index.html	Thu Oct 17 03:46:45 2002
+++ Twisted/doc/examples/index.html	Thu Oct 17 04:20:02 2002
@@ -87,6 +87,7 @@
         <li><a href="hello.rpy.py">hello.rpy.py</a></li>
         <li><a href="simple.rtl">simple.rtl</a></li>
 	<li><a href="courier.py">courier.py</a></li>
+	<li><a href="rootscript.py">rootscript.py</a></li>
     </ul>
     
     <h2>Obsolete?</h2>
diff -urN -x *.pyc -x CVS -x static -x howto Twisted.clean/doc/examples/rootscript.py Twisted/doc/examples/rootscript.py
--- Twisted.clean/doc/examples/rootscript.py	Wed Dec 31 18:00:00 1969
+++ Twisted/doc/examples/rootscript.py	Thu Oct 17 03:42:57 2002
@@ -0,0 +1,9 @@
+from twisted.web import vhost, static, script
+
+default = static.Data('text/html', '')
+default.putChild('vhost', vhost.VHostMonsterResource())
+resource = vhost.NameVirtualHost()
+resource.default = default
+file = static.File('static')
+file.processors = {'.rpy': script.ResourceScript}
+resource.addHost('twistedmatrix.com', file)
diff -urN -x *.pyc -x CVS -x static -x howto Twisted.clean/doc/man/index.html Twisted/doc/man/index.html
--- Twisted.clean/doc/man/index.html	Thu Oct  3 14:24:18 2002
+++ Twisted/doc/man/index.html	Thu Oct 17 04:17:34 2002
@@ -19,6 +19,7 @@
 <li><a href="tap2deb.html"> tap2deb</a></li>
 <li><a href="tapconvert.html">tapconvert</a></li>
 <li><a href="twistd.html">twistd</a></li>
+<li><a href="websetroot.html">websetroot</a></li>
 </ul>
 
 </body></html>
diff -urN -x *.pyc -x CVS -x static -x howto Twisted.clean/doc/man/websetroot.1 Twisted/doc/man/websetroot.1
--- Twisted.clean/doc/man/websetroot.1	Wed Dec 31 18:00:00 1969
+++ Twisted/doc/man/websetroot.1	Thu Oct 17 04:17:15 2002
@@ -0,0 +1,36 @@
+.TH WEBSETROOT "1" "July 2001" "" ""
+.SH NAME
+websetroot \- set the root of a Twisted web server
+.SH SYNOPSIS
+.B websetroot {-f \fItapfile\fR | -y \fIcodefile\fR | -x \fIXML\fR | -s \fIAOT\fR } {--pickle \fIpickle\fR | --script \fIscript\fR } [ -e ] [--port \fIport\fR]
+.B websetroot --help
+.SH DESCRIPTION
+.PP
+The \fB\--help\fR prints out a usage message to standard output.
+.TP \fB-e\fR, \fB--encrypted\fR
+The specified tap/aos/xml file is encrypted.
+.TP \fB-p\fR, \fB--port\fR <port>
+The port the web server is running on [default: 80]
+.TP \fB-f\fR, \fB--file\fR <file>
+read the given .tap file [default: twistd.tap]
+.TP \fB-y\fR, \fB--python\fR <file>
+read an application from within a Python file
+.TP \fB-x\fR, \fB--xml\fR <file>
+Read an application from a .tax file (Marmalade format).
+.TP \fB-s\fR, \fB--source\fR <file>
+Read an application from a .tas file (AOT format).
+.TP \fB--script\fR <file>
+Read the root resource from the given resource script file
+.TP \fB--pickle\fR <file>
+Read the root resource from the given resource pickle file
+.SH AUTHOR
+Written by Moshe Zadka, based on websetroot's help messages
+.SH "REPORTING BUGS"
+Report bugs to <twisted-python at twistedmatrix.com>.
+.SH COPYRIGHT
+Copyright \(co 2000 Matthew W. Lefkowitz
+.br
+This is free software; see the source for copying conditions.  There is NO
+warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+.SH "SEE ALSO"
+mktap(1)
diff -urN -x *.pyc -x CVS -x static -x howto Twisted.clean/twisted/scripts/websetroot.py Twisted/twisted/scripts/websetroot.py
--- Twisted.clean/twisted/scripts/websetroot.py	Wed Dec 31 18:00:00 1969
+++ Twisted/twisted/scripts/websetroot.py	Thu Oct 17 03:33:35 2002
@@ -0,0 +1,75 @@
+# Twisted, the Framework of Your Internet
+# Copyright (C) 2001-2002 Matthew W. Lefkowitz
+# 
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of version 2.1 of the GNU Lesser General Public
+# License as published by the Free Software Foundation.
+# 
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+# 
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+# 
+from twisted.scripts import twistd
+from twisted.python import usage
+
+class Options(usage.Options):
+
+    optFlags = [
+                ['encrypted', 'e' ,
+                 "The specified tap/aos/xml file is encrypted."]
+               ]
+
+
+    optParameters = [
+                  ['port','p', 80,
+                   "The port the web server is running on"],
+                  ['file','f','twistd.tap',
+                   "read the given .tap file"],
+                  ['python','y', None,
+                   "read an application from within a Python file"],
+                  ['xml', 'x', None,
+                   "Read an application from a .tax file (Marmalade format)."],
+                  ['source', 's', None,
+                   "Read an application from a .tas file (AOT format)."],
+                    ]
+
+    def opt_script(self, scriptname):
+        d = {}
+        execfile(scriptname, d)
+        self['root'] = d['resource']
+
+    def opt_pickle(self, picklename):
+        self['root'] = cPickle.load(open(picklename))
+ 
+
+def getFactory(app, port):
+    for (num, fact, _, _) in app.tcpPorts:
+        if num == port:
+            return fact
+    raise LookupError('no such port')
+
+def main(config):
+    if config['encrypted']:
+        import getpass
+        passphrase = getpass.getpass('Passphrase: ')
+    else:
+        passphrase = None
+    application = twistd.loadApplication(config, passphrase)
+    site = getFactory(application, int(config['port']))
+    site.resource = config['root']
+    application.save()
+
+
+def run():
+    import sys
+    config = Options()
+    config.parseOptions()
+    try:
+        main(config)
+    except LookupError, err:
+        sys.exit(sys.argv[0]+": "+str(err))





More information about the Twisted-Python mailing list