1#!/usr/bin/env python
2
3r"""
4This module provides command execution functions such as cmd_fnc and cmd_fnc_u.
5"""
6
7import sys
8import subprocess
9
10robot_env = 1
11try:
12    from robot.libraries.BuiltIn import BuiltIn
13except ImportError:
14    robot_env = 0
15import gen_print as gp
16import gen_valid as gv
17import gen_misc as gm
18if robot_env:
19    import gen_robot_print as grp
20
21
22###############################################################################
23def cmd_fnc(cmd_buf,
24            quiet=None,
25            test_mode=None,
26            debug=0,
27            print_output=1,
28            show_err=1):
29
30    r"""
31    Run the given command in a shell and return the shell return code.
32
33    Description of arguments:
34    cmd_buf                         The command string to be run in a shell.
35    quiet                           Indicates whether this function should run
36                                    the pissuing()
37                  function prints an "Issuing: <cmd string>" to stdout.
38    test_mode                       If test_mode is set, this function will
39                                    not actually run
40                  the command.
41    debug                           If debug is set, this function will print
42                                    extra debug info.
43    print_output                    If this is set, this function will print
44                                    the stdout/stderr
45                  generated by the shell command.
46    show_err                        If show_err is set, this function will
47                                    print a standardized
48                  error report if the shell command returns non-zero.
49    """
50
51    quiet = int(gm.global_default(quiet, 0))
52    test_mode = int(gm.global_default(test_mode, 0))
53
54    if debug:
55        gp.print_vars(cmd_buf, quiet, test_mode, debug)
56
57    err_msg = gv.svalid_value(cmd_buf)
58    if err_msg != "":
59        raise ValueError(err_msg)
60
61    if not quiet:
62        gp.pissuing(cmd_buf, test_mode)
63
64    if test_mode:
65        return 0, ""
66
67    sub_proc = subprocess.Popen(cmd_buf,
68                                bufsize=1,
69                                shell=True,
70                                stdout=subprocess.PIPE,
71                                stderr=subprocess.STDOUT)
72    out_buf = ""
73    for line in sub_proc.stdout:
74        out_buf += line
75        if not print_output:
76            continue
77        if robot_env:
78            grp.rprint(line)
79        else:
80            sys.stdout.write(line)
81    if print_output and not robot_env:
82        sys.stdout.flush()
83    sub_proc.communicate()
84    shell_rc = sub_proc.returncode
85    if shell_rc != 0 and show_err:
86        if robot_env:
87            grp.rprint_error_report("The prior command failed.\n" +
88                                    gp.sprint_var(shell_rc, 1))
89        else:
90            gp.print_error_report("The prior command failed.\n" +
91                                  gp.sprint_var(shell_rc, 1))
92
93    return shell_rc, out_buf
94
95###############################################################################
96
97
98###############################################################################
99def cmd_fnc_u(cmd_buf,
100              quiet=None,
101              debug=None,
102              print_output=1,
103              show_err=1):
104
105    r"""
106    Call cmd_fnc with test_mode=0.  See cmd_fnc (above) for details.
107
108    Note the "u" in "cmd_fnc_u" stands for "unconditional".
109    """
110
111    return cmd_fnc(cmd_buf, test_mode=0, quiet=quiet, debug=debug,
112                   print_output=print_output, show_err=show_err)
113
114###############################################################################
115