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