]> git.ipfire.org Git - nitsi.git/commitdiff
Add new function settings_parse_copy_from
authorJonatan Schlag <jonatan.schlag@ipfire.org>
Sat, 23 Jun 2018 07:45:54 +0000 (09:45 +0200)
committerJonatan Schlag <jonatan.schlag@ipfire.org>
Sat, 23 Jun 2018 07:45:54 +0000 (09:45 +0200)
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 <jonatan.schlag@ipfire.org>
Makefile.am
src/nitsi/settings.py [new file with mode: 0644]

index 66c41103bb49d1f261f15e184b35bc96d99e8486..40133347d7f2c78983e801d150bc23b5131a4cac 100644 (file)
@@ -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 (file)
index 0000000..b2dd71d
--- /dev/null
@@ -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