Jack Wang | 2abd80a | 2009-06-29 18:47:03 -0700 | [diff] [blame] | 1 | #!/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 | |
| 20 | Specification of AndroidManifest.xml can be found at |
| 21 | http://developer.android.com/guide/topics/manifest/manifest-intro.html |
| 22 | """ |
| 23 | |
| 24 | # python imports |
Brett Chabot | 1d9928a | 2009-09-29 15:14:05 -0700 | [diff] [blame^] | 25 | import os |
Jack Wang | 2abd80a | 2009-06-29 18:47:03 -0700 | [diff] [blame] | 26 | import xml.dom.minidom |
| 27 | import xml.parsers |
| 28 | |
| 29 | |
| 30 | class AndroidManifest(object): |
| 31 | """In memory representation of AndroidManifest.xml file.""" |
| 32 | |
Brett Chabot | 1d9928a | 2009-09-29 15:14:05 -0700 | [diff] [blame^] | 33 | FILENAME = 'AndroidManifest.xml' |
Jack Wang | 2abd80a | 2009-06-29 18:47:03 -0700 | [diff] [blame] | 34 | |
| 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 Chabot | 1d9928a | 2009-09-29 15:14:05 -0700 | [diff] [blame^] | 45 | manifest = self._GetManifestElement() |
| 46 | if not manifest or not manifest.hasAttribute('package'): |
Jack Wang | 2abd80a | 2009-06-29 18:47:03 -0700 | [diff] [blame] | 47 | return None |
Brett Chabot | 1d9928a | 2009-09-29 15:14:05 -0700 | [diff] [blame^] | 48 | return manifest.getAttribute('package') |
Jack Wang | 2abd80a | 2009-06-29 18:47:03 -0700 | [diff] [blame] | 49 | |
| 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 Chabot | 1d9928a | 2009-09-29 15:14:05 -0700 | [diff] [blame^] | 59 | 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') |