1
1
import os
2
+ import ast
2
3
import imp
3
4
import yaml
4
5
import subprocess
5
-
6
6
from sonic_py_common import device_info
7
7
8
8
@@ -24,7 +24,7 @@ class Common:
24
24
25
25
SET_METHOD_IPMI = 'ipmitool'
26
26
NULL_VAL = 'N/A'
27
- HOST_CHK_CMD = "docker > /dev/null 2>&1"
27
+ HOST_CHK_CMD = [ "docker" ]
28
28
REF_KEY = '$ref:'
29
29
30
30
def __init__ (self , conf = None ):
@@ -46,8 +46,7 @@ def run_command(self, command):
46
46
status = False
47
47
output = ""
48
48
try :
49
- p = subprocess .Popen (
50
- command , shell = True , stdout = subprocess .PIPE , stderr = subprocess .PIPE )
49
+ p = subprocess .Popen (command , universal_newlines = True , stdout = subprocess .PIPE , stderr = subprocess .PIPE )
51
50
raw_data , err = p .communicate ()
52
51
if p .returncode == 0 :
53
52
status , output = True , raw_data .strip ()
@@ -67,7 +66,7 @@ def _clean_input(self, input, config):
67
66
cleaned_input = input_translator .get (input )
68
67
69
68
elif type (input_translator ) is str :
70
- cleaned_input = eval (input_translator .format (input ))
69
+ cleaned_input = ast . literal_eval (input_translator .format (input ))
71
70
72
71
return cleaned_input
73
72
@@ -77,19 +76,12 @@ def _clean_output(self, index, output, config):
77
76
if type (output_translator ) is dict :
78
77
output = output_translator .get (output )
79
78
elif type (output_translator ) is str :
80
- output = eval (output_translator .format (output ))
79
+ output = ast . literal_eval (output_translator .format (output ))
81
80
elif type (output_translator ) is list :
82
- output = eval (output_translator [index ].format (output ))
81
+ output = ast . literal_eval (output_translator [index ].format (output ))
83
82
84
83
return output
85
84
86
- def _ipmi_get (self , index , config ):
87
- argument = config .get ('argument' )
88
- cmd = config ['command' ].format (
89
- config ['argument' ][index ]) if argument else config ['command' ]
90
- status , output = self .run_command (cmd )
91
- return output if status else None
92
-
93
85
def _sysfs_read (self , index , config ):
94
86
sysfs_path = config .get ('sysfs_path' )
95
87
argument = config .get ('argument' , '' )
@@ -132,10 +124,6 @@ def _sysfs_write(self, index, config, input):
132
124
return False , output
133
125
return True , output
134
126
135
- def _ipmi_set (self , index , config , input ):
136
- arg = config ['argument' ][index ].format (input )
137
- return self .run_command (config ['command' ].format (arg ))
138
-
139
127
def _hex_ver_decode (self , hver , num_of_bits , num_of_points ):
140
128
ver_list = []
141
129
c_bit = 0
@@ -159,14 +147,16 @@ def _get_class(self, config):
159
147
return class_
160
148
161
149
def get_reg (self , path , reg_addr ):
162
- cmd = "echo {1} > {0}; cat {0}" .format (path , reg_addr )
163
- status , output = self .run_command (cmd )
164
- return output if status else None
150
+ with open (path , 'w' ) as file :
151
+ file .write (reg_addr + '\n ' )
152
+ with open (path , 'r' ) as file :
153
+ output = file .readline ().strip ()
154
+ return output
165
155
166
156
def set_reg (self , path , reg_addr , value ):
167
- cmd = "echo {0} {1} > {2}" . format ( reg_addr , value , path )
168
- status , output = self . run_command ( cmd )
169
- return output if status else None
157
+ with open ( path , 'w' ) as file :
158
+ file . write ( "{0} {1} \n " . format ( reg_addr , value ) )
159
+ return None
170
160
171
161
def read_txt_file (self , path ):
172
162
try :
@@ -195,7 +185,11 @@ def write_txt_file(self, file_path, value):
195
185
return True
196
186
197
187
def is_host (self ):
198
- return os .system (self .HOST_CHK_CMD ) == 0
188
+ try :
189
+ subprocess .call (self .HOST_CHK_CMD , stdout = subprocess .DEVNULL , stderr = subprocess .DEVNULL )
190
+ except FileNotFoundError :
191
+ return False
192
+ return True
199
193
200
194
def load_json_file (self , path ):
201
195
"""
@@ -221,87 +215,6 @@ def get_config_path(self, config_name):
221
215
"""
222
216
return os .path .join (self .DEVICE_PATH , self .platform , self .CONFIG_DIR , config_name ) if self .is_host () else os .path .join (self .PMON_PLATFORM_PATH , self .CONFIG_DIR , config_name )
223
217
224
- def get_output (self , index , config , default ):
225
- """
226
- Retrieves the output for each function base on config
227
-
228
- Args:
229
- index: An integer containing the index of device.
230
- config: A dict object containing the configuration of specified function.
231
- default: A string containing the default output of specified function.
232
-
233
- Returns:
234
- A string containing the output of specified function in config
235
- """
236
- output_source = config .get ('output_source' )
237
-
238
- if output_source == self .OUTPUT_SOURCE_IPMI :
239
- output = self ._ipmi_get (index , config )
240
-
241
- elif output_source == self .OUTPUT_SOURCE_GIVEN_VALUE :
242
- output = config ["value" ]
243
-
244
- elif output_source == self .OUTPUT_SOURCE_GIVEN_CLASS :
245
- output = self ._get_class (config )
246
-
247
- elif output_source == self .OUTPUT_SOURCE_GIVEN_LIST :
248
- output = config ["value_list" ][index ]
249
-
250
- elif output_source == self .OUTPUT_SOURCE_SYSFS :
251
- output = self ._sysfs_read (index , config )
252
-
253
- elif output_source == self .OUTPUT_SOURCE_FUNC :
254
- func_conf = self ._main_conf [config ['function' ][index ]]
255
- output = self .get_output (index , func_conf , default )
256
-
257
- elif output_source == self .OUTPUT_SOURCE_GIVEN_TXT_FILE :
258
- path = config .get ('path' )
259
- output = self .read_txt_file (path )
260
-
261
- elif output_source == self .OUTPUT_SOURCE_GIVEN_VER_HEX_FILE :
262
- path = config .get ('path' )
263
- hex_ver = self .read_txt_file (path )
264
- output = self ._hex_ver_decode (
265
- hex_ver , config ['num_of_bits' ], config ['num_of_points' ])
266
-
267
- elif output_source == self .OUTPUT_SOURCE_GIVEN_VER_HEX_ADDR :
268
- path = config .get ('path' )
269
- addr = config .get ('reg_addr' )
270
- hex_ver = self .get_reg (path , addr )
271
- output = self ._hex_ver_decode (
272
- hex_ver , config ['num_of_bits' ], config ['num_of_points' ])
273
-
274
- else :
275
- output = default
276
-
277
- return self ._clean_output (index , output , config ) or default
278
-
279
- def set_output (self , index , input , config ):
280
- """
281
- Sets the output of specified function on config
282
-
283
- Args:
284
- config: A dict object containing the configuration of specified function.
285
- index: An integer containing the index of device.
286
- input: A string containing the input of specified function.
287
-
288
- Returns:
289
- bool: True if set function is successfully, False if not
290
- """
291
- cleaned_input = self ._clean_input (input , config )
292
- if not cleaned_input :
293
- return False
294
-
295
- set_method = config .get ('set_method' )
296
- if set_method == self .SET_METHOD_IPMI :
297
- output = self ._ipmi_set (index , config , cleaned_input )[0 ]
298
- elif set_method == self .OUTPUT_SOURCE_SYSFS :
299
- output = self ._sysfs_write (index , config , cleaned_input )[0 ]
300
- else :
301
- output = False
302
-
303
- return output
304
-
305
218
def get_event (self , timeout , config , sfp_list ):
306
219
"""
307
220
Returns a nested dictionary containing all devices which have
0 commit comments