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
|
#!/usr/bin/python
#demo version
#XO2 is P1..P3
#XO3 is P4..P6
#loads one module per 'nick'
#ignore faculty
#if current module not installed, install it
#remove previous module if installed
##essentially one student per grade for demo purposes
#maintain journal entry by 'nick'
from sugar.activity import activity
from sugar.activity.activity import get_bundle_path
from sugar.datastore import datastore
import subprocess, time, sys
from path import path
import CGIHTTPServer
import BaseHTTPServer
DATAPATH = path(activity.get_activity_root()) / "data"
WORKPATH = DATAPATH / "work"
SUBJECTS = ['English','English11','Mathematics','Mathematics11']
MOODLE_ENGLISH = [0,20,21,9,10,11,22]
ENGLISH_MILESTONES = [0,8,8,8,6,4,4]
MOODLE_MATHEMATICS = [0,13,14,15,16,17,18]
MATHEMATICS_MILESTONES = [0,8,8,8,4,5,6]
class Learn(activity.Activity):
def __init__(self, handle):
self.flog = open('logfile','w')
activity.Activity.__init__(self, handle)
if path('firstrun').exists():
subprocess.call('cp -rf .mozilla/ .macromedia /home/olpc', shell=True)
subprocess.call('rm firstrun', shell=True)
#get serial number and nick
fin = open('/ofw/serial-number','r')
self.serial = fin.read()[:11]
fin.close()
try:
fin = open('/home/olpc/.sugar/default/config','r')
txt = fin.read()
fin.close
lines = txt.split('\n')
for line in lines:
if line.find('nickname') > -1:
pos = line.find('=')
self.nickname = line[pos+1:].strip()
if line.find('grade') > -1:
pos = line.find('=')
self.grade = line[pos+1:].strip()
except:
self.nickname = 'newbuild'
self.grade = 'P6'
print >> self.flog, self.serial, self.nickname, self.grade
#add links for karma folder
base = path(get_bundle_path()) / 'content'
karma = base / 'karma'
for pth in SUBJECTS:
cwd = base / pth
subprocess.call('ln -s ' + karma + ' karma',shell=True,cwd=cwd)
#start localserver
#print 'close any open localserver'
tpid = subprocess.Popen('ps aux |grep CGIHTTPServer', stdout = subprocess.PIPE, shell=True)
tstr = tpid.communicate()[0]
tt = tstr.split('\n')
for t in tt:
if t.find('python') > -1:
cmd = 'kill -9 ' + t[4:13].strip()
subprocess.call(cmd, shell=True)
try:
rt = subprocess.call('ls /media/SS', shell=True)
except:
print >> self.flog, '/media/SS not found', sys.exc_info()[:2]
rt = 2
cmd = 'python -m CGIHTTPServer 8008'
if rt > 0:
p= subprocess.Popen(cmd, stdout=subprocess.PIPE,shell=True)
self.sftpflag = True
else:
p= subprocess.Popen(cmd, stdout=subprocess.PIPE,shell=True,cwd = '/media/SS')
self.sftpflag = True
print >> self.flog, 'CGIHTTPServer started', p.pid, 'rt=', rt, self.sftpflag
subprocess.call('mkdir -p ' + WORKPATH, shell=True)
print 'sftp flag=',self.sftpflag
if self.sftpflag == True:
print >> self.flog, 'call openLearn'
self.openLearn()
#start Firefox
cmd = './firefox http://localhost:8008/content/index.html'
subprocess.call(cmd, shell=True)
#is it possible to save state here?
def openLearn(self):
#set up current state for this learner
#this should all be in openLearn
print >> self.flog, 'processing openLearn'
self.sinfo = {'English':{'1':[]},'Mathematics':{'1':[]}}
self.sinfo['nick'] = self.nickname
self.sinfo['grade'] = self.grade
print >> self.flog, 'openLearn', str(self.sinfo)
#open local statefile and set self.sinfo from it
#if none, we can use the default
try:
fin = open(DATAPATH / self.nickname,'r')
sinfo = eval(fin.read())
fin.close()
self.sinfoflag = True
except:
print >> self.flog, 'user', self.nickname, 'not found'
self.sinfoflag = False
if self.sinfoflag:
keys = self.sinfo.keys()
keys.sort()
for key in keys:
try:
self.sinfo[key] = sinfo[key]
except:
print >> self.flog, 'key error', key
print >> self.flog, 'sinfoflag', self.sinfoflag
#gets most recent Journal entry
timestamp = 0
jentry = None
ds_objects, num_objects = datastore.find({'title':'Learn Activity'})
print >> self.flog, 'journal entries', num_objects
for i in xrange(0, num_objects, 1):
ds_objects[i].destroy()
try:
nick = ds_objects[i].metadata['nick']
if not nick == self.nickname:
continue
except:
nick = ""
continue
try:
this = ds_objects[i].metadata['timestamp']
except:
this = timestamp
if this > timestamp:
jentry = ds_objects[i]
timestamp = this
if jentry:
#the values from the Journal entry should be the same as the
#local copy
#we should read the local file and compare to the latest entry
#for now, any discrepancy will be logged but the local copy will be
#used
print >> self.flog, 'latest journal entry', this
temp = self.sinfo.keys()
try:
keys= temp.sort()
except:
keys = []
print >> self.flog, 'no keys', str(self.sinfo)
for key in keys:
try:
if not self.sinfo[key] == jentry.metadata[key]:
print >> self.flog, 'key value differs', key, self.sinfo[key], jentry.metadata[key]
except:
print >> self.flog, 'key error', key
else:
print 'journal entry not found'
cfg = str(self.sinfo)
print >> self.flog, self.nickname, cfg
fout = open(DATAPATH / self.nickname, 'w')
fout.write(cfg)
fout.close()
#executes sftp command contained in scrpt
#note scrpt must be written to disk and read by sftp command
def sftp(self, script, folder = None):
pth = WORKPATH / 'script'
try:
fout = open(pth, 'w')
fout.write(script)
fout.close()
except:
print >> self.flog, 'write of script failed', sys.exc_info()[:2]
print >> self.flog, 'in sftp, script written'
authpth = '/home/olpc/Activities/Learn.activity/.ssh/id_rsa'
auth = ' -oIdentityFile=' + authpth
scrpt = ' -b ' + pth
srvr = ' admin@schoolserver'
cmd = 'sftp ' + auth + scrpt + srvr
if folder:
pid= subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=folder, shell=True)
pid.wait()
print >> self.flog, 'sftp', folder, pid.returncode
if pid.returncode == 0:
result = pid.communicate()[0]
if len(result) < 1:
print >> self.flog, 'not connected'
else:
result = pid.communicate()[0]
#err = pid.communicate()[1]
else:
pid = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
pid.wait()
print >> self.flog, 'sftp', pid.returncode
if pid.returncode == 0:
result = pid.communicate()[0]
if len(result) < 1:
print >> self.flog, 'not connected'
else:
result = pid.communicate()[0]
#err = pid.communicate()[1]
if pid.returncode == 0:
print >> self.flog, 'in sftp, result', result
#print >> self.flog, 'in sftp, err', err
return result
def write_file(self, file_path):
#nick of learner is in /tmp/learner
try:
fin = open('/tmp/learner','r')
learner = fin.read()
fin.close()
except:
return
if not learner == 'unknown':
#we need to read the student record
fin = open(DATAPATH / learner,'r')
txt = fin.read()
fin.close()
#and save it to the Journal
fout = open(file_path,'w')
try:
fout.write(txt)
finally:
fout.close()
#rm /tmp/learner to log out
subprocess.call('rm -rf /tmp/learner',shell=True)
def activity_close(self):
self.close(True)
|