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