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: |
Brett Chabot | ecfcc7d | 2011-07-17 14:04:37 -0700 | [diff] [blame] | 37 | self._ParseManifest(app_path) |
Jack Wang | 2abd80a | 2009-06-29 18:47:03 -0700 | [diff] [blame] | 38 | |
Brett Chabot | 34fb7f6 | 2009-10-21 11:16:42 -0700 | [diff] [blame] | 39 | def GetAppPath(self): |
| 40 | """Retrieve file system path to this manifest file's directory.""" |
| 41 | return self._app_path |
| 42 | |
Jack Wang | 2abd80a | 2009-06-29 18:47:03 -0700 | [diff] [blame] | 43 | def GetPackageName(self): |
| 44 | """Retrieve package name defined at <manifest package="...">. |
| 45 | |
| 46 | Returns: |
| 47 | Package name if defined, otherwise None |
| 48 | """ |
Brett Chabot | 1d9928a | 2009-09-29 15:14:05 -0700 | [diff] [blame] | 49 | manifest = self._GetManifestElement() |
| 50 | if not manifest or not manifest.hasAttribute('package'): |
Jack Wang | 2abd80a | 2009-06-29 18:47:03 -0700 | [diff] [blame] | 51 | return None |
Brett Chabot | 1d9928a | 2009-09-29 15:14:05 -0700 | [diff] [blame] | 52 | return manifest.getAttribute('package') |
Jack Wang | 2abd80a | 2009-06-29 18:47:03 -0700 | [diff] [blame] | 53 | |
Brett Chabot | ecfcc7d | 2011-07-17 14:04:37 -0700 | [diff] [blame] | 54 | def _ParseManifest(self, app_path): |
Jack Wang | 2abd80a | 2009-06-29 18:47:03 -0700 | [diff] [blame] | 55 | """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 Chabot | 34fb7f6 | 2009-10-21 11:16:42 -0700 | [diff] [blame] | 63 | self._app_path = app_path |
Brett Chabot | 1d9928a | 2009-09-29 15:14:05 -0700 | [diff] [blame] | 64 | 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 Chabot | 34fb7f6 | 2009-10-21 11:16:42 -0700 | [diff] [blame] | 84 | 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 Chabot | 1d9928a | 2009-09-29 15:14:05 -0700 | [diff] [blame] | 96 | 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 Chabot | 34fb7f6 | 2009-10-21 11:16:42 -0700 | [diff] [blame] | 110 | |
Brett Chabot | ecfcc7d | 2011-07-17 14:04:37 -0700 | [diff] [blame] | 111 | |
| 112 | def 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 |