]> git.ipfire.org Git - nitsi.git/blobdiff - src/nitsi/recipe.py
Allow including of recipe files
[nitsi.git] / src / nitsi / recipe.py
index 394f50b264574ab86335ba8c432599309cb6910d..e1e2af8904a4edb721ca01c942b0cef623a6a9e0 100644 (file)
@@ -1,8 +1,7 @@
 #!/usr/bin/python3
 
-import os
-
 import logging
+import os
 
 logger = logging.getLogger("nitsi.recipe")
 
@@ -16,20 +15,23 @@ 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=[]):
+class Recipe():
+    def __init__(self, path, circle=[], machines=[]):
         self.recipe_file = path
         try:
             self.path = os.path.dirname(self.recipe_file)
+            self.path = os.path.abspath(self.path)
             self.name = os.path.basename(self.path)
         except BaseException as e:
-            logger.error("Failed to get the name of the test to this recipe")
+            logger.error("Failed to get the path to this recipe")
             raise e
 
         self.log = logger.getChild(self.name)
         self.log.debug("Path of recipe is: {}".format(self.recipe_file))
         self._recipe = None
-        self._machines = None
+        self._machines = machines
+
+        self.log.debug("Machine names we use when we substitute the all statement: {}".format(self._machines))
 
         self.in_recursion = True
         if len(circle) == 0:
@@ -40,13 +42,13 @@ class recipe():
 
         if not os.path.isfile(self.recipe_file):
             self.log.error("{} is not a file".format(self.recipe_file))
-            raise RecipeExeption("{} is not a file".format(self.recipe_file)())
+            raise RecipeExeption("{} is not a file".format(self.recipe_file))
 
         try:
             with open(self.recipe_file) as fobj:
                 self.raw_recipe = fobj.readlines()
         except FileNotFoundError as error:
-            self.log.error("No such file: {}".format(vm_xml_file))
+            self.log.error("No such file: {}".format(self.recipe_file))
             raise error
 
     @property
@@ -58,18 +60,24 @@ class recipe():
 
     @property
     def machines(self):
-        if not self._machines:
-            self._machines = []
-            for line in self._recipe:
-                if line[0] != "all" and line[0] not in self._machines:
-                    self._machines.append(line[0])
-
         return self._machines
 
     def parse(self):
         self._recipe = []
         i = 1
         for line in self.raw_recipe:
+            # Check if the line is empty
+            if line.strip() == "":
+                self.log.debug("Skipping empty line {}".format(i))
+                i = i + 1
+                continue
+
+            # Check if the line is a comment
+            if line.strip().startswith("#"):
+                self.log.debug("Skipping comment in line {}".format(i))
+                i = i + 1
+                continue
+
             raw_line = line.split(":", 1)
             if len(raw_line) < 2:
                 self.log.error("Error parsing the recipe in line {}".format(i))
@@ -95,12 +103,16 @@ class recipe():
             if machine == "include":
                 path = cmd.strip()
                 path = os.path.normpath(self.path + "/" + path)
-                path = path + "/recipe"
+
+                # 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"
+
                 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)
 
             if machine == "include":
                 self._recipe.extend(recipe_to_include.recipe)