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