]> TLD Linux GIT Repositories - tld-builder.git/blob - PLD_Builder/request_handler_server.py
- from https://github.com/pld-linux/pld-builder.new
[tld-builder.git] / PLD_Builder / request_handler_server.py
1 #!/usr/bin/python
2
3 import socket
4 import string
5 import cgi
6 import time
7 import log
8 import sys
9 import traceback
10 import os
11 from config import config, init_conf
12
13 from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
14
15 import request_handler
16 import path
17
18 class MyHandler(BaseHTTPRequestHandler):
19
20         def do_GET(self):
21                 self.send_error(401);
22
23         def do_POST(self):
24                 global rootnode
25                 try:
26                         length = int(self.headers.getheader('content-length'))
27                         ctype, pdict = cgi.parse_header(self.headers.getheader('content-type'))
28                         if ctype != 'application/x-www-form-urlencoded':
29                                 log.error("request_handler_server: [%s]: 401 Unauthorized" % self.client_address[0])
30                                 self.send_error(401)
31                                 self.end_headers()
32                                 return
33
34                         query = self.rfile.read(length)
35
36                         filename = self.headers.getheader('x-filename')
37
38                         if not request_handler.handle_request_main(query, filename = filename):
39                                 error = log.last_log();
40                                 log.error("request_handler_server: [%s]: handle_request_main(..., %s) failed" % (self.client_address[0], filename))
41                                 self.send_error(500, "%s: request failed. %s" % (filename, error))
42                                 self.end_headers()
43                                 return
44
45                         self.send_response(200)
46                         self.end_headers()
47
48                 except Exception, e:
49                         self.send_error(500, "%s: %s" % (filename, e))
50                         self.end_headers()
51                         log.error("request_handler_server: [%s]: exception: %s\n%s" % (self.client_address[0], e, traceback.format_exc()))
52                         raise
53                         pass
54
55 def write_css():
56         css_file = path.www_dir + "/style.css"
57         # skip if file exists and code is not newer
58         if os.path.exists(css_file) and os.stat(__file__).st_mtime < os.stat(css_file).st_mtime:
59                 return
60
61         # css from www.pld-linux.org wiki theme, using css usage firebug plugin to cleanup
62         css = """
63 html {
64         background-color: white;
65         color: #5e5e5e;
66         font-family: Tahoma, Arial, Lucida Grande, sans-serif;
67         font-size: 0.75em;
68         line-height: 1.25em;
69 }
70
71 a {
72         text-decoration: underline;
73         color: #006;
74 }
75
76 a:hover {
77         color: #006;
78 }
79
80 pre {
81         background: #FFF8EB;
82         border: 1pt solid #FFE2AB;
83         font-family: courier, monospace;
84         padding: 0.5em;
85         white-space: pre-wrap;
86         word-wrap: break-word;
87 }
88
89 @media screen, projection {
90         html {
91                 background-color: #f3efe3;
92         }
93
94         body {
95                 position: relative;
96         }
97
98         div {
99                 background-color: white;
100                 margin: 10px 0px;
101                 padding: 2px;
102         }
103         div > a {
104                 font-weight: bold;
105                 color: #5e5e5e;
106         }
107         div > a:hover {
108                 color: #5e5e5e;
109         }
110         div.upgrade {
111                 background-color: #e4f1cf;
112         }
113         div:target {
114                 background-color: #ffffcc;
115                 color: black;
116         }
117 }
118 @media print {
119         a {
120                 background-color: inherit;
121                 color: inherit;
122         }
123 }
124
125 @media projection {
126         html { line-height: 1.8em; }
127         body, b, a, p { font-size: 22pt; }
128 }
129 """
130         old_umask = os.umask(0022)
131         f = open(css_file, "w")
132         f.write(css)
133         f.close()
134         os.umask(old_umask)
135
136 def write_js():
137         js_file = path.www_dir + "/script.js"
138         # skip if file exists and code is not newer
139         if os.path.exists(js_file) and os.stat(__file__).st_mtime < os.stat(js_file).st_mtime:
140                 return
141
142         js = """
143 // update date stamps to reflect viewers timezone
144 function update_tz(t) {
145         var el, off, dt,
146                 collection = document.getElementsByTagName('span');
147         for (off in collection) {
148                 el = collection[off];
149                 if (el.id == 'tz') {
150                         dt = new Date(el.innerHTML).toString();
151                         // strip timezone name, it is usually wrong when not initialized
152                         // from TZ env, but reverse calculated from os data
153                         dt = dt.replace(/\s+\(.+\)/, "");
154                         // strip "GMT"
155                         dt = dt.replace(/GMT/, "");
156                         el.innerHTML = dt;
157                 }
158         }
159 }
160 window.onload = update_tz;
161 """
162         old_umask = os.umask(0022)
163         f = open(js_file, "w")
164         f.write(js)
165         f.close()
166         os.umask(old_umask)
167
168 def main():
169         write_css();
170         write_js();
171         socket.setdefaulttimeout(30)
172         try:
173                 init_conf()
174                 host = ""
175                 port = config.request_handler_server_port
176
177                 try:
178                         server = HTTPServer((host, port), MyHandler)
179                 except Exception, e:
180                         log.notice("request_handler_server: can't start server on [%s:%d]: %s" % (host, port, e))
181                         print >> sys.stderr, "ERROR: Can't start server on [%s:%d]: %s" % (host, port, e)
182                         sys.exit(1)
183
184                 log.notice('request_handler_server: started on [%s:%d]...' % (host, port))
185                 server.serve_forever()
186         except KeyboardInterrupt:
187                 log.notice('request_handler_server: ^C received, shutting down server')
188                 server.socket.close()
189
190 if __name__ == '__main__':
191         main()
192