-
Notifications
You must be signed in to change notification settings - Fork 3
/
ows-wsgi
executable file
·164 lines (142 loc) · 5.42 KB
/
ows-wsgi
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/python
import os, sys
lib_path = os.path.abspath(os.path.join('..'))
sys.path.append(lib_path)
from bottle import Bottle, run
from bottle import get, post, request, response, static_file
import json
import base64
import git
from git.exc import GitCommandError
import uuid
import tempfile
import owsdiff
from jinja2 import Environment, FileSystemLoader, FileSystemBytecodeCache
from urlparse import urlparse
import pprint
pp = pprint.PrettyPrinter(indent=4)
TEMPLATES="/home/ows/ows"
bcc = FileSystemBytecodeCache('/tmp/jinja_cache', '%s.cache')
j2_env = Environment(loader=FileSystemLoader(TEMPLATES),trim_blocks=True,bytecode_cache=bcc,cache_size=100)
app = application = Bottle()
os.environ = owsdiff.source(os.getenv('DEFAULTS',"/etc/default/ows2"))
VERSIONS = "4.02.2 3.12.1 4.00.1 4.01.0 4.02.0 4.02.1"
GITURL = 'git://github.com/ocaml/opam-repository'
options = {
'versions' : os.getenv('VERSIONS',VERSIONS).split(),
'distcheck' : os.getenv('DISTCHECK','dose-distcheck'),
'opam' : os.getenv('OPAM','opam'),
'opamrepo' : os.path.join(os.getenv('DATADIR','/tmp'),"opam-repository"),
'opamroot' : os.path.join(os.getenv('DATADIR','/tmp'),"opam-root"),
'opamcomp' : os.path.join(os.getenv('DATADIR','/tmp'),"opam-compilers"),
'owscache' : os.path.join(os.getenv('DATADIR','/tmp'),"cache"),
'giturl' : None,
'nocache' : False }
def makeoptions(nocache,giturl) :
# I'm wondering if build the url this way opens up a security hole
reponame = (urlparse(giturl).netloc + urlparse(giturl).path).replace("/","-")
options['nocache'] = nocache
options['opamrepo'] = os.path.join(os.getenv('DATADIR','/tmp'),reponame,"opam-repository")
options['opamroot'] = os.path.join(os.getenv('DATADIR','/tmp'),reponame,"opam-root")
options['opamcomp'] = os.path.join(os.getenv('DATADIR','/tmp'),reponame,"opam-compilers")
options['owscache'] = os.path.join(os.getenv('DATADIR','/tmp'),reponame,"cache")
options['giturl'] = giturl
return options
def dataget(data) :
commit1 = data.get('commit1',None)
commit2 = data.get('commit2',None)
patch = data.get('patch',None)
nocache = True if (data.get('nocache',"False")) == "True" else False
giturl = data.get('giturl',GITURL)
return (commit1,commit2,patch,nocache,giturl)
@app.get('/compare/form')
def form():
template = j2_env.get_template('templates/compare/form.html')
output = template.render({'baseurl' : os.getenv('BASEURL',"http://ows.irill.org")})
return output
@app.post('/compare/submit')
def submit():
diff = []
try :
(commit1,commit2,patch,nocache,giturl) = dataget(request.forms)
options = makeoptions(nocache,giturl)
status = "success"
message = None
except ValueError :
status = "error"
message = "FORM Data Error"
patch = None
commit2 = None
print "diff %s %s" % (commit2,patch)
if patch :
(fid,patchfile) = tempfile.mkstemp('ows-wsgi')
with open(patchfile, 'w') as open_file:
open_file.write(patch.file.read())
open_file.close()
try :
diff = owsdiff.patch(commit1,patchfile,options)
except GitCommandError :
error = "Apply Patch Failed"
except owsdiff.OwsDiffError as e :
error = e.value
os.remove(patchfile)
elif commit2 :
try :
diff = owsdiff.run(commit1,commit2,options)
except GitCommandError :
error = "Checkout commit Failed"
except owsdiff.OwsDiffError as e :
error = e.value
template = j2_env.get_template('templates/compare/modal.html')
output = template.render({'report' : diff, 'error' : message, 'commit1' : commit1, 'commit2' : commit2, 'baseurl' : BASEURL})
return output
@app.post('/compare/api')
def jsondiff():
diff = []
try :
(commit1,commit2,patch,nocache,giturl) = dataget(request.json)
options = makeoptions(nocache,giturl)
status = "success"
message = ""
except ValueError :
status = "error"
message = "JSON Object Error"
patch = None
commit2 = None
if patch :
(fid,patchfile) = tempfile.mkstemp('ows-wsgi')
with open(patchfile, 'w') as open_file:
open_file.write(base64.b64decode(patch))
open_file.close()
try :
diff = owsdiff.patch(commit1,patchfile,options)
except GitCommandError :
status = "fail"
message = "Apply Patch Failed"
except owsdiff.OwsDiffError as e :
status = "fail"
message = e.value
os.remove(patchfile)
elif commit2 :
try :
diff = owsdiff.run(commit1,commit2,options)
except GitCommandError :
status = "fail"
message = "Checkout commit Failed"
except owsdiff.OwsDiffError as e :
status = "fail"
message = e.value
response.content_type = 'application/json'
return json.dumps({ 'status' : status, 'message' : message, 'result' : diff})
class StripPathMiddleware(object):
'''
Get that slash out of the request
'''
def __init__(self, a):
self.a = a
def __call__(self, e, h):
e['PATH_INFO'] = e['PATH_INFO'].rstrip('/')
return self.a(e, h)
if __name__ == '__main__':
run(app=StripPathMiddleware(app),
host='localhost', port=8080, debug=True)