- add third_party src.
[platform/framework/web/crosswalk.git] / src / xwalk / app / tools / android / manifest_json_parser.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2013, 2014 Intel Corporation. All rights reserved.
4 # Use of this source code is governed by a BSD-style license that can be
5 # found in the LICENSE file.
6
7 """
8 Parse JSON-format manifest configuration file and
9 provide the specific fields, which have to be integrated with
10 packaging tool(e.g. make_apk.py) to generate xml-format manifest file.
11
12 Sample usage from shell script:
13 python manifest_json_parser.py --jsonfile=/path/to/manifest.json
14 """
15
16 import json
17 import optparse
18 import os
19 import re
20 import sys
21
22 def HandlePermissionList(permission_list):
23   """This function is used to handle the permission list and return the string
24   of permissions.
25
26   Args:
27     permission_list: the permission list, e.g.["permission1", "permission2"].
28
29   Returns:
30     The string of permissions with ':' as separator.
31     e.g. "permission1:permission2".
32   """
33   permissions = list(permission_list)
34   reg_permission = re.compile(r'^[a-zA-Z\.]*$')
35   for permission in permissions:
36     if not reg_permission.match(permission):
37       print '\'Permissions\' field error, only alphabets and \'.\' are allowed.'
38       sys.exit(1)
39   return ':'.join(permissions)
40
41
42 class ManifestJsonParser(object):
43   """ The class is used to parse json-format manifest file, recompose the fields
44   and provide the field interfaces required by the packaging tool.
45
46   Args:
47     input_path: the full path of the json-format manifest file.
48   """
49   def __init__(self, input_path):
50     self.input_path = input_path
51     input_file = file(self.input_path)
52     try:
53       input_src = input_file.read()
54       self.data_src = json.JSONDecoder().decode(input_src)
55       self.ret_dict = self._output_items()
56     except (TypeError, ValueError, IOError):
57       print 'There is a parser error in manifest.json file.'
58       sys.exit(1)
59     except KeyError:
60       print 'There is a field error in manifest.json file.'
61       sys.exit(1)
62     finally:
63       input_file.close()
64
65   def _output_items(self):
66     """ The manifest field items are reorganized and returned as a
67     dictionary to support single or multiple values of keys.
68
69     Returns:
70       A dictionary to the corresponding items. the dictionary keys are
71       described as follows, the value is set to "" if the value of the
72       key is not set.
73     app_name:         The application name.
74     version:          The version number.
75     icons:            An array of icons.
76     app_url:          The url of application, e.g. hosted app.
77     description:      The description of application.
78     app_root:         The root path of the web, this flag allows to package
79                       local web application as apk.
80     app_local_path:   The relative path of entry file based on app_root,
81                       this flag should work with "--app-root" together.
82     permissions:      The permission list.
83     required_version: The required crosswalk runtime version.
84     plugin:           The plug-in information.
85     fullscreen:       The fullscreen flag of the application.
86     """
87     ret_dict = {}
88     if not self.data_src.has_key('name'):
89       print 'Error: no \'name\' field in manifest.json file.'
90       sys.exit(1)
91     ret_dict['app_name'] = self.data_src['name']
92     if not self.data_src.has_key('version'):
93       print 'Error: no \'version\' field in manifest.json file.'
94       sys.exit(1)
95     ret_dict['version'] = self.data_src['version']
96     if self.data_src.has_key('launch_path'):
97       app_url = self.data_src['launch_path']
98     elif (self.data_src.has_key('app') and
99         self.data_src['app'].has_key('launch') and
100             self.data_src['app']['launch'].has_key('local_path')):
101       app_url = self.data_src['app']['launch']['local_path']
102     else:
103       app_url = ''
104     if app_url.lower().startswith(('http', 'https')):
105       app_local_path = ''
106     else:
107       app_local_path = app_url
108       app_url = ''
109     file_path_prefix = os.path.split(self.input_path)[0]
110     if self.data_src.has_key('icons'):
111       ret_dict['icons'] = self.data_src['icons']
112     else:
113       ret_dict['icons'] = ''
114     app_root = file_path_prefix
115     ret_dict['description'] = ''
116     if self.data_src.has_key('description'):
117       ret_dict['description'] = self.data_src['description']
118     ret_dict['app_url'] = app_url
119     ret_dict['app_root'] = app_root
120     ret_dict['app_local_path'] = app_local_path
121     ret_dict['permissions'] = ''
122     if self.data_src.has_key('permissions'):
123       try:
124         permission_list = self.data_src['permissions']
125         ret_dict['permissions'] = HandlePermissionList(permission_list)
126       except (TypeError, ValueError, IOError):
127         print '\'Permissions\' field error in manifest.json file.'
128         sys.exit(1)
129     ret_dict['required_version'] = ''
130     if self.data_src.has_key('required_version'):
131       ret_dict['required_version'] = self.data_src['required_version']
132     ret_dict['plugin'] = ''
133     if self.data_src.has_key('plugin'):
134       ret_dict['plugin'] = self.data_src['plugin']
135     if self.data_src.has_key('fullscreen'):
136       ret_dict['fullscreen'] = self.data_src['fullscreen']
137     else:
138       ret_dict['fullscreen'] = 'False'
139     return ret_dict
140
141   def ShowItems(self):
142     """Show the processed results, it is used for command-line
143     internal debugging."""
144     print "app_name: %s" % self.GetAppName()
145     print "version: %s" % self.GetVersion()
146     print "description: %s" % self.GetDescription()
147     print "icons: %s" % self.GetIcons()
148     print "app_url: %s" % self.GetAppUrl()
149     print "app_root: %s" % self.GetAppRoot()
150     print "app_local_path: %s" % self.GetAppLocalPath()
151     print "permissions: %s" % self.GetPermissions()
152     print "required_version: %s" % self.GetRequiredVersion()
153     print "plugins: %s" % self.GetPlugins()
154     print "fullscreen: %s" % self.GetFullScreenFlag()
155
156   def GetAppName(self):
157     """Return the application name."""
158     return self.ret_dict['app_name']
159
160   def GetVersion(self):
161     """Return the version number."""
162     return self.ret_dict['version']
163
164   def GetIcons(self):
165     """Return the icons."""
166     return self.ret_dict['icons']
167
168   def GetAppUrl(self):
169     """Return the URL of the application."""
170     return self.ret_dict['app_url']
171
172   def GetDescription(self):
173     """Return the description of the application."""
174     return self.ret_dict['description']
175
176   def GetAppRoot(self):
177     """Return the root path of the local web application."""
178     return self.ret_dict['app_root']
179
180   def GetAppLocalPath(self):
181     """Return the local relative path of the local web application."""
182     return self.ret_dict['app_local_path']
183
184   def GetPermissions(self):
185     """Return the permissions."""
186     return self.ret_dict['permissions']
187
188   def GetRequiredVersion(self):
189     """Return the required crosswalk runtime version."""
190     return self.ret_dict['required_version']
191
192   def GetPlugins(self):
193     """Return the plug-in path and file name."""
194     return self.ret_dict['plugin']
195
196   def GetFullScreenFlag(self):
197     """Return the set fullscreen flag of the application."""
198     return self.ret_dict['fullscreen']
199
200
201 def main(argv):
202   """Respond to command mode and show the processed field values."""
203   parser = optparse.OptionParser()
204   info = ('The input json-format file name. Such as: '
205           '--jsonfile=manifest.json')
206   parser.add_option('-j', '--jsonfile', action='store', dest='jsonfile',
207                     help=info)
208   opts, _ = parser.parse_args()
209   if len(argv) == 1:
210     parser.print_help()
211     return 0
212   json_parser = ManifestJsonParser(opts.jsonfile)
213   json_parser.ShowItems()
214   return 0
215
216
217 if __name__ == '__main__':
218   sys.exit(main(sys.argv))