b@2264
|
1 #!/usr/bin/python
|
b@2264
|
2
|
b@2264
|
3 # Detect the Python version to switch code between 2.x and 3.x
|
b@2264
|
4 # http://stackoverflow.com/questions/9079036/detect-python-version-at-runtime
|
b@2264
|
5 import sys
|
b@2264
|
6
|
b@2264
|
7 import inspect
|
b@2264
|
8 import os
|
b@2264
|
9 import pickle
|
b@2264
|
10 import datetime
|
nicholas@2430
|
11 import operator
|
nicholas@2430
|
12 import xml.etree.ElementTree as ET
|
nicholas@2431
|
13 import copy
|
nicholas@2510
|
14 import string
|
nicholas@2510
|
15 import random
|
nicholas@3028
|
16 import errno
|
b@2264
|
17
|
b@2264
|
18 if sys.version_info[0] == 2:
|
b@2264
|
19 # Version 2.x
|
b@2264
|
20 import BaseHTTPServer
|
b@2264
|
21 import urllib2
|
b@2264
|
22 import urlparse
|
b@2264
|
23 elif sys.version_info[0] == 3:
|
b@2264
|
24 # Version 3.x
|
b@2264
|
25 from http.server import BaseHTTPRequestHandler, HTTPServer
|
b@2264
|
26 import urllib as urllib2
|
b@2264
|
27
|
b@2264
|
28 # Go to right folder.
|
b@2264
|
29 scriptdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) # script directory
|
b@2264
|
30 os.chdir(scriptdir) # does this work?
|
b@2264
|
31
|
nicholas@3028
|
32 try:
|
nicholas@3028
|
33 os.makedirs("../saves")
|
nicholas@3028
|
34 except OSError as e:
|
nicholas@3028
|
35 if e.errno != errno.EEXIST:
|
nicholas@3028
|
36 raise
|
nicholas@3028
|
37
|
b@2264
|
38 PSEUDO_PATH = '../tests/'
|
b@2264
|
39 pseudo_files = []
|
nicholas@2450
|
40 pseudo_index = 0
|
nicholas@2430
|
41 for filename in os.listdir(PSEUDO_PATH):
|
b@2264
|
42 print(filename)
|
b@2264
|
43 if filename.endswith('.xml'):
|
b@2264
|
44 pseudo_files.append(filename)
|
b@2264
|
45
|
b@2264
|
46 curSaveIndex = 0;
|
b@2264
|
47 curFileName = 'test-0.xml'
|
nicholas@2430
|
48 while(os.path.isfile('../saves/'+curFileName)):
|
b@2264
|
49 curSaveIndex += 1;
|
b@2264
|
50 curFileName = 'test-'+str(curSaveIndex)+'.xml'
|
b@2264
|
51
|
b@2264
|
52 if len(pseudo_files) > 0:
|
b@2264
|
53 pseudo_index = curSaveIndex % len(pseudo_files)
|
b@2264
|
54 else:
|
b@2264
|
55 pseudo_index = 0
|
b@2264
|
56
|
b@2264
|
57 print('URL: http://localhost:8000/index.html')
|
b@2264
|
58
|
b@2264
|
59 def send404(s):
|
b@2264
|
60 s.send_response(404)
|
b@2264
|
61 s.send_header("Content-type", "text/html")
|
b@2264
|
62 s.end_headers()
|
b@2264
|
63
|
b@2264
|
64 def processFile(s):
|
b@2264
|
65 if sys.version_info[0] == 2:
|
b@2264
|
66 s.path = s.path.rsplit('?')
|
b@2264
|
67 s.path = s.path[0]
|
b@2264
|
68 s.path = s.path[1:len(s.path)]
|
b@2264
|
69 st = s.path.rsplit(',')
|
b@2264
|
70 lenSt = len(st)
|
b@2264
|
71 fmt = st[lenSt-1].rsplit('.')
|
nicholas@3065
|
72 fmt = fmt[len(fmt)-1]
|
b@2264
|
73 fpath = "../"+urllib2.unquote(s.path)
|
n@2432
|
74 size = os.path.getsize(fpath)
|
nicholas@3065
|
75 fileDump = open(fpath, mode='rb')
|
b@2264
|
76 s.send_response(200)
|
b@2264
|
77
|
nicholas@3065
|
78 if (fmt == 'html'):
|
b@2264
|
79 s.send_header("Content-type", 'text/html')
|
nicholas@3065
|
80 elif (fmt == 'css'):
|
b@2264
|
81 s.send_header("Content-type", 'text/css')
|
nicholas@3065
|
82 elif (fmt == 'js'):
|
b@2264
|
83 s.send_header("Content-type", 'application/javascript')
|
b@2264
|
84 else:
|
b@2264
|
85 s.send_header("Content-type", 'application/octet-stream')
|
nicholas@2542
|
86 fileRead = fileDump.read()
|
nicholas@2542
|
87 s.send_header("Content-Length", len(fileRead))
|
b@2264
|
88 s.end_headers()
|
nicholas@2542
|
89 s.wfile.write(fileRead)
|
b@2264
|
90 fileDump.close()
|
b@2264
|
91 elif sys.version_info[0] == 3:
|
b@2264
|
92 s.path = s.path.rsplit('?')
|
b@2264
|
93 s.path = s.path[0]
|
b@2264
|
94 s.path = s.path[1:len(s.path)]
|
b@2264
|
95 st = s.path.rsplit(',')
|
b@2264
|
96 lenSt = len(st)
|
b@2264
|
97 fmt = st[lenSt-1].rsplit('.')
|
b@2264
|
98 fpath = "../"+urllib2.parse.unquote(s.path)
|
b@2264
|
99 s.send_response(200)
|
b@2264
|
100 if (fmt[1] == 'html'):
|
b@2264
|
101 s.send_header("Content-type", 'text/html')
|
b@2264
|
102 fileDump = open(fpath, encoding='utf-8')
|
b@2264
|
103 fileBytes = bytes(fileDump.read(), "utf-8")
|
b@2264
|
104 fileDump.close()
|
b@2264
|
105 elif (fmt[1] == 'css'):
|
b@2264
|
106 s.send_header("Content-type", 'text/css')
|
b@2264
|
107 fileDump = open(fpath, encoding='utf-8')
|
b@2264
|
108 fileBytes = bytes(fileDump.read(), "utf-8")
|
b@2264
|
109 fileDump.close()
|
b@2264
|
110 elif (fmt[1] == 'js'):
|
b@2264
|
111 s.send_header("Content-type", 'application/javascript')
|
b@2264
|
112 fileDump = open(fpath, encoding='utf-8')
|
b@2264
|
113 fileBytes = bytes(fileDump.read(), "utf-8")
|
b@2264
|
114 fileDump.close()
|
b@2264
|
115 else:
|
b@2264
|
116 s.send_header("Content-type", 'application/octet-stream')
|
b@2264
|
117 fileDump = open(fpath, 'rb')
|
b@2264
|
118 fileBytes = fileDump.read()
|
b@2264
|
119 fileDump.close()
|
b@2264
|
120 s.send_header("Content-Length", len(fileBytes))
|
b@2264
|
121 s.end_headers()
|
b@2264
|
122 s.wfile.write(fileBytes)
|
nicholas@2510
|
123
|
nicholas@2510
|
124 def requestKey(s):
|
nicholas@2510
|
125 reply = ""
|
nicholas@2510
|
126 key = ''
|
nicholas@2510
|
127 while key == '':
|
nicholas@2510
|
128 tempKey = ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(32));
|
nicholas@2510
|
129 if (os.path.isfile("saves/save-"+tempKey+".xml") == False):
|
nicholas@2510
|
130 key = tempKey
|
nicholas@2940
|
131 options = s.path.rsplit('?')
|
nicholas@2940
|
132 options = options[1].rsplit('&')
|
nicholas@2940
|
133 for option in options:
|
nicholas@2940
|
134 optionPair = option.rsplit('=')
|
nicholas@2940
|
135 if optionPair[0] == "saveFilenamePrefix":
|
nicholas@2940
|
136 prefix = optionPair[1]
|
nicholas@2940
|
137 if prefix == None:
|
nicholas@2940
|
138 prefix = "save"
|
nicholas@2510
|
139 s.send_response(200)
|
nicholas@2510
|
140 s.send_header("Content-type", "application/xml");
|
nicholas@2510
|
141 s.end_headers()
|
nicholas@2510
|
142 reply = "<response><state>OK</state><key>"+key+"</key></response>"
|
nicholas@2510
|
143 if sys.version_info[0] == 2:
|
nicholas@2510
|
144 s.wfile.write(reply)
|
nicholas@2510
|
145 elif sys.version_info[0] == 3:
|
nicholas@2510
|
146 s.wfile.write(bytes(reply, "utf-8"))
|
nicholas@2940
|
147 file = open("../saves/"+prefix+"-"+key+".xml",'w')
|
nicholas@2932
|
148 file.write("<waetresult key=\""+key+"\"/>")
|
nicholas@2510
|
149 file.close()
|
nicholas@2510
|
150
|
b@2264
|
151
|
b@2264
|
152 def saveFile(self):
|
b@2264
|
153 global curFileName
|
b@2264
|
154 global curSaveIndex
|
b@2264
|
155 options = self.path.rsplit('?')
|
nicholas@2722
|
156 options = options[1].rsplit('&')
|
n@2997
|
157 update = False
|
nicholas@2722
|
158 for option in options:
|
nicholas@2722
|
159 optionPair = option.rsplit('=')
|
nicholas@2722
|
160 if optionPair[0] == "key":
|
nicholas@2722
|
161 key = optionPair[1]
|
nicholas@2722
|
162 elif optionPair[0] == "saveFilenamePrefix":
|
nicholas@2722
|
163 prefix = optionPair[1]
|
n@2997
|
164 elif optionPair[0] == "state":
|
n@2997
|
165 update = optionPair[1] == "update"
|
nicholas@2722
|
166 if key == None:
|
nicholas@2722
|
167 self.send_response(404)
|
nicholas@2722
|
168 return
|
nicholas@2722
|
169 if prefix == None:
|
nicholas@2722
|
170 prefix = "save"
|
b@2264
|
171 varLen = int(self.headers['Content-Length'])
|
b@2264
|
172 postVars = self.rfile.read(varLen)
|
b@2264
|
173 print("Saving file key "+key)
|
nicholas@2722
|
174 filename = prefix+'-'+key+'.xml'
|
n@2997
|
175 if update:
|
n@2997
|
176 filename = "update-"+filename
|
nicholas@2722
|
177 file = open('../saves/'+filename,'wb')
|
b@2264
|
178 file.write(postVars)
|
b@2264
|
179 file.close()
|
b@2264
|
180 try:
|
nicholas@2722
|
181 wbytes = os.path.getsize('../saves/'+filename)
|
b@2264
|
182 except OSError:
|
b@2264
|
183 self.send_response(200)
|
b@2264
|
184 self.send_header("Content-type", "text/xml")
|
b@2264
|
185 self.end_headers()
|
b@2264
|
186 self.wfile.write('<response state="error"><message>Could not open file</message></response>')
|
b@2264
|
187 self.send_response(200)
|
b@2264
|
188 self.send_header("Content-type", "text/xml")
|
b@2264
|
189 self.end_headers()
|
nicholas@2722
|
190 reply = '<response state="OK"><message>OK</message><file bytes="'+str(wbytes)+'">"saves/'+filename+'"</file></response>'
|
nicholas@2382
|
191 if sys.version_info[0] == 2:
|
nicholas@2382
|
192 self.wfile.write(reply)
|
nicholas@2382
|
193 elif sys.version_info[0] == 3:
|
nicholas@2382
|
194 self.wfile.write(bytes(reply, "utf-8"))
|
b@2264
|
195 curSaveIndex += 1
|
b@2264
|
196 curFileName = 'test-'+str(curSaveIndex)+'.xml'
|
n@2997
|
197 if update == False:
|
nicholas@3066
|
198 if(os.path.isfile("../saves/update-"+filename)):
|
nicholas@3066
|
199 os.remove("../saves/update-"+filename)
|
nicholas@2934
|
200
|
nicholas@2934
|
201 def testSave(self):
|
nicholas@2934
|
202 self.send_response(200)
|
nicholas@2934
|
203 self.send_header("Content-type", "text/xml")
|
nicholas@2934
|
204 self.end_headers()
|
nicholas@2934
|
205 filename = "../saves/test-save.xml"
|
nicholas@2934
|
206 file = open(filename,'wb')
|
nicholas@2934
|
207 if sys.version_info[0] == 2:
|
nicholas@2934
|
208 file.write("<xml></xml>")
|
nicholas@2934
|
209 elif sys.version_info[0] == 3:
|
nicholas@2934
|
210 file.write(bytes("<xml></xml>", "utf-8"))
|
nicholas@2934
|
211 file.close()
|
nicholas@2934
|
212 message = ""
|
nicholas@2934
|
213 try:
|
nicholas@2934
|
214 wbytes = os.path.getsize(filename)
|
nicholas@2934
|
215 except OSError:
|
nicholas@2934
|
216 message = '<response state="error"><message>Could not open file</message></response>';
|
nicholas@2934
|
217 if sys.version_info[0] == 2:
|
nicholas@2934
|
218 self.wfile.write(message)
|
nicholas@2934
|
219 elif sys.version_info[0] == 3:
|
nicholas@2934
|
220 self.wfile.write(bytes(message, "utf-8"))
|
nicholas@2934
|
221 return
|
nicholas@2934
|
222 os.remove(filename)
|
nicholas@2934
|
223 message = '<response state="OK"><message>OK</message></response>';
|
nicholas@2934
|
224 if sys.version_info[0] == 2:
|
nicholas@2934
|
225 self.wfile.write(message)
|
nicholas@2934
|
226 elif sys.version_info[0] == 3:
|
nicholas@2934
|
227 self.wfile.write(bytes(message, "utf-8"))
|
b@2264
|
228
|
nicholas@2430
|
229 def poolXML(s):
|
nicholas@2430
|
230 pool = ET.parse('../tests/pool.xml')
|
nicholas@2430
|
231 root = pool.getroot()
|
nicholas@2430
|
232 setupNode = root.find("setup");
|
nicholas@2430
|
233 poolSize = setupNode.get("poolSize",0);
|
nicholas@2430
|
234 if (poolSize == 0):
|
nicholas@2430
|
235 s.path = s.path.split("/php",1)[0]+"/tests/pool/xml"
|
nicholas@2430
|
236 processFile(s)
|
nicholas@2430
|
237 return
|
nicholas@2431
|
238 poolSize = int(poolSize)
|
nicholas@2430
|
239 # Set up the store will all the test page key nodes
|
nicholas@2430
|
240 pages = {};
|
nicholas@2430
|
241 for page in root.iter("page"):
|
nicholas@2430
|
242 id = page.get("id")
|
nicholas@2430
|
243 pages[id] = 0
|
nicholas@2430
|
244 # Read the saves and determine the completed pages
|
nicholas@2430
|
245 for filename in os.listdir("../saves/"):
|
nicholas@2430
|
246 if filename.endswith(".xml"):
|
nicholas@2430
|
247 save = ET.parse("../saves/"+filename)
|
nicholas@2430
|
248 save_root = save.getroot();
|
nicholas@2431
|
249 if (save_root.find("waet").get("url") == "http://localhost:8000/php/pool.php"):
|
nicholas@2431
|
250 for page in save_root.findall("./page"):
|
nicholas@2431
|
251 id = page.get("ref")
|
nicholas@2430
|
252 pages[id] = pages[id] + 1
|
nicholas@2430
|
253
|
nicholas@2430
|
254 # Sort the dictionary
|
nicholas@2431
|
255 rot_pages = {}
|
nicholas@2431
|
256 for key, value in pages.items():
|
nicholas@2431
|
257 if (value in rot_pages):
|
nicholas@2431
|
258 rot_pages[value].append(key)
|
nicholas@2431
|
259 else:
|
nicholas@2431
|
260 rot_pages[value] = [key]
|
nicholas@2431
|
261
|
nicholas@2431
|
262 Keys = list(rot_pages)
|
nicholas@2431
|
263 print ("Current pool state:")
|
nicholas@2431
|
264 print (rot_pages)
|
nicholas@2431
|
265
|
nicholas@2431
|
266 return_node = ET.fromstring('<waet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="test-schema.xsd"/>');
|
nicholas@2431
|
267 return_node.append(copy.deepcopy(root.find("setup")))
|
nicholas@2431
|
268 page_elements = root.findall("page")
|
nicholas@2430
|
269
|
nicholas@2431
|
270 # Now append the pages
|
nicholas@2431
|
271 i = 0
|
nicholas@2431
|
272 while(len(return_node.findall("page")) < poolSize):
|
nicholas@2431
|
273 if (i > 0):
|
nicholas@2431
|
274 for page in return_node.iter("page"):
|
nicholas@2431
|
275 page.set("alwaysInclude","true")
|
nicholas@2431
|
276 for id in rot_pages[Keys[i]]:
|
nicholas@2431
|
277 return_node.append(copy.deepcopy(root.find('./page[@id="'+id+'"]')))
|
nicholas@2431
|
278 i=i+1
|
nicholas@2431
|
279 s.send_response(200)
|
nicholas@2431
|
280 s.send_header("Content-type", "text/xml")
|
nicholas@2431
|
281 s.end_headers()
|
nicholas@2431
|
282 s.wfile.write(ET.tostring(return_node))
|
nicholas@2431
|
283
|
b@2264
|
284 def http_do_HEAD(s):
|
b@2264
|
285 s.send_response(200)
|
b@2264
|
286 s.send_header("Content-type", "text/html")
|
b@2264
|
287 s.end_headers()
|
b@2264
|
288
|
b@2264
|
289 def http_do_GET(request):
|
nicholas@2450
|
290 global pseudo_index
|
b@2264
|
291 if(request.client_address[0] == "127.0.0.1"):
|
b@2264
|
292 if (request.path == "/favicon.ico"):
|
b@2264
|
293 send404(request)
|
nicholas@2510
|
294 elif (request.path.split('?',1)[0] == "/php/requestKey.php"):
|
nicholas@2510
|
295 requestKey(request);
|
nicholas@2430
|
296 elif (request.path.split('?',1)[0] == "/php/pool.php"):
|
nicholas@2430
|
297 poolXML(request);
|
nicholas@2934
|
298 elif (request.path.split('?',1)[0] == "/php/test_write.php"):
|
nicholas@2934
|
299 testSave(request);
|
b@2264
|
300 else:
|
b@2264
|
301 request.path = request.path.split('?',1)[0]
|
b@2264
|
302 if (request.path == '/'):
|
b@2264
|
303 request.path = '/index.html'
|
b@2264
|
304 elif (request.path == '/pseudo.xml'):
|
nicholas@2450
|
305 request.path = PSEUDO_PATH + pseudo_files[pseudo_index]
|
b@2264
|
306 print(request.path)
|
b@2264
|
307 pseudo_index += 1
|
b@2264
|
308 pseudo_index %= len(pseudo_files)
|
b@2264
|
309 processFile(request)
|
b@2264
|
310 else:
|
b@2264
|
311 send404(request)
|
b@2264
|
312
|
b@2264
|
313 def http_do_POST(request):
|
b@2264
|
314 if(request.client_address[0] == "127.0.0.1"):
|
b@2264
|
315 if (request.path.rsplit('?',1)[0] == "/save" or request.path.rsplit('?',1)[0] == "/php/save.php"):
|
b@2264
|
316 saveFile(request)
|
b@2264
|
317 else:
|
b@2264
|
318 send404(request)
|
b@2264
|
319
|
b@2264
|
320 if sys.version_info[0] == 2:
|
b@2264
|
321 class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler):
|
b@2264
|
322 def do_HEAD(s):
|
b@2264
|
323 http_do_HEAD(s)
|
b@2264
|
324 def do_GET(request):
|
b@2264
|
325 http_do_GET(request)
|
b@2264
|
326 def do_POST(request):
|
b@2264
|
327 http_do_POST(request)
|
b@2264
|
328 def run(server_class=BaseHTTPServer.HTTPServer,handler_class=MyHandler):
|
b@2264
|
329 server_address = ('', 8000)
|
b@2264
|
330 httpd = server_class(server_address, handler_class)
|
b@2264
|
331 httpd.serve_forever()
|
b@2264
|
332 run()
|
b@2264
|
333 elif sys.version_info[0] == 3:
|
b@2264
|
334 class MyHandler(BaseHTTPRequestHandler):
|
b@2264
|
335 def do_HEAD(s):
|
b@2264
|
336 send404(s)
|
b@2264
|
337 def do_GET(request):
|
b@2264
|
338 http_do_GET(request)
|
b@2264
|
339 def do_POST(request):
|
b@2264
|
340 http_do_POST(request)
|
b@2264
|
341 def run(server_class=HTTPServer,handler_class=MyHandler):
|
b@2264
|
342 server_address = ('', 8000)
|
b@2264
|
343 httpd = server_class(server_address, handler_class)
|
b@2264
|
344 httpd.serve_forever()
|
b@2264
|
345 run()
|