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