]>
git.ipfire.org Git - nitsi.git/blob - recipe.py
7 class RecipeExeption(Exception):
12 # Should read the test, check if the syntax are valid
13 # and return tuples with the ( host, command ) structure
15 def __init__(self
, path
, circle
=[]):
17 self
.recipe_file
= path
18 self
.path
= os
.path
.dirname(self
.recipe_file
)
19 self
.log
.debug("Path of recipe is: {}".format(self
.recipe_file
))
23 self
.in_recursion
= True
25 self
.in_recursion
= False
28 self
.log
.debug(circle
)
29 self
.log
.debug(self
.circle
)
31 if not os
.path
.isfile(self
.recipe_file
):
32 self
.log
.error("No such file: {}".format(self
.recipe_file
))
35 with
open(self
.recipe_file
) as fobj
:
36 self
.raw_recipe
= fobj
.readlines()
37 except FileNotFoundError
as error
:
38 self
.log
.error("No such file: {}".format(vm_xml_file
))
49 if not self
._machines
:
51 for line
in self
._recipe
:
52 if line
[0] != "all" and line
[0] not in self
._machines
:
53 self
._machines
.append(line
[0])
60 for line
in self
.raw_recipe
:
61 raw_line
= line
.split(":")
63 self
.log
.error("Error parsing the recipe in line {}".format(i
))
65 cmd
= raw_line
[1].strip()
66 raw_line
= raw_line
[0].strip().split(" ")
67 if len(raw_line
) == 0:
68 self
.log
.error("Failed to parse the recipe in line {}".format(i
))
71 if raw_line
[0].strip() == "":
72 self
.log
.error("Failed to parse the recipe in line {}".format(i
))
75 machine
= raw_line
[0].strip()
77 if len(raw_line
) == 2:
78 extra
= raw_line
[1].strip()
82 # We could get a machine here or a include statement
83 if machine
== "include":
85 path
= os
.path
.normpath(self
.path
+ "/" + path
)
86 path
= path
+ "/recipe"
87 if path
in self
.circle
:
88 self
.log
.error("Detect import loop!")
90 self
.circle
.append(path
)
91 recipe_to_include
= recipe(path
, circle
=self
.circle
)
93 if machine
== "include":
94 self
._recipe
.extend(recipe_to_include
.recipe
)
96 # Support also something like 'alice,bob: echo'
97 machines
= machine
.split(",")
98 for machine
in machines
:
99 self
._recipe
.append((machine
.strip(), extra
.strip(), cmd
.strip()))
102 if not self
.in_recursion
:
104 for line
in self
._recipe
:
106 tmp_recipe
.append(line
)
108 for machine
in self
.machines
:
109 tmp_recipe
.append((machine
.strip(), line
[1], line
[2]))
111 self
._recipe
= tmp_recipe