Repository navigation
Expand file tree
/
Copy pathvariant.py
More file actions
260 lines (200 loc) · 8.87 KB
/
Copy pathvariant.py
File metadata and controls
260 lines (200 loc) · 8.87 KB
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
248
249
250
251
252
253
254
255
256
257
258
259
260
import optparse
import urllib2
import json
import os
import urlparse
import filecmp
import shutil
OUTPUT_FOLDER = 'temp'
APPS_FOLDER = 'apps'
APPS_DATA_FOLDER = 'data'
APPS_CONF_FOLDER = 'conf'
APPS_CONF_FILENAME = 'singlevariantconf.json'
PROFILE_APPS_FOLDER = 'svoperapps'
def getOrigin(url):
origin = urlparse.urlsplit(url)
origin = urlparse.urlunparse((origin.scheme, origin.netloc, '', '', '', ''))
return origin
def deleteFolder(path):
if not os.path.exists(path):
return
for root, dirs, files in os.walk(path, topdown=False):
for name in files:
os.unlink(os.path.join(root, name))
for name in dirs:
os.rmdir(os.path.join(root, name))
def check_cached_app(path):
files = os.walk(path).next()[2]
if not 'metadata.json' in files:
return False
if 'update.webapp' in files and 'application.zip' in files:
return True
if 'manifest.webapp' in files:
return True
return False
def copy_app(app_path, profile_path, app_id):
dst = os.path.join(profile_path, PROFILE_APPS_FOLDER, app_id)
if not os.path.exists(dst):
os.makedirs(dst)
for filename in os.walk(app_path).next()[2]:
shutil.copy(os.path.join(app_path, filename), os.path.join(dst, filename))
def download_app(manifest_url, manifest, app_path, app_id, origin, installOrigin):
""" Determine if application is hosted or packaged and save manifest """
filename = 'manifest.webapp'
manifestJSON = json.loads(manifest)
is_packaged_app = False
if 'package_path' in manifestJSON:
is_packaged_app = True
if is_packaged_app:
""" packaged app """
filename = 'update.webapp'
package_path = manifestJSON['package_path']
origin = getOrigin(package_path)
""" download application """
print 'Download application ', package_path
response = urllib2.urlopen(package_path)
app = response.read()
if not origin:
raise Exception("installOrigin required for app '" + app_id + "' in local_apps.json configuration file")
""" Generate metadata.json """
metadata = {
'id': app_id,
'installOrigin': installOrigin,
'manifestURL': manifest_url,
'origin': origin
}
metadata = json.dumps(metadata)
manifest = json.loads(manifest)
manifest.pop('appcache_path', None)
manifest = json.dumps(manifest)
""" Save to filesystem """
with open(os.path.join(app_path, filename), 'wb') as temp_file:
temp_file.write(manifest)
with open(os.path.join(app_path, 'metadata.json'), 'wb') as temp_file:
temp_file.write(metadata)
if is_packaged_app:
with open(os.path.join(app_path, 'application.zip'), 'wb') as temp_file:
temp_file.write(app)
def fetch_manifest(app_id, manifest_url, origin, installOrigin, profile_path, app_path, is_app_cached):
print 'Application ', app_id
""" Download manifest """
print 'Download manifest ', manifest_url
isRemoteManifestAvailable = True
try:
manifest = (urllib2.urlopen(manifest_url)).read()
except:
isRemoteManifestAvailable = False
""" If there is an error getting remote manifest and the app is not cached throw error """
if not isRemoteManifestAvailable:
if not is_app_cached:
raise Exception('Network error')
""" App is cached, use cached one """
copy_app(app_path, profile_path, app_id)
return
""" Remote manifest available """
if not os.path.exists(app_path):
os.makedirs(app_path)
if is_app_cached:
""" Compare manifests """
manifestName = 'manifest.webapp'
if os.path.exists(os.path.join(app_path, 'update.webapp')):
manifestName = 'update.webapp'
with open(os.path.join(app_path, manifestName), 'r') as json_file:
local_manifest = json.load(json_file)
manifest = json.loads(manifest)
manifest.pop('appcache_path', None)
manifest = json.dumps(manifest, sort_keys=True)
if manifest == json.dumps(local_manifest, sort_keys=True):
""" Cached manifest and rmeote are equal """
copy_app(app_path, profile_path, app_id)
return
""" remote manifest has changed, app needs to be updated """
download_app(manifest_url, manifest, app_path, app_id, origin, installOrigin)
copy_app(app_path, profile_path, app_id)
def fetch_apps(data, profile_path, distribution_path):
apps = data['apps']
distribution_path = os.path.join(distribution_path, OUTPUT_FOLDER, APPS_FOLDER, APPS_DATA_FOLDER)
cached_apps = os.walk(distribution_path).next()[1]
for app in apps:
app_path = os.path.join(distribution_path, app)
is_app_cached = False
if app in cached_apps:
cached_apps.remove(app)
is_app_cached = True
if not check_cached_app(app_path):
deleteFolder(app_path)
is_app_cached = False
origin = None
if 'origin' in apps[app]:
origin = apps[app]['origin']
installOrigin = None
if 'installOrigin' in apps[app]:
installOrigin = apps[app]['installOrigin']
fetch_manifest(app, apps[app]['manifestURL'], origin, installOrigin, profile_path, app_path, is_app_cached)
for app in cached_apps:
deleteFolder(os.path.join(distribution_path, app))
os.rmdir(os.path.join(distribution_path, app))
def fetch_conf(data, profile_path, distribution_path):
print 'Generating metadata'
""" Get apps to check faster if operator's apps are valid and get access to manifests """
apps = data['apps']
""" Iterate through operators """
output = {}
output_homescreen = {}
for operator in data['operators']:
""" Build an array with the apps for one operator and check if id is valid"""
row = []
row_homescreen = []
if operator.has_key('apps'):
for app in operator['apps']:
app_id = app['id']
if not apps.has_key(app_id):
raise Exception("Invalid application id \'" + app_id + "\'")
row.append(app_id)
if not apps[app_id].has_key('manifestURL'):
raise Exception("manifestURL not found for application \'" + app_id + "\'")
app['manifestURL'] = apps[app_id]['manifestURL']
if app.has_key('id'):
del app['id']
row_homescreen.append(app)
""" For each mcc-mnc create an object with its apps array and add it to output """
for mcc in operator['mcc-mnc']:
output[mcc] = row
output_homescreen[mcc] = row_homescreen
with open(os.path.join(profile_path, PROFILE_APPS_FOLDER, APPS_CONF_FILENAME), 'w') as temp_file:
temp_file.write(json.dumps(output))
with open(os.path.join(distribution_path, OUTPUT_FOLDER, APPS_FOLDER, APPS_CONF_FOLDER, APPS_CONF_FILENAME), 'w') as temp_file:
temp_file.write(json.dumps(output_homescreen))
def main():
""" Arguments & options management """
parser = optparse.OptionParser(description="Prepare local applications")
parser.add_option('-l', '--local-apps-path', help="Path to the JSON defining the local apps")
parser.add_option('-p', '--profile-path', help="Path to the profile folder")
parser.add_option('-d', '--distribution-path', help="Path to the gaia distribution folder")
(options, args) = parser.parse_args()
if not options.local_apps_path:
parser.error('local-aps-path not given')
if not options.profile_path:
parser.error('profile-path not given')
if not options.distribution_path:
parser.error('distribution-path not given')
""" Create and clear temporal output folders """
output_folder = os.path.join(options.distribution_path, OUTPUT_FOLDER)
if not os.path.exists(output_folder):
os.makedirs(output_folder)
if not os.path.exists(os.path.join(output_folder, APPS_FOLDER, APPS_DATA_FOLDER)):
os.makedirs(os.path.join(output_folder, APPS_FOLDER, APPS_DATA_FOLDER))
if not os.path.exists(os.path.join(output_folder, APPS_FOLDER, APPS_CONF_FOLDER)):
os.makedirs(os.path.join(output_folder, APPS_FOLDER, APPS_CONF_FOLDER))
deleteFolder(os.path.join(output_folder, APPS_FOLDER, APPS_CONF_FOLDER))
""" Clear profile folder """
deleteFolder(os.path.join(options.profile_path, PROFILE_APPS_FOLDER))
""" Open JSON containning the apps and get saved apps to process and fetch each application """
with open(options.local_apps_path, 'r') as json_file:
data = json.load(json_file)
""" Prepare apps """
fetch_apps(data, options.profile_path, options.distribution_path)
""" Generate internal JSON based on mnc/mcc """
fetch_conf(data, options.profile_path, options.distribution_path)
if __name__ == "__main__":
main()