blob: 6e8bd6c71b5e62c331b5daa4c4c3f683744dd3a2 [file] [log] [blame]
Jack Wang2abd80a2009-06-29 18:47:03 -07001#!/usr/bin/python2.4
2#
3#
4# Copyright 2009, The Android Open Source Project
5#
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17
18"""In memory representation of AndroidManifest.xml file.
19
20Specification of AndroidManifest.xml can be found at
21http://developer.android.com/guide/topics/manifest/manifest-intro.html
22"""
23
24# python imports
Brett Chabot1d9928a2009-09-29 15:14:05 -070025import os
Jack Wang2abd80a2009-06-29 18:47:03 -070026import xml.dom.minidom
27import xml.parsers
28
29
30class AndroidManifest(object):
31 """In memory representation of AndroidManifest.xml file."""
32
Brett Chabot1d9928a2009-09-29 15:14:05 -070033 FILENAME = 'AndroidManifest.xml'
Jack Wang2abd80a2009-06-29 18:47:03 -070034
35 def __init__(self, app_path=None):
36 if app_path:
Brett Chabotecfcc7d2011-07-17 14:04:37 -070037 self._ParseManifest(app_path)
Jack Wang2abd80a2009-06-29 18:47:03 -070038
Brett Chabot34fb7f62009-10-21 11:16:42 -070039 def GetAppPath(self):
40 """Retrieve file system path to this manifest file's directory."""
41 return self._app_path
42
Jack Wang2abd80a2009-06-29 18:47:03 -070043 def GetPackageName(self):
44 """Retrieve package name defined at <manifest package="...">.
45
46 Returns:
47 Package name if defined, otherwise None
48 """
Brett Chabot1d9928a2009-09-29 15:14:05 -070049 manifest = self._GetManifestElement()
50 if not manifest or not manifest.hasAttribute('package'):
Jack Wang2abd80a2009-06-29 18:47:03 -070051 return None
Brett Chabot1d9928a2009-09-29 15:14:05 -070052 return manifest.getAttribute('package')
Jack Wang2abd80a2009-06-29 18:47:03 -070053
Brett Chabotecfcc7d2011-07-17 14:04:37 -070054 def _ParseManifest(self, app_path):
Jack Wang2abd80a2009-06-29 18:47:03 -070055 """Parse AndroidManifest.xml at the specified path.
56
57 Args:
58 app_path: path to folder containing AndroidManifest.xml
59 Raises:
60 IOError: AndroidManifest.xml cannot be found at given path, or cannot be
61 opened for reading
62 """
Brett Chabot34fb7f62009-10-21 11:16:42 -070063 self._app_path = app_path
Brett Chabot1d9928a2009-09-29 15:14:05 -070064 self._manifest_path = os.path.join(app_path, self.FILENAME)
65 self._dom = xml.dom.minidom.parse(self._manifest_path)
66
67 def AddUsesSdk(self, min_sdk_version):
68 """Adds a uses-sdk element to manifest.
69
70 Args:
71 min_sdk_version: value to provide for minSdkVersion attribute.
72 """
73 manifest = self._GetManifestElement()
74 uses_sdk_elements = manifest.getElementsByTagName('uses-sdk')
75 if uses_sdk_elements:
76 uses_sdk_element = uses_sdk_elements[0]
77 else:
78 uses_sdk_element = self._dom.createElement('uses-sdk')
79 manifest.appendChild(uses_sdk_element)
80
81 uses_sdk_element.setAttribute('android:minSdkVersion', min_sdk_version)
82 self._SaveXml()
83
Brett Chabot34fb7f62009-10-21 11:16:42 -070084 def GetInstrumentationNames(self):
85 """Get the instrumentation names from manifest.
86
87 Returns:
88 list of names, might be empty
89 """
90 instr_elements = self._dom.getElementsByTagName('instrumentation')
91 instrs = []
92 for element in instr_elements:
93 instrs.append(element.getAttribute('android:name'))
94 return instrs
95
Brett Chabot1d9928a2009-09-29 15:14:05 -070096 def _GetManifestElement(self):
97 """Retrieve the root manifest element.
98
99 Returns:
100 the DOM element for manifest or None.
101 """
102 manifests = self._dom.getElementsByTagName('manifest')
103 if not manifests:
104 return None
105 return manifests[0]
106
107 def _SaveXml(self):
108 """Saves the manifest to disk."""
109 self._dom.writexml(open(self._manifest_path, mode='w'), encoding='utf-8')
Brett Chabot34fb7f62009-10-21 11:16:42 -0700110
Brett Chabotecfcc7d2011-07-17 14:04:37 -0700111
112def CreateAndroidManifest(path):
113 """Factory method for creating a AndroidManifest.
114
115 Args:
116 path: the directory for the manifest file
117
118 Return:
119 the AndroidManifest or None if there was no file present
120 """
121 manifest_path = os.path.join(path, AndroidManifest.FILENAME)
122 if os.path.isfile(manifest_path):
123 manifest = AndroidManifest()
124 manifest._ParseManifest(path)
125 return manifest
126 else:
127 return None