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
|
#!/usr/bin/env python
from __future__ import with_statement, division
import sys, cgitb
cgitb.enable(format="plain")
cgitb.handler = sys.excepthook.handle
from pprint import pprint
from sugar.datastore import datastore as ds
from os.path import exists, join, splitext
from os import makedirs, rename, link, chmod
from commands import mkarg
from errno import EEXIST
from urllib import quote_plus
from jarabe.model.bundleregistry import get_registry
from sugar import env
import cjson
import sugar.mime
def mkdir_p(path):
try: makedirs(path)
except OSError, e:
if e.errno == EEXIST: pass
else: raise
def get_bundle(md):
return get_registry().get_bundle(str(md['activity']))
def get_command(md, bundle):
command = bundle.get_command().split(' ')
command.extend(['-b', str(md['activity'])])
command.extend(['-a', str(md['activity_id'])])
if 'object_id' in md and md['object_id'] != "":
command.extend(['-o', str(md['object_id'])])
if 'uri' in md and md['uri'] != "":
command.extend(['-u', str(md['uri'])])
# if the command is in $BUNDLE_ROOT/bin, execute the absolute path so there
# is no need to mangle with the shell's PATH
if '/' not in command[0]:
bin_path = join(bundle.get_path(), 'bin')
absolute_path = join(bin_path, command[0])
if exists(absolute_path):
command[0] = absolute_path
return [mkarg(v)[1:] for v in command]
def get_environ(md, bundle):
ret = {}
bin_path = join(bundle.get_path(), 'bin')
activity_root = env.get_profile_path(str(md['activity']))
ret['SUGAR_BUNDLE_PATH'] = bundle.get_path()
ret['SUGAR_BUNDLE_ID'] = str(md['activity'])
ret['SUGAR_ACTIVITY_ROOT'] = activity_root
ret['PATH'] = bin_path
if bundle.get_path().startswith(env.get_user_activities_path()):
ret['SUGAR_LOCALEDIR'] = join(bundle.get_path(), 'locale')
return ret
def export(e):
md = e.metadata.get_dictionary()
md['object_id'] = e.object_id
print md['activity_id']
title = str(md.get('title', ''))
if 'activity_id' in md and md['activity_id'] != "":
title = title + "_" + str(md.get('activity_id', '')[0:8])
title = quote_plus(title)
print title
preview = md['preview']
del md['preview']
final_dir = title
dir = final_dir + ".tmp"
if exists(final_dir):
print 'already exists'
return
xos_dir = join(dir, ".xos")
mkdir_p(xos_dir)
with open(join(xos_dir, "metadata.json"), "w") as f:
f.write(cjson.encode(md))
with open(join(xos_dir, "preview.png"), "w") as f:
f.write(preview)
if e.file_path != "":
base, ext = splitext(title)
if ext == '':
mime_type = md['mime_type']
ext = sugar.mime.get_primary_extension(mime_type)
if ext == None: ext = ".bin"
else: ext = "." + ext
link(e.file_path, join(dir, base + ext))
if 'activity' in md and md['activity'] != '' and \
'activity_id' in md and md['activity_id'] != '':
bundle = get_bundle(md)
cmd = get_command(md, bundle)
env = get_environ(md, bundle)
resume_path = join(xos_dir, "resume")
with open(resume_path, "w") as f:
f.write("#!/bin/bash\n")
f.write("cd" + mkarg(str(bundle.get_path())) + "\n")
f.write("env ")
for k, v in env.iteritems():
if k == 'PATH':
f.write('%s=%s' % (mkarg(k)[1:], mkarg(v)[1:]))
f.write(':"$PATH"\\\n ')
else:
f.write('%s=%s\\\n ' % (mkarg(k)[1:], mkarg(v)[1:]))
f.write(" ".join(cmd) + "\n")
chmod(resume_path, 0755)
rename(dir, final_dir)
def main():
results, count = ds.find({})
for e in results:
try: export(e)
except: cgitb.handler()
print
main()
|