Mercurial > piecrust2
comparison piecrust/main.py @ 341:bd726306d4dc
chef: Add a `--config-set` option to set ad-hoc site configuration settings.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Mon, 06 Apr 2015 20:19:45 -0700 |
parents | 869a206facd5 |
children | d8677ad748f0 |
comparison
equal
deleted
inserted
replaced
340:794b047c9726 | 341:bd726306d4dc |
---|---|
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 PieCrust, PieCrustConfiguration |
10 from piecrust.chefutil import (format_timed, log_friendly_exception, | 10 from piecrust.chefutil import ( |
11 print_help_item) | 11 format_timed, log_friendly_exception, print_help_item) |
12 from piecrust.commands.base import CommandContext | 12 from piecrust.commands.base import CommandContext |
13 from piecrust.environment import StandardEnvironment | |
14 from piecrust.pathutil import SiteNotFoundError, find_app_root | 13 from piecrust.pathutil import SiteNotFoundError, find_app_root |
15 from piecrust.plugins.base import PluginLoader | 14 from piecrust.plugins.base import PluginLoader |
16 | 15 |
17 | 16 |
18 logger = logging.getLogger(__name__) | 17 logger = logging.getLogger(__name__) |
74 sys.exit(exit_code) | 73 sys.exit(exit_code) |
75 | 74 |
76 | 75 |
77 class PreParsedChefArgs(object): | 76 class PreParsedChefArgs(object): |
78 def __init__(self, root=None, cache=True, debug=False, quiet=False, | 77 def __init__(self, root=None, cache=True, debug=False, quiet=False, |
79 log_file=None, log_debug=False, config_variant=None): | 78 log_file=None, log_debug=False, config_variant=None): |
80 self.root = root | 79 self.root = root |
81 self.cache = cache | 80 self.cache = cache |
82 self.debug = debug | 81 self.debug = debug |
83 self.quiet = quiet | 82 self.quiet = quiet |
84 self.log_file = log_file | 83 self.log_file = log_file |
85 self.log_debug = log_debug | 84 self.log_debug = log_debug |
86 self.config_variant = config_variant | 85 self.config_variant = config_variant |
86 self.config_values = [] | |
87 | |
88 | |
89 def _parse_config_value(arg): | |
90 try: | |
91 name, value = arg.split('=') | |
92 except Exception: | |
93 raise Exception("Invalid configuration name and value: %s" % arg) | |
94 return (name, value) | |
87 | 95 |
88 | 96 |
89 def _pre_parse_chef_args(argv): | 97 def _pre_parse_chef_args(argv): |
90 # We need to parse some arguments before we can build the actual argument | 98 # We need to parse some arguments before we can build the actual argument |
91 # parser, because it can affect which plugins will be loaded. Also, log- | 99 # parser, because it can affect which plugins will be loaded. Also, log- |
96 while i < len(argv): | 104 while i < len(argv): |
97 arg = argv[i] | 105 arg = argv[i] |
98 if arg.startswith('--root='): | 106 if arg.startswith('--root='): |
99 res.root = os.path.expanduser(arg[len('--root='):]) | 107 res.root = os.path.expanduser(arg[len('--root='):]) |
100 elif arg == '--root': | 108 elif arg == '--root': |
101 res.root = argv[i + 1] | 109 res.root = os.path.expanduser(argv[i + 1]) |
102 ++i | 110 ++i |
103 elif arg.startswith('--config='): | 111 elif arg.startswith('--config='): |
104 res.config_variant = arg[len('--config='):] | 112 res.config_variant = arg[len('--config='):] |
105 elif arg == '--config': | 113 elif arg == '--config': |
106 res.config_variant = argv[i + 1] | 114 res.config_variant = argv[i + 1] |
115 ++i | |
116 elif arg.startswith('--config-set='): | |
117 res.config_values.append( | |
118 _parse_config_value(arg[len('--config-set='):])) | |
119 elif arg == '--config-set': | |
120 res.config_values.append(_parse_config_value(argv[i + 1])) | |
107 ++i | 121 ++i |
108 elif arg == '--log': | 122 elif arg == '--log': |
109 res.log_file = argv[i + 1] | 123 res.log_file = argv[i + 1] |
110 ++i | 124 ++i |
111 elif arg == '--log-debug': | 125 elif arg == '--log-debug': |
171 # Handle a configuration variant. | 185 # Handle a configuration variant. |
172 if pre_args.config_variant is not None: | 186 if pre_args.config_variant is not None: |
173 if not root: | 187 if not root: |
174 raise SiteNotFoundError("Can't apply any variant.") | 188 raise SiteNotFoundError("Can't apply any variant.") |
175 app.config.applyVariant('variants/' + pre_args.config_variant) | 189 app.config.applyVariant('variants/' + pre_args.config_variant) |
190 for name, value in pre_args.config_values: | |
191 logger.debug("Setting configuration '%s' to: %s" % (name, value)) | |
192 app.config.set(name, value) | |
176 | 193 |
177 # Setup the arg parser. | 194 # Setup the arg parser. |
178 parser = argparse.ArgumentParser( | 195 parser = argparse.ArgumentParser( |
179 prog='chef', | 196 prog='chef', |
180 description="The PieCrust chef manages your website.", | 197 description="The PieCrust chef manages your website.", |
181 formatter_class=argparse.RawDescriptionHelpFormatter) | 198 formatter_class=argparse.RawDescriptionHelpFormatter) |
182 parser.add_argument('--version', action='version', version=('%(prog)s ' + APP_VERSION)) | 199 parser.add_argument( |
183 parser.add_argument('--root', help="The root directory of the website.") | 200 '--version', |
184 parser.add_argument('--config', help="The configuration variant to use for this command.") | 201 action='version', |
185 parser.add_argument('--debug', help="Show debug information.", action='store_true') | 202 version=('%(prog)s ' + APP_VERSION)) |
186 parser.add_argument('--no-cache', help="When applicable, disable caching.", action='store_true') | 203 parser.add_argument( |
187 parser.add_argument('--quiet', help="Print only important information.", action='store_true') | 204 '--root', |
188 parser.add_argument('--log', help="Send log messages to the specified file.") | 205 help="The root directory of the website.") |
189 parser.add_argument('--log-debug', help="Log debug messages to the log file.", action='store_true') | 206 parser.add_argument( |
207 '--config', | |
208 help="The configuration variant to use for this command.") | |
209 parser.add_argument( | |
210 '--config-set', | |
211 help="Sets a specific site configuration setting.") | |
212 parser.add_argument( | |
213 '--debug', | |
214 help="Show debug information.", action='store_true') | |
215 parser.add_argument( | |
216 '--no-cache', | |
217 help="When applicable, disable caching.", | |
218 action='store_true') | |
219 parser.add_argument( | |
220 '--quiet', | |
221 help="Print only important information.", | |
222 action='store_true') | |
223 parser.add_argument( | |
224 '--log', | |
225 help="Send log messages to the specified file.") | |
226 parser.add_argument( | |
227 '--log-debug', | |
228 help="Log debug messages to the log file.", | |
229 action='store_true') | |
190 | 230 |
191 commands = sorted(app.plugin_loader.getCommands(), | 231 commands = sorted(app.plugin_loader.getCommands(), |
192 key=lambda c: c.name) | 232 key=lambda c: c.name) |
193 subparsers = parser.add_subparsers(title='list of commands') | 233 subparsers = parser.add_subparsers(title='list of commands') |
194 for c in commands: | 234 for c in commands: |
195 p = subparsers.add_parser(c.name, help=c.description) | 235 p = subparsers.add_parser(c.name, help=c.description) |
196 c.setupParser(p, app) | 236 c.setupParser(p, app) |
197 p.set_defaults(func=c.checkedRun) | 237 p.set_defaults(func=c.checkedRun) |
202 epilog.write("additional help topics:\n") | 242 epilog.write("additional help topics:\n") |
203 for name, desc in help_cmd.getTopics(): | 243 for name, desc in help_cmd.getTopics(): |
204 print_help_item(epilog, name, desc) | 244 print_help_item(epilog, name, desc) |
205 parser.epilog = epilog.getvalue() | 245 parser.epilog = epilog.getvalue() |
206 | 246 |
207 | |
208 # Parse the command line. | 247 # Parse the command line. |
209 result = parser.parse_args() | 248 result = parser.parse_args() |
210 logger.debug(format_timed(start_time, 'initialized PieCrust', colored=False)) | 249 logger.debug(format_timed(start_time, 'initialized PieCrust', |
250 colored=False)) | |
211 | 251 |
212 # Print the help if no command was specified. | 252 # Print the help if no command was specified. |
213 if not hasattr(result, 'func'): | 253 if not hasattr(result, 'func'): |
214 parser.print_help() | 254 parser.print_help() |
215 return 0 | 255 return 0 |