@@ -11,6 +11,7 @@ import subprocess
11
11
import sys
12
12
13
13
from shlex import quote
14
+ from sonic_py_common .general import getstatusoutput_noshell_pipe
14
15
15
16
regex_dict = {
16
17
'acl' : r'acl\|ACL\|Acl' ,
@@ -28,31 +29,35 @@ reference_file = '/tmp/storyteller_time_reference'
28
29
29
30
def exec_cmd (cmd ):
30
31
# Use universal_newlines (instead of text) so that this tool can work with any python versions.
31
- out = subprocess .Popen (cmd , stdout = subprocess .PIPE , stderr = subprocess .STDOUT , shell = True , universal_newlines = True )
32
+ out = subprocess .Popen (cmd , stdout = subprocess .PIPE , stderr = subprocess .STDOUT , universal_newlines = True )
32
33
stdout , stderr = out .communicate ()
33
34
return out .returncode , stdout , stderr
34
35
35
36
36
37
def build_options (after = 0 , before = 0 , context = 0 ):
37
38
options = []
38
39
if after :
39
- options . append ( '-A {}' . format (after ))
40
+ options += [ '-A' , str (after )]
40
41
if before :
41
- options . append ( '-B {}' . format (before ))
42
+ options += [ '-B' , str (before )]
42
43
if context :
43
- options . append ( '-C {}' . format (context ))
44
+ options += [ '-C' , str (context )]
44
45
45
- return ' ' . join ( x for x in options )
46
+ return options
46
47
47
48
48
49
def find_log (logpath , log , regex , after = 0 , before = 0 , context = 0 , field = 0 ):
49
50
options = build_options (after , before , context )
50
51
if field <= 0 :
51
- cmd = 'find -L {}/{}* -newer {} | xargs ls -rt | xargs zgrep -a {} "{}"' .format (logpath , log , reference_file , options , regex )
52
+ cmd0 = ['find' , logpath , "-name" , "{}*" .format (log ), "-newer" , reference_file ]
53
+ cmd1 = ["xargs" , "ls" , "-rt" ]
54
+ cmd2 = ["xargs" , "zgrep" , "-a" ] + options + [regex ]
52
55
else :
53
- cmd = 'find -L {0}/{1}* -newer {2} | sort -rn -t . -k {3},{3} | xargs zgrep -a {4} "{5}"' .format (logpath , log , reference_file , field , options , regex )
56
+ cmd0 = ['find' , logpath , "-name" , "{}*" .format (log ), "-newer" , reference_file ]
57
+ cmd1 = ["sort" , "-rn" , "-t" , "." , "-k" , "{0},{0}" .format (field )]
58
+ cmd2 = ["xargs" , "zgrep" , "-a" ] + options + [regex ]
54
59
55
- _ , out , _ = exec_cmd ( cmd )
60
+ _ , out = getstatusoutput_noshell_pipe ( cmd0 , cmd1 , cmd2 )
56
61
'''
57
62
Opportunity to improve:
58
63
output (out) can be split to lines and send to a filter to
@@ -71,12 +76,12 @@ def build_regex(category):
71
76
72
77
73
78
def configure_time_filter (since ):
74
- ret_code , _ , _ = exec_cmd ('date --date {}' . format ( since ) )
79
+ ret_code , _ , _ = exec_cmd ([ 'date' , ' --date' , since ] )
75
80
if ret_code :
76
81
print ('invalid date "{}"' .format (since ))
77
82
sys .exit (1 )
78
83
79
- exec_cmd ('touch --date "{}" {}' . format ( since , reference_file ) )
84
+ exec_cmd ([ 'touch' , ' --date' , since , reference_file ] )
80
85
81
86
82
87
def main ():
0 commit comments