Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/cgi-bin/activities.py
blob: 959eb3a8d8b1b6df1a6dc3760c3e285cb7f5199f (plain)
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
#!/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 getNick()
    nickname = 'Funknown'
    pth = path('/home') / 'olpc' / '.sugar' / 'default' / 'config'
    if pth.exists():
        fin = open(pth,'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()
    return nickname

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)