-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce sonic-py-common package (#5003)
Consolidate common SONiC Python-language functionality into one shared package (sonic-py-common) and eliminate duplicate code. The package currently includes three modules: - daemon_base - device_info - logger
- Loading branch information
Showing
15 changed files
with
623 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
SPATH := $($(SONIC_PY_COMMON_PY2)_SRC_PATH) | ||
DEP_FILES := $(SONIC_COMMON_FILES_LIST) rules/sonic-py-common.mk rules/sonic-py-common.dep | ||
DEP_FILES += $(SONIC_COMMON_BASE_FILES_LIST) | ||
DEP_FILES += $(shell git ls-files $(SPATH)) | ||
|
||
$(SONIC_PY_COMMON_PY2)_CACHE_MODE := GIT_CONTENT_SHA | ||
$(SONIC_PY_COMMON_PY2)_DEP_FLAGS := $(SONIC_COMMON_FLAGS_LIST) | ||
$(SONIC_PY_COMMON_PY2)_DEP_FILES := $(DEP_FILES) | ||
|
||
$(SONIC_PY_COMMON_PY3)_CACHE_MODE := GIT_CONTENT_SHA | ||
$(SONIC_PY_COMMON_PY3)_DEP_FLAGS := $(SONIC_COMMON_FLAGS_LIST) | ||
$(SONIC_PY_COMMON_PY3)_DEP_FILES := $(DEP_FILES) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# SONIC_PY_COMMON_PY2 package | ||
|
||
SONIC_PY_COMMON_PY2 = sonic_py_common-1.0-py2-none-any.whl | ||
$(SONIC_PY_COMMON_PY2)_SRC_PATH = $(SRC_PATH)/sonic-py-common | ||
$(SONIC_PY_COMMON_PY2)_DEPENDS += $(SWSSSDK_PY2) | ||
$(SONIC_PY_COMMON_PY2)_PYTHON_VERSION = 2 | ||
SONIC_PYTHON_WHEELS += $(SONIC_PY_COMMON_PY2) | ||
|
||
# SONIC_PY_COMMON_PY3 package | ||
|
||
SONIC_PY_COMMON_PY3 = sonic_py_common-1.0-py3-none-any.whl | ||
$(SONIC_PY_COMMON_PY3)_SRC_PATH = $(SRC_PATH)/sonic-py-common | ||
$(SONIC_PY_COMMON_PY3)_DEPENDS += $(SWSSSDK_PY3) | ||
$(SONIC_PY_COMMON_PY3)_PYTHON_VERSION = 3 | ||
SONIC_PYTHON_WHEELS += $(SONIC_PY_COMMON_PY3) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from setuptools import setup | ||
|
||
dependencies = [ | ||
'natsort', | ||
'pyyaml', | ||
'swsssdk>=2.0.1', | ||
] | ||
|
||
high_performance_deps = [ | ||
'swsssdk[high_perf]>=2.0.1', | ||
] | ||
|
||
setup( | ||
name='sonic-py-common', | ||
version='1.0', | ||
description='Common Python libraries for SONiC', | ||
license='Apache 2.0', | ||
author='SONiC Team', | ||
author_email='[email protected]', | ||
url='https://github.com/Azure/SONiC', | ||
maintainer='Joe LeVeque', | ||
maintainer_email='[email protected]', | ||
install_requires=dependencies, | ||
extras_require={ | ||
'high_perf': high_performance_deps, | ||
}, | ||
packages=[ | ||
'sonic_py_common', | ||
], | ||
classifiers=[ | ||
'Intended Audience :: Developers', | ||
'Operating System :: Linux', | ||
'Programming Language :: Python :: 2.7', | ||
'Programming Language :: Python :: 3.5', | ||
'Programming Language :: Python', | ||
], | ||
keywords='SONiC sonic PYTHON python COMMON common', | ||
) | ||
|
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import imp | ||
import signal | ||
import sys | ||
|
||
from . import device_info | ||
from .logger import Logger | ||
|
||
# | ||
# Constants ==================================================================== | ||
# | ||
REDIS_TIMEOUT_MSECS = 0 | ||
|
||
EEPROM_MODULE_NAME = 'eeprom' | ||
EEPROM_CLASS_NAME = 'board' | ||
|
||
# | ||
# Helper functions ============================================================= | ||
# | ||
|
||
def db_connect(db_name): | ||
from swsscommon import swsscommon | ||
return swsscommon.DBConnector(db_name, REDIS_TIMEOUT_MSECS, True) | ||
|
||
# | ||
# DaemonBase =================================================================== | ||
# | ||
|
||
class DaemonBase(Logger): | ||
def __init__(self, log_identifier): | ||
super(DaemonBase, self).__init__(log_identifier, Logger.LOG_FACILITY_DAEMON) | ||
|
||
# Register our default signal handlers, unless the signal already has a | ||
# handler registered, most likely from a subclass implementation | ||
if not signal.getsignal(signal.SIGHUP): | ||
signal.signal(signal.SIGHUP, self.signal_handler) | ||
if not signal.getsignal(signal.SIGINT): | ||
signal.signal(signal.SIGINT, self.signal_handler) | ||
if not signal.getsignal(signal.SIGTERM): | ||
signal.signal(signal.SIGTERM, self.signal_handler) | ||
|
||
# Default signal handler; can be overridden by subclass | ||
def signal_handler(self, sig, frame): | ||
if sig == signal.SIGHUP: | ||
log_info("DaemonBase: Caught SIGHUP - ignoring...") | ||
elif sig == signal.SIGINT: | ||
log_info("DaemonBase: Caught SIGINT - exiting...") | ||
sys.exit(128 + sig) | ||
elif sig == signal.SIGTERM: | ||
log_info("DaemonBase: Caught SIGTERM - exiting...") | ||
sys.exit(128 + sig) | ||
else: | ||
log_warning("DaemonBase: Caught unhandled signal '{}'".format(sig)) | ||
|
||
# Loads platform specific platform 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) = device_info.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 | ||
|
||
# Runs daemon | ||
def run(self): | ||
raise NotImplementedError() |
Oops, something went wrong.