]> git.ipfire.org Git - nitsi.git/blame - src/nitsi/recipe.py
Improve recipe parsing
[nitsi.git] / src / nitsi / recipe.py
CommitLineData
3a4eb2c7
JS
1#!/usr/bin/python3
2
1ed8ca9f 3import logging
6632e137 4import os
1ed8ca9f
JS
5
6logger = logging.getLogger("nitsi.recipe")
7
3a4eb2c7
JS
8
9
10class RecipeExeption(Exception):
61b44c10
JS
11 def __init__(self, message):
12 self.message = message
3a4eb2c7
JS
13
14
15
16# Should read the test, check if the syntax are valid
17# and return tuples with the ( host, command ) structure
18class recipe():
19 def __init__(self, path, circle=[]):
3a4eb2c7 20 self.recipe_file = path
518441de
JS
21 try:
22 self.path = os.path.dirname(self.recipe_file)
23 self.name = os.path.basename(self.path)
24 except BaseException as e:
25 logger.error("Failed to get the name of the test to this recipe")
26 raise e
27
28 self.log = logger.getChild(self.name)
3a4eb2c7
JS
29 self.log.debug("Path of recipe is: {}".format(self.recipe_file))
30 self._recipe = None
31 self._machines = None
32
33 self.in_recursion = True
34 if len(circle) == 0:
35 self.in_recursion = False
36
37 self.circle = circle
2fa4467d 38 self.log.debug("Recipes we have already included: {}".format(self.circle))
3a4eb2c7
JS
39
40 if not os.path.isfile(self.recipe_file):
faff2d5c 41 self.log.error("{} is not a file".format(self.recipe_file))
624e9083 42 raise RecipeExeption("{} is not a file".format(self.recipe_file))
3a4eb2c7
JS
43
44 try:
45 with open(self.recipe_file) as fobj:
46 self.raw_recipe = fobj.readlines()
47 except FileNotFoundError as error:
624e9083 48 self.log.error("No such file: {}".format(self.recipe_file))
61b44c10 49 raise error
3a4eb2c7
JS
50
51 @property
52 def recipe(self):
53 if not self._recipe:
54 self.parse()
55
56 return self._recipe
57
58 @property
59 def machines(self):
60 if not self._machines:
61 self._machines = []
62 for line in self._recipe:
63 if line[0] != "all" and line[0] not in self._machines:
64 self._machines.append(line[0])
65
66 return self._machines
67
68 def parse(self):
69 self._recipe = []
70 i = 1
71 for line in self.raw_recipe:
6cb7e939
JS
72 # Check if the line is empty
73 if line.strip() == "":
74 self.log.debug("Skipping empty line {}".format(i))
75 i = i + 1
76 continue
77
78 # Check if the line is a comment
79 if line.strip().startswith("#"):
80 self.log.debug("Skipping comment in line {}".format(i))
81 i = i + 1
82 continue
83
2e8d0473 84 raw_line = line.split(":", 1)
3a4eb2c7
JS
85 if len(raw_line) < 2:
86 self.log.error("Error parsing the recipe in line {}".format(i))
61b44c10 87 raise RecipeExeption("Error parsing the recipe in line {}".format(i))
3a4eb2c7
JS
88 cmd = raw_line[1].strip()
89 raw_line = raw_line[0].strip().split(" ")
90 if len(raw_line) == 0:
91 self.log.error("Failed to parse the recipe in line {}".format(i))
61b44c10 92 raise RecipeExeption("Failed to parse the recipe in line {}".format(i))
3a4eb2c7
JS
93
94 if raw_line[0].strip() == "":
95 self.log.error("Failed to parse the recipe in line {}".format(i))
61b44c10 96 raise RecipeExeption("Failed to parse the recipe in line {}".format(i))
3a4eb2c7
JS
97
98 machine = raw_line[0].strip()
99
100 if len(raw_line) == 2:
101 extra = raw_line[1].strip()
102 else:
103 extra = ""
104
105 # We could get a machine here or a include statement
106 if machine == "include":
107 path = cmd.strip()
108 path = os.path.normpath(self.path + "/" + path)
109 path = path + "/recipe"
110 if path in self.circle:
111 self.log.error("Detect import loop!")
61b44c10 112 raise RecipeExeption("Detect import loop!")
3a4eb2c7
JS
113 self.circle.append(path)
114 recipe_to_include = recipe(path, circle=self.circle)
115
116 if machine == "include":
117 self._recipe.extend(recipe_to_include.recipe)
118 else:
119 # Support also something like 'alice,bob: echo'
120 machines = machine.split(",")
121 for machine in machines:
122 self._recipe.append((machine.strip(), extra.strip(), cmd.strip()))
123 i = i + 1
124
125 if not self.in_recursion:
126 tmp_recipe = []
127 for line in self._recipe:
128 if line[0] != "all":
129 tmp_recipe.append(line)
130 else:
131 for machine in self.machines:
132 tmp_recipe.append((machine.strip(), line[1], line[2]))
133
134 self._recipe = tmp_recipe