annotate piecrust/main.py @ 415:0e9a94b7fdfa

bake: Improve bake record information. * Store things in the bake record that require less interaction between the master process and the workers. For instance, don't store the paginator object in the render pass info -- instead, just store whether pagination was used, and whether it had more items. * Simplify information passing between workers and bake passes by saving the rendering info to the JSON cache. This means the "render first sub" job doesn't have to return anything except errors now. * Add more performance counter info.
author Ludovic Chabant <ludovic@chabant.com>
date Sat, 20 Jun 2015 19:23:16 -0700
parents d241585412ad
children 456db44dcc53
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
99
8703be118430 Changes to `help` command and extendable commands:
Ludovic Chabant <ludovic@chabant.com>
parents: 98
diff changeset
1 import io
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2 import sys
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
3 import time
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
4 import os.path
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
5 import logging
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
6 import argparse
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
7 import colorama
69
cb1ed436642c Always use version generated by `setup.py`. Better version generation.
Ludovic Chabant <ludovic@chabant.com>
parents: 61
diff changeset
8 from piecrust import APP_VERSION
cb1ed436642c Always use version generated by `setup.py`. Better version generation.
Ludovic Chabant <ludovic@chabant.com>
parents: 61
diff changeset
9 from piecrust.app import PieCrust, PieCrustConfiguration
341
bd726306d4dc chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents: 307
diff changeset
10 from piecrust.chefutil import (
bd726306d4dc chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents: 307
diff changeset
11 format_timed, log_friendly_exception, print_help_item)
57
c8c522dacfea Add `help` function, cleanup argument handling.
Ludovic Chabant <ludovic@chabant.com>
parents: 44
diff changeset
12 from piecrust.commands.base import CommandContext
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
13 from piecrust.pathutil import SiteNotFoundError, find_app_root
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
14 from piecrust.plugins.base import PluginLoader
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
15
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
16
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
17 logger = logging.getLogger(__name__)
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
18
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
19
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
20 class ColoredFormatter(logging.Formatter):
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
21 COLORS = {
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
22 'DEBUG': colorama.Fore.BLACK + colorama.Style.BRIGHT,
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
23 'INFO': '',
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
24 'WARNING': colorama.Fore.YELLOW,
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
25 'ERROR': colorama.Fore.RED,
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
26 'CRITICAL': colorama.Back.RED + colorama.Fore.WHITE
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
27 }
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
28
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
29 def __init__(self, fmt=None, datefmt=None):
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
30 super(ColoredFormatter, self).__init__(fmt, datefmt)
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
31
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
32 def format(self, record):
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
33 color = self.COLORS.get(record.levelname)
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
34 res = super(ColoredFormatter, self).format(record)
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
35 if color:
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
36 res = color + res + colorama.Style.RESET_ALL
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
37 return res
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
38
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
39
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
40 class NullPieCrust:
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
41 def __init__(self):
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
42 self.root_dir = None
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
43 self.debug = False
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
44 self.templates_dirs = []
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
45 self.theme_dir = None
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
46 self.cache_dir = None
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
47 self.config = PieCrustConfiguration()
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
48 self.plugin_loader = PluginLoader(self)
103
028df35a690e Fix using `chef` outside of a website.
Ludovic Chabant <ludovic@chabant.com>
parents: 99
diff changeset
49 self.env = None
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
50
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
51
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
52 def main():
167
0a1736ef740d chef: Work around a bug in MacOSX where the default locale doesn't work.
Ludovic Chabant <ludovic@chabant.com>
parents: 103
diff changeset
53 if sys.platform == 'darwin':
0a1736ef740d chef: Work around a bug in MacOSX where the default locale doesn't work.
Ludovic Chabant <ludovic@chabant.com>
parents: 103
diff changeset
54 # There's a bug on MacOSX that can cause Python to be confused
0a1736ef740d chef: Work around a bug in MacOSX where the default locale doesn't work.
Ludovic Chabant <ludovic@chabant.com>
parents: 103
diff changeset
55 # about the locale. Let's try to fix that.
0a1736ef740d chef: Work around a bug in MacOSX where the default locale doesn't work.
Ludovic Chabant <ludovic@chabant.com>
parents: 103
diff changeset
56 # See: http://bugs.python.org/issue18378
0a1736ef740d chef: Work around a bug in MacOSX where the default locale doesn't work.
Ludovic Chabant <ludovic@chabant.com>
parents: 103
diff changeset
57 import locale
0a1736ef740d chef: Work around a bug in MacOSX where the default locale doesn't work.
Ludovic Chabant <ludovic@chabant.com>
parents: 103
diff changeset
58 try:
0a1736ef740d chef: Work around a bug in MacOSX where the default locale doesn't work.
Ludovic Chabant <ludovic@chabant.com>
parents: 103
diff changeset
59 locale.getdefaultlocale()
0a1736ef740d chef: Work around a bug in MacOSX where the default locale doesn't work.
Ludovic Chabant <ludovic@chabant.com>
parents: 103
diff changeset
60 except ValueError:
0a1736ef740d chef: Work around a bug in MacOSX where the default locale doesn't work.
Ludovic Chabant <ludovic@chabant.com>
parents: 103
diff changeset
61 locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
0a1736ef740d chef: Work around a bug in MacOSX where the default locale doesn't work.
Ludovic Chabant <ludovic@chabant.com>
parents: 103
diff changeset
62
384
d241585412ad internal: Make it possible to pass `argv` to the main Chef function.
Ludovic Chabant <ludovic@chabant.com>
parents: 371
diff changeset
63 argv = sys.argv[1:]
38
091f99bfbe44 Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
64 pre_args = _pre_parse_chef_args(argv)
091f99bfbe44 Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
65 try:
384
d241585412ad internal: Make it possible to pass `argv` to the main Chef function.
Ludovic Chabant <ludovic@chabant.com>
parents: 371
diff changeset
66 exit_code = _run_chef(pre_args, argv)
38
091f99bfbe44 Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
67 except Exception as ex:
091f99bfbe44 Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
68 if pre_args.debug:
091f99bfbe44 Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
69 logger.exception(ex)
091f99bfbe44 Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
70 else:
39
2f717f961996 Better error reporting and cache validation.
Ludovic Chabant <ludovic@chabant.com>
parents: 38
diff changeset
71 log_friendly_exception(logger, ex)
98
5959a117a943 Exit with the proper code.
Ludovic Chabant <ludovic@chabant.com>
parents: 97
diff changeset
72 exit_code = 1
5959a117a943 Exit with the proper code.
Ludovic Chabant <ludovic@chabant.com>
parents: 97
diff changeset
73 sys.exit(exit_code)
38
091f99bfbe44 Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
74
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
75
38
091f99bfbe44 Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
76 class PreParsedChefArgs(object):
091f99bfbe44 Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
77 def __init__(self, root=None, cache=True, debug=False, quiet=False,
341
bd726306d4dc chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents: 307
diff changeset
78 log_file=None, log_debug=False, config_variant=None):
38
091f99bfbe44 Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
79 self.root = root
091f99bfbe44 Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
80 self.cache = cache
091f99bfbe44 Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
81 self.debug = debug
091f99bfbe44 Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
82 self.quiet = quiet
091f99bfbe44 Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
83 self.log_file = log_file
97
00a9b24ca944 Add `--log-debug` option.
Ludovic Chabant <ludovic@chabant.com>
parents: 69
diff changeset
84 self.log_debug = log_debug
38
091f99bfbe44 Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
85 self.config_variant = config_variant
341
bd726306d4dc chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents: 307
diff changeset
86 self.config_values = []
bd726306d4dc chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents: 307
diff changeset
87
bd726306d4dc chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents: 307
diff changeset
88
bd726306d4dc chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents: 307
diff changeset
89 def _parse_config_value(arg):
bd726306d4dc chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents: 307
diff changeset
90 try:
bd726306d4dc chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents: 307
diff changeset
91 name, value = arg.split('=')
bd726306d4dc chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents: 307
diff changeset
92 except Exception:
bd726306d4dc chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents: 307
diff changeset
93 raise Exception("Invalid configuration name and value: %s" % arg)
bd726306d4dc chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents: 307
diff changeset
94 return (name, value)
38
091f99bfbe44 Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
95
091f99bfbe44 Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
96
091f99bfbe44 Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
97 def _pre_parse_chef_args(argv):
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
98 # We need to parse some arguments before we can build the actual argument
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
99 # parser, because it can affect which plugins will be loaded. Also, log-
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
100 # related arguments must be parsed first because we want to log everything
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
101 # from the beginning.
38
091f99bfbe44 Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
102 res = PreParsedChefArgs()
384
d241585412ad internal: Make it possible to pass `argv` to the main Chef function.
Ludovic Chabant <ludovic@chabant.com>
parents: 371
diff changeset
103 i = 0
38
091f99bfbe44 Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
104 while i < len(argv):
091f99bfbe44 Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
105 arg = argv[i]
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
106 if arg.startswith('--root='):
38
091f99bfbe44 Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
107 res.root = os.path.expanduser(arg[len('--root='):])
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
108 elif arg == '--root':
341
bd726306d4dc chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents: 307
diff changeset
109 res.root = os.path.expanduser(argv[i + 1])
342
d8677ad748f0 chef: Fix pre-parsing.
Ludovic Chabant <ludovic@chabant.com>
parents: 341
diff changeset
110 i += 1
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
111 elif arg.startswith('--config='):
38
091f99bfbe44 Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
112 res.config_variant = arg[len('--config='):]
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
113 elif arg == '--config':
38
091f99bfbe44 Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
114 res.config_variant = argv[i + 1]
342
d8677ad748f0 chef: Fix pre-parsing.
Ludovic Chabant <ludovic@chabant.com>
parents: 341
diff changeset
115 i += 1
341
bd726306d4dc chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents: 307
diff changeset
116 elif arg.startswith('--config-set='):
bd726306d4dc chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents: 307
diff changeset
117 res.config_values.append(
bd726306d4dc chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents: 307
diff changeset
118 _parse_config_value(arg[len('--config-set='):]))
bd726306d4dc chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents: 307
diff changeset
119 elif arg == '--config-set':
bd726306d4dc chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents: 307
diff changeset
120 res.config_values.append(_parse_config_value(argv[i + 1]))
342
d8677ad748f0 chef: Fix pre-parsing.
Ludovic Chabant <ludovic@chabant.com>
parents: 341
diff changeset
121 i += 1
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
122 elif arg == '--log':
38
091f99bfbe44 Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
123 res.log_file = argv[i + 1]
342
d8677ad748f0 chef: Fix pre-parsing.
Ludovic Chabant <ludovic@chabant.com>
parents: 341
diff changeset
124 i += 1
97
00a9b24ca944 Add `--log-debug` option.
Ludovic Chabant <ludovic@chabant.com>
parents: 69
diff changeset
125 elif arg == '--log-debug':
00a9b24ca944 Add `--log-debug` option.
Ludovic Chabant <ludovic@chabant.com>
parents: 69
diff changeset
126 res.log_debug = True
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
127 elif arg == '--no-cache':
38
091f99bfbe44 Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
128 res.cache = False
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
129 elif arg == '--debug':
38
091f99bfbe44 Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
130 res.debug = True
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
131 elif arg == '--quiet':
38
091f99bfbe44 Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
132 res.quiet = True
342
d8677ad748f0 chef: Fix pre-parsing.
Ludovic Chabant <ludovic@chabant.com>
parents: 341
diff changeset
133 else:
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
134 break
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
135
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
136 i = i + 1
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
137
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
138 # Setup the logger.
38
091f99bfbe44 Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
139 if res.debug and res.quiet:
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
140 raise Exception("You can't specify both --debug and --quiet.")
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
141
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
142 colorama.init()
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
143 root_logger = logging.getLogger()
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
144 root_logger.setLevel(logging.INFO)
97
00a9b24ca944 Add `--log-debug` option.
Ludovic Chabant <ludovic@chabant.com>
parents: 69
diff changeset
145 if res.debug or res.log_debug:
00a9b24ca944 Add `--log-debug` option.
Ludovic Chabant <ludovic@chabant.com>
parents: 69
diff changeset
146 root_logger.setLevel(logging.DEBUG)
00a9b24ca944 Add `--log-debug` option.
Ludovic Chabant <ludovic@chabant.com>
parents: 69
diff changeset
147
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
148 log_handler = logging.StreamHandler(sys.stdout)
38
091f99bfbe44 Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
149 if res.debug:
97
00a9b24ca944 Add `--log-debug` option.
Ludovic Chabant <ludovic@chabant.com>
parents: 69
diff changeset
150 log_handler.setLevel(logging.DEBUG)
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
151 log_handler.setFormatter(ColoredFormatter("[%(name)s] %(message)s"))
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
152 else:
38
091f99bfbe44 Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
153 if res.quiet:
97
00a9b24ca944 Add `--log-debug` option.
Ludovic Chabant <ludovic@chabant.com>
parents: 69
diff changeset
154 log_handler.setLevel(logging.WARNING)
00a9b24ca944 Add `--log-debug` option.
Ludovic Chabant <ludovic@chabant.com>
parents: 69
diff changeset
155 else:
00a9b24ca944 Add `--log-debug` option.
Ludovic Chabant <ludovic@chabant.com>
parents: 69
diff changeset
156 log_handler.setLevel(logging.INFO)
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
157 log_handler.setFormatter(ColoredFormatter("%(message)s"))
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
158 root_logger.addHandler(log_handler)
97
00a9b24ca944 Add `--log-debug` option.
Ludovic Chabant <ludovic@chabant.com>
parents: 69
diff changeset
159
38
091f99bfbe44 Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
160 if res.log_file:
97
00a9b24ca944 Add `--log-debug` option.
Ludovic Chabant <ludovic@chabant.com>
parents: 69
diff changeset
161 file_handler = logging.FileHandler(res.log_file, mode='w')
00a9b24ca944 Add `--log-debug` option.
Ludovic Chabant <ludovic@chabant.com>
parents: 69
diff changeset
162 root_logger.addHandler(file_handler)
00a9b24ca944 Add `--log-debug` option.
Ludovic Chabant <ludovic@chabant.com>
parents: 69
diff changeset
163 if res.log_debug:
00a9b24ca944 Add `--log-debug` option.
Ludovic Chabant <ludovic@chabant.com>
parents: 69
diff changeset
164 file_handler.setLevel(logging.DEBUG)
38
091f99bfbe44 Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
165
091f99bfbe44 Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
166 return res
091f99bfbe44 Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
167
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
168
384
d241585412ad internal: Make it possible to pass `argv` to the main Chef function.
Ludovic Chabant <ludovic@chabant.com>
parents: 371
diff changeset
169 def _run_chef(pre_args, argv):
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
170 # Setup the app.
38
091f99bfbe44 Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
171 start_time = time.clock()
091f99bfbe44 Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
172 root = pre_args.root
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
173 if root is None:
38
091f99bfbe44 Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
174 try:
091f99bfbe44 Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
175 root = find_app_root()
091f99bfbe44 Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
176 except SiteNotFoundError:
091f99bfbe44 Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
177 root = None
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
178
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
179 if not root:
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
180 app = NullPieCrust()
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
181 else:
44
a62452ab5080 Correctly set the `debug` flag on the app.
Ludovic Chabant <ludovic@chabant.com>
parents: 39
diff changeset
182 app = PieCrust(root, cache=pre_args.cache, debug=pre_args.debug)
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
183
371
c2ca72fb7f0b caching: Use separate caches for config variants and other contexts.
Ludovic Chabant <ludovic@chabant.com>
parents: 342
diff changeset
184 # Build a hash for a custom cache directory.
c2ca72fb7f0b caching: Use separate caches for config variants and other contexts.
Ludovic Chabant <ludovic@chabant.com>
parents: 342
diff changeset
185 cache_key = 'default'
c2ca72fb7f0b caching: Use separate caches for config variants and other contexts.
Ludovic Chabant <ludovic@chabant.com>
parents: 342
diff changeset
186
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
187 # Handle a configuration variant.
38
091f99bfbe44 Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
188 if pre_args.config_variant is not None:
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
189 if not root:
38
091f99bfbe44 Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
190 raise SiteNotFoundError("Can't apply any variant.")
091f99bfbe44 Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
191 app.config.applyVariant('variants/' + pre_args.config_variant)
371
c2ca72fb7f0b caching: Use separate caches for config variants and other contexts.
Ludovic Chabant <ludovic@chabant.com>
parents: 342
diff changeset
192 cache_key += ',variant=%s' % pre_args.config_variant
341
bd726306d4dc chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents: 307
diff changeset
193 for name, value in pre_args.config_values:
bd726306d4dc chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents: 307
diff changeset
194 logger.debug("Setting configuration '%s' to: %s" % (name, value))
bd726306d4dc chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents: 307
diff changeset
195 app.config.set(name, value)
371
c2ca72fb7f0b caching: Use separate caches for config variants and other contexts.
Ludovic Chabant <ludovic@chabant.com>
parents: 342
diff changeset
196 cache_key += ',%s=%s' % (name, value)
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
197
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
198 # Setup the arg parser.
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
199 parser = argparse.ArgumentParser(
57
c8c522dacfea Add `help` function, cleanup argument handling.
Ludovic Chabant <ludovic@chabant.com>
parents: 44
diff changeset
200 prog='chef',
99
8703be118430 Changes to `help` command and extendable commands:
Ludovic Chabant <ludovic@chabant.com>
parents: 98
diff changeset
201 description="The PieCrust chef manages your website.",
8703be118430 Changes to `help` command and extendable commands:
Ludovic Chabant <ludovic@chabant.com>
parents: 98
diff changeset
202 formatter_class=argparse.RawDescriptionHelpFormatter)
341
bd726306d4dc chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents: 307
diff changeset
203 parser.add_argument(
bd726306d4dc chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents: 307
diff changeset
204 '--version',
bd726306d4dc chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents: 307
diff changeset
205 action='version',
bd726306d4dc chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents: 307
diff changeset
206 version=('%(prog)s ' + APP_VERSION))
bd726306d4dc chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents: 307
diff changeset
207 parser.add_argument(
bd726306d4dc chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents: 307
diff changeset
208 '--root',
bd726306d4dc chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents: 307
diff changeset
209 help="The root directory of the website.")
bd726306d4dc chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents: 307
diff changeset
210 parser.add_argument(
bd726306d4dc chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents: 307
diff changeset
211 '--config',
bd726306d4dc chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents: 307
diff changeset
212 help="The configuration variant to use for this command.")
bd726306d4dc chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents: 307
diff changeset
213 parser.add_argument(
bd726306d4dc chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents: 307
diff changeset
214 '--config-set',
bd726306d4dc chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents: 307
diff changeset
215 help="Sets a specific site configuration setting.")
bd726306d4dc chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents: 307
diff changeset
216 parser.add_argument(
bd726306d4dc chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents: 307
diff changeset
217 '--debug',
bd726306d4dc chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents: 307
diff changeset
218 help="Show debug information.", action='store_true')
bd726306d4dc chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents: 307
diff changeset
219 parser.add_argument(
bd726306d4dc chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents: 307
diff changeset
220 '--no-cache',
bd726306d4dc chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents: 307
diff changeset
221 help="When applicable, disable caching.",
bd726306d4dc chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents: 307
diff changeset
222 action='store_true')
bd726306d4dc chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents: 307
diff changeset
223 parser.add_argument(
bd726306d4dc chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents: 307
diff changeset
224 '--quiet',
bd726306d4dc chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents: 307
diff changeset
225 help="Print only important information.",
bd726306d4dc chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents: 307
diff changeset
226 action='store_true')
bd726306d4dc chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents: 307
diff changeset
227 parser.add_argument(
bd726306d4dc chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents: 307
diff changeset
228 '--log',
bd726306d4dc chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents: 307
diff changeset
229 help="Send log messages to the specified file.")
bd726306d4dc chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents: 307
diff changeset
230 parser.add_argument(
bd726306d4dc chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents: 307
diff changeset
231 '--log-debug',
bd726306d4dc chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents: 307
diff changeset
232 help="Log debug messages to the log file.",
bd726306d4dc chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents: 307
diff changeset
233 action='store_true')
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
234
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
235 commands = sorted(app.plugin_loader.getCommands(),
341
bd726306d4dc chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents: 307
diff changeset
236 key=lambda c: c.name)
99
8703be118430 Changes to `help` command and extendable commands:
Ludovic Chabant <ludovic@chabant.com>
parents: 98
diff changeset
237 subparsers = parser.add_subparsers(title='list of commands')
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
238 for c in commands:
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
239 p = subparsers.add_parser(c.name, help=c.description)
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
240 c.setupParser(p, app)
57
c8c522dacfea Add `help` function, cleanup argument handling.
Ludovic Chabant <ludovic@chabant.com>
parents: 44
diff changeset
241 p.set_defaults(func=c.checkedRun)
371
c2ca72fb7f0b caching: Use separate caches for config variants and other contexts.
Ludovic Chabant <ludovic@chabant.com>
parents: 342
diff changeset
242 p.set_defaults(cache_name=c.cache_name)
99
8703be118430 Changes to `help` command and extendable commands:
Ludovic Chabant <ludovic@chabant.com>
parents: 98
diff changeset
243
57
c8c522dacfea Add `help` function, cleanup argument handling.
Ludovic Chabant <ludovic@chabant.com>
parents: 44
diff changeset
244 help_cmd = next(filter(lambda c: c.name == 'help', commands), None)
c8c522dacfea Add `help` function, cleanup argument handling.
Ludovic Chabant <ludovic@chabant.com>
parents: 44
diff changeset
245 if help_cmd and help_cmd.has_topics:
99
8703be118430 Changes to `help` command and extendable commands:
Ludovic Chabant <ludovic@chabant.com>
parents: 98
diff changeset
246 with io.StringIO() as epilog:
8703be118430 Changes to `help` command and extendable commands:
Ludovic Chabant <ludovic@chabant.com>
parents: 98
diff changeset
247 epilog.write("additional help topics:\n")
8703be118430 Changes to `help` command and extendable commands:
Ludovic Chabant <ludovic@chabant.com>
parents: 98
diff changeset
248 for name, desc in help_cmd.getTopics():
8703be118430 Changes to `help` command and extendable commands:
Ludovic Chabant <ludovic@chabant.com>
parents: 98
diff changeset
249 print_help_item(epilog, name, desc)
8703be118430 Changes to `help` command and extendable commands:
Ludovic Chabant <ludovic@chabant.com>
parents: 98
diff changeset
250 parser.epilog = epilog.getvalue()
57
c8c522dacfea Add `help` function, cleanup argument handling.
Ludovic Chabant <ludovic@chabant.com>
parents: 44
diff changeset
251
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
252 # Parse the command line.
384
d241585412ad internal: Make it possible to pass `argv` to the main Chef function.
Ludovic Chabant <ludovic@chabant.com>
parents: 371
diff changeset
253 result = parser.parse_args(argv)
341
bd726306d4dc chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents: 307
diff changeset
254 logger.debug(format_timed(start_time, 'initialized PieCrust',
bd726306d4dc chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents: 307
diff changeset
255 colored=False))
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
256
61
64f37c4cce68 Print the help by default when running `chef` with no command.
Ludovic Chabant <ludovic@chabant.com>
parents: 57
diff changeset
257 # Print the help if no command was specified.
64f37c4cce68 Print the help by default when running `chef` with no command.
Ludovic Chabant <ludovic@chabant.com>
parents: 57
diff changeset
258 if not hasattr(result, 'func'):
64f37c4cce68 Print the help by default when running `chef` with no command.
Ludovic Chabant <ludovic@chabant.com>
parents: 57
diff changeset
259 parser.print_help()
64f37c4cce68 Print the help by default when running `chef` with no command.
Ludovic Chabant <ludovic@chabant.com>
parents: 57
diff changeset
260 return 0
64f37c4cce68 Print the help by default when running `chef` with no command.
Ludovic Chabant <ludovic@chabant.com>
parents: 57
diff changeset
261
371
c2ca72fb7f0b caching: Use separate caches for config variants and other contexts.
Ludovic Chabant <ludovic@chabant.com>
parents: 342
diff changeset
262 # Use a customized cache for the command and current config.
c2ca72fb7f0b caching: Use separate caches for config variants and other contexts.
Ludovic Chabant <ludovic@chabant.com>
parents: 342
diff changeset
263 if result.cache_name != 'default' or cache_key != 'default':
c2ca72fb7f0b caching: Use separate caches for config variants and other contexts.
Ludovic Chabant <ludovic@chabant.com>
parents: 342
diff changeset
264 app.useSubCache(result.cache_name, cache_key)
c2ca72fb7f0b caching: Use separate caches for config variants and other contexts.
Ludovic Chabant <ludovic@chabant.com>
parents: 342
diff changeset
265
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
266 # Run the command!
57
c8c522dacfea Add `help` function, cleanup argument handling.
Ludovic Chabant <ludovic@chabant.com>
parents: 44
diff changeset
267 ctx = CommandContext(app, parser, result)
c8c522dacfea Add `help` function, cleanup argument handling.
Ludovic Chabant <ludovic@chabant.com>
parents: 44
diff changeset
268 exit_code = result.func(ctx)
98
5959a117a943 Exit with the proper code.
Ludovic Chabant <ludovic@chabant.com>
parents: 97
diff changeset
269 if exit_code is None:
5959a117a943 Exit with the proper code.
Ludovic Chabant <ludovic@chabant.com>
parents: 97
diff changeset
270 return 0
5959a117a943 Exit with the proper code.
Ludovic Chabant <ludovic@chabant.com>
parents: 97
diff changeset
271 if not isinstance(exit_code, int):
5959a117a943 Exit with the proper code.
Ludovic Chabant <ludovic@chabant.com>
parents: 97
diff changeset
272 logger.error("Got non-integer exit code: %s" % exit_code)
5959a117a943 Exit with the proper code.
Ludovic Chabant <ludovic@chabant.com>
parents: 97
diff changeset
273 return -1
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
274 return exit_code
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
275