5
Copyright (c) 2010 Carnegie Mellon University
7
Permission is hereby granted, free of charge, to any person obtaining a copy
8
of this software and associated documentation files (the "Software"), to deal
9
in the Software without restriction, including without limitation the rights
10
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
copies of the Software, and to permit persons to whom the Software is
12
furnished to do so, subject to the following conditions:
14
The above copyright notice and this permission notice shall be included in
15
all copies or substantial portions of the Software.
17
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28
import os,urllib,getopt,traceback,sys,os
30
Stores a resource to the server. Entrypoint for the uploader tool
34
import libs.auth as auth
35
import libs.filenames as filenames
36
import libs.save as save
37
import libs.protocol as protocolmodule
38
##import libs.modelexporter.fileparser as fileparser
39
##import libs.modelexporter.model as model
40
##import libs.modelexporter.modelExporter as modelExporter
41
##import libs.modelexporter.animation as animation
42
##import libs.modelexporter.animexporter as animexporter
43
##import libs.modelexporter.tokens as tokens
47
class AuthenticationError(Exception):
48
def __init__(self,value):
51
return repr(self.value)
53
def applyCommands(parser,myImporter):
55
Applies the commands in the parser to a model
57
To apply commands, the first token in the command is assumed
58
to be the command name. A function with that name is called in model;
59
the remaining parsed tokens are arguments to that function
65
cmd=parser.nextCommand()
69
raise importer.ImporterError("Invalid command "+cmd[0])
70
getattr(myImporter,cmd[0])(*(cmd[1:]))
71
except (importer.ImporterError,Exception),e:
72
raise Exception("Error on line "+str(parser.getCurrentLine())+": "+str(e))
74
def convertFile(username,filename,filetype,keywords,version,inputFile):
79
libName="libs.modelexporter"+str(version)
89
fileparser = __import__(libName+".fileparser",globals(),locals(),["*"])
90
model = __import__(libName+".model",globals(),locals(),["*"])
91
modelExporter = __import__(libName+".modelExporter",globals(),locals(),["*"])
92
animation = __import__(libName+".animation",globals(),locals(),["*"])
93
animexporter = __import__(libName+".animexporter",globals(),locals(),["*"])
94
tokens = __import__(libName+".tokens",globals(),locals(),["*"])
95
importer = __import__(libName+".importer",globals(),locals(),["*"])
99
CgiError("Error while loading handler for resource version '"+str(version)+"':\n"+str(e))
101
parser=fileparser.FileParser(inputFile)
106
if filetype=="model":
107
importsystem=model.Model()
108
outputter=modelExporter.ModelExporter()
109
elif filetype=="animation":
110
importsystem=animation.Animation()
111
outputter=animexporter.AnimExporter()
113
raise Exception("Unknown resource type '"+filetype+"'")
117
raise Exception("Error while determining resource type: "+str(e))
119
applyCommands(parser,importsystem)
120
outstream=outputter.output(importsystem)
121
upload(username,filename,keywords,outstream)
123
def upload(username,filename,keywords,binaryOutput):
125
Uploads the raw output to the server
128
result=save.doSaveResource(username,filename,datastream=binaryOutput,keywords=keywords,isCgi=False)
130
if result is not None:
131
CgiSuccess("Resource uploaded.\n"+result)
133
CgiError("Resource failed to be uploaded.")
135
def CgiError(errorString):
136
print 'Content-Type: text/plain'
142
def CgiSuccess(report):
143
print 'Content-Type: text/plain'
150
def consoleError(errorString):
155
def consoleSuccess(report):
160
def processCgi(queryString):
162
Processes arguments provided as a query string
164
rawQuery=queryString.split('&')
166
for pair in rawQuery:
167
key,value=pair.split("=")
172
protocol = int(query['protocol'])
176
CgiError("This exporter has been replaced, please download the new exporter")
180
rsprot = int(query['rsprot'])
183
protocolmodule.rsprot = rsprot
186
username=urllib.unquote(query['user'])
188
CgiError("No username specified")
190
if 'filename' in query:
191
filename=urllib.unquote(query['filename'])
193
CgiError("No filename specified")
195
if not filenames.validateFilename(filename):
196
CgiError("Invalid filename")
198
if 'filetype' in query:
199
filetype=urllib.unquote(query['filetype'])
201
CgiError("No filetype specified")
203
if 'keywords' in query:
204
keywords=urllib.unquote(query['keywords']).split(" ")
208
if 'protocol' in query:
209
version=urllib.unquote(query['protocol'])
211
CgiError("No uploader version specified.")
213
req = auth.AuthenticationRequest(query=query)
214
req.requestWriteAccess(filename)
216
if not req.haveAccess():
217
CgiError("Access denied")
220
convertFile(username, filename, filetype, keywords, version, sys.stdin)
224
errString=traceback.format_exc()
227
def processCommandLine(cmdline):
229
Handles commands coming in from the command line
231
optlist,args=getopt.getopt(cmdline,"u:p:f:t:k:")
240
consoleError("No file specified for upload")
255
consoleError("No username specified.")
257
consoleError("No file name specified.")
259
if not filenames.validateFilename(filename):
260
CgiError("Invalid filename")
262
req = auth.AuthenticationRequest(query={'authservice':'wpwebp', 'user':username, 'password':authtoken})
264
req.requestWriteAccess(filename)
266
if not req.haveAccess():
267
CgiError("Access denied")
270
convertFile(username, filename, filetype, keywords, 3,file(args[0]))
274
errString=traceback.format_exc()
275
consoleError(errString)
276
consoleSuccess("Model uploaded.")
278
alternateErrorHandler = CgiError
279
if __name__=="__main__":
280
if 'QUERY_STRING' in os.environ:
281
processCgi(os.environ['QUERY_STRING'])
283
processCommandLine(sys.argv[1:])