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 | 12 |
13 | 13 |
14 _AAPT_PATH = os.path.join(constants.ANDROID_SDK_TOOLS, 'aapt') | 14 _AAPT_PATH = os.path.join(constants.ANDROID_SDK_TOOLS, 'aapt') |
15 _MANIFEST_ATTRIBUTE_RE = re.compile( | 15 _MANIFEST_ATTRIBUTE_RE = re.compile( |
16 r'\s*A: ([^\(\)= ]*)\([^\(\)= ]*\)="(.*)" \(Raw: .*\)$') | 16 r'\s*A: ([^\(\)= ]*)\([^\(\)= ]*\)="(.*)" \(Raw: .*\)$') |
17 _MANIFEST_ELEMENT_RE = re.compile(r'\s*(?:E|N): (\S*) .*$') | 17 _MANIFEST_ELEMENT_RE = re.compile(r'\s*(?:E|N): (\S*) .*$') |
18 | 18 |
19 | 19 |
20 def GetPackageName(apk_path): | 20 def GetPackageName(apk_path): |
21 """Returns the package name of the apk.""" | 21 """Returns the package name of the apk.""" |
22 aapt_cmd = [_AAPT_PATH, 'dump', 'badging', apk_path] | 22 return ApkHelper(apk_path).GetPackageName() |
23 aapt_output = cmd_helper.GetCmdOutput(aapt_cmd).split('\n') | 23 |
24 package_name_re = re.compile(r'package: .*name=\'(\S*)\'') | 24 |
25 for line in aapt_output: | 25 # TODO(jbudorick): Deprecate and remove this function once callers have been |
26 m = package_name_re.match(line) | 26 # converted to ApkHelper.GetInstrumentationName |
27 if m: | 27 def GetInstrumentationName(apk_path): |
28 return m.group(1) | 28 """Returns the name of the Instrumentation in the apk.""" |
29 raise Exception('Failed to determine package name of %s' % apk_path) | 29 return ApkHelper(apk_path).GetInstrumentationName() |
30 | 30 |
31 | 31 |
32 def _ParseManifestFromApk(apk_path): | 32 def _ParseManifestFromApk(apk_path): |
33 aapt_cmd = [_AAPT_PATH, 'dump', 'xmltree', apk_path, 'AndroidManifest.xml'] | 33 aapt_cmd = [_AAPT_PATH, 'dump', 'xmltree', apk_path, 'AndroidManifest.xml'] |
34 aapt_output = cmd_helper.GetCmdOutput(aapt_cmd).split('\n') | 34 aapt_output = cmd_helper.GetCmdOutput(aapt_cmd).split('\n') |
35 | 35 |
36 parsed_manifest = {} | 36 parsed_manifest = {} |
37 node_stack = [parsed_manifest] | 37 node_stack = [parsed_manifest] |
38 indent = ' ' | 38 indent = ' ' |
39 | 39 |
(...skipping 18 matching lines...) Expand all Loading... |
58 m = _MANIFEST_ATTRIBUTE_RE.match(line[len(indent) * indent_depth:]) | 58 m = _MANIFEST_ATTRIBUTE_RE.match(line[len(indent) * indent_depth:]) |
59 if m: | 59 if m: |
60 if not m.group(1) in node: | 60 if not m.group(1) in node: |
61 node[m.group(1)] = [] | 61 node[m.group(1)] = [] |
62 node[m.group(1)].append(m.group(2)) | 62 node[m.group(1)].append(m.group(2)) |
63 continue | 63 continue |
64 | 64 |
65 return parsed_manifest | 65 return parsed_manifest |
66 | 66 |
67 | 67 |
68 def GetInstrumentationName( | 68 class ApkHelper(object): |
69 apk_path, default='android.test.InstrumentationTestRunner'): | 69 def __init__(self, apk_path): |
70 """Returns the name of the Instrumentation in the apk.""" | 70 self._apk_path = apk_path |
| 71 self._manifest = None |
| 72 self._package_name = None |
71 | 73 |
72 try: | 74 def GetActivityName(self): |
73 manifest_info = _ParseManifestFromApk(apk_path) | 75 """Returns the name of the Activity in the apk.""" |
74 return manifest_info['manifest']['instrumentation']['android:name'][0] | 76 manifest_info = self._GetManifest() |
75 except KeyError: | 77 try: |
76 return default | 78 activity = ( |
| 79 manifest_info['manifest']['application']['activity'] |
| 80 ['android:name'][0]) |
| 81 except KeyError: |
| 82 return None |
| 83 if '.' not in activity: |
| 84 activity = '%s.%s' % (self.GetPackageName(), activity) |
| 85 elif activity.startswith('.'): |
| 86 activity = '%s%s' % (self.GetPackageName(), activity) |
| 87 return activity |
| 88 |
| 89 def GetInstrumentationName( |
| 90 self, default='android.test.InstrumentationTestRunner'): |
| 91 """Returns the name of the Instrumentation in the apk.""" |
| 92 manifest_info = self._GetManifest() |
| 93 try: |
| 94 return manifest_info['manifest']['instrumentation']['android:name'][0] |
| 95 except KeyError: |
| 96 return default |
| 97 |
| 98 def GetPackageName(self): |
| 99 """Returns the package name of the apk.""" |
| 100 if self._package_name: |
| 101 return self._package_name |
| 102 |
| 103 aapt_cmd = [_AAPT_PATH, '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 m = package_name_re.match(line) |
| 108 if m: |
| 109 self._package_name = m.group(1) |
| 110 return self._package_name |
| 111 raise Exception('Failed to determine package name of %s' % self._apk_path) |
| 112 |
| 113 def _GetManifest(self): |
| 114 if not self._manifest: |
| 115 self._manifest = _ParseManifestFromApk(self._apk_path) |
| 116 return self._manifest |
| 117 |
OLD | NEW |