annotate piecrust/main.py @ 579:42a5e78e782a

cli: Add `--no-color` option.
author Ludovic Chabant <ludovic@chabant.com>
date Wed, 30 Dec 2015 14:46:51 -0800
parents 353a0b30f412
children 1eda551ee681
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
466
456db44dcc53 bake: Pass the config variants and values from the CLI to the baker.
Ludovic Chabant <ludovic@chabant.com>
parents: 384
diff changeset
9 from piecrust.app import (
456db44dcc53 bake: Pass the config variants and values from the CLI to the baker.
Ludovic Chabant <ludovic@chabant.com>
parents: 384
diff changeset
10 PieCrust, PieCrustConfiguration, apply_variant_and_values)
341
bd726306d4dc chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents: 307
diff changeset
11 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
12 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
13 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
14 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
15 from piecrust.plugins.base import PluginLoader
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
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
18 logger = logging.getLogger(__name__)
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
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
21 class ColoredFormatter(logging.Formatter):
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
22 COLORS = {
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
23 '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
24 'INFO': '',
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
25 'WARNING': colorama.Fore.YELLOW,
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
26 'ERROR': colorama.Fore.RED,
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
27 '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
28 }
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
29
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
30 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
31 super(ColoredFormatter, self).__init__(fmt, datefmt)
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
32
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
33 def format(self, record):
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
34 color = self.COLORS.get(record.levelname)
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
35 res = super(ColoredFormatter, self).format(record)
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
36 if color:
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
37 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
38 return res
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
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
41 class NullPieCrust:
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
42 def __init__(self):
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
43 self.root_dir = None
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
44 self.debug = False
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
45 self.templates_dirs = []
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
46 self.theme_dir = None
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
47 self.cache_dir = None
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
48 self.config = PieCrustConfiguration()
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
49 self.plugin_loader = PluginLoader(self)
103
028df35a690e Fix using `chef` outside of a website.
Ludovic Chabant <ludovic@chabant.com>
parents: 99
diff changeset
50 self.env = None
3
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
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
53 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
54 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
55 # 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
56 # 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
57 # 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
58 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
59 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
60 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
61 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
62 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
63
384
d241585412ad internal: Make it possible to pass `argv` to the main Chef function.
Ludovic Chabant <ludovic@chabant.com>
parents: 371
diff changeset
64 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
65 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
66 try:
384
d241585412ad internal: Make it possible to pass `argv` to the main Chef function.
Ludovic Chabant <ludovic@chabant.com>
parents: 371
diff changeset
67 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
68 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
69 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
70 logger.exception(ex)
091f99bfbe44 Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
71 else:
39
2f717f961996 Better error reporting and cache validation.
Ludovic Chabant <ludovic@chabant.com>
parents: 38
diff changeset
72 log_friendly_exception(logger, ex)
98
5959a117a943 Exit with the proper code.
Ludovic Chabant <ludovic@chabant.com>
parents: 97
diff changeset
73 exit_code = 1
5959a117a943 Exit with the proper code.
Ludovic Chabant <ludovic@chabant.com>
parents: 97
diff changeset
74 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
75
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
76
38
091f99bfbe44 Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
77 class PreParsedChefArgs(object):
091f99bfbe44 Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
78 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
79 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
80 self.root = root
091f99bfbe44 Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
81 self.cache = cache
091f99bfbe44 Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
82 self.debug = debug
091f99bfbe44 Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
83 self.quiet = quiet
091f99bfbe44 Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
84 self.log_file = log_file
97
00a9b24ca944 Add `--log-debug` option.
Ludovic Chabant <ludovic@chabant.com>
parents: 69
diff changeset
85 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
86 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
87 self.config_values = []
564
353a0b30f412 chef: Add `--debug-only` option to only show debug logging for a given logger.
Ludovic Chabant <ludovic@chabant.com>
parents: 489
diff changeset
88 self.debug_only = []
579
42a5e78e782a cli: Add `--no-color` option.
Ludovic Chabant <ludovic@chabant.com>
parents: 564
diff changeset
89 self.no_color = 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
90
bd726306d4dc chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents: 307
diff changeset
91
bd726306d4dc chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents: 307
diff changeset
92 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
93 try:
bd726306d4dc chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents: 307
diff changeset
94 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
95 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
96 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
97 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
98
091f99bfbe44 Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
99
091f99bfbe44 Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
100 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
101 # 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
102 # 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
103 # 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
104 # 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
105 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
106 i = 0
38
091f99bfbe44 Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
107 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
108 arg = argv[i]
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
109 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
110 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
111 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
112 res.root = os.path.expanduser(argv[i + 1])
342
d8677ad748f0 chef: Fix pre-parsing.
Ludovic Chabant <ludovic@chabant.com>
parents: 341
diff changeset
113 i += 1
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
114 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
115 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
116 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
117 res.config_variant = argv[i + 1]
342
d8677ad748f0 chef: Fix pre-parsing.
Ludovic Chabant <ludovic@chabant.com>
parents: 341
diff changeset
118 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
119 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
120 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
121 _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
122 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
123 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
124 i += 1
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
125 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
126 res.log_file = argv[i + 1]
342
d8677ad748f0 chef: Fix pre-parsing.
Ludovic Chabant <ludovic@chabant.com>
parents: 341
diff changeset
127 i += 1
97
00a9b24ca944 Add `--log-debug` option.
Ludovic Chabant <ludovic@chabant.com>
parents: 69
diff changeset
128 elif arg == '--log-debug':
00a9b24ca944 Add `--log-debug` option.
Ludovic Chabant <ludovic@chabant.com>
parents: 69
diff changeset
129 res.log_debug = True
564
353a0b30f412 chef: Add `--debug-only` option to only show debug logging for a given logger.
Ludovic Chabant <ludovic@chabant.com>
parents: 489
diff changeset
130 elif arg == '--debug-only':
353a0b30f412 chef: Add `--debug-only` option to only show debug logging for a given logger.
Ludovic Chabant <ludovic@chabant.com>
parents: 489
diff changeset
131 res.debug_only.append(argv[i + 1])
353a0b30f412 chef: Add `--debug-only` option to only show debug logging for a given logger.
Ludovic Chabant <ludovic@chabant.com>
parents: 489
diff changeset
132 i += 1
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
133 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
134 res.cache = False
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
135 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
136 res.debug = True
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
137 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
138 res.quiet = True
579
42a5e78e782a cli: Add `--no-color` option.
Ludovic Chabant <ludovic@chabant.com>
parents: 564
diff changeset
139 elif arg == '--no-color':
42a5e78e782a cli: Add `--no-color` option.
Ludovic Chabant <ludovic@chabant.com>
parents: 564
diff changeset
140 res.no_color = True
342
d8677ad748f0 chef: Fix pre-parsing.
Ludovic Chabant <ludovic@chabant.com>
parents: 341
diff changeset
141 else:
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
142 break
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
143
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
144 i = i + 1
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
145
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
146 # 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
147 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
148 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
149
579
42a5e78e782a cli: Add `--no-color` option.
Ludovic Chabant <ludovic@chabant.com>
parents: 564
diff changeset
150 strip_colors = None
42a5e78e782a cli: Add `--no-color` option.
Ludovic Chabant <ludovic@chabant.com>
parents: 564
diff changeset
151 if res.no_color:
42a5e78e782a cli: Add `--no-color` option.
Ludovic Chabant <ludovic@chabant.com>
parents: 564
diff changeset
152 strip_colors = True
42a5e78e782a cli: Add `--no-color` option.
Ludovic Chabant <ludovic@chabant.com>
parents: 564
diff changeset
153
42a5e78e782a cli: Add `--no-color` option.
Ludovic Chabant <ludovic@chabant.com>
parents: 564
diff changeset
154 colorama.init(strip=strip_colors)
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
155 root_logger = logging.getLogger()
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
156 root_logger.setLevel(logging.INFO)
97
00a9b24ca944 Add `--log-debug` option.
Ludovic Chabant <ludovic@chabant.com>
parents: 69
diff changeset
157 if res.debug or res.log_debug:
00a9b24ca944 Add `--log-debug` option.
Ludovic Chabant <ludovic@chabant.com>
parents: 69
diff changeset
158 root_logger.setLevel(logging.DEBUG)
00a9b24ca944 Add `--log-debug` option.
Ludovic Chabant <ludovic@chabant.com>
parents: 69
diff changeset
159
564
353a0b30f412 chef: Add `--debug-only` option to only show debug logging for a given logger.
Ludovic Chabant <ludovic@chabant.com>
parents: 489
diff changeset
160 for n in res.debug_only:
353a0b30f412 chef: Add `--debug-only` option to only show debug logging for a given logger.
Ludovic Chabant <ludovic@chabant.com>
parents: 489
diff changeset
161 logging.getLogger(n).setLevel(logging.DEBUG)
353a0b30f412 chef: Add `--debug-only` option to only show debug logging for a given logger.
Ludovic Chabant <ludovic@chabant.com>
parents: 489
diff changeset
162
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
163 log_handler = logging.StreamHandler(sys.stdout)
564
353a0b30f412 chef: Add `--debug-only` option to only show debug logging for a given logger.
Ludovic Chabant <ludovic@chabant.com>
parents: 489
diff changeset
164 if res.debug or res.debug_only:
97
00a9b24ca944 Add `--log-debug` option.
Ludovic Chabant <ludovic@chabant.com>
parents: 69
diff changeset
165 log_handler.setLevel(logging.DEBUG)
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
166 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
167 else:
38
091f99bfbe44 Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
168 if res.quiet:
97
00a9b24ca944 Add `--log-debug` option.
Ludovic Chabant <ludovic@chabant.com>
parents: 69
diff changeset
169 log_handler.setLevel(logging.WARNING)
00a9b24ca944 Add `--log-debug` option.
Ludovic Chabant <ludovic@chabant.com>
parents: 69
diff changeset
170 else:
00a9b24ca944 Add `--log-debug` option.
Ludovic Chabant <ludovic@chabant.com>
parents: 69
diff changeset
171 log_handler.setLevel(logging.INFO)
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
172 log_handler.setFormatter(ColoredFormatter("%(message)s"))
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
173 root_logger.addHandler(log_handler)
97
00a9b24ca944 Add `--log-debug` option.
Ludovic Chabant <ludovic@chabant.com>
parents: 69
diff changeset
174
38
091f99bfbe44 Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
175 if res.log_file:
97
00a9b24ca944 Add `--log-debug` option.
Ludovic Chabant <ludovic@chabant.com>
parents: 69
diff changeset
176 file_handler = logging.FileHandler(res.log_file, mode='w')
00a9b24ca944 Add `--log-debug` option.
Ludovic Chabant <ludovic@chabant.com>
parents: 69
diff changeset
177 root_logger.addHandler(file_handler)
00a9b24ca944 Add `--log-debug` option.
Ludovic Chabant <ludovic@chabant.com>
parents: 69
diff changeset
178 if res.log_debug:
00a9b24ca944 Add `--log-debug` option.
Ludovic Chabant <ludovic@chabant.com>
parents: 69
diff changeset
179 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
180
091f99bfbe44 Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
181 return res
091f99bfbe44 Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
182
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
183
384
d241585412ad internal: Make it possible to pass `argv` to the main Chef function.
Ludovic Chabant <ludovic@chabant.com>
parents: 371
diff changeset
184 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
185 # Setup the app.
489
186a29f61ddc internal: Fix timing info.
Ludovic Chabant <ludovic@chabant.com>
parents: 467
diff changeset
186 start_time = time.perf_counter()
38
091f99bfbe44 Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
187 root = pre_args.root
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
188 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
189 try:
091f99bfbe44 Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
190 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
191 except SiteNotFoundError:
091f99bfbe44 Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
192 root = None
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
193
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
194 if not root:
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
195 app = NullPieCrust()
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
196 else:
44
a62452ab5080 Correctly set the `debug` flag on the app.
Ludovic Chabant <ludovic@chabant.com>
parents: 39
diff changeset
197 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
198
371
c2ca72fb7f0b caching: Use separate caches for config variants and other contexts.
Ludovic Chabant <ludovic@chabant.com>
parents: 342
diff changeset
199 # 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
200 cache_key = 'default'
c2ca72fb7f0b caching: Use separate caches for config variants and other contexts.
Ludovic Chabant <ludovic@chabant.com>
parents: 342
diff changeset
201
466
456db44dcc53 bake: Pass the config variants and values from the CLI to the baker.
Ludovic Chabant <ludovic@chabant.com>
parents: 384
diff changeset
202 # Handle custom configurations.
456db44dcc53 bake: Pass the config variants and values from the CLI to the baker.
Ludovic Chabant <ludovic@chabant.com>
parents: 384
diff changeset
203 if pre_args.config_variant is not None and not root:
456db44dcc53 bake: Pass the config variants and values from the CLI to the baker.
Ludovic Chabant <ludovic@chabant.com>
parents: 384
diff changeset
204 raise SiteNotFoundError("Can't apply any variant.")
467
a8028bf329a2 bug: Fix CLI crash caused by configuration variants.
Ludovic Chabant <ludovic@chabant.com>
parents: 466
diff changeset
205 apply_variant_and_values(app, pre_args.config_variant,
466
456db44dcc53 bake: Pass the config variants and values from the CLI to the baker.
Ludovic Chabant <ludovic@chabant.com>
parents: 384
diff changeset
206 pre_args.config_values)
456db44dcc53 bake: Pass the config variants and values from the CLI to the baker.
Ludovic Chabant <ludovic@chabant.com>
parents: 384
diff changeset
207
456db44dcc53 bake: Pass the config variants and values from the CLI to the baker.
Ludovic Chabant <ludovic@chabant.com>
parents: 384
diff changeset
208 # Adjust the cache key.
38
091f99bfbe44 Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
209 if pre_args.config_variant is not None:
371
c2ca72fb7f0b caching: Use separate caches for config variants and other contexts.
Ludovic Chabant <ludovic@chabant.com>
parents: 342
diff changeset
210 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
211 for name, value in pre_args.config_values:
371
c2ca72fb7f0b caching: Use separate caches for config variants and other contexts.
Ludovic Chabant <ludovic@chabant.com>
parents: 342
diff changeset
212 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
213
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
214 # Setup the arg parser.
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
215 parser = argparse.ArgumentParser(
57
c8c522dacfea Add `help` function, cleanup argument handling.
Ludovic Chabant <ludovic@chabant.com>
parents: 44
diff changeset
216 prog='chef',
99
8703be118430 Changes to `help` command and extendable commands:
Ludovic Chabant <ludovic@chabant.com>
parents: 98
diff changeset
217 description="The PieCrust chef manages your website.",
8703be118430 Changes to `help` command and extendable commands:
Ludovic Chabant <ludovic@chabant.com>
parents: 98
diff changeset
218 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
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 '--version',
bd726306d4dc chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents: 307
diff changeset
221 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
222 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
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 '--root',
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="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
226 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
227 '--config',
bd726306d4dc chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents: 307
diff changeset
228 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
229 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
230 '--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
231 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
232 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
233 '--debug',
bd726306d4dc chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents: 307
diff changeset
234 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
235 parser.add_argument(
564
353a0b30f412 chef: Add `--debug-only` option to only show debug logging for a given logger.
Ludovic Chabant <ludovic@chabant.com>
parents: 489
diff changeset
236 '--debug-only',
353a0b30f412 chef: Add `--debug-only` option to only show debug logging for a given logger.
Ludovic Chabant <ludovic@chabant.com>
parents: 489
diff changeset
237 help="Only show debug information for the given categories.")
353a0b30f412 chef: Add `--debug-only` option to only show debug logging for a given logger.
Ludovic Chabant <ludovic@chabant.com>
parents: 489
diff changeset
238 parser.add_argument(
341
bd726306d4dc chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents: 307
diff changeset
239 '--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
240 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
241 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
242 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
243 '--quiet',
bd726306d4dc chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents: 307
diff changeset
244 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
245 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
246 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
247 '--log',
bd726306d4dc chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents: 307
diff changeset
248 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
249 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
250 '--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
251 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
252 action='store_true')
579
42a5e78e782a cli: Add `--no-color` option.
Ludovic Chabant <ludovic@chabant.com>
parents: 564
diff changeset
253 parser.add_argument(
42a5e78e782a cli: Add `--no-color` option.
Ludovic Chabant <ludovic@chabant.com>
parents: 564
diff changeset
254 '--no-color',
42a5e78e782a cli: Add `--no-color` option.
Ludovic Chabant <ludovic@chabant.com>
parents: 564
diff changeset
255 help="Don't use colorized output.",
42a5e78e782a cli: Add `--no-color` option.
Ludovic Chabant <ludovic@chabant.com>
parents: 564
diff changeset
256 action='store_true')
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
257
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
258 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
259 key=lambda c: c.name)
99
8703be118430 Changes to `help` command and extendable commands:
Ludovic Chabant <ludovic@chabant.com>
parents: 98
diff changeset
260 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
261 for c in commands:
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
262 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
263 c.setupParser(p, app)
57
c8c522dacfea Add `help` function, cleanup argument handling.
Ludovic Chabant <ludovic@chabant.com>
parents: 44
diff changeset
264 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
265 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
266
57
c8c522dacfea Add `help` function, cleanup argument handling.
Ludovic Chabant <ludovic@chabant.com>
parents: 44
diff changeset
267 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
268 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
269 with io.StringIO() as epilog:
8703be118430 Changes to `help` command and extendable commands:
Ludovic Chabant <ludovic@chabant.com>
parents: 98
diff changeset
270 epilog.write("additional help topics:\n")
8703be118430 Changes to `help` command and extendable commands:
Ludovic Chabant <ludovic@chabant.com>
parents: 98
diff changeset
271 for name, desc in help_cmd.getTopics():
8703be118430 Changes to `help` command and extendable commands:
Ludovic Chabant <ludovic@chabant.com>
parents: 98
diff changeset
272 print_help_item(epilog, name, desc)
8703be118430 Changes to `help` command and extendable commands:
Ludovic Chabant <ludovic@chabant.com>
parents: 98
diff changeset
273 parser.epilog = epilog.getvalue()
57
c8c522dacfea Add `help` function, cleanup argument handling.
Ludovic Chabant <ludovic@chabant.com>
parents: 44
diff changeset
274
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
275 # 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
276 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
277 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
278 colored=False))
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
279
61
64f37c4cce68 Print the help by default when running `chef` with no command.
Ludovic Chabant <ludovic@chabant.com>
parents: 57
diff changeset
280 # 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
281 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
282 parser.print_help()
64f37c4cce68 Print the help by default when running `chef` with no command.
Ludovic Chabant <ludovic@chabant.com>
parents: 57
diff changeset
283 return 0
64f37c4cce68 Print the help by default when running `chef` with no command.
Ludovic Chabant <ludovic@chabant.com>
parents: 57
diff changeset
284
371
c2ca72fb7f0b caching: Use separate caches for config variants and other contexts.
Ludovic Chabant <ludovic@chabant.com>
parents: 342
diff changeset
285 # 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
286 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
287 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
288
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
289 # Run the command!
57
c8c522dacfea Add `help` function, cleanup argument handling.
Ludovic Chabant <ludovic@chabant.com>
parents: 44
diff changeset
290 ctx = CommandContext(app, parser, result)
466
456db44dcc53 bake: Pass the config variants and values from the CLI to the baker.
Ludovic Chabant <ludovic@chabant.com>
parents: 384
diff changeset
291 ctx.config_variant = pre_args.config_variant
456db44dcc53 bake: Pass the config variants and values from the CLI to the baker.
Ludovic Chabant <ludovic@chabant.com>
parents: 384
diff changeset
292 ctx.config_values = pre_args.config_values
456db44dcc53 bake: Pass the config variants and values from the CLI to the baker.
Ludovic Chabant <ludovic@chabant.com>
parents: 384
diff changeset
293
57
c8c522dacfea Add `help` function, cleanup argument handling.
Ludovic Chabant <ludovic@chabant.com>
parents: 44
diff changeset
294 exit_code = result.func(ctx)
98
5959a117a943 Exit with the proper code.
Ludovic Chabant <ludovic@chabant.com>
parents: 97
diff changeset
295 if exit_code is None:
5959a117a943 Exit with the proper code.
Ludovic Chabant <ludovic@chabant.com>
parents: 97
diff changeset
296 return 0
5959a117a943 Exit with the proper code.
Ludovic Chabant <ludovic@chabant.com>
parents: 97
diff changeset
297 if not isinstance(exit_code, int):
5959a117a943 Exit with the proper code.
Ludovic Chabant <ludovic@chabant.com>
parents: 97
diff changeset
298 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
299 return -1
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
300 return exit_code
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
301