| OLD | NEW |
| 1 # Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 """Module containing utilities for apk packages.""" | 5 """Module containing utilities for apk packages.""" |
| 6 | 6 |
| 7 import os.path | 7 import os.path |
| 8 import re | 8 import re |
| 9 | 9 |
| 10 from pylib import cmd_helper | 10 from pylib import cmd_helper |
| 11 from pylib import constants | 11 from pylib import constants |
| 12 from pylib.sdk import aapt |
| 12 | 13 |
| 13 | 14 |
| 14 _AAPT_PATH = os.path.join(constants.ANDROID_SDK_TOOLS, 'aapt') | 15 _AAPT_PATH = os.path.join(constants.ANDROID_SDK_TOOLS, 'aapt') |
| 15 _MANIFEST_ATTRIBUTE_RE = re.compile( | 16 _MANIFEST_ATTRIBUTE_RE = re.compile( |
| 16 r'\s*A: ([^\(\)= ]*)\([^\(\)= ]*\)="(.*)" \(Raw: .*\)$') | 17 r'\s*A: ([^\(\)= ]*)\([^\(\)= ]*\)="(.*)" \(Raw: .*\)$') |
| 17 _MANIFEST_ELEMENT_RE = re.compile(r'\s*(?:E|N): (\S*) .*$') | 18 _MANIFEST_ELEMENT_RE = re.compile(r'\s*(?:E|N): (\S*) .*$') |
| 19 _PACKAGE_NAME_RE = re.compile(r'package: .*name=\'(\S*)\'') |
| 20 _SPLIT_NAME_RE = re.compile(r'package: .*split=\'(\S*)\'') |
| 18 | 21 |
| 19 | 22 |
| 20 def GetPackageName(apk_path): | 23 def GetPackageName(apk_path): |
| 21 """Returns the package name of the apk.""" | 24 """Returns the package name of the apk.""" |
| 22 return ApkHelper(apk_path).GetPackageName() | 25 return ApkHelper(apk_path).GetPackageName() |
| 23 | 26 |
| 24 | 27 |
| 25 # TODO(jbudorick): Deprecate and remove this function once callers have been | 28 # TODO(jbudorick): Deprecate and remove this function once callers have been |
| 26 # converted to ApkHelper.GetInstrumentationName | 29 # converted to ApkHelper.GetInstrumentationName |
| 27 def GetInstrumentationName(apk_path): | 30 def GetInstrumentationName(apk_path): |
| 28 """Returns the name of the Instrumentation in the apk.""" | 31 """Returns the name of the Instrumentation in the apk.""" |
| 29 return ApkHelper(apk_path).GetInstrumentationName() | 32 return ApkHelper(apk_path).GetInstrumentationName() |
| 30 | 33 |
| 31 | 34 |
| 32 def _ParseManifestFromApk(apk_path): | 35 def _ParseManifestFromApk(apk_path): |
| 33 aapt_cmd = [_AAPT_PATH, 'dump', 'xmltree', apk_path, 'AndroidManifest.xml'] | 36 aapt_output = aapt.Dump('xmltree', apk_path, 'AndroidManifest.xml') |
| 34 aapt_output = cmd_helper.GetCmdOutput(aapt_cmd).split('\n') | |
| 35 | 37 |
| 36 parsed_manifest = {} | 38 parsed_manifest = {} |
| 37 node_stack = [parsed_manifest] | 39 node_stack = [parsed_manifest] |
| 38 indent = ' ' | 40 indent = ' ' |
| 39 | 41 |
| 40 for line in aapt_output[1:]: | 42 for line in aapt_output[1:]: |
| 41 if len(line) == 0: | 43 if len(line) == 0: |
| 42 continue | 44 continue |
| 43 | 45 |
| 44 indent_depth = 0 | 46 indent_depth = 0 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 63 continue | 65 continue |
| 64 | 66 |
| 65 return parsed_manifest | 67 return parsed_manifest |
| 66 | 68 |
| 67 | 69 |
| 68 class ApkHelper(object): | 70 class ApkHelper(object): |
| 69 def __init__(self, apk_path): | 71 def __init__(self, apk_path): |
| 70 self._apk_path = apk_path | 72 self._apk_path = apk_path |
| 71 self._manifest = None | 73 self._manifest = None |
| 72 self._package_name = None | 74 self._package_name = None |
| 75 self._split_name = None |
| 73 | 76 |
| 74 def GetActivityName(self): | 77 def GetActivityName(self): |
| 75 """Returns the name of the Activity in the apk.""" | 78 """Returns the name of the Activity in the apk.""" |
| 76 manifest_info = self._GetManifest() | 79 manifest_info = self._GetManifest() |
| 77 try: | 80 try: |
| 78 activity = ( | 81 activity = ( |
| 79 manifest_info['manifest']['application']['activity'] | 82 manifest_info['manifest']['application']['activity'] |
| 80 ['android:name'][0]) | 83 ['android:name'][0]) |
| 81 except KeyError: | 84 except KeyError: |
| 82 return None | 85 return None |
| (...skipping 10 matching lines...) Expand all Loading... |
| 93 try: | 96 try: |
| 94 return manifest_info['manifest']['instrumentation']['android:name'][0] | 97 return manifest_info['manifest']['instrumentation']['android:name'][0] |
| 95 except KeyError: | 98 except KeyError: |
| 96 return default | 99 return default |
| 97 | 100 |
| 98 def GetPackageName(self): | 101 def GetPackageName(self): |
| 99 """Returns the package name of the apk.""" | 102 """Returns the package name of the apk.""" |
| 100 if self._package_name: | 103 if self._package_name: |
| 101 return self._package_name | 104 return self._package_name |
| 102 | 105 |
| 103 aapt_cmd = [_AAPT_PATH, 'dump', 'badging', self._apk_path] | 106 aapt_output = aapt.Dump('badging', self._apk_path) |
| 104 aapt_output = cmd_helper.GetCmdOutput(aapt_cmd).split('\n') | |
| 105 package_name_re = re.compile(r'package: .*name=\'(\S*)\'') | |
| 106 for line in aapt_output: | 107 for line in aapt_output: |
| 107 m = package_name_re.match(line) | 108 m = _PACKAGE_NAME_RE.match(line) |
| 108 if m: | 109 if m: |
| 109 self._package_name = m.group(1) | 110 self._package_name = m.group(1) |
| 110 return self._package_name | 111 return self._package_name |
| 111 raise Exception('Failed to determine package name of %s' % self._apk_path) | 112 raise Exception('Failed to determine package name of %s' % self._apk_path) |
| 112 | 113 |
| 114 def GetSplitName(self): |
| 115 """Returns the name of the split of the apk.""" |
| 116 if self._split_name: |
| 117 return self._split_name |
| 118 |
| 119 aapt_output = aapt.Dump('badging', self._apk_path) |
| 120 for line in aapt_output: |
| 121 m = _SPLIT_NAME_RE.match(line) |
| 122 if m: |
| 123 self._split_name = m.group(1) |
| 124 return self._split_name |
| 125 return None |
| 126 |
| 113 def _GetManifest(self): | 127 def _GetManifest(self): |
| 114 if not self._manifest: | 128 if not self._manifest: |
| 115 self._manifest = _ParseManifestFromApk(self._apk_path) | 129 self._manifest = _ParseManifestFromApk(self._apk_path) |
| 116 return self._manifest | 130 return self._manifest |
| 117 | 131 |
| OLD | NEW |