]>
git.ipfire.org Git - people/ms/nitsi.git/blob - src/nitsi/recipe.py
7 logger
= logging
.getLogger("nitsi.recipe")
11 class RecipeExeption(Exception):
12 def __init__(self
, message
):
13 self
.message
= message
17 # Should read the test, check if the syntax are valid
18 # and return tuples with the ( host, command ) structure
20 def __init__(self
, path
, circle
=[]):
21 self
.recipe_file
= path
23 self
.path
= os
.path
.dirname(self
.recipe_file
)
24 self
.name
= os
.path
.basename(self
.path
)
25 except BaseException
as e
:
26 logger
.error("Failed to get the name of the test to this recipe")
29 self
.log
= logger
.getChild(self
.name
)
30 self
.log
.debug("Path of recipe is: {}".format(self
.recipe_file
))
34 self
.in_recursion
= True
36 self
.in_recursion
= False
39 self
.log
.debug("Recipes we have already included: {}".format(self
.circle
))
41 if not os
.path
.isfile(self
.recipe_file
):
42 self
.log
.error("{} is not a file".format(self
.recipe_file
))
43 raise RecipeExeption("{} is not a file".format(self
.recipe_file
)())
46 with
open(self
.recipe_file
) as fobj
:
47 self
.raw_recipe
= fobj
.readlines()
48 except FileNotFoundError
as error
:
49 self
.log
.error("No such file: {}".format(vm_xml_file
))
61 if not self
._machines
:
63 for line
in self
._recipe
:
64 if line
[0] != "all" and line
[0] not in self
._machines
:
65 self
._machines
.append(line
[0])
72 for line
in self
.raw_recipe
:
73 raw_line
= line
.split(":", 1)
75 self
.log
.error("Error parsing the recipe in line {}".format(i
))
76 raise RecipeExeption("Error parsing the recipe in line {}".format(i
))
77 cmd
= raw_line
[1].strip()
78 raw_line
= raw_line
[0].strip().split(" ")
79 if len(raw_line
) == 0:
80 self
.log
.error("Failed to parse the recipe in line {}".format(i
))
81 raise RecipeExeption("Failed to parse the recipe in line {}".format(i
))
83 if raw_line
[0].strip() == "":
84 self
.log
.error("Failed to parse the recipe in line {}".format(i
))
85 raise RecipeExeption("Failed to parse the recipe in line {}".format(i
))
87 machine
= raw_line
[0].strip()
89 if len(raw_line
) == 2:
90 extra
= raw_line
[1].strip()
94 # We could get a machine here or a include statement
95 if machine
== "include":
97 path
= os
.path
.normpath(self
.path
+ "/" + path
)
98 path
= path
+ "/recipe"
99 if path
in self
.circle
:
100 self
.log
.error("Detect import loop!")
101 raise RecipeExeption("Detect import loop!")
102 self
.circle
.append(path
)
103 recipe_to_include
= recipe(path
, circle
=self
.circle
)
105 if machine
== "include":
106 self
._recipe
.extend(recipe_to_include
.recipe
)
108 # Support also something like 'alice,bob: echo'
109 machines
= machine
.split(",")
110 for machine
in machines
:
111 self
._recipe
.append((machine
.strip(), extra
.strip(), cmd
.strip()))
114 if not self
.in_recursion
:
116 for line
in self
._recipe
:
118 tmp_recipe
.append(line
)
120 for machine
in self
.machines
:
121 tmp_recipe
.append((machine
.strip(), line
[1], line
[2]))
123 self
._recipe
= tmp_recipe