From f33494aa1a1e73c6e2a4dc35a51ef1f8afe5996c Mon Sep 17 00:00:00 2001 From: Jonatan Schlag Date: Sat, 23 Jun 2018 09:45:54 +0200 Subject: [PATCH] Add new function settings_parse_copy_from When we get the copy_from settings from the command line we need different parsing as if we get the setting from a file. To avoid code duplication I maked this a function useable at both places in the code. Signed-off-by: Jonatan Schlag --- Makefile.am | 1 + src/nitsi/settings.py | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 src/nitsi/settings.py diff --git a/Makefile.am b/Makefile.am index 66c4110..4013334 100644 --- a/Makefile.am +++ b/Makefile.am @@ -37,6 +37,7 @@ nitsi_PYTHON = \ src/nitsi/network.py \ src/nitsi/recipe.py \ src/nitsi/serial_connection.py \ + src/nitsi/settings.py \ src/nitsi/test.py \ src/nitsi/virtual_environ.py diff --git a/src/nitsi/settings.py b/src/nitsi/settings.py new file mode 100644 index 0000000..b2dd71d --- /dev/null +++ b/src/nitsi/settings.py @@ -0,0 +1,40 @@ +#!/usr/bin/python3 + +import logging +import os + +logger = logging.getLogger("nitsi.settings") + +class SettingsException(Exception): + def __init__(self, message): + self.message = message + +def settings_parse_copy_from(copy_from, path=None): + logger.debug("Going to parse the copy_from setting.") + + # Check if we already get a list: + if not isinstance(copy_from, list): + copy_from = copy_from.split(" ") + + tmp = [] + for file in copy_from: + file = file.strip() + # If file is empty we do not want to add it to the list + if not file == "": + # If we get an absolut path we do nothing + # If not we add self.path to get an absolut path + if not os.path.isabs(file): + if path: + file = os.path.normpath(path + "/" + file) + else: + file = os.path.abspath(file) + + logger.debug("Checking if '{}' is a valid file or dir".format(file)) + # We need to check if file is a valid file or dir + if not (os.path.isdir(file) or os.path.isfile(file)): + raise SettingsException("'{}' is not a valid file nor a valid directory".format(file)) + + logger.debug("'{}' will be copied into all images".format(file)) + tmp.append(file) + + return tmp \ No newline at end of file -- 2.39.2