-
Notifications
You must be signed in to change notification settings - Fork 669
/
Copy pathutil_base.py
151 lines (116 loc) · 5.03 KB
/
util_base.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/env python2
try:
import imp
import subprocess
import os
import syslog
except ImportError as e:
raise ImportError (str(e) + " - required module not found")
#
# Constants ====================================================================
#
# Platform root directory inside docker
PLATFORM_ROOT_DOCKER = '/usr/share/sonic/platform'
SONIC_CFGGEN_PATH = '/usr/local/bin/sonic-cfggen'
HWSKU_KEY = 'DEVICE_METADATA.localhost.hwsku'
PLATFORM_KEY = 'DEVICE_METADATA.localhost.platform'
PDDF_FILE_PATH = '/usr/share/sonic/platform/pddf_support'
# Port config information
PORT_CONFIG = 'port_config.ini'
PORTMAP = 'portmap.ini'
EEPROM_MODULE_NAME = 'eeprom'
EEPROM_CLASS_NAME = 'board'
class UtilLogger(object):
def __init__(self, syslog_identifier):
self.syslog = syslog
self.syslog.openlog(ident=syslog_identifier, logoption=self.syslog.LOG_NDELAY, facility=self.syslog.LOG_DAEMON)
def __del__(self):
self.syslog.closelog()
def log_error(self, msg, print_to_console=False):
self.syslog.syslog(self.syslog.LOG_ERR, msg)
if print_to_console:
print msg
def log_warning(self, msg, print_to_console=False):
self.syslog.syslog(self.syslog.LOG_WARNING, msg)
if print_to_console:
print msg
def log_notice(self, msg, print_to_console=False):
self.syslog.syslog(self.syslog.LOG_NOTICE, msg)
if print_to_console:
print msg
def log_info(self, msg, print_to_console=False):
self.syslog.syslog(self.syslog.LOG_INFO, msg)
if print_to_console:
print msg
def log_debug(self, msg, print_to_console=False):
self.syslog.syslog(self.syslog.LOG_DEBUG, msg)
if print_to_console:
print msg
class UtilHelper(object):
def __init__(self):
pass
# Returns platform and hwsku
def get_platform_and_hwsku(self):
try:
proc = subprocess.Popen([SONIC_CFGGEN_PATH, '-H', '-v', PLATFORM_KEY],
stdout=subprocess.PIPE,
shell=False,
stderr=subprocess.STDOUT)
stdout = proc.communicate()[0]
proc.wait()
platform = stdout.rstrip('\n')
proc = subprocess.Popen([SONIC_CFGGEN_PATH, '-d', '-v', HWSKU_KEY],
stdout=subprocess.PIPE,
shell=False,
stderr=subprocess.STDOUT)
stdout = proc.communicate()[0]
proc.wait()
hwsku = stdout.rstrip('\n')
except OSError as e:
raise OSError("Failed to detect platform: %s" % (str(e)))
return (platform, hwsku)
# Returns path to platform and hwsku
def get_path_to_platform_and_hwsku(self):
# Get platform and hwsku
(platform, hwsku) = self.get_platform_and_hwsku()
# Load platform module from source
platform_path = PLATFORM_ROOT_DOCKER
hwsku_path = "/".join([platform_path, hwsku])
return (platform_path, hwsku_path)
# Returns path to port config file
def get_path_to_port_config_file(self):
# Get platform and hwsku path
(platform_path, hwsku_path) = self.get_path_to_platform_and_hwsku()
# First check for the presence of the new 'port_config.ini' file
port_config_file_path = "/".join([hwsku_path, PORT_CONFIG])
if not os.path.isfile(port_config_file_path):
# port_config.ini doesn't exist. Try loading the legacy 'portmap.ini' file
port_config_file_path = "/".join([hwsku_path, PORTMAP])
if not os.path.isfile(port_config_file_path):
raise IOError("Failed to detect port config file: %s" % (port_config_file_path))
return port_config_file_path
# Loads platform specific psuutil module from source
def load_platform_util(self, module_name, class_name):
platform_util = None
# Get path to platform and hwsku
(platform_path, hwsku_path) = self.get_path_to_platform_and_hwsku()
try:
module_file = "/".join([platform_path, "plugins", module_name + ".py"])
module = imp.load_source(module_name, module_file)
except IOError as e:
raise IOError("Failed to load platform module '%s': %s" % (module_name, str(e)))
try:
platform_util_class = getattr(module, class_name)
# board class of eeprom requires 4 paramerters, need special treatment here.
if module_name == EEPROM_MODULE_NAME and class_name == EEPROM_CLASS_NAME:
platform_util = platform_util_class('','','','')
else:
platform_util = platform_util_class()
except AttributeError as e:
raise AttributeError("Failed to instantiate '%s' class: %s" % (class_name, str(e)))
return platform_util
def check_pddf_mode(self):
if os.path.exists(PDDF_FILE_PATH):
return True
else:
return False