Skip to content

Commit

Permalink
[PDDF] Build and install Python 3 package (#6286)
Browse files Browse the repository at this point in the history
- Make PDDF code compliant with both Python 2 and Python 3
- Align code with PEP8 standards using autopep8
- Build and install both Python 2 and Python 3 PDDF packages
  • Loading branch information
jleveque committed Jan 7, 2021
1 parent 0ad2098 commit e52581e
Show file tree
Hide file tree
Showing 15 changed files with 837 additions and 868 deletions.
1 change: 0 additions & 1 deletion build_debian.sh
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,6 @@ sudo LANG=C DEBIAN_FRONTEND=noninteractive chroot $FILESYSTEM_ROOT apt-get -y in
ntpstat \
openssh-server \
python \
python-jsonschema \
python-apt \
traceroute \
iputils-ping \
Expand Down
4 changes: 2 additions & 2 deletions dockers/docker-platform-monitor/Dockerfile.j2
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ RUN apt-get update && \
python-smbus \
ethtool \
dmidecode \
i2c-tools \
python-jsonschema
i2c-tools

# TODO: Remove these lines once we no longer need Python 2
RUN apt-get install -f -y python-dev python-pip
RUN pip2 install --upgrade 'pip<21'
RUN apt-get purge -y python-pip
RUN pip2 install 'setuptools==40.8.0'
# On Arista devices, the sonic_platform wheel is not installed in the container.
# Instead, the installation directory is mounted from the host OS. However, this method
Expand Down
18 changes: 12 additions & 6 deletions files/build_templates/sonic_debian_extension.j2
Original file line number Diff line number Diff line change
Expand Up @@ -193,19 +193,25 @@ sudo cp {{platform_common_py2_wheel_path}} $FILESYSTEM_ROOT/$PLATFORM_COMMON_PY2
sudo https_proxy=$https_proxy LANG=C chroot $FILESYSTEM_ROOT pip2 install $PLATFORM_COMMON_PY2_WHEEL_NAME
sudo rm -rf $FILESYSTEM_ROOT/$PLATFORM_COMMON_PY2_WHEEL_NAME

# Install sonic-platform-common Python 3 package
PLATFORM_COMMON_PY3_WHEEL_NAME=$(basename {{platform_common_py3_wheel_path}})
sudo cp {{platform_common_py3_wheel_path}} $FILESYSTEM_ROOT/$PLATFORM_COMMON_PY3_WHEEL_NAME
sudo https_proxy=$https_proxy LANG=C chroot $FILESYSTEM_ROOT pip3 install $PLATFORM_COMMON_PY3_WHEEL_NAME
sudo rm -rf $FILESYSTEM_ROOT/$PLATFORM_COMMON_PY3_WHEEL_NAME

{% if pddf_support == "y" %}
# Install pddf-platform-api-base Python 2 package
PLATFORM_PDDF_COMMON_PY2_WHEEL_NAME=$(basename {{pddf_platform_api_base_py2_wheel_path}})
sudo cp {{pddf_platform_api_base_py2_wheel_path}} $FILESYSTEM_ROOT/$PLATFORM_PDDF_COMMON_PY2_WHEEL_NAME
sudo https_proxy=$https_proxy LANG=C chroot $FILESYSTEM_ROOT pip2 install $PLATFORM_PDDF_COMMON_PY2_WHEEL_NAME
sudo rm -rf $FILESYSTEM_ROOT/$PLATFORM_PDDF_COMMON_PY2_WHEEL_NAME
{% endif %}

# Install sonic-platform-common Python 3 package
PLATFORM_COMMON_PY3_WHEEL_NAME=$(basename {{platform_common_py3_wheel_path}})
sudo cp {{platform_common_py3_wheel_path}} $FILESYSTEM_ROOT/$PLATFORM_COMMON_PY3_WHEEL_NAME
sudo https_proxy=$https_proxy LANG=C chroot $FILESYSTEM_ROOT pip3 install $PLATFORM_COMMON_PY3_WHEEL_NAME
sudo rm -rf $FILESYSTEM_ROOT/$PLATFORM_COMMON_PY3_WHEEL_NAME
# Install pddf-platform-api-base Python 3 package
PLATFORM_PDDF_COMMON_PY3_WHEEL_NAME=$(basename {{pddf_platform_api_base_py3_wheel_path}})
sudo cp {{pddf_platform_api_base_py3_wheel_path}} $FILESYSTEM_ROOT/$PLATFORM_PDDF_COMMON_PY3_WHEEL_NAME
sudo https_proxy=$https_proxy LANG=C chroot $FILESYSTEM_ROOT pip3 install $PLATFORM_PDDF_COMMON_PY3_WHEEL_NAME
sudo rm -rf $FILESYSTEM_ROOT/$PLATFORM_PDDF_COMMON_PY3_WHEEL_NAME
{% endif %}

{# Barefoot platform vendors' sonic_platform packages import the Python 'thrift' library #}
{% if sonic_asic_platform == "barefoot" %}
Expand Down
4 changes: 4 additions & 0 deletions platform/pddf/platform-api-pddf-base.dep
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ DEP_FILES += $(shell git ls-files $(MPATH))
$(PDDF_PLATFORM_API_BASE_PY2)_CACHE_MODE := GIT_CONTENT_SHA
$(PDDF_PLATFORM_API_BASE_PY2)_DEP_FLAGS := $(SONIC_COMMON_FLAGS_LIST)
$(PDDF_PLATFORM_API_BASE_PY2)_DEP_FILES := $(DEP_FILES)

$(PDDF_PLATFORM_API_BASE_PY3)_CACHE_MODE := GIT_CONTENT_SHA
$(PDDF_PLATFORM_API_BASE_PY3)_DEP_FLAGS := $(SONIC_COMMON_FLAGS_LIST)
$(PDDF_PLATFORM_API_BASE_PY3)_DEP_FILES := $(DEP_FILES)
13 changes: 13 additions & 0 deletions platform/pddf/platform-api-pddf-base.mk
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,16 @@ SONIC_PYTHON_WHEELS += $(PDDF_PLATFORM_API_BASE_PY2)

export pddf_platform_api_base_py2_wheel_path="$(addprefix $(PYTHON_WHEELS_PATH)/,$(PDDF_PLATFORM_API_BASE_PY2))"
export PDDF_PLATFORM_API_BASE_PY2

PDDF_PLATFORM_API_BASE_PY3 = sonic_platform_pddf_common-$(PDDF_PLATFORM_API_BASE_VERSION)-py3-none-any.whl
$(PDDF_PLATFORM_API_BASE_PY3)_SRC_PATH = $(PLATFORM_PDDF_PATH)/platform-api-pddf-base
$(PDDF_PLATFORM_API_BASE_PY3)_PYTHON_VERSION = 3
$(PDDF_PLATFORM_API_BASE_PY3)_DEPENDS = $(SONIC_CONFIG_ENGINE)
# Synthetic dependency to avoid building the Python 2 and 3 packages
# simultaneously and any potential conflicts which may arise
$(PDDF_PLATFORM_API_BASE_PY3)_DEPENDS += $(PDDF_PLATFORM_API_BASE_PY2)
$(PDDF_PLATFORM_API_BASE_PY3)_TEST = n
SONIC_PYTHON_WHEELS += $(PDDF_PLATFORM_API_BASE_PY3)

export pddf_platform_api_base_py3_wheel_path="$(addprefix $(PYTHON_WHEELS_PATH)/,$(PDDF_PLATFORM_API_BASE_PY3))"
export PDDF_PLATFORM_API_BASE_PY3
4 changes: 4 additions & 0 deletions platform/pddf/platform-api-pddf-base/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
packages=[
'sonic_platform_pddf_base',
],
install_requires=[
'jsonschema==2.6.0'
],
classifiers=[
'Development Status :: 3 - Alpha',
'Environment :: Plugins',
Expand All @@ -24,6 +27,7 @@
'Natural Language :: English',
'Operating System :: POSIX :: Linux',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3.7',
'Topic :: Utilities',
],
keywords='sonic SONiC platform PLATFORM',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
except ImportError as e:
raise ImportError(str(e) + "- required module not found")


class PddfChassis(ChassisBase):
"""
PDDF Generic Chassis class
Expand All @@ -33,7 +34,7 @@ def __init__(self, pddf_data=None, pddf_plugin_data=None):
self.plugin_data = pddf_plugin_data if pddf_plugin_data else None
if not self.pddf_obj or not self.plugin_data:
try:
import pddfparse
from . import pddfparse
import json
self.pddf_obj = pddfparse.PddfParse()
with open('/usr/share/sonic/platform/pddf/pd-plugin.json') as pd:
Expand Down Expand Up @@ -67,8 +68,8 @@ def __init__(self, pddf_data=None, pddf_plugin_data=None):
thermal = Thermal(i, self.pddf_obj, self.plugin_data)
self._thermal_list.append(thermal)

# SYSTEM LED Test Cases
"""
# SYSTEM LED Test Cases
"""
#comment out test cases
sys_led_list= { "LOC":0,
"DIAG":0,
Expand All @@ -90,8 +91,6 @@ def __init__(self, pddf_data=None, pddf_plugin_data=None):
print "Set off: " + color
"""



def get_name(self):
"""
Retrieves the name of the chassis
Expand Down Expand Up @@ -438,34 +437,32 @@ def get_sfp(self, index):
# System LED methods
##############################################
def set_system_led(self, led_device_name, color):
result, msg = self.pddf_obj.is_supported_sysled_state(led_device_name, color);
result, msg = self.pddf_obj.is_supported_sysled_state(led_device_name, color)
if result == False:
print msg
return (False)
print(msg)
return (False)

index=self.pddf_obj.data[led_device_name]['dev_attr']['index']
device_name=self.pddf_obj.data[led_device_name]['dev_info']['device_name']
index = self.pddf_obj.data[led_device_name]['dev_attr']['index']
device_name = self.pddf_obj.data[led_device_name]['dev_info']['device_name']
self.pddf_obj.create_attr('device_name', device_name, self.pddf_obj.get_led_path())
self.pddf_obj.create_attr('index', index, self.pddf_obj.get_led_path())
self.pddf_obj.create_attr('color', color, self.pddf_obj.get_led_cur_state_path())
self.pddf_obj.create_attr('dev_ops', 'set_status', self.pddf_obj.get_led_path())
return (True)


def get_system_led(self, led_device_name):
if (not led_device_name in self.pddf_obj.data.keys()):
status= "[FAILED] " + led_device_name + " is not configured"
return (status)
if led_device_name not in self.pddf_obj.data.keys():
status = "[FAILED] " + led_device_name + " is not configured"
return (status)

index=self.pddf_obj.data[led_device_name]['dev_attr']['index']
device_name=self.pddf_obj.data[led_device_name]['dev_info']['device_name']
index = self.pddf_obj.data[led_device_name]['dev_attr']['index']
device_name = self.pddf_obj.data[led_device_name]['dev_info']['device_name']
self.pddf_obj.create_attr('device_name', device_name, self.pddf_obj.get_led_path())
self.pddf_obj.create_attr('index', index, self.pddf_obj.get_led_path())
self.pddf_obj.create_attr('dev_ops', 'get_status', self.pddf_obj.get_led_path())
color=self.pddf_obj.get_led_color()
color = self.pddf_obj.get_led_color()
return (color)


##############################################
# Other methods
##############################################
Expand Down Expand Up @@ -513,4 +510,3 @@ def get_change_event(self, timeout=0):
has been inserted and sfp 11 has been removed.
"""
raise NotImplementedError

Original file line number Diff line number Diff line change
Expand Up @@ -67,47 +67,41 @@ def __init__(self, pddf_data=None, pddf_plugin_data=None):
tlv_index += ord(eeprom[tlv_index+1]) + 2

def serial_number_str(self):
(is_valid, results) = self.get_tlv_field(
self.eeprom_data, self._TLV_CODE_SERIAL_NUMBER)
(is_valid, results) = self.get_tlv_field(self.eeprom_data, self._TLV_CODE_SERIAL_NUMBER)
if not is_valid:
return "N/A"
return results[2]

def base_mac_addr(self):
(is_valid, t) = self.get_tlv_field(
self.eeprom_data, self._TLV_CODE_MAC_BASE)
(is_valid, t) = self.get_tlv_field(self.eeprom_data, self._TLV_CODE_MAC_BASE)
if not is_valid or t[1] != 6:
return super(TlvInfoDecoder, self).switchaddrstr(e)

return ":".join([binascii.b2a_hex(T) for T in t[2]])

def modelstr(self):
(is_valid, results) = self.get_tlv_field(
self.eeprom_data, self._TLV_CODE_PRODUCT_NAME)
(is_valid, results) = self.get_tlv_field(self.eeprom_data, self._TLV_CODE_PRODUCT_NAME)
if not is_valid:
return "N/A"

return results[2]

def part_number_str(self):
(is_valid, results) = self.get_tlv_field(
self.eeprom_data, self._TLV_CODE_PART_NUMBER)
(is_valid, results) = self.get_tlv_field(self.eeprom_data, self._TLV_CODE_PART_NUMBER)
if not is_valid:
return "N/A"

return results[2]

def serial_str(self):
(is_valid, results) = self.get_tlv_field(
self.eeprom_data, self._TLV_CODE_SERVICE_TAG)
(is_valid, results) = self.get_tlv_field(self.eeprom_data, self._TLV_CODE_SERVICE_TAG)
if not is_valid:
return "N/A"

return results[2]

def revision_str(self):
(is_valid, results) = self.get_tlv_field(
self.eeprom_data, self._TLV_CODE_DEVICE_VERSION)
(is_valid, results) = self.get_tlv_field(self.eeprom_data, self._TLV_CODE_DEVICE_VERSION)
if not is_valid:
return "N/A"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,20 @@ class PddfFan(FanBase):
plugin_data = {}

def __init__(self, tray_idx, fan_idx=0, pddf_data=None, pddf_plugin_data=None, is_psu_fan=False, psu_index=0):
# idx is 0-based
# idx is 0-based
if not pddf_data or not pddf_plugin_data:
raise ValueError('PDDF JSON data error')

self.pddf_obj = pddf_data
self.plugin_data = pddf_plugin_data
self.platform = self.pddf_obj.get_platform()

if tray_idx<0 or tray_idx>=self.platform['num_fantrays']:
print "Invalid fantray index %d\n"%tray_idx
if tray_idx < 0 or tray_idx >= self.platform['num_fantrays']:
print("Invalid fantray index %d\n" % tray_idx)
return
if fan_idx<0 or fan_idx>=self.platform['num_fans_pertray']:
print "Invalid fan index (within a tray) %d\n"%fan_idx

if fan_idx < 0 or fan_idx >= self.platform['num_fans_pertray']:
print("Invalid fan index (within a tray) %d\n" % fan_idx)
return

self.fantray_index = tray_idx+1
Expand Down Expand Up @@ -160,7 +160,7 @@ def get_speed(self):

if not output:
return 0

output['status'] = output['status'].rstrip()
if output['status'].isalpha():
return 0
Expand All @@ -185,7 +185,7 @@ def get_speed_rpm(self):
output = self.pddf_obj.get_attr_name_output(device, attr)
if not output:
return 0

output['status'] = output['status'].rstrip()
if output['status'].isalpha():
return 0
Expand Down Expand Up @@ -229,7 +229,7 @@ def get_target_speed(self):

if not output:
return 0

output['status'] = output['status'].rstrip()
if output['status'].isalpha():
return 0
Expand All @@ -250,7 +250,7 @@ def get_speed_tolerance(self):
An integer, the percentage of variance from target speed which is
considered tolerable
"""
# Fix the speed vairance to 10 percent. If it changes based on platforms, overwrite
# Fix the speed vairance to 10 percent. If it changes based on platforms, overwrite
# this value in derived pddf fan class
return 10

Expand All @@ -266,28 +266,27 @@ def set_speed(self, speed):
A boolean, True if speed is set successfully, False if not
"""
if self.is_psu_fan:
print "Setting PSU fan speed is not allowed"
print("Setting PSU fan speed is not allowed")
return False
else:
if speed<0 or speed>100:
print "Error: Invalid speed %d. Please provide a valid speed percentage"%speed
if speed < 0 or speed > 100:
print("Error: Invalid speed %d. Please provide a valid speed percentage" % speed)
return False

if 'duty_cycle_to_pwm' not in self.plugin_data['FAN']:
print "Setting fan speed is not allowed !"
print("Setting fan speed is not allowed !")
return False
else:
duty_cycle_to_pwm = eval(self.plugin_data['FAN']['duty_cycle_to_pwm'])
pwm = int(round(duty_cycle_to_pwm(speed)))


status = False
idx = (self.fantray_index-1)*self.platform['num_fans_pertray'] + self.fan_index
attr = "fan" + str(idx) + "_pwm"
output = self.pddf_obj.set_attr_name_output("FAN-CTRL", attr, pwm)
if not output:
return False

status = output['status']

return status
Expand All @@ -296,36 +295,34 @@ def set_status_led(self, color):
index = str(self.fantray_index-1)
led_device_name = "FANTRAY{}".format(self.fantray_index) + "_LED"

result, msg = self.pddf_obj.is_supported_sysled_state(led_device_name, color);
result, msg = self.pddf_obj.is_supported_sysled_state(led_device_name, color)
if result == False:
print msg
return (False)
print(msg)
return (False)


device_name=self.pddf_obj.data[led_device_name]['dev_info']['device_name']
device_name = self.pddf_obj.data[led_device_name]['dev_info']['device_name']
self.pddf_obj.create_attr('device_name', device_name, self.pddf_obj.get_led_path())
self.pddf_obj.create_attr('index', index, self.pddf_obj.get_led_path())
self.pddf_obj.create_attr('color', color, self.pddf_obj.get_led_cur_state_path())
self.pddf_obj.create_attr('dev_ops', 'set_status', self.pddf_obj.get_led_path())
return (True)


def get_status_led(self):
index = str(self.fantray_index-1)
fan_led_device = "FANTRAY{}".format(self.fantray_index) + "_LED"

if (not fan_led_device in self.pddf_obj.data.keys()):
if fan_led_device not in self.pddf_obj.data.keys():
# Implement a generic status_led color scheme
if self.get_status():
return self.STATUS_LED_COLOR_GREEN
else:
return self.STATUS_LED_COLOR_OFF

device_name=self.pddf_obj.data[fan_led_device]['dev_info']['device_name']
device_name = self.pddf_obj.data[fan_led_device]['dev_info']['device_name']
self.pddf_obj.create_attr('device_name', device_name, self.pddf_obj.get_led_path())
self.pddf_obj.create_attr('index', index, self.pddf_obj.get_led_path())
self.pddf_obj.create_attr('dev_ops', 'get_status', self.pddf_obj.get_led_path())
color=self.pddf_obj.get_led_color()
color = self.pddf_obj.get_led_color()
return (color)

def dump_sysfs(self):
Expand Down
Loading

0 comments on commit e52581e

Please sign in to comment.