-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpywebtex.py
249 lines (185 loc) · 8.33 KB
/
pywebtex.py
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
from wsgiref.simple_server import make_server
from pyramid.config import Configurator
from pyramid.view import view_config
from pyramid.response import Response
from pyramid.response import FileResponse
from pyramid.static import static_view
from sqlalchemy import create_engine, MetaData, Table, Column, Integer, String, update, select
from sqlalchemy.sql import and_
import subprocess
import codecs
import os
import hashlib
import urlparse
# Global variables
# same as above, create meta-data
engine = create_engine('sqlite:///users.db', echo=True)
meta = MetaData()
meta.bind = engine
users_table = Table('users_table', meta, autoload=True)
workingDir = os.path.dirname(__file__)
projectDir = 'projects'
buildDir = 'build'
# Init
# Create project dir
if not os.path.exists(os.path.join(workingDir, projectDir)):
os.makedirs(os.path.join(workingDir, projectDir))
@view_config(route_name='showDocument', renderer='pywebtex:templates/editor.pt')
def showDocument(request):
projectHash = request.matchdict['projectHash']
fileName = request.matchdict['fileName']
projectPath = os.path.join(workingDir, projectDir, projectHash)
path = os.path.join(workingDir, projectDir, projectHash, fileName)
f = codecs.open(path, 'r', "utf-8")
content = f.read()
f.close()
files = [f for f in os.listdir(
projectPath) if os.path.isfile(os.path.join(projectPath, f))]
directories = [f for f in os.listdir(
projectPath) if os.path.isdir(os.path.join(projectPath, f))]
return {'files': files, 'directories': directories, 'filename': fileName, 'editorcontent': content}
@view_config(route_name='home', renderer='pywebtex:templates/index.pt')
def home(request):
relativePath = '.'
styles = [relativePath + "/lib/bootstrap/css/bootstrap.min.css",
relativePath + "/lib/jasny-bootstrap/css/jasny-bootstrap.min.css", relativePath + "/css/style.css"]
scripts = [
"http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js", "http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js",
relativePath + "/lib/bootstrap/js/bootstrap.js", relativePath + "/lib/jasny-bootstrap/js/jasny-bootstrap.min.js", relativePath + '/js/app.js']
return {'title': 'pyWebTeX', 'scripts': scripts, 'styles': styles}
@view_config(route_name='user', renderer='pywebtex:templates/user.pt')
def user(request):
userName = request.matchdict['userName']
return {'userName': userName, 'projects': 'Test'}
@view_config(route_name='register', renderer='json')
def register(request):
userName = request.matchdict['userName']
userPasswordHash = request.matchdict['userPasswordHash']
return {'userName': userName, 'projects': 'Test'}
@view_config(route_name='openPDF', renderer='pywebtex:templates/pdfviewer.pt')
def openPDF(request):
fileName = request.matchdict['fileName']
projectHash = request.matchdict['projectHash']
path = os.path.join(workingDir, projectDir, projectHash, 'build', fileName)
pdfUrl = '../../../' + projectDir + '/' + \
projectHash + '/' + buildDir + '/' + fileName
return {'pdfPath': pdfUrl}
@view_config(route_name='new_project')
def newFile(request):
fileName = request.POST.getone('fileName')
projectName = request.POST.getone('projectName')
projectHash = hashlib.sha1(projectName).hexdigest()
print 'Creating project ' + projectName + ' in Folder ' + projectHash
# Creating new file
projectPath = os.path.join(workingDir, projectDir, projectHash)
filePath = os.path.join(projectPath, fileName)
if os.path.exists(projectPath):
print 'Error creating project. Folder already used.'
return Response('Error creating project. Folder already used.')
else:
os.makedirs(projectPath)
f = codecs.open(filePath, 'w', "utf-8")
f.close()
print 'Init GIT'
# Init the Git Repo
gitInitArgs = ["git", "init"]
gitInitProcess = subprocess.Popen(
gitInitArgs, cwd=projectPath, shell=True)
print 'Add File to GIT'
# Add file
gitAddArgs = ["git", "add", fileName]
gitAddProcess = subprocess.Popen(
gitAddArgs, cwd=projectPath)
projectUrl = './' + projectHash + '/' + fileName
return Response(projectUrl)
@view_config(route_name='saveFile')
def saveFile(request):
projectHash = request.POST.getone('projectHash')
fileName = request.POST.getone('fileName')
content = request.POST.getone('content')
path = os.path.join(workingDir, projectDir, projectHash, fileName)
projectPath = os.path.join(workingDir, projectDir, projectHash)
f = codecs.open(path, 'r+', "utf-8")
f.write(content)
f.close()
# Add file
gitArgs = ["git", "commit", "-m \"Auto Commit by pyWebTeX\""]
gitProcess = subprocess.Popen(
gitArgs, cwd=projectPath)
return Response(fileName)
@view_config(route_name='uploadFile')
def uploadFile(request):
fileName = request.POST['file'].filename
input_file = request.POST['file'].file
projectHash = request.POST['projectHash']
outputPath = os.path.join(workingDir, projectDir, projectHash, fileName)
output_file = open(outputPath, 'wb')
input_file.seek(0)
while True:
data = input_file.read(2 << 16)
if not data:
break
output_file.write(data)
output_file.close()
return Response(outputPath)
@view_config(route_name='showpdf')
def showpdf(request):
fileName = request.matchdict['fileName']
projectHash = request.matchdict['projectHash']
path = os.path.join(workingDir, projectDir, projectHash, 'build', fileName)
return FileResponse(path)
@view_config(route_name='compile', renderer='json')
def compile(request):
projectHash = request.POST.getone('projectHash')
fileName = request.POST.getone('fileName')
content = request.POST.getone('content')
latexWorkingDir = os.path.join(workingDir, projectDir, projectHash)
latexBuildDir = os.path.join(latexWorkingDir, buildDir)
latexFilePath = os.path.join(latexWorkingDir, fileName)
f = codecs.open(latexFilePath, 'r+', "utf-8")
f.write(content)
f.close()
compileArgs = ["latexmk", "-latexoption=\"-interaction=nonstopmode\"",
"-p", "-pdf", "-f", "-output-directory=./" + buildDir, fileName]
compileProcess = subprocess.Popen(
compileArgs, cwd=latexWorkingDir, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
compileLog = compileProcess.communicate()[0]
cleanArgs = ["latexmk", "-latexoption=\"-interaction=nonstopmode\"",
"-c", "-quiet", "-output-directory=./" + buildDir, fileName]
subprocess.Popen(cleanArgs, cwd=latexWorkingDir,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
pdfUrl = './' + buildDir + '/' + fileName[:-4] + '.pdf'
return {'pdfUrl': pdfUrl, 'compileLog': compileLog}
if __name__ == '__main__':
config = Configurator()
config.include('pyramid_chameleon')
config.add_route('showDocument', '/' +
projectDir + '/{projectHash}/{fileName}')
config.add_route('new_project', '/api/project/new')
config.add_route('saveFile', '/api/save')
config.add_route('compile', '/api/compile')
config.add_route('user', '/{userName}')
config.add_route('register', '/api/user/register')
config.add_route('home', '/')
config.add_route(
'openPDF', '/pdf/{projectHash}/' + buildDir + '/{fileName}')
config.add_route('uploadFile', '/api/file/upload')
config.add_static_view(name='lib', path='pywebtex:lib')
config.add_static_view(name='css', path='pywebtex:css')
config.add_static_view(name='test', path='pywebtex:website')
config.add_static_view(name='js', path='pywebtex:js')
config.add_static_view(name='texlive', path='pywebtex:lib/texlive')
config.add_route('showpdf', '/' + projectDir +
'/{projectHash}/' + buildDir + '/{fileName}')
config.add_route('imageuploadform', '/upload')
# upload processing
# After replacing server/php/ in imageupload.pt with tal:attributes="action actionurl"
# the following can be replaced with any URL base
config.add_route('imageupload', '/server/php{sep:/*}{name:.*}')
# retrieving images
config.add_route('imageview', '/image/{name:.+}')
config.add_static_view(name='', path='pywebtex:')
config.scan()
app = config.make_wsgi_app()
server = make_server('0.0.0.0', 8080, app)
server.serve_forever()