zero-knowledge: rename recipe
[slapos.git] / slapos / recipe / zero_knowledge.py
1 ##############################################################################
2 #
3 # Copyright (c) 2013 Vifib SARL and Contributors. All Rights Reserved.
4 #
5 # WARNING: This program as such is intended to be used by professional
6 # programmers who take the whole responsibility of assessing all potential
7 # consequences resulting from its eventual inadequacies and bugs
8 # End users who are looking for a ready-to-use solution with commercial
9 # guarantees and support are strongly adviced to contract a Free Software
10 # Service Company
11 #
12 # This program is Free Software; you can redistribute it and/or
13 # modify it under the terms of the GNU General Public License
14 # as published by the Free Software Foundation; either version 3
15 # of the License, or (at your option) any later version.
16 #
17 # This program is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 # GNU General Public License for more details.
21 #
22 # You should have received a copy of the GNU General Public License
23 # along with this program; if not, write to the Free Software
24 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 #
26 ##############################################################################
27
28 import ConfigParser
29 import os
30 import zc.buildout
31
32 from slapos.recipe.librecipe import GenericBaseRecipe
33
34
35 class WriteRecipe(GenericBaseRecipe):
36 """
37 """
38 def __init__(self, buildout, name, options):
39 if not "filename" in options:
40 raise zc.buildout.UserError("You have to provide the parameter \"filename\"")
41
42 self.filename = options['filename'].strip()
43 self.path = os.path.join(buildout['buildout']['directory'], self.filename)
44 self.name = name
45 self.options = options.copy()
46 del self.options['filename']
47 del self.options['recipe']
48
49 # Set up the parser, and write config file if needed
50 self.parser = ConfigParser.ConfigParser()
51 try:
52 self.parser.read(self.path)
53 #clean_options(options)
54 for key in self.options:
55 if key not in self.parser.options(self.name):
56 self.parser.set(self.name, key, self.options[key])
57 with open(self.path, 'w') as file:
58 self.parser.write(file)
59 # If the file or section do not exist
60 except (ConfigParser.NoSectionError, IOError) as e:
61 self.full_install()
62
63 install = update = lambda self: []
64
65 def full_install(self):
66 """XXX-Nicolas : when some parameter's value is changed in
67 buildout profile, this will override custom user defined values"""
68 self.parser.read(self.path)
69 if self.parser.has_section(self.name):
70 self.parser.remove_section(self.name)
71 self.parser.add_section(self.name)
72 for key in self.options:
73 self.parser.set(self.name, key, self.options[key])
74 with open(self.path, 'w') as file:
75 self.parser.write(file)
76
77
78 class ReadRecipe(GenericBaseRecipe):
79 """
80 """
81 def __init__(self, buildout, name, options):
82 if not "filename" in options:
83 raise zc.buildout.UserError("You have to provide the parameter \"filename\"")
84
85 self.filename = options['filename'].strip()
86 self.path = os.path.join(buildout['buildout']['directory'], self.filename)
87
88 # Set up the parser, and write config file if needed
89 self.parser = ConfigParser.ConfigParser()
90 if os.path.exists(self.path):
91 self.parser.read(self.path)
92 for section in self.parser.sections():
93 for key ,value in self.parser.items(section):
94 options[key] = value
95
96 install = update = lambda self: []