7
7
import os .path
8
8
import subprocess
9
9
import logging
10
+ from sonic_py_common .general import check_output_pipe
10
11
11
12
try :
12
13
from sonic_psu .psu_base import PsuBase
@@ -22,11 +23,13 @@ def show_log(txt):
22
23
return
23
24
24
25
25
- def exec_cmd (cmd , show ):
26
+ def exec_cmd (cmd_args , out_file , show ):
27
+ cmd = ' ' .join (cmd_args ) + ' > ' + out_file
26
28
logging .info ('Run :' + cmd )
27
29
try :
28
- output = subprocess .check_output (cmd , shell = True , universal_newlines = True )
29
- show_log (cmd + "output:" + str (output ))
30
+ with open (out_file , 'w' ) as f :
31
+ output = subprocess .check_output (cmd_args , stdout = f , universal_newlines = True )
32
+ show_log (cmd + "output:" + str (output ))
30
33
except subprocess .CalledProcessError as e :
31
34
logging .info ("Failed :" + cmd )
32
35
if show :
@@ -40,12 +43,13 @@ def my_log(txt):
40
43
return
41
44
42
45
43
- def log_os_system (cmd , show ):
46
+ def log_os_system (cmd1_args , cmd2_args , show ):
47
+ cmd = ' ' .join (cmd1_args ) + ' | ' + ' ' .join (cmd2_args )
44
48
logging .info ('Run :' + cmd )
45
49
status = 1
46
50
output = ""
47
51
try :
48
- output = subprocess . check_output ( cmd , shell = True , universal_newlines = True )
52
+ output = check_output_pipe ( cmd1_args , cmd2_args )
49
53
my_log (cmd + "output:" + str (output ))
50
54
except subprocess .CalledProcessError as e :
51
55
logging .info ('Failed :' + cmd )
@@ -55,28 +59,28 @@ def log_os_system(cmd, show):
55
59
56
60
57
61
def gpio16_exist ():
58
- ls = log_os_system ("ls /sys/class/gpio/ | grep gpio16" , 0 )
62
+ ls = log_os_system ([ "ls" , " /sys/class/gpio/" ], [ " grep" , " gpio16"] , 0 )
59
63
logging .info ('mods:' + ls )
60
64
if len (ls ) == 0 :
61
65
return False
62
66
63
67
64
68
def gpio17_exist ():
65
- ls = log_os_system ("ls /sys/class/gpio/ | grep gpio17" , 0 )
69
+ ls = log_os_system ([ "ls" , " /sys/class/gpio/" ], [ " grep" , " gpio17"] , 0 )
66
70
logging .info ('mods:' + ls )
67
71
if len (ls ) == 0 :
68
72
return False
69
73
70
74
71
75
def gpio19_exist ():
72
- ls = log_os_system ("ls /sys/class/gpio/ | grep gpio19" , 0 )
76
+ ls = log_os_system ([ "ls" , " /sys/class/gpio/" ], [ " grep" , " gpio19"] , 0 )
73
77
logging .info ('mods:' + ls )
74
78
if len (ls ) == 0 :
75
79
return False
76
80
77
81
78
82
def gpio20_exist ():
79
- ls = log_os_system ("ls /sys/class/gpio/ | grep gpio20" , 0 )
83
+ ls = log_os_system ([ "ls" , " /sys/class/gpio/" ], [ " grep" , " gpio20"] , 0 )
80
84
logging .info ('mods:' + ls )
81
85
if len (ls ) == 0 :
82
86
return False
@@ -95,20 +99,20 @@ def __init__(self):
95
99
PsuBase .__init__ (self )
96
100
97
101
if gpio16_exist () == False :
98
- output = exec_cmd ("echo 16 > /sys/class/gpio/export " , 1 )
99
- output = exec_cmd ("echo in > /sys/class/gpio/gpio16/direction " , 1 )
102
+ output = exec_cmd ([ "echo" , "16" ], " /sys/class/gpio/export" , 1 )
103
+ output = exec_cmd ([ "echo" , "in" ], " /sys/class/gpio/gpio16/direction" , 1 )
100
104
101
105
if gpio17_exist () == False :
102
- output = exec_cmd ("echo 17 > /sys/class/gpio/export " , 1 )
103
- output = exec_cmd ("echo in > /sys/class/gpio/gpio17/direction " , 1 )
106
+ output = exec_cmd ([ "echo" , "17" ], " /sys/class/gpio/export" , 1 )
107
+ output = exec_cmd ([ "echo" , "in" ], " /sys/class/gpio/gpio17/direction" , 1 )
104
108
105
109
if gpio19_exist () == False :
106
- output = exec_cmd ("echo 19 > /sys/class/gpio/export " , 1 )
107
- output = exec_cmd ("echo in > /sys/class/gpio/gpio19/direction " , 1 )
110
+ output = exec_cmd ([ "echo" , "19" ], " /sys/class/gpio/export" , 1 )
111
+ output = exec_cmd ([ "echo" , "in" ], " /sys/class/gpio/gpio19/direction" , 1 )
108
112
109
113
if gpio20_exist () == False :
110
- output = exec_cmd ("echo 20 > /sys/class/gpio/export " , 1 )
111
- output = exec_cmd ("echo in > /sys/class/gpio/gpio20/direction " , 1 )
114
+ output = exec_cmd ([ "echo" , "20" ], " /sys/class/gpio/export" , 1 )
115
+ output = exec_cmd ([ "echo" , "in" ], " /sys/class/gpio/gpio20/direction" , 1 )
112
116
113
117
# Get sysfs attribute
114
118
def get_attr_value (self , attr_path ):
0 commit comments