Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(436)

Side by Side Diff: build/android/pylib/utils/apk_helper.py

Issue 1126543009: [Android] Refactor the native test wrappers. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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):
perezju 2015/05/05 16:15:01 maybe add TODO notes to deprecate these? Or we exp
jbudorick 2015/05/06 18:00:00 I definitely expect to keep using this one. Not su
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 def GetInstrumentationName(apk_path):
26 m = package_name_re.match(line) 26 """Returns the name of the Instrumentation in the apk."""
27 if m: 27 return ApkHelper(apk_path).GetInstrumentationName()
28 return m.group(1)
29 raise Exception('Failed to determine package name of %s' % apk_path)
30 28
31 29
32 def _ParseManifestFromApk(apk_path): 30 def _ParseManifestFromApk(apk_path):
33 aapt_cmd = [_AAPT_PATH, 'dump', 'xmltree', apk_path, 'AndroidManifest.xml'] 31 aapt_cmd = [_AAPT_PATH, 'dump', 'xmltree', apk_path, 'AndroidManifest.xml']
34 aapt_output = cmd_helper.GetCmdOutput(aapt_cmd).split('\n') 32 aapt_output = cmd_helper.GetCmdOutput(aapt_cmd).split('\n')
35 33
36 parsed_manifest = {} 34 parsed_manifest = {}
37 node_stack = [parsed_manifest] 35 node_stack = [parsed_manifest]
38 indent = ' ' 36 indent = ' '
39 37
(...skipping 18 matching lines...) Expand all
58 m = _MANIFEST_ATTRIBUTE_RE.match(line[len(indent) * indent_depth:]) 56 m = _MANIFEST_ATTRIBUTE_RE.match(line[len(indent) * indent_depth:])
59 if m: 57 if m:
60 if not m.group(1) in node: 58 if not m.group(1) in node:
61 node[m.group(1)] = [] 59 node[m.group(1)] = []
62 node[m.group(1)].append(m.group(2)) 60 node[m.group(1)].append(m.group(2))
63 continue 61 continue
64 62
65 return parsed_manifest 63 return parsed_manifest
66 64
67 65
68 def GetInstrumentationName( 66 class ApkHelper(object):
69 apk_path, default='android.test.InstrumentationTestRunner'): 67 def __init__(self, apk_path):
70 """Returns the name of the Instrumentation in the apk.""" 68 self._apk_path = apk_path
69 self._manifest = None
70 self._package_name = None
71 71
72 try: 72 def GetActivityName(self):
73 manifest_info = _ParseManifestFromApk(apk_path) 73 """Returns the name of the Activity in the apk."""
74 return manifest_info['manifest']['instrumentation']['android:name'][0] 74 try:
75 except KeyError: 75 manifest_info = self._GetManifest()
76 return default 76 activity = (
77 manifest_info['manifest']['application']['activity']
78 ['android:name'][0])
perezju 2015/05/05 16:15:01 I guess only this line should be in the try-except
jbudorick 2015/05/06 18:00:00 Done.
79 if '.' not in activity:
80 activity = '%s.%s' % (self.GetPackageName(), activity)
81 elif activity.startswith('.'):
82 activity = '%s%s' % (self.GetPackageName(), activity)
83 return activity
84 except KeyError:
85 return None
86
87 def GetInstrumentationName(
88 self, default='android.test.InstrumentationTestRunner'):
89 """Returns the name of the Instrumentation in the apk."""
90 try:
91 manifest_info = self._GetManifest()
92 return manifest_info['manifest']['instrumentation']['android:name'][0]
perezju 2015/05/05 16:15:01 same here, only this line in the try-except block
jbudorick 2015/05/06 18:00:00 Done.
93 except KeyError:
94 return default
95
96 def GetPackageName(self):
97 """Returns the package name of the apk."""
98 if self._package_name:
99 return self._package_name
100
101 aapt_cmd = [_AAPT_PATH, 'dump', 'badging', self._apk_path]
102 aapt_output = cmd_helper.GetCmdOutput(aapt_cmd).split('\n')
103 package_name_re = re.compile(r'package: .*name=\'(\S*)\'')
104 for line in aapt_output:
105 m = package_name_re.match(line)
106 if m:
107 self._package_name = m.group(1)
108 return self._package_name
109 raise Exception('Failed to determine package name of %s' % self._apk_path)
110
111 def _GetManifest(self):
112 if not self._manifest:
113 self._manifest = _ParseManifestFromApk(self._apk_path)
114 return self._manifest
115
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698