blob: 5406f56eeaa4a9271deae376def82b3f609d5fe7 [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:
37 self.ParseManifest(app_path)
38
39 def GetPackageName(self):
40 """Retrieve package name defined at <manifest package="...">.
41
42 Returns:
43 Package name if defined, otherwise None
44 """
Brett Chabot1d9928a2009-09-29 15:14:05 -070045 manifest = self._GetManifestElement()
46 if not manifest or not manifest.hasAttribute('package'):
Jack Wang2abd80a2009-06-29 18:47:03 -070047 return None
Brett Chabot1d9928a2009-09-29 15:14:05 -070048 return manifest.getAttribute('package')
Jack Wang2abd80a2009-06-29 18:47:03 -070049
50 def ParseManifest(self, app_path):
51 """Parse AndroidManifest.xml at the specified path.
52
53 Args:
54 app_path: path to folder containing AndroidManifest.xml
55 Raises:
56 IOError: AndroidManifest.xml cannot be found at given path, or cannot be
57 opened for reading
58 """
Brett Chabot1d9928a2009-09-29 15:14:05 -070059 self._manifest_path = os.path.join(app_path, self.FILENAME)
60 self._dom = xml.dom.minidom.parse(self._manifest_path)
61
62 def AddUsesSdk(self, min_sdk_version):
63 """Adds a uses-sdk element to manifest.
64
65 Args:
66 min_sdk_version: value to provide for minSdkVersion attribute.
67 """
68 manifest = self._GetManifestElement()
69 uses_sdk_elements = manifest.getElementsByTagName('uses-sdk')
70 if uses_sdk_elements:
71 uses_sdk_element = uses_sdk_elements[0]
72 else:
73 uses_sdk_element = self._dom.createElement('uses-sdk')
74 manifest.appendChild(uses_sdk_element)
75
76 uses_sdk_element.setAttribute('android:minSdkVersion', min_sdk_version)
77 self._SaveXml()
78
79 def _GetManifestElement(self):
80 """Retrieve the root manifest element.
81
82 Returns:
83 the DOM element for manifest or None.
84 """
85 manifests = self._dom.getElementsByTagName('manifest')
86 if not manifests:
87 return None
88 return manifests[0]
89
90 def _SaveXml(self):
91 """Saves the manifest to disk."""
92 self._dom.writexml(open(self._manifest_path, mode='w'), encoding='utf-8')