Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import json
from collections import OrderedDict
from sonic_py_common import device_info
from platform_utils import limit_execution_time

except ImportError as e:
raise ImportError(str(e) + "- required module not found")
Expand All @@ -24,6 +25,7 @@ def get_bios_version():
except subprocess.CalledProcessError as e:
raise RuntimeError("Failed to get BIOS version")

@limit_execution_time(1)
def get_bmc_version():
"""
Retrieves the firmware version of the BMC
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
raise ImportError(str(e) + "- required module not found")

def file_create(path, mode=None):
"""
Ensure that file is created with the appropriate permissions
Args:
path: full path of a file
mode: file permission in octal representation
"""
def run_cmd(cmd):
if os.geteuid() != 0:
cmd.insert(0, 'sudo')
Expand All @@ -20,7 +26,7 @@ def run_cmd(cmd):
run_cmd(['mkdir', '-p', file_path])
if not os.path.isfile(path):
run_cmd(['touch', path])
if (mode is not None):
if (mode is not None):
run_cmd(['chmod', mode, path])

def cancel_on_sigterm(func):
Expand All @@ -42,4 +48,33 @@ def handler(sig, frame):
finally:
signal.signal(signal.SIGTERM, sigterm_handler)
return result
return wrapper
return wrapper

def limit_execution_time(execution_time_secs: int):
"""
Wrapper for a function whose execution time must be limited
Args:
execution_time_secs: maximum execution time in seconds,
after which the function execution will be stopped
"""
def wrapper(func):
@wraps(func)
def execution_func(*args, **kwargs):
def handler(sig, frame):
if sigalrm_handler:
sigalrm_handler(sig, frame)
raise Exception("Canceling {}() execution...".format(func.__name__))

sigalrm_handler = signal.getsignal(signal.SIGALRM)
signal.signal(signal.SIGALRM, handler)
signal.alarm(execution_time_secs)
result = None
try:
result = func(*args, **kwargs)
finally:
signal.alarm(0)

return result
return execution_func
return wrapper