Add include_path as setting
authorJonatan Schlag <jonatan.schlag@ipfire.org>
Sun, 16 Sep 2018 13:44:24 +0000 (15:44 +0200)
committerJonatan Schlag <jonatan.schlag@ipfire.org>
Sun, 16 Sep 2018 13:44:24 +0000 (15:44 +0200)
To make including files easier a new setting named "include_path" is
introduced.
When this path is given nitsi searches relative to this path for the
file to include.

Signed-off-by: Jonatan Schlag <jonatan.schlag@ipfire.org>
src/nitsi/recipe.py
src/nitsi/settings.py
src/nitsi/test.py

index d98bac5..f8cd67c 100644 (file)
@@ -16,7 +16,7 @@ class RecipeExeption(Exception):
 # Should read the test, check if the syntax are valid
 # and return tuples with the ( host, command ) structure
 class Recipe():
-    def __init__(self, path, circle=[], machines=[], fallback_machines=[]):
+    def __init__(self, path, circle=[], machines=[], fallback_machines=[], include_path=None):
         self.recipe_file = path
         try:
             self.path = os.path.dirname(self.recipe_file)
@@ -28,6 +28,15 @@ class Recipe():
 
         self.log = logger.getChild(self.name)
         self.log.debug("Path of recipe is: {}".format(self.recipe_file))
+
+        # This path must be absolut
+        self.include_path = include_path
+
+        if self.include_path and not os.path.isabs(self.include_path):
+            raise RecipeExeption("Include path must be absolut.")
+
+        self.log.debug("Include path is: {}".format(self.include_path))
+
         self._recipe = None
         self._machines = machines
         self._fallback_machines = fallback_machines
@@ -107,17 +116,22 @@ class Recipe():
             # We could get a machine here or a include statement
             if machine == "include":
                 path = cmd.strip()
-                path = os.path.normpath(self.path + "/" + path)
+                if self.include_path:
+                    path = os.path.normpath(self.include_path + "/" + path)
+                else:
+                    path = os.path.normpath(self.path + "/" + path)
 
                 # If we did not get a valid file we asume that we get a valid path to a test.
                 if os.path.isdir(path):
                     path = path + "/recipe"
 
+                self.log.debug("Path of recipe to include is: {}".format(path))
+
                 if path in self.circle:
                     self.log.error("Detect import loop!")
                     raise RecipeExeption("Detect import loop!")
                 self.circle.append(path)
-                recipe_to_include = Recipe(path, circle=self.circle)
+                recipe_to_include = Recipe(path, circle=self.circle, include_path=self.include_path)
 
             if machine == "include":
                 self._recipe.extend(recipe_to_include.recipe)
@@ -168,4 +182,4 @@ class Recipe():
                     for machine in tmp_machines:
                         tmp_recipe.append((machine.strip(), line[1], line[2]))
 
-            self._recipe = tmp_recipe
\ No newline at end of file
+            self._recipe = tmp_recipe
index 8cbece8..04dcb95 100644 (file)
@@ -102,6 +102,7 @@ class NitsiSettings(CommonSettings):
         self.set_config_value("copy_to", None, type="nitsi-default")
         self.set_config_value("virtual_environ_path", None, type="nitsi-default")
         self.set_config_value("interactive_error_handling", False, type="nitsi-default")
+        self.set_config_value("include_path", None, type="nitsi-default")
 
     def set_config_values_from_file(self, file, type):
         self.check_type(type)
@@ -122,11 +123,13 @@ class NitsiSettings(CommonSettings):
             raise e
 
         if "GENERAL" in config:
-            for key in ["name", "description", "copy_to", "copy_from"]:
+            for key in ["name", "description", "copy_to", "copy_from", "include_path"]:
                 if key in config["GENERAL"]:
                     # Handle the copy from setting in a special way
                     if key == "copy_from":
                         self.set_config_value(key, settings_parse_copy_from(config["GENERAL"][key], path=os.path.dirname(file)), type=type)
+                    elif key == "include_path":
+                        self.set_config_value(key, os.path.normpath(os.path.dirname(file) + "/" + config["GENERAL"][key]), type=type)
                     else:
                         self.set_config_value(key, config["GENERAL"][key], type=type)
 
index 8c454a5..8234c3d 100755 (executable)
@@ -151,7 +151,8 @@ class Test():
         self.log.info("Going to load the recipe")
         try:
             self.recipe = recipe.Recipe(self.recipe_file,
-                fallback_machines=self.virtual_environ.machine_names)
+                fallback_machines=self.virtual_environ.machine_names,
+                include_path=self.settings.get_config_value("include_path"))
 
             for line in self.recipe.recipe:
                 self.log.debug(line)