Ejemplo n.º 1
0
  def ExecuteLegacyScript(cls, script_name, args, interpreter=None,
                          timeout=None, stdin=None, env=None,
                          add_python_paths=True):
    """Execute the given legacy script with the given args.

    Args:
      script_name: str, The script to run.
      args: [str], The arguments for the script.
      interpreter: str, An interpreter to use rather than trying to derive it
        from the extension name.
      timeout: int, The number of seconds to wait before killing the process.
      stdin: str, Optional input for the script.
      env: {str: str}, Optional environment variables for the script.
      add_python_paths: bool, Add PYTHONPATH=sys.path to env if True.

    Returns:
      An ExecutionResult object.
    """
    args = exec_utils.GetArgsForLegacyScript(
        script_name, args, interpreter=interpreter)
    if add_python_paths:
      env = cls._AddPythonPathsToEnv(env)
    # pylint: disable=protected-access
    runner = exec_utils._ProcessRunner(
        args, timeout=timeout, stdin=stdin, env=env)
    runner.Run()
    return runner.result
Ejemplo n.º 2
0
  def ExecuteLegacyScriptAsync(cls, script_name, args, interpreter=None,
                               match_strings=None, timeout=None, stdin=None,
                               env=None, add_python_paths=True):
    """Execute the given legacy script asynchronously in another thread.

    Same as ExecuteScript() except it does not wait for the process to return.
    Instead it returns a context manager that will kill the process once the
    scope exits.  If match_strings is given, the current thread will block until
    the process outputs lines of text that matches the strings in order, then
    the context manager will be returned and the process is kept alive.

    Args:
      script_name: str, The script to run.
      args: [str], The arguments for the script.
      interpreter: str, An interpreter to use rather than trying to derive it
        from the extension name.
      match_strings: [str], The output strings to match before returning.
      timeout: int, The number of seconds to wait before killing the process.
      stdin: str, Optional input for the script.
      env: {str: str}, Optional environment variables for the script.
      add_python_paths: bool, Add PYTHONPATH=sys.path to env if True.

    Returns:
      A context manager that will kill the process once the scope exists.
    """
    args = exec_utils.GetArgsForLegacyScript(
        script_name, args, interpreter=interpreter)
    if add_python_paths:
      env = cls._AddPythonPathsToEnv(env)
    # pylint: disable=protected-access
    runner = exec_utils._ProcessRunner(
        args, timeout=timeout, stdin=stdin, env=env)
    runner.RunAsync(match_strings=match_strings)
    # pylint: disable=protected-access
    return exec_utils._ProcessContext(runner)
Ejemplo n.º 3
0
  def ExecuteScript(cls, script_name, args, timeout=None, stdin=None,
                    env=None, add_python_paths=True):
    """Execute the given wrapper script with the given args.

    This wrapper must be a sh script on non-windows, or a .cmd file, without the
    '.cmd' extension (so it has the same script_name as non-windows), on
    windows.

    Args:
      script_name: str, The script to run.
      args: [str], The arguments for the script.
      timeout: int, The number of seconds to wait before killing the process.
      stdin: str, Optional input for the script.
      env: {str: str}, Optional environment variables for the script.
      add_python_paths: bool, Add PYTHONPATH=sys.path to env if True.

    Returns:
      An ExecutionResult object.
    """
    args = exec_utils.GetArgsForScript(script_name, args)
    if add_python_paths:
      env = cls._AddPythonPathsToEnv(env)
    # pylint: disable=protected-access
    runner = exec_utils._ProcessRunner(
        args, timeout=timeout, stdin=stdin, env=env)
    runner.Run()
    return runner.result