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