Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/Experior.Activity/sbrpcserver.py
blob: 1b6b9979a5801a562c6509d26505e08df3cc336a (plain)
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
#! /usr/bin/env python
# -*- encoding: utf-8 -*-

#sbrpcserver.py

#This file is part of sugarbot.

#sugarbot is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.

#sugarbot is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.

#You should have received a copy of the GNU General Public License
#along with sugarbot.  If not, see <http://www.gnu.org/licenses/>.

import binascii
import logging
import optparse
import os
import random
import sblog
import sys
import time
import xmlrpclib

from SimpleXMLRPCServer import SimpleXMLRPCServer
try:
	from sbconfig import host, port
except ImportError:
	from sbconfig_sample import host, port

def proxyString():
	return "http://%s:%s/" %  (host, str(port))
	
def rebuildMarshalledObject(obj, dictionary):
	for key in dictionary:
		setattr(obj, key, dictionary[key])
	
class sugarbotSession():
	def __init__(self,ID=0):
		self.id 			= ID
		# self.responses 		= []
		self.responses		= {}
		self.currentScript 	= -1
		self.scriptName		= ""
		self.failureText	= {}
		# self.log = logging.getLogger("%s" % ID)
		
	def getSuccessValue(self):
		retval = 0
		
		failed = [self.responses[k] for k in self.responses if not self.responses[k]]
		if failed:
			retval = 1
		return retval
		
	log = property(lambda self: logging.getLogger(str(self.id)))
	
class sbRpcServer(SimpleXMLRPCServer):
	sugarbotActivityVar = 'sugarActivityName'
	
	"""
	Sugarbot RPC Server
	
	Responsible for listening on the XML-RPC port (defined as
	sbconfig.port).  The object loads the scripts provided
	on the command-line as individual text lines.  It then provides those
	lines of text over XML-RPC to the XML-RPC client.  The client is
	responsible for parsing the lines into meaningful objects/functions.
	
	The list of files, as well as each file's contents, are kept entirely in
	memory.  This may lead to issues in the future with particularly large
	script files, but this is currently not a problem.
	"""		
	def __init__(self, args=[], xmlport=port, kill=False, restart=False):
		# Random port?
		if xmlport is None:
			xmlport = random.randint(1024,65000)
		self.port = xmlport
		
		# Create the RPC server		
		SimpleXMLRPCServer.__init__(self, (host, xmlport), 
									allow_none=True, logRequests=False)

		# Create the logger
		self.log = self.initializeLogging()
		self.log.info("Listening on port %i" % xmlport)

		# A listing of the filenames for each script
		self.listOfScripts = []
		
		# Keep list of all of the clients
		self.clients		= {0: sugarbotSession(0)}
		
		# Misc other stuffs...
		self.kill			= kill
		self.restart		= restart
		self.log.info("Kill: %s\tRestart: %s" % (kill,restart))
		
		# All of the arguments should be filenames for files to parse.
		for arg in args:
			self.addScript(arg)
		
		# Register all of the functions so that callers can use them
		self.registerFunctions()
		
		# Initialize the random seed for generating client ID's
		self.initRandomSeed()
	
	def testConnectivity(self):
		"""
		This function exists so that the client can test the connection to the
		XML-RPC server.  The xmlrpclib throws a socket.error if a function is
		called when the ServerProxy object is not connected.  
		This provides a simple method so that we don't have to worry 
		about screwing with the internal state of other stuff.
		"""
		pass
	
	def initRandomSeed(self):
		"""
		Initializes the random seed using os.urandom.
		"""
		# Initialize the random seed...
		seed = 0
		try:
			seed = long(binascii.hexlify(os.urandom(64)), 16)
		except NotImplementedError:
			seed = time.time()
		random.seed(seed)
		
	def generateSessionID(self):
		"""
		In the event a client does not specify an identifier, provide one.
		"""
		return random.randint(0, sys.maxint)
		
	def initializeLogging(self):
		"""
		Initialize logging for the sbRpcServer object.  This provides logging
		of INFO and above to the terminal, and all statements go to
		a file named sbRpcServer.log.
		"""
		if __name__ == '__main__':
			logging.basicConfig(level=logging.DEBUG,
			                    format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
			                    datefmt='%m-%d %H:%M',
			                    filename='sbRpcServer.log')
			
			# define a Handler which writes INFO messages or higher to the sys.stderr
			console = logging.StreamHandler()
			console.setLevel(logging.INFO)			
			# set a format which is simpler for console use
			formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
			# tell the handler to use this format
			console.setFormatter(formatter)
			# add the handler to the root logger
			logging.getLogger('').addHandler(console)

		return logging.getLogger('sbRpcServer')
		
	def registerFunctions(self):
		"""
		Register the functions for use with the XML server.
		Recommended internal use only.
		"""
		self.register_function(self.addScript)
		self.register_function(self.completionStatus)
		self.register_function(self.fail)
		self.register_function(self.generateSessionID)
		self.register_function(self.getActivityName)
		self.register_function(self.getKillFlag)
		self.register_function(self.getRestartFlag)
		self.register_function(self.getScript)
		self.register_function(self.numberOfScripts)
		self.register_function(self.resetClientState)		
		self.register_function(self.startScript)
		self.register_function(self.success)
		self.register_function(self.testConnectivity)

	def numberOfScripts(self):
		"""
		Returns the number of scripts for execution.
		"""
		return len(self.listOfScripts)
		
	def haveClient(self,ID):
		"""
		Determine whether or not we have a given client in tracking.
		"""
		return self.clients.has_key(ID)
		
	def getClient(self, ID):
		"""
		Returns the sugarbotSession object corresponding to the provided ID.
		
		If such a sugarbotSession object does not exist, it is created.
		"""
		if not self.haveClient(ID):
			self.clients[ID] = sugarbotSession(ID)
			
		return self.clients[ID]
		
	def completionStatus(self,ID=0):
		"""
		Returns the completion status of all of the scripts as a list.
		For example, if there were a total of three scripts, and the second
		script had a command that did not complete successfully, the return
		value would be:
		[True, False, True]
		
		If no failures were reported:
		[True, True, True]
		
		All values are initialized to False.  If a script is still running,
		then at least one of the result-values will be False.
		
		Removes the client from tracking.  When the client re-connects, it
		will effectively have a clean slate.
		"""
		retval = []
		if self.haveClient(ID):	
			retval = self.clients[ID]
			self.resetClientState(ID)
		else:
			retval = None	
		
		return retval
		
	def resetClientState(self, ID=0):
		"""
		Clears a client from tracking.  
		
		When the client re-connects, it will start with a clean slate.
		"""
		if self.haveClient(ID):
			client = self.getClient(ID)
			
			# Get a completed ratio
			r 			= client.responses
			successes	= len([r[k] for k in r if r[k] is True])
			completed 	= len(r)
			overall		= len(self.listOfScripts)
			self.clients[ID].log.info("Disconnected [%s\%s\%s]" % \
						(successes, completed, overall))
			del self.clients[ID]
		
	def scriptOutOfBounds(self,ID):	
		"""
		Checks to see if a client's script index is out-of-bounds.
		"""
		client = self.getClient(ID)
		
		if client.currentScript < 0:
			return True
		
		if client.currentScript >= len(self.listOfScripts):
			return True
				
	def success(self, ID=0):
		"""
		This method is called after the successful completion of a script
		"""
		client = self.getClient(ID)
		client.log.info("Success (%s)" % client.scriptName) 
		client.responses[client.scriptName] = True
		
	def modifyTraceBackText(self, text, ID=0):
		"""
		Replaces '<string>' with the script name in a backtrace report.
		
		This is useful in determining _which_ script failed execution.
		"""
		client = self.getClient(ID)
		
		if "File \"<string>\"" in text:
			# Get the text..
			replacementText = "Sugarbot Script: '%s'" % client.scriptName
			
			# Replace the string with the filename
			text = text.replace("<string>", replacementText)
		return text
					
	def fail(self, status="No status provided", ID=0):
		"""
		This method is called if, for some reason, the client needs to disconnect
		prematurely.  A status message/reason is required.
		"""
		client = self.getClient(ID)
		
		# If the client is sending us exception text, replace that select
		# portions of the text to be more useful.
		status = self.modifyTraceBackText(status, ID)
		
		# Print the error
		client.log.error("%s" % status)
		client.responses[client.scriptName] = False
		client.failureText[client.scriptName] = status
					
		return status
		
	def startScript(self,ID=0):
		"""
		Increments the internal script counter, looping back to the first
		script if necessary.  This should be the first method that a client
		calls.
		"""
		# Get the client.  This will create it if it does not exist.
		client = self.getClient(ID)
		
		# Next script! (Note that the value is initialized to be -1)
		client.currentScript += 1
	
		# Prevent overflows if, for some reason, we loop back to script 0
		if self.scriptOutOfBounds(ID):
			client.currentScript = 0
			
		# Update the client's script name
		client.scriptName = self.listOfScripts[client.currentScript]
		
		# Pretty status message...
		client.log.info("Starting %s" % client.scriptName)

		# Make room for another response (default to failure)
		client.responses[client.scriptName] = False
		
		return True
		
	def getScript(self,ID=0):
		"""
		Returns the script that the client should execute.
		"""
		client = self.getClient(ID)
		client.log.info("Getting Script: %s" % client.scriptName )
		
		f = open(self.listOfScripts[client.currentScript])
		listOfLines = f.readlines()
		longString	= ''
		for line in listOfLines:
			longString += line
		
		f.close()
			
		return longString
		
	def getActivityName(self, ID=0, which=None):
		"""
		Imports from the specified script file in order to 
		"""
		client = self.getClient(ID)
		if which is None:
			which = client.currentScript
			
		if which == -1:
			raise IndexError, "Invalid script index. If calling " + \
				"getActivityNameMake without arguments, make sure that " + \
				"startScript gets called first."
				
		try:
			moduleName 	= self.listOfScripts[which]
			index 		= moduleName.find(".py")
			if index is not -1:
				moduleName	= moduleName[:index]
			
			execStr = 'from %s import %s' %  \
					(moduleName, self.sugarbotActivityVar)
					
			evalStr = '%s' % self.sugarbotActivityVar
			
			exec execStr
			return eval(evalStr)
		except:
			raise
	
	def getKillFlag(self):
		return self.kill
	
	def getRestartFlag(self):
		return self.restart
		
	def addScript(self,scriptPath):
		"""
		Adds a script to the internal list of scripts.  This causes the parser
		to load the script contents into memory for fast/easy access.
		"""
		self.listOfScripts.append(scriptPath)
				
		activityName = self.getActivityName(which=len(self.listOfScripts)-1 )
		self.log.info("Added script %s [Activity %s]" \
					% (scriptPath, activityName) )

		
def main(argv=None):
	p = optparse.OptionParser()
	p.add_option('--no-restart', dest='restart', action='store_false',default=True,
					help='do NOT restart sugarbot after execution has completed')
	p.add_option('--no-kill', dest='kill', action='store_false',default=True,
					help='do NOT kill sugar after all sugarbot scripts have executed')
			
	(options,args) = p.parse_args()
	
	if len(args) < 1:
		p.print_help()
		return 1
		
	server = sbRpcServer(args, kill=options.kill, restart=options.restart)		
	
	try:
		server.serve_forever()
	except KeyboardInterrupt:
		print 'Ctrl+C pressed, exiting...'
    
if __name__ == '__main__':
	main()