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: .*\)$') |
mikecase (-- gone --)
2015/06/26 18:59:33
This regex would previously only match attributes
| |
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 return ApkHelper(apk_path).GetPackageName() | 22 return ApkHelper(apk_path).GetPackageName() |
23 | 23 |
24 | 24 |
25 # TODO(jbudorick): Deprecate and remove this function once callers have been | 25 # TODO(jbudorick): Deprecate and remove this function once callers have been |
26 # converted to ApkHelper.GetInstrumentationName | 26 # converted to ApkHelper.GetInstrumentationName |
(...skipping 25 matching lines...) Expand all Loading... | |
52 if m: | 52 if m: |
53 if not m.group(1) in node: | 53 if not m.group(1) in node: |
54 node[m.group(1)] = {} | 54 node[m.group(1)] = {} |
55 node_stack += [node[m.group(1)]] | 55 node_stack += [node[m.group(1)]] |
56 continue | 56 continue |
57 | 57 |
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(3)) |
63 continue | 63 continue |
64 | 64 |
65 return parsed_manifest | 65 return parsed_manifest |
66 | 66 |
67 | 67 |
68 class ApkHelper(object): | 68 class ApkHelper(object): |
69 def __init__(self, apk_path): | 69 def __init__(self, apk_path): |
70 self._apk_path = apk_path | 70 self._apk_path = apk_path |
71 self._manifest = None | 71 self._manifest = None |
72 self._package_name = None | |
73 | 72 |
74 def GetActivityName(self): | 73 def GetActivityName(self): |
75 """Returns the name of the Activity in the apk.""" | 74 """Returns the name of the Activity in the apk.""" |
76 manifest_info = self._GetManifest() | 75 manifest_info = self._GetManifest() |
77 try: | 76 try: |
78 activity = ( | 77 activity = ( |
79 manifest_info['manifest']['application']['activity'] | 78 manifest_info['manifest']['application']['activity'] |
80 ['android:name'][0]) | 79 ['android:name'][0]) |
81 except KeyError: | 80 except KeyError: |
82 return None | 81 return None |
83 if '.' not in activity: | 82 if '.' not in activity: |
84 activity = '%s.%s' % (self.GetPackageName(), activity) | 83 activity = '%s.%s' % (self.GetPackageName(), activity) |
85 elif activity.startswith('.'): | 84 elif activity.startswith('.'): |
86 activity = '%s%s' % (self.GetPackageName(), activity) | 85 activity = '%s%s' % (self.GetPackageName(), activity) |
87 return activity | 86 return activity |
88 | 87 |
89 def GetInstrumentationName( | 88 def GetInstrumentationName( |
90 self, default='android.test.InstrumentationTestRunner'): | 89 self, default='android.test.InstrumentationTestRunner'): |
91 """Returns the name of the Instrumentation in the apk.""" | 90 """Returns the name of the Instrumentation in the apk.""" |
92 manifest_info = self._GetManifest() | 91 manifest_info = self._GetManifest() |
93 try: | 92 try: |
94 return manifest_info['manifest']['instrumentation']['android:name'][0] | 93 return manifest_info['manifest']['instrumentation']['android:name'][0] |
95 except KeyError: | 94 except KeyError: |
96 return default | 95 return default |
97 | 96 |
98 def GetPackageName(self): | 97 def GetPackageName(self): |
99 """Returns the package name of the apk.""" | 98 """Returns the package name of the apk.""" |
100 if self._package_name: | 99 manifest_info = self._GetManifest() |
jbudorick
2015/06/26 23:26:59
Why did we switch the implementation here? Does aa
mikecase (-- gone --)
2015/06/27 00:21:22
I changed this impl to be consistent with the rest
jbudorick
2015/06/29 15:49:01
This was intentional. Parsing the output of aapt d
mikecase (-- gone --)
2015/06/29 20:06:21
Changed GetPackageName back to old implementation.
| |
101 return self._package_name | 100 try: |
101 return manifest_info['manifest']['package'][0] | |
102 except KeyError: | |
103 raise Exception('Failed to determine package name of %s' % self._apk_path) | |
102 | 104 |
103 aapt_cmd = [_AAPT_PATH, 'dump', 'badging', self._apk_path] | 105 def GetSplitName(self): |
104 aapt_output = cmd_helper.GetCmdOutput(aapt_cmd).split('\n') | 106 """Returns the split attribute of the apk.""" |
105 package_name_re = re.compile(r'package: .*name=\'(\S*)\'') | 107 manifest_info = self._GetManifest() |
106 for line in aapt_output: | 108 try: |
107 m = package_name_re.match(line) | 109 return manifest_info['manifest']['split'][0] |
108 if m: | 110 except KeyError: |
109 self._package_name = m.group(1) | 111 return None |
110 return self._package_name | |
111 raise Exception('Failed to determine package name of %s' % self._apk_path) | |
112 | 112 |
113 def _GetManifest(self): | 113 def _GetManifest(self): |
114 if not self._manifest: | 114 if not self._manifest: |
115 self._manifest = _ParseManifestFromApk(self._apk_path) | 115 self._manifest = _ParseManifestFromApk(self._apk_path) |
116 return self._manifest | 116 return self._manifest |
117 | 117 |
OLD | NEW |