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
|
#!/usr/bin/env python
# encoding: utf-8
"""
sbexecutionengine.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 gobject
import logging
import os
import pygtk
import sys
import threading
import time
import traceback
pygtk.require('2.0')
import gtk
from sbpython import *
class sbExecutionEngine:
"""
Responsible for communications with the XML-RPC server regarding command
retrieval, as well as parsing and execution of those commands.
"""
def __init__(self, sbGUI, xmlRpcServer):
self.sbgui = sbGUI
self.rpc = xmlRpcServer
self.widgets = sbwidgets
self.delayedExecution = False
self.executionComplete = False
self.lastCommand = None
self.log = logging.getLogger("ExecutionEngine")
try:
self.id = os.environ['SUGARBOT_ID']
except KeyError:
self.log.error("Sugarbot ID is not set. Using default 0. ")
self.id = 0
def killSugarbot(self):
"""
Kills the sugarbot activity.
"""
self.log.info("Attempting to kill sugarbot")
sys.exit(0)
def executePy(self):
"""
Executes the Sugarbot p◊ython script.
Executes the Sugarbot python script provided by the XML-RPC server.
Regards any unhandled exceptions as fatal errors, and reports them
to the server. Also handles the termination of Sugarbot if the
auto-restart flag has been set by the XML-RPC server.
"""
self.script = self.rpc.getScript(self.id)
self.log.info("Executing Script:\n%s" % self.script)
self.widgets.gui = self.sbgui
# Execute the actual python script.
try:
exec self.script
# t = threading.Thread(args=(self.widgets,))
# t.run = sugarbot_main
# t.start()
sugarbot_main(self.widgets)
self.rpc.success(self.id)
# Something bad happened. Report it, and log it.
except:
text = "Execution failed:\n%s" % traceback.format_exc()
text = self.rpc.fail(text, self.id)
self.log.error( text )
# Regarless of the success status, check to see if we need to restart
# sugarbot or not.
finally:
restart = self.rpc.getRestartFlag()
self.log.info("Got restart flag: " + str(restart))
if restart:
self.killSugarbot()
def isComplete(self):
"""
Returns true if execution is complete.
Returns true if execution is complete, i.e. there are no more
commands that can be executed.
"""
return self.executionComplete
|