Commit 0fe3a21c authored by Bryton Lacquement's avatar Bryton Lacquement 🚪 Committed by Julien Muchembled

default: new 'init' option

The purpose is to replace colletive.recipe.shelloutput

/reviewed-on nexedi/slapos.recipe.build!11
parent 008302f0
...@@ -4,12 +4,13 @@ ...@@ -4,12 +4,13 @@
.. contents:: .. contents::
Examples Default
-------- -------
Recipe to build the software. The default recipe can be used to execute ad-hoc Python code at both
init & install phases.
Example buildout:: Example buildout that builds software::
[buildout] [buildout]
parts = parts =
...@@ -83,6 +84,19 @@ For example:: ...@@ -83,6 +84,19 @@ For example::
print('%(foo)s') print('%(foo)s')
# will print “%(foo)s” # will print “%(foo)s”
Using the init option::
[section-one]
recipe = slapos.recipe.build
init =
import os
options['foo'] = os.uname()[4]
[section-two]
bar = ${section-one:foo}
It is also possible to use both `init` & `script` in the same section.
Pure download Pure download
~~~~~~~~~~~~~ ~~~~~~~~~~~~~
......
...@@ -268,18 +268,28 @@ class Script(EnvironMixin): ...@@ -268,18 +268,28 @@ class Script(EnvironMixin):
except KeyError: except KeyError:
self.options['location'] = os.path.join( self.options['location'] = os.path.join(
buildout['buildout']['parts-directory'], self.name) buildout['buildout']['parts-directory'], self.name)
self.script = self.options['script'] format = options.get('format', 'yes') in TRUE_LIST
init = options.get('init')
self.script = options.get('script')
if self.script: if self.script:
if self.options.get('format', 'yes') in TRUE_LIST: if format:
self.script = self.script % self.options self.script = self.script % self.options
elif not init:
raise zc.buildout.UserError('either init or script is required')
if self.options.get('keep-on-error', '').strip().lower() in TRUE_LIST: if self.options.get('keep-on-error', '').strip().lower() in TRUE_LIST:
self.logger.debug('Keeping directories in case of errors') self.logger.debug('Keeping directories in case of errors')
self.keep_on_error = True self.keep_on_error = True
else: else:
self.keep_on_error = False self.keep_on_error = False
EnvironMixin.__init__(self, False) EnvironMixin.__init__(self, False)
if init:
if format:
init = init % options
exec(init, {'self': self, 'options': options, 'env': self.environ})
def install(self): def install(self):
if not self.script:
return ""
location = self.options['location'] location = self.options['location']
if os.path.lexists(location): if os.path.lexists(location):
self.logger.warning('Removing already existing path %r', location) self.logger.warning('Removing already existing path %r', location)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment