anandology / hacks

Utility scripts

This URL has Read+Write access

hacks / lightty
100755 141 lines (107 sloc) 3.423 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#! /usr/bin/env python
"""Starts lighttpd with a give document root.
Optional script argument can be specified to run it as fastcgi.
 
Examples:
 
Start Lighttpd with current working dir as document-root.
 
$ lightty code.py
http://0.0.0.0:8080
...
 
Start Lighttpd with run.py as fastcgi.
 
$ lightty code.py -p 9000 -s run.py
http://0.0.0.0:9000
...
"""
 
__author__ = "Anand Chitipothu <anandology@gmail.com>"
 
import sys
import os
import string
from optparse import OptionParser
 
CONF = """
server.modules = ("mod_fastcgi", "mod_rewrite", "mod_accesslog", "mod_redirect")
 
server.document-root = "$root"
 
index-file.names = (
"index.php",
"index.html",
"index.htm"
)
 
$fastcgi
 
mimetype.assign = (
".text" => "text/plain",
".txt" => "text/plain",
".css" => "text/css",
".js" => "text/javascript",
 
".conf" => "text/plain",
".c" => "text/plain",
".py" => "text/plain",
 
".html" => "text/html",
".htm" => "text/html",
".xml" => "text/xml",
 
".gif" => "image/gif",
".jpg" => "image/jpeg",
".jpeg" => "image/jpeg",
".png" => "image/png",
 
".zip" => "application/zip",
".gz" => "application/x-gzip"
)
 
server.errorlog = "/dev/tty"
accesslog.filename = "/dev/tty"
 
server.port = $port
"""
 
FASTCGI_TEMPLATE = """
fastcgi.server = ( "/$script" =>
((
"socket" => ".fastcgi.$script.$pid.socket",
"bin-path" => "$root/$script",
"max-procs" => 1,
"bin-environment" => (
"REAL_SCRIPT_NAME" => ""
),
"check-local" => "disable"
))
)
 
url.rewrite-once = (
"^/favicon.ico$$" => "/static/favicon.ico",
"^/static/(.*)$$" => "/static/$$1",
"^/(.*)$$" => "/$script/$$1"
)
"""
 
def make_config(root, script, port):
    pid = os.getpid()
    if script:
        fastcgi = string.Template(FASTCGI_TEMPLATE).substitute(locals())
    else:
        fastcgi = ""
    return string.Template(CONF).substitute(locals())
 
def setup_path():
    paths = ["/usr/sbin", "/usr/local/sbin"]
    path = os.getenv('PATH') + ":" + ":".join(paths)
    os.putenv("PATH", path)
 
def main():
    options = parse_args()
    config = make_config(options.root, options.script, options.port)
 
    if options.debug:
        print config
        sys.exit(0)
 
    conf_file = ".lightty_%s.conf" % os.getpid()
    
    f = open(conf_file, 'w')
    f.write(config)
    f.close()
 
    print "http://0.0.0.0:%s/" % options.port
    print
 
    setup_path()
    os.system("lighttpd -D -f " + conf_file)
    os.unlink(conf_file)
 
def parse_args():
    parser = OptionParser("lightty [-p PORT] [-s SCRIPT] [-d] [document_root]")
 
    pwd = os.getcwd()
    parser.add_option("-p", "--port", dest="port", default=8080, type="int", help="server port (default: %default)")
    parser.add_option("-s", "--script", dest="script", default=None, help="fastcgi script")
    parser.add_option("-d", "--debug", action="store_true", dest="debug", default=False, help="print config and exit")
 
    (options, args) = parser.parse_args()
 
    if args:
        options.root = args[0]
    else:
        options.root = os.getcwd()
    return options
 
if __name__ == "__main__":
    main()