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

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

Issue 1138993009: Revert of [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):
21 """Returns the package name of the apk.""" 21 """Returns the package name of the apk."""
22 return ApkHelper(apk_path).GetPackageName() 22 aapt_cmd = [_AAPT_PATH, 'dump', 'badging', apk_path]
23 23 aapt_output = cmd_helper.GetCmdOutput(aapt_cmd).split('\n')
24 24 package_name_re = re.compile(r'package: .*name=\'(\S*)\'')
25 # TODO(jbudorick): Deprecate and remove this function once callers have been 25 for line in aapt_output:
26 # converted to ApkHelper.GetInstrumentationName 26 m = package_name_re.match(line)
27 def GetInstrumentationName(apk_path): 27 if m:
28 """Returns the name of the Instrumentation in the apk.""" 28 return m.group(1)
29 return ApkHelper(apk_path).GetInstrumentationName() 29 raise Exception('Failed to determine package name of %s' % apk_path)
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
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 class ApkHelper(object): 68 def GetInstrumentationName(
69 def __init__(self, apk_path): 69 apk_path, default='android.test.InstrumentationTestRunner'):
70 self._apk_path = apk_path 70 """Returns the name of the Instrumentation in the apk."""
71 self._manifest = None
72 self._package_name = None
73 71
74 def GetActivityName(self): 72 try:
75 """Returns the name of the Activity in the apk.""" 73 manifest_info = _ParseManifestFromApk(apk_path)
76 manifest_info = self._GetManifest() 74 return manifest_info['manifest']['instrumentation']['android:name'][0]
77 try: 75 except KeyError:
78 activity = ( 76 return default
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
OLDNEW
« no previous file with comments | « build/android/pylib/instrumentation/instrumentation_test_instance.py ('k') | build/apk_browsertest.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698