#!/usr/bin/python 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 firstrun = False if not path('activities.py').exists(): subprocess.call('ln -s cgi-bin/activities.py activities.py',shell=True) firstrun = True from activities import getSubjects from activities import getKarma try: from sugar import profile _using_gconf = False except ImportError: _using_gconf = True try: import gconf except ImportError: _using_gconf = False DATAPATH = path(activity.get_activity_root()) / "data" WORKPATH = DATAPATH / "work" class Learn(activity.Activity): def __init__(self, handle): activity.Activity.__init__(self, handle) log = open('/tmp/loglaunch','w') print >> log, 'activity init' #get serial number and nick fin = open('/ofw/serial-number','r') self.serial = fin.read()[:11] fin.close() print >> log, self.serial self.using_gconf = _using_gconf self.nickname = self.get_nick_from_sugar() self.grade = self.nickname[:2] print >> log, self.serial, self.nickname, self.grade if not path(DATAPATH / self.nickname).exists(): #set up student record based on nick print 'new student - set up student record',self.nickname student_record = {'grade':self.grade, 'progress':[], 'milestones':['1.1','1.1'] } fout = open(DATAPATH / self.nickname,'w') fout.write(str(student_record)) fout.close() if firstrun: #reset content print >> log,'firstrun: reset content' subprocess.call('rm -rf content/*',shell=True) subprocess.call('cp -rf .mozilla/ .macromedia /home/olpc', shell=True) pth = path('content') / 'karma' if not pth.exists(): getKarma() if not pth.exists(): print >> log, 'karma folder is not loaded' pth = path('content') / 'subjects.js' if self.nickname[0] == 'F': mode = 'Faculty' else: mode = self.nickname[:2] if not pth.exists(): getSubjects(mode) if not pth.exists(): print >> log, 'subject.js not loaded' fin = open(pth,'r') txt = fin.read() fin.close() print >> log, 'subjects.js',txt #start 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') for t in tt: if t.find('python') > -1: cmd = 'kill -9 ' + t[4:13].strip() subprocess.call(cmd, shell=True) cmd = 'python -m CGIHTTPServer 8008' p= subprocess.Popen(cmd, stdout=subprocess.PIPE,shell=True) print >> log, 'CGIHTTPServer started', p.pid #start Firefox cmd = './firefox http://localhost:8008/content/index.html?'+self.nickname print >> log, 'launch Firefox', cmd pid = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE,shell=True) (result,err) = pid.communicate() print >> log, 'Firefox quit result:', result,'err:', err log.close() activity.close() def get_nick_from_sugar(self): """ Returns nick from Sugar """ if self.using_gconf: client = gconf.client_get_default() return client.get_string('/desktop/sugar/user/nick') else: return profile.get_nick_name() def write_file(self, file_path): #we need to read the student record try: fin = open(DATAPATH / self.nickname,'r') txt = fin.read() fin.close() #and save it to the Journal fout = open(file_path,'w') try: fout.write(txt) finally: fout.close() except: pass