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

Side by Side Diff: tools/telemetry/telemetry/core/backends/android_command_line_backend.py

Issue 811703007: Create AndroidCommandLineBackend to handle setting up cmdline args (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: add context manager for command line backend Created 5 years, 11 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
(Empty)
1 # Copyright 2013 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
4
5 import logging
6 import pipes
7 import sys
8
9 from telemetry.core import util
10
11 util.AddDirToPythonPath(util.GetChromiumSrcDir(), 'build', 'android')
12 from pylib.device import device_errors # pylint: disable=F0401
13
14
15 def _QuoteIfNeeded(arg):
16 # Properly escape "key=valueA valueB" to "key='valueA valueB'"
17 # Values without spaces, or that seem to be quoted are left untouched.
18 # This is required so CommandLine.java can parse valueB correctly rather
19 # than as a separate switch.
20 params = arg.split('=', 1)
21 if len(params) != 2:
22 return arg
23 key, values = params
24 if ' ' not in values:
25 return arg
26 if values[0] in '"\'' and values[-1] == values[0]:
27 return arg
28 return '%s=%s' % (key, pipes.quote(values))
29
30
31 class SetUpCommandLineFlags(object):
32 """A context manager for setting up the android command line flags.
33
34 This provides a readable way of using the android command line backend class.
35 Example usage:
36
37 with android_command_line_backend.SetUpCommandLineFlags(
38 adb, backend_settings, startup_args):
39 # Something to run while the command line flags are set appropriately.
40 """
41 def __init__(self, adb, backend_settings, startup_args):
42 self._android_command_line_backend = _AndroidCommandLineBackend(
43 adb, backend_settings, startup_args)
44
45 def __enter__(self):
46 self._android_command_line_backend.SetUpCommandLineFlags()
47
48 def __exit__(self, *args):
49 self._android_command_line_backend.RestoreCommandLineFlags()
50
51
52 class _AndroidCommandLineBackend(object):
53 """The backend for providing command line flags on android.
54
55 There are command line flags that Chromium accept in order to enable
56 particular features or modify otherwise default functionality. To set the
57 flags for Chrome on Android, specific files on the device must be updated
58 with the flags to enable. This class provides a wrapper around this
59 functionality.
60 """
61
62 def __init__(self, adb, backend_settings, startup_args):
63 self._adb = adb
64 self._backend_settings = backend_settings
65 self._startup_args = startup_args
66 self._saved_command_line_file_contents = ''
67
68 @property
69 def command_line_file(self):
70 return self._backend_settings.GetCommandLineFile(self._adb.IsUserBuild())
71
72 def SetUpCommandLineFlags(self):
73 args = [self._backend_settings.pseudo_exec_name]
74 args.extend(self._startup_args)
75 content = ' '.join(_QuoteIfNeeded(arg) for arg in args)
76
77 try:
78 # Save the current command line to restore later, except if it appears to
79 # be a Telemetry created one. This is to prevent a common bug where
80 # --host-resolver-rules borks people's browsers if something goes wrong
81 # with Telemetry.
82 self._saved_command_line_file_contents = ''.join(self._ReadFile())
83 if '--host-resolver-rules' in self._saved_command_line_file_contents:
84 self._saved_command_line_file_contents = ''
85 self._WriteFile(content)
86 except device_errors.CommandFailedError:
87 logging.critical('Cannot set Chrome command line. '
88 'Fix this by flashing to a userdebug build.')
89 sys.exit(1)
90
91 def RestoreCommandLineFlags(self):
92 self._WriteFile(self._saved_command_line_file_contents)
93
94 def _ReadFile(self):
95 return self._adb.device().ReadFile(self.command_line_file)
96
97 def _WriteFile(self, contents):
98 self._adb.device().WriteFile(self.command_line_file, contents, as_root=True)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698