Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/cgi-bin/activities.py
diff options
context:
space:
mode:
Diffstat (limited to 'cgi-bin/activities.py')
-rwxr-xr-xcgi-bin/activities.py125
1 files changed, 125 insertions, 0 deletions
diff --git a/cgi-bin/activities.py b/cgi-bin/activities.py
new file mode 100755
index 0000000..35b9d05
--- /dev/null
+++ b/cgi-bin/activities.py
@@ -0,0 +1,125 @@
+#!/usr/bin/python
+
+import os, sys, subprocess
+from sugar.activity import activity
+from path import path
+
+DATAPATH = path(activity.get_activity_root())/ 'data'
+WORKPATH = DATAPATH / 'work'
+USB = path('/media/2011/courseware')
+SS = path('/library/courseware')
+
+#executes sftp command contained in scrpt
+#note scrpt must be written to disk and read by sftp command
+def sftp(script, folder = None):
+ pth = WORKPATH / 'script'
+ subprocess.call('rm -rf ' + pth, shell=True)
+ fout = open(pth, 'w')
+ fout.write(script)
+ fout.close()
+ authpth = '/home/olpc/.sugar/default/owner.key'
+ auth = ' -oIdentityFile=' + authpth
+ scrpt = ' -b ' + pth
+ fin = open('/ofw/serial-number','r')
+ serial_number = fin.read()
+ fin.close()
+ srvr = serial_number[:-1] + '@schoolserver'
+ cmd = 'sftp ' + auth + scrpt + ' ' + srvr
+ if folder:
+ subprocess.call(cmd, cwd=folder, shell=True)
+ else:
+ subprocess.call(cmd, shell=True)
+ return 0
+
+def getInstalled(subject):
+ pth = path('content') / subject
+ temp = pth.dirs()
+ milestones = []
+ for item in temp:
+ milestones.append(str(item.namebase))
+ milestones.remove('karma')
+ return milestones
+
+def getEntries(subject, milestone):
+ pth = path('content') /subject
+ if len(milestone) > 0:
+ fpth = pth / milestone / 'activities.js'
+ else:
+ fpth = pth / 'activities.js'
+ fin = open(fpth,'r')
+ txt = fin.read()
+ fin.close()
+ entries = []
+ lines = txt.split('\n')
+ for line in lines:
+ try:
+ entry = eval(line)[0]
+ entries.append(entry)
+ except:
+ continue
+ return entries
+
+def setEntries(subject, grade, milestone, entries):
+ pth = path('content') /subject
+ if len(milestone) > 0:
+ fpth = pth / milestone / 'activities.js'
+ else:
+ fpth = pth / 'activities.js'
+ fin = open(fpth,'r')
+ txt = fin.read()
+ fin.close()
+ txtout = 'var activities = [\n'
+ for entry in entries:
+ txtout = txtout + str(entry) + ',\n'
+ txtout = txtout + '];\nvar assetList = [];\nvar logos = [];\n'
+ fout = open(fpth,'w')
+ fout.write(txtout)
+ fout.close()
+ if grade > 0:
+ fout = open(pth /milestone / 'activities_'+grade+'.js','w')
+ fout.write(txtout)
+ fout.close()
+
+def getMilestones(subject):
+ milestones = getEntries(subject, "")
+ return milestones
+
+def getActivities(subject, milestone):
+ activities = getEntries(subject, milestone)
+ return activities
+
+def setMilestones(subject, grade, milestones):
+ setEntries(subject,grade, "", milestones)
+
+def setActivities(subject, milestone, activities):
+ setEntries(subject, 0, milestone, activities)
+
+def fetchMilestone(subject, grade, milestone):
+ if USB.exists(): #get from usb drive
+ pth = USB / subject
+ cmd = 'cp -r ' + pth / milestone + '.msxo ' + 'content/'+ subject
+ try:
+ subprocess.call(cmd,shell=True)
+ except:
+ print 'failed',sys.exc_info()[:2]
+ else: #get from schoolserver
+ folder = 'content/'+ subject
+ pth = SS / subject
+ script = 'cd ' + str(pth) + '\nget ' + milestone + '.msxo'
+ result = sftp(script, folder)
+ cmd = 'unzip '+ milestone +'.msxo'
+ cwd = path('content') / subject
+ subprocess.call(cmd,cwd=cwd,shell=True)
+ cmd = 'rm -rf content/'+subject+'/'+milestone+'.msxo'
+ subprocess.call(cmd, shell=True)
+
+def getActivitiesJs(subject):
+ if USB.exists(): #get from usb drive
+ pth = USB / subject
+ cmd = 'cp -r ' + pth / 'activities*' + ' ' + 'content/'+ subject
+ subprocess.call(cmd,shell=True)
+ else: #get from schoolserver
+ folder = 'content/'+ subject
+ pth = SS / subject
+ script = 'cd ' + str(pth) + '\nget activities*'
+ result = sftp(script, folder)