Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/cgi-bin/activities.py
blob: b2f7b0e72fee5ab532d746039248cbb1d4c0750d (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
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
243
244
245
246
247
#!/usr/bin/python

import os, sys, subprocess
from sugar.activity import activity
from path import path

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'
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)
    subprocess.call('mkdir -p ' + WORKPATH, 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
    pipe1 = 'stdout=subprocess.PIPE'
    pipe2 = 'stderr=subprocess.PIPE'
    if folder:
        pid = subprocess.Popen(
            cmd, 
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            cwd=folder, 
            shell=True)
    else:
        pid = subprocess.Popen(
            cmd, 
            stdout=subprocess.PIPE,  
            stderr=subprocess.PIPE, 
            shell=True)
    (result,err) = pid.communicate()
    return result, err

def getNick():
        """ Returns nick from Sugar """
        if _using_gconf:
            client = gconf.client_get_default()
       	    return client.get_string('/desktop/sugar/user/nick')
        else:
            return profile.get_nick_name()

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 extractEntries(txt,log):
    lines = txt.split(';')
    line = lines[0].replace('\n',' ')
    pos = line.find('=')
    line = line[pos+1:]
    entries = eval(line)
    return entries

def getEntries(subject, course, milestone,log):
    pth = path('content') /subject
    if len(milestone) > 0:
        fpth = pth / course / milestone / 'activities.js'
    else:
        fpth = pth / course / 'milestones.js'
    fin = open(fpth,'r')
    txt = fin.read()
    fin.close()
    return extractEntries(txt,log)

def setEntries(subject, course, milestone, entries):
    pth = path('content') /subject
    if len(milestone) > 0:
        fpth = pth / course / milestone / 'activities.js'
    else:
        fpth = pth / course / 'milestones.js'
    txtout = 'var activities = [\n'
    for entry in  entries:
        txtout = txtout + str(entry) + ',\n' 
    txtout = txtout + '];\n'
    fout = open(fpth,'w')
    fout.write(txtout)
    fout.close()

def getMilestones(subject, grade):
    milestones = getEntries(subject,grade, "")
    return milestones

def getActivities(subject, milestone):
    activities = getEntries(subject, 0, 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, course, milestone):
    if USB.exists(): #get from usb drive
        src = USB / subject / course / milestone + '.msxo'
        dst = path('content') / subject /course
        cmd = 'cp -r ' + src + ' ' + dst
        subprocess.call(cmd,shell=True)
    else: #get from schoolserver
        folder = path('content') / subject / course
        pth = SS / subject / course
        script = 'cd ' + str(pth) + '\nget ' + milestone + '.msxo'
        (result,err) = sftp(script, folder)
    bndl = milestone + '.msxo'
    cwd = path('content') / subject / course
    pth = cwd / bndl
    if pth.exists():
        cmd = 'unzip ' + bndl
        subprocess.call(cmd,cwd=cwd,shell=True)
        cmd = 'rm -rf ' + pth
        subprocess.call(cmd, shell=True)
        return 0
    else:
        return 1

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,err) = sftp(script, folder)

def getSubjects(mode):
    alog = open('/tmp/loggetSubjects','w')
    if USB.exists(): #get from usb drive
        pth = USB / 'subjects.js'
        cmd = 'cp ' + pth  + ' ' + 'content/'
        subprocess.call(cmd,shell=True)
        pth = USB / 'index.html'
        cmd = 'cp ' + pth  + ' ' + 'content/'
        subprocess.call(cmd,shell=True)
        files = USB.files('version*')
        pth = USB / files[0]
        cmd = 'cp ' + pth  + ' ' + 'content/'
        subprocess.call(cmd,shell=True)
    else: #get from schoolserver
        folder = 'content/'
        pth = SS
        script = 'cd ' + str(pth) + '\nget subjects.js\nget index.html\nmget version-*'
        (result,err) = sftp(script, folder)
        print >> alog, 'sftp',result,'err',err
    print >> alog, 'set mode', mode
    #set mode
    fin = open('content/subjects.js','r')
    txt = fin.read()
    fin.close()
    txtout = 'var mode = "' +mode + '"\n\n'
    txtout += txt
    fout = open('content/subjects.js','w')
    fout.write(txtout)
    fout.close()
    #set up subject folders
    print >> alog, 'set up subject folders', txt
    lines = txt.split('\n')
    for line in lines:
        if not '[' in line:
            continue
        try:
            entry = eval(line)[0]
        except:
	    continue
        if len(entry)<3:
            entry = eval(line)
        subject = entry[1]
        srcpth = path(subject)
        tgtpth = path('content') / subject
        print >> alog, 'pths', srcpth, tgtpth, tgtpth.exists()
        if not tgtpth.exists():
            subprocess.call('mkdir ' + tgtpth,shell=True)
            result = getFile(srcpth, 'courses.js')
            result = getFile(srcpth, 'index.html')
            result = getFile(srcpth, subject.lower()+'.png') 
    alog.close()

def getKarma():
    if USB.exists(): #get from usb drive
        pth = USB / 'karma.zip'
        cmd = 'cp -r ' + pth  + ' ' + 'content/'
        subprocess.call(cmd,shell=True)
    else: #get from schoolserver
        folder = 'content/'
        pth = SS
        script = 'cd ' + str(pth) + '\nget karma.zip'
        (result,err) = sftp(script, folder)
    cmd = 'unzip -q karma.zip'
    cwd = path('content') 
    subprocess.call(cmd,cwd=cwd,shell=True)
    cmd = 'rm -rf content/karma.zip'
    subprocess.call(cmd, shell=True)

def getFile(filepth,filename):
    pth = filepth / filename
    tgtpth = path('content') / filepth
    result = ''
    if USB.exists(): #get from usb drive
        pth = USB / filepth
        cmd = 'cp -r ' + pth / filename  + ' ' + tgtpth
        subprocess.call(cmd,shell=True)
    else: #get from schoolserver
        pth = SS / filepth
        if filename.find('*') > -1:
            script = 'cd ' + str(pth) + '\nmget ' + filename
        else:
            script = 'cd ' + str(pth) + '\nget ' + filename
        (result,err) = sftp(script, tgtpth)
    return result

def _getParam(txt):
    pos1 = txt.find('var =')
    pos2 = txt.find('=')
    key = txt[pos1+5:pos2].strip()
    value = txt[pos2+1:-1].strip()
    value = value.replace("'","")
    return(key,value)