comparison piecrust/main.py @ 466:456db44dcc53

bake: Pass the config variants and values from the CLI to the baker. TODO: add support for that for the processor pipeline.
author Ludovic Chabant <ludovic@chabant.com>
date Sat, 11 Jul 2015 23:51:02 -0700
parents d241585412ad
children a8028bf329a2
comparison
equal deleted inserted replaced
465:b6e797463798 466:456db44dcc53
4 import os.path 4 import os.path
5 import logging 5 import logging
6 import argparse 6 import argparse
7 import colorama 7 import colorama
8 from piecrust import APP_VERSION 8 from piecrust import APP_VERSION
9 from piecrust.app import PieCrust, PieCrustConfiguration 9 from piecrust.app import (
10 PieCrust, PieCrustConfiguration, apply_variant_and_values)
10 from piecrust.chefutil import ( 11 from piecrust.chefutil import (
11 format_timed, log_friendly_exception, print_help_item) 12 format_timed, log_friendly_exception, print_help_item)
12 from piecrust.commands.base import CommandContext 13 from piecrust.commands.base import CommandContext
13 from piecrust.pathutil import SiteNotFoundError, find_app_root 14 from piecrust.pathutil import SiteNotFoundError, find_app_root
14 from piecrust.plugins.base import PluginLoader 15 from piecrust.plugins.base import PluginLoader
182 app = PieCrust(root, cache=pre_args.cache, debug=pre_args.debug) 183 app = PieCrust(root, cache=pre_args.cache, debug=pre_args.debug)
183 184
184 # Build a hash for a custom cache directory. 185 # Build a hash for a custom cache directory.
185 cache_key = 'default' 186 cache_key = 'default'
186 187
187 # Handle a configuration variant. 188 # Handle custom configurations.
189 if pre_args.config_variant is not None and not root:
190 raise SiteNotFoundError("Can't apply any variant.")
191 apply_variant_and_values(pre_args.config_variant,
192 pre_args.config_values)
193
194 # Adjust the cache key.
188 if pre_args.config_variant is not None: 195 if pre_args.config_variant is not None:
189 if not root:
190 raise SiteNotFoundError("Can't apply any variant.")
191 app.config.applyVariant('variants/' + pre_args.config_variant)
192 cache_key += ',variant=%s' % pre_args.config_variant 196 cache_key += ',variant=%s' % pre_args.config_variant
193 for name, value in pre_args.config_values: 197 for name, value in pre_args.config_values:
194 logger.debug("Setting configuration '%s' to: %s" % (name, value))
195 app.config.set(name, value)
196 cache_key += ',%s=%s' % (name, value) 198 cache_key += ',%s=%s' % (name, value)
197 199
198 # Setup the arg parser. 200 # Setup the arg parser.
199 parser = argparse.ArgumentParser( 201 parser = argparse.ArgumentParser(
200 prog='chef', 202 prog='chef',
263 if result.cache_name != 'default' or cache_key != 'default': 265 if result.cache_name != 'default' or cache_key != 'default':
264 app.useSubCache(result.cache_name, cache_key) 266 app.useSubCache(result.cache_name, cache_key)
265 267
266 # Run the command! 268 # Run the command!
267 ctx = CommandContext(app, parser, result) 269 ctx = CommandContext(app, parser, result)
270 ctx.config_variant = pre_args.config_variant
271 ctx.config_values = pre_args.config_values
272
268 exit_code = result.func(ctx) 273 exit_code = result.func(ctx)
269 if exit_code is None: 274 if exit_code is None:
270 return 0 275 return 0
271 if not isinstance(exit_code, int): 276 if not isinstance(exit_code, int):
272 logger.error("Got non-integer exit code: %s" % exit_code) 277 logger.error("Got non-integer exit code: %s" % exit_code)