]>
Commit | Line | Data |
---|---|---|
8b8ebb78 | 1 | #!/usr/bin/env python |
1d924d97 | 2 | # this entire thing is a hack and badly needs reimplementing |
a409b906 CP |
3 | import bin.compile |
4 | bin.compile.vcheck() | |
5 | ||
8b8ebb78 CP |
6 | DEFAULT_PORT = 9090 |
7 | ||
8b8ebb78 | 8 | from optparse import OptionParser |
42fdc4c6 | 9 | import sys, os, config |
8b8ebb78 CP |
10 | |
11 | def run_twistd(args1=None, args2=None): | |
724f3003 | 12 | from twisted.scripts.twistd import run |
8b8ebb78 CP |
13 | args = [sys.argv[0]] |
14 | if args1 is not None: | |
15 | args.extend(args1) | |
16 | args.append("qwebirc") | |
17 | if args2 is not None: | |
18 | args.extend(args2) | |
19 | sys.argv = args | |
20 | run() | |
21 | ||
22 | def help_reactors(*args): | |
23 | run_twistd(["--help-reactors"]) | |
24 | sys.exit(1) | |
25 | ||
7637ceb0 CP |
26 | try: |
27 | from select import epoll | |
28 | DEFAULT_REACTOR = "epoll" | |
29 | except ImportError: | |
30 | try: | |
31 | from select import kqueue | |
32 | DEFAULT_REACTOR = "kqueue" | |
33 | except ImportError: | |
34 | try: | |
35 | from select import poll | |
36 | DEFAULT_REACTOR = "poll" | |
37 | except ImportError: | |
38 | DEFAULT_REACTOR = "select" | |
8b8ebb78 CP |
39 | |
40 | parser = OptionParser() | |
41 | parser.add_option("-n", "--no-daemon", help="Don't run in the background.", action="store_false", dest="daemonise", default=True) | |
42 | parser.add_option("--help-reactors", help="Display a list of reactor names.", action="callback", callback=help_reactors) | |
43 | parser.add_option("-b", "--debug", help="Run in the Python Debugger.", action="store_true", dest="debug", default=False) | |
0f3c92dc | 44 | parser.add_option("-t", "--tracebacks", help="Display tracebacks in error pages (this reveals a LOT of information, do NOT use in production!)", action="store_true", dest="tracebacks", default=False) |
8b8ebb78 CP |
45 | parser.add_option("-r", "--reactor", help="Which reactor to use (see --help-reactors for a list).", dest="reactor", default=DEFAULT_REACTOR) |
46 | parser.add_option("-p", "--port", help="Port to start the server on.", type="int", dest="port", default=DEFAULT_PORT) | |
ef74f2c1 | 47 | parser.add_option("-i", "--ip", help="IP address to listen on.", dest="ip", default="0.0.0.0") |
8b8ebb78 CP |
48 | parser.add_option("-l", "--logfile", help="Path to twisted log file.", dest="logfile") |
49 | parser.add_option("-c", "--clf", help="Path to web CLF (Combined Log Format) log file.", dest="clogfile") | |
64429ed7 CP |
50 | parser.add_option("-C", "--certificate", help="Path to SSL certificate.", dest="sslcertificate") |
51 | parser.add_option("-k", "--key", help="Path to SSL key.", dest="sslkey") | |
9e93b6fe | 52 | parser.add_option("-H", "--certificate-chain", help="Path to SSL certificate chain file.", dest="sslchain") |
4cfa1a92 | 53 | parser.add_option("-P", "--pidfile", help="Path to store PID file", dest="pidfile") |
39cf79cc | 54 | parser.add_option("-s", "--syslog", help="Log to syslog", action="store_true", dest="syslog", default=False) |
b1e3cd67 CP |
55 | parser.add_option("--profile", help="Run in profile mode, dumping results to this file", dest="profile") |
56 | parser.add_option("--profiler", help="Name of profiler to use", dest="profiler") | |
39cf79cc | 57 | parser.add_option("--syslog-prefix", help="Syslog prefix", dest="syslog_prefix", default="qwebirc") |
42fdc4c6 CP |
58 | |
59 | sargs = sys.argv[1:] | |
60 | if "ARGS" in dir(config): | |
61 | import shlex | |
62 | sargs = shlex.split(config.ARGS) + sargs | |
63 | ||
64 | (options, args) = parser.parse_args(args=sargs) | |
8b8ebb78 CP |
65 | |
66 | args1, args2 = [], [] | |
67 | ||
68 | if not options.daemonise: | |
69 | args1.append("-n") | |
70 | if options.debug: | |
71 | args1.append("-b") | |
4bc06f30 CP |
72 | |
73 | if options.reactor != DEFAULT_REACTOR: | |
724f3003 CP |
74 | rn = options.reactor + "reactor" |
75 | getattr(__import__("twisted.internet", fromlist=[rn]), rn).install() | |
8b8ebb78 | 76 | if options.logfile: |
4cfa1a92 CP |
77 | args1+=["--logfile", options.logfile] |
78 | if options.pidfile: | |
79 | args1+=["--pidfile", options.pidfile] | |
39cf79cc CP |
80 | if options.syslog: |
81 | args1+=["--syslog"] | |
b1e3cd67 CP |
82 | if options.profile: |
83 | args1+=["--profile", options.profile] | |
84 | if options.profiler: | |
85 | args1+=["--profiler", options.profiler] | |
86 | ||
94ba3643 | 87 | if options.syslog and options.syslog_prefix: |
39cf79cc CP |
88 | import syslog |
89 | syslog.openlog(options.syslog_prefix) | |
8b8ebb78 | 90 | |
0f3c92dc CP |
91 | if not options.tracebacks: |
92 | args2.append("-n") | |
8b8ebb78 CP |
93 | if options.clogfile: |
94 | args2+=["--logfile", options.clogfile] | |
0f3c92dc | 95 | |
64429ed7 CP |
96 | if options.sslcertificate and options.sslkey: |
97 | args2+=["--certificate", options.sslcertificate, "--privkey", options.sslkey, "--https", options.port] | |
9e93b6fe CP |
98 | if options.sslchain: |
99 | args2+=["--certificate-chain", options.sslchain] | |
64429ed7 CP |
100 | else: |
101 | args2+=["--port", options.port] | |
102 | ||
ef74f2c1 | 103 | args2+=["--ip", options.ip] |
104 | ||
df5f9be6 CP |
105 | if os.name == "posix" and os.getuid() == 0: |
106 | print >>sys.stderr, "refusing to run as root" | |
107 | os.exit(1) | |
108 | ||
8b8ebb78 | 109 | run_twistd(args1, args2) |