4
4
See help text for details.
5
5
"""
6
6
7
- import sys
8
- import subprocess
9
7
import re
8
+ import subprocess
9
+ import sys
10
10
11
11
save_dir_path = sys .path .pop (0 )
12
12
13
- modules = ['gen_arg' , 'gen_print' , 'gen_valid' , 'gen_misc' , 'gen_cmd' , 'var_funcs' ]
13
+ modules = [
14
+ "gen_arg" ,
15
+ "gen_print" ,
16
+ "gen_valid" ,
17
+ "gen_misc" ,
18
+ "gen_cmd" ,
19
+ "var_funcs" ,
20
+ ]
14
21
for module in modules :
15
22
exec ("from " + module + " import *" )
16
23
17
24
sys .path .insert (0 , save_dir_path )
18
25
19
26
parser = argparse .ArgumentParser (
20
- usage = ' %(prog)s [OPTIONS]' ,
27
+ usage = " %(prog)s [OPTIONS]" ,
21
28
description = "%(prog)s will create a status file path name adhering to the"
22
- + " following pattern: <status dir path>/<prefix>.yymmdd."
23
- + "hhmmss.status. It will then run the command string and"
24
- + " direct its stdout/stderr to the status file and optionally"
25
- + " to stdout. This dual output streaming will be"
26
- + " accomplished using either the \ " script\ " or the \ " tee\" "
27
- + " program. %(prog)s will also set and export environment"
28
- + " variable \ " AUTO_STATUS_FILE_PATH\ " for the benefit of"
29
- + " child programs." ,
29
+ + " following pattern: <status dir path>/<prefix>.yymmdd."
30
+ + "hhmmss.status. It will then run the command string and"
31
+ + " direct its stdout/stderr to the status file and optionally"
32
+ + " to stdout. This dual output streaming will be"
33
+ + ' accomplished using either the "script" or the "tee"'
34
+ + " program. %(prog)s will also set and export environment"
35
+ + ' variable "AUTO_STATUS_FILE_PATH" for the benefit of'
36
+ + " child programs." ,
30
37
formatter_class = argparse .ArgumentDefaultsHelpFormatter ,
31
- prefix_chars = '-+' )
38
+ prefix_chars = "-+" ,
39
+ )
32
40
33
41
parser .add_argument (
34
- ' --status_dir_path' ,
35
- default = '' ,
42
+ " --status_dir_path" ,
43
+ default = "" ,
36
44
help = "The path to the directory where the status file will be created."
37
- + "%(default)s The default value is obtained from environment"
38
- + " variable \" ${STATUS_DIR_PATH}\" , if set or from \" ${HOME}/"
39
- + "status/\" ." )
45
+ + "%(default)s The default value is obtained from environment"
46
+ + ' variable "${STATUS_DIR_PATH}", if set or from "${HOME}/'
47
+ + 'status/".' ,
48
+ )
40
49
41
50
parser .add_argument (
42
- ' --prefix' ,
43
- default = '' ,
51
+ " --prefix" ,
52
+ default = "" ,
44
53
help = "The prefix for the generated file name.%(default)s The default value"
45
- + " is the command portion (i.e. the first token) of the command"
46
- + " string." )
54
+ + " is the command portion (i.e. the first token) of the command"
55
+ + " string." ,
56
+ )
47
57
48
58
parser .add_argument (
49
- ' --status_file_name' ,
50
- default = '' ,
59
+ " --status_file_name" ,
60
+ default = "" ,
51
61
help = "This allows the user to explicitly specify the status file name. If"
52
- + " this argument is not used, %(prog)s composes a status file name."
53
- + " If this argument is specified, the \" --prefix\" argument is"
54
- + " ignored." )
62
+ + " this argument is not used, %(prog)s composes a status file name."
63
+ + ' If this argument is specified, the "--prefix" argument is'
64
+ + " ignored." ,
65
+ )
55
66
56
67
parser .add_argument (
57
- ' --stdout' ,
68
+ " --stdout" ,
58
69
default = 1 ,
59
70
type = int ,
60
71
choices = [1 , 0 ],
61
72
help = "Indicates that stdout/stderr from the command string execution"
62
- + " should be written to stdout as well as to the status file." )
73
+ + " should be written to stdout as well as to the status file." ,
74
+ )
63
75
64
76
parser .add_argument (
65
- ' --tee' ,
77
+ " --tee" ,
66
78
default = 1 ,
67
79
type = int ,
68
80
choices = [1 , 0 ],
69
- help = "Indicates that \" tee\" rather than \" script\" should be used." )
81
+ help = 'Indicates that "tee" rather than "script" should be used.' ,
82
+ )
70
83
71
84
parser .add_argument (
72
- ' --show_url' ,
85
+ " --show_url" ,
73
86
default = 0 ,
74
87
type = int ,
75
88
choices = [1 , 0 ],
76
89
help = "Indicates that the status file path shown should be shown in the"
77
- + " form of a url. If the output is to be viewed from a browser,"
78
- + " this may well become a clickable link. Note that the"
79
- + " get_file_path_url.py program must be found in the \" PATH\" "
80
- + " environment variable for this argument to be effective." )
90
+ + " form of a url. If the output is to be viewed from a browser,"
91
+ + " this may well become a clickable link. Note that the"
92
+ + ' get_file_path_url.py program must be found in the "PATH"'
93
+ + " environment variable for this argument to be effective." ,
94
+ )
81
95
82
96
parser .add_argument (
83
- 'command_string' ,
84
- default = '' ,
85
- nargs = '*' ,
86
- help = "The command string to be run.%(default)s" )
97
+ "command_string" ,
98
+ default = "" ,
99
+ nargs = "*" ,
100
+ help = "The command string to be run.%(default)s" ,
101
+ )
87
102
88
103
# Populate stock_list with options we want.
89
104
stock_list = [("test_mode" , 0 ), ("quiet" , 1 ), ("debug" , 0 )]
@@ -103,9 +118,9 @@ def validate_parms():
103
118
valid_value (command_string )
104
119
105
120
if status_dir_path == "" :
106
- status_dir_path = \
107
- os .environ .get ("STATUS_DIR_PATH" ,
108
- os . environ . get ( "HOME" ) + "/status/" )
121
+ status_dir_path = os . environ . get (
122
+ "STATUS_DIR_PATH" , os .environ .get ("HOME" ) + "/status/"
123
+ )
109
124
status_dir_path = add_trailing_slash (status_dir_path )
110
125
set_pgm_arg (status_dir_path )
111
126
valid_dir_path (status_dir_path )
@@ -126,9 +141,9 @@ def validate_parms():
126
141
127
142
status_file_path = status_dir_path + status_file_name
128
143
# Set environment variable for the benefit of child programs.
129
- os .environ [' AUTO_STATUS_FILE_PATH' ] = status_file_path
144
+ os .environ [" AUTO_STATUS_FILE_PATH" ] = status_file_path
130
145
# Set deprecated but still used AUTOSCRIPT_STATUS_FILE_PATH value.
131
- os .environ [' AUTOSCRIPT_STATUS_FILE_PATH' ] = status_file_path
146
+ os .environ [" AUTOSCRIPT_STATUS_FILE_PATH" ] = status_file_path
132
147
133
148
134
149
def script_func (command_string , status_file_path ):
@@ -141,9 +156,15 @@ def script_func(command_string, status_file_path):
141
156
status_file_path The path to the status file which is to contain a copy of all stdout.
142
157
"""
143
158
144
- cmd_buf = "script -a -q -f " + status_file_path + " -c '" \
145
- + escape_bash_quotes (command_string ) + " ; printf \" \\ n" \
146
- + sprint_varx (ret_code_str , "${?}" ).rstrip ("\n " ) + "\\ n\" '"
159
+ cmd_buf = (
160
+ "script -a -q -f "
161
+ + status_file_path
162
+ + " -c '"
163
+ + escape_bash_quotes (command_string )
164
+ + ' ; printf "\\ n'
165
+ + sprint_varx (ret_code_str , "${?}" ).rstrip ("\n " )
166
+ + "\\ n\" '"
167
+ )
147
168
qprint_issuing (cmd_buf )
148
169
sub_proc = subprocess .Popen (cmd_buf , shell = True )
149
170
sub_proc .communicate ()
@@ -152,8 +173,13 @@ def script_func(command_string, status_file_path):
152
173
# Retrieve return code by examining ret_code_str output statement from status file.
153
174
# Example text to be analyzed.
154
175
# auto_status_file_ret_code: 127
155
- cmd_buf = "tail -n 10 " + status_file_path + " | egrep -a \" " \
156
- + ret_code_str + ":[ ]+\" "
176
+ cmd_buf = (
177
+ "tail -n 10 "
178
+ + status_file_path
179
+ + ' | egrep -a "'
180
+ + ret_code_str
181
+ + ':[ ]+"'
182
+ )
157
183
rc , output = shell_cmd (cmd_buf )
158
184
key , value = parse_key_value (output )
159
185
shell_rc = int (value )
@@ -170,8 +196,12 @@ def tee_func(command_string, status_file_path):
170
196
status_file_path The path to the status file which is to contain a copy of all stdout.
171
197
"""
172
198
173
- cmd_buf = "set -o pipefail ; " + command_string + " 2>&1 | tee -a " \
199
+ cmd_buf = (
200
+ "set -o pipefail ; "
201
+ + command_string
202
+ + " 2>&1 | tee -a "
174
203
+ status_file_path
204
+ )
175
205
qprint_issuing (cmd_buf )
176
206
sub_proc = subprocess .Popen (cmd_buf , shell = True )
177
207
sub_proc .communicate ()
@@ -187,10 +217,11 @@ def tee_func(command_string, status_file_path):
187
217
188
218
189
219
def main ():
190
-
191
220
gen_setup ()
192
221
193
- set_term_options (term_requests = {'pgm_names' : [command_string .split (" " )[0 ]]})
222
+ set_term_options (
223
+ term_requests = {"pgm_names" : [command_string .split (" " )[0 ]]}
224
+ )
194
225
195
226
global ret_code_str
196
227
ret_code_str = re .sub ("\\ .py$" , "" , pgm_name ) + "_ret_code"
@@ -202,8 +233,9 @@ def main():
202
233
show_url = 0
203
234
set_pgm_arg (show_url )
204
235
else :
205
- shell_rc , status_file_url = shell_cmd ("get_file_path_url.py "
206
- + status_file_path )
236
+ shell_rc , status_file_url = shell_cmd (
237
+ "get_file_path_url.py " + status_file_path
238
+ )
207
239
status_file_url = status_file_url .rstrip ("\n " )
208
240
209
241
# Print status file path/url to stdout and to status file.
0 commit comments