diff options
Diffstat (limited to 'launch.py')
-rwxr-xr-x | launch.py | 208 |
1 files changed, 31 insertions, 177 deletions
@@ -1,14 +1,5 @@ #!/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 @@ -21,11 +12,12 @@ import BaseHTTPServer DATAPATH = path(activity.get_activity_root()) / "data" WORKPATH = DATAPATH / "work" -SUBJECTS = ['Mathematics','Mathematics11'] +SUBJECTS = ['English','Mathematics'] + +log = open('/tmp/loglaunch','w') class Learn(activity.Activity): def __init__(self, handle): - self.flog = open('logfile','w') activity.Activity.__init__(self, handle) #get serial number and nick fin = open('/ofw/serial-number','r') @@ -46,27 +38,28 @@ class Learn(activity.Activity): except: self.nickname = 'newbuild' self.grade = 'P6' - print >> self.flog, self.serial, self.nickname, self.grade + print >> log, self.serial, self.nickname, self.grade if path('firstrun').exists(): + print >> log, 'firstrun' subprocess.call('cp -rf .mozilla/ .macromedia /home/olpc', shell=True) subprocess.call('rm firstrun', shell=True) #set up student record based on nick fout = open(DATAPATH / self.nickname,'w') - fout.write("{'grade':'P4','progress':[],'milestones:['1.1','1.2']}") + fout.write("{'grade':'P4','progress':[],'milestones':['1.1','1.1']}") fout.close() #set up learner based on nick fout = open(DATAPATH / 'learner', 'w') fout.write(self.nickname) fout.close() - #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) + #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' + #print >> log, 'close any open localserver' tpid = subprocess.Popen('ps aux |grep CGIHTTPServer', stdout = subprocess.PIPE, shell=True) tstr = tpid.communicate()[0] tt = tstr.split('\n') @@ -74,172 +67,33 @@ class Learn(activity.Activity): 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 + p= subprocess.Popen(cmd, stdout=subprocess.PIPE,shell=True) + self.sftpflag = True + print >> log, 'CGIHTTPServer started', p.pid, 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 + activity_close() def write_file(self, file_path): - #nick of learner is in /tmp/learner + #nick of learner is in DATAPATH / learner + fin = open(DATAPATH / 'learner','r') + learner = fin.read() + fin.close() + #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: - 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) + fout.write(txt) + finally: + fout.close() def activity_close(self): + log.close() self.close(True) |