python: Added logging initialization to scgi-wsgi.
[ashd.git] / python / scgi-wsgi
1 #!/usr/bin/python
2
3 import sys, os, getopt, logging
4 import socket
5 import ashd.scgi
6
7 def usage(out):
8     out.write("usage: scgi-wsgi [-hAL] [-p MODPATH] [-T [HOST:]PORT] HANDLER-MODULE [ARGS...]\n")
9
10 sk = None
11 modwsgi_compat = False
12 setlog = True
13 opts, args = getopt.getopt(sys.argv[1:], "+hALp:T:")
14 for o, a in opts:
15     if o == "-h":
16         usage(sys.stdout)
17         sys.exit(0)
18     elif o == "-p":
19         sys.path.insert(0, a)
20     elif o == "-L":
21         setlog = False
22     elif o == "-T":
23         sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
24         sk.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
25         p = a.rfind(":")
26         if p < 0:
27             bindhost = "localhost"
28             bindport = int(a)
29         else:
30             bindhost = a[:p]
31             bindport = int(a[p + 1:])
32         sk.bind((bindhost, bindport))
33         sk.listen(32)
34     elif o == "-A":
35         modwsgi_compat = True
36 if len(args) < 1:
37     usage(sys.stderr)
38     sys.exit(1)
39 if setlog:
40     logging.basicConfig(format="ashd-wsgi(%(name)s): %(levelname)s: %(message)s")
41
42 if sk is None:
43     # This is suboptimal, since the socket on stdin is not necessarily
44     # AF_UNIX, but Python does not seem to offer any way around it,
45     # that I can find.
46     sk = socket.fromfd(0, socket.AF_UNIX, socket.SOCK_STREAM)
47
48 try:
49     handlermod = __import__(args[0], fromlist = ["dummy"])
50 except ImportError, exc:
51     sys.stderr.write("scgi-wsgi: handler %s not found: %s\n" % (args[0], exc.message))
52     sys.exit(1)
53 if not modwsgi_compat:
54     if not hasattr(handlermod, "wmain"):
55         sys.stderr.write("scgi-wsgi: handler %s has no `wmain' function\n" % args[0])
56         sys.exit(1)
57     handler = handlermod.wmain(*args[1:])
58 else:
59     if not hasattr(handlermod, "application"):
60         sys.stderr.write("scgi-wsgi: handler %s has no `application' object\n" % args[0])
61         sys.exit(1)
62     handler = handlermod.application
63
64 ashd.scgi.servescgi(sk, ashd.scgi.wrapwsgi(handler))