OLD | NEW |
---|---|
(Empty) | |
1 # Copyright 2015 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 os | |
7 import re | |
msw
2015/03/04 21:04:32
nit: not needed
| |
8 import subprocess | |
9 import sys | |
msw
2015/03/04 21:04:32
nit: not needed
| |
10 | |
11 _logging = logging.getLogger() | |
12 | |
13 from mopy import android | |
14 from mopy.config import Config | |
15 from mopy.paths import Paths | |
16 from mopy.print_process_error import print_process_error | |
17 | |
18 | |
19 def build_shell_arguments(shell_args, apps_and_args=None): | |
20 """Build the list of arguments for the shell. |shell_args| are the base | |
21 arguments, |apps_and_args| is a dictionary that associates each application to | |
22 its specific arguments|. Each app included will be run by the shell. | |
23 """ | |
24 result = shell_args[:] | |
25 if apps_and_args: | |
26 for (application, args) in apps_and_args.items(): | |
27 result += ["--args-for=%s %s" % (application, " ".join(args))] | |
28 result += apps_and_args.keys() | |
29 return result | |
30 | |
31 | |
32 def get_shell_executable(config, run_launcher): | |
33 paths = Paths(config=config) | |
34 if config.target_os == Config.OS_ANDROID: | |
35 return os.path.join(paths.src_root, "mojo", "tools", | |
36 "android_mojo_shell.py") | |
37 elif run_launcher: | |
38 return paths.mojo_launcher_path | |
39 else: | |
40 return paths.mojo_shell_path | |
41 | |
42 | |
43 def build_command_line(config, shell_args, apps_and_args, run_launcher=False): | |
44 executable = get_shell_executable(config, run_launcher) | |
45 return "%s %s" % (executable, " ".join(["%r" % x for x in | |
46 build_shell_arguments( | |
47 shell_args, apps_and_args)])) | |
48 | |
49 | |
50 def run_test_android(shell_args, apps_and_args): | |
51 """Run the given test on the single/default android device.""" | |
52 (r, w) = os.pipe() | |
53 with os.fdopen(r, "r") as rf: | |
54 with os.fdopen(w, "w") as wf: | |
55 arguments = build_shell_arguments(shell_args, apps_and_args) | |
56 android.StartShell(arguments, wf, wf.close) | |
57 return rf.read() | |
58 | |
59 | |
60 def run_test(config, shell_args, apps_and_args, run_launcher=False): | |
61 """Run the given test, using mojo_launcher if |run_launcher| is True.""" | |
62 if (config.target_os == Config.OS_ANDROID): | |
63 return run_test_android(shell_args, apps_and_args) | |
64 else: | |
65 executable = get_shell_executable(config, run_launcher) | |
66 command = ([executable] + build_shell_arguments(shell_args, apps_and_args)) | |
67 return subprocess.check_output(command, stderr=subprocess.STDOUT) | |
68 | |
69 | |
70 def try_run_test(config, shell_args, apps_and_args, run_launcher): | |
71 """Returns the output of a command line or an empty string on error.""" | |
72 command_line = build_command_line(config, shell_args, apps_and_args, | |
73 run_launcher=run_launcher) | |
74 _logging.debug("Running command line: %s" % command_line) | |
75 try: | |
76 return run_test(config, shell_args, apps_and_args, run_launcher) | |
77 except Exception as e: | |
78 print_process_error(command_line, e) | |
79 return None | |
OLD | NEW |