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 |
| 25 | import xml.dom.minidom |
| 26 | import xml.parsers |
| 27 | |
| 28 | |
| 29 | class AndroidManifest(object): |
| 30 | """In memory representation of AndroidManifest.xml file.""" |
| 31 | |
| 32 | FILENAME = "AndroidManifest.xml" |
| 33 | |
| 34 | def __init__(self, app_path=None): |
| 35 | if app_path: |
| 36 | self.ParseManifest(app_path) |
| 37 | |
| 38 | def GetPackageName(self): |
| 39 | """Retrieve package name defined at <manifest package="...">. |
| 40 | |
| 41 | Returns: |
| 42 | Package name if defined, otherwise None |
| 43 | """ |
| 44 | manifests = self._dom.getElementsByTagName("manifest") |
| 45 | if not manifests or not manifests[0].getAttribute("package"): |
| 46 | return None |
| 47 | return manifests[0].getAttribute("package") |
| 48 | |
| 49 | def ParseManifest(self, app_path): |
| 50 | """Parse AndroidManifest.xml at the specified path. |
| 51 | |
| 52 | Args: |
| 53 | app_path: path to folder containing AndroidManifest.xml |
| 54 | Raises: |
| 55 | IOError: AndroidManifest.xml cannot be found at given path, or cannot be |
| 56 | opened for reading |
| 57 | """ |
| 58 | self.app_path = app_path.rstrip("/") |
| 59 | self.manifest_path = "%s/%s" % (self.app_path, self.FILENAME) |
| 60 | self._dom = xml.dom.minidom.parse(self.manifest_path) |