Mercurial > piecrust2
annotate piecrust/main.py @ 661:2f780b191541
internal: Fix a bug with registering taxonomy terms that are not strings.
Some objects, like the blog data provider's taxnonomy entries, can render as
strings, but are objects themselves. When registering them as "used terms", we
need to use their string representation.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Tue, 01 Mar 2016 22:26:09 -0800 |
parents | 9dd2f68f243b |
children | 3ceeca7bb71c |
rev | line source |
---|---|
612
2edaefcb82cd
chef: Add `--pid-file` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
583
diff
changeset
|
1 import os |
2edaefcb82cd
chef: Add `--pid-file` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
583
diff
changeset
|
2 import os.path |
99
8703be118430
Changes to `help` command and extendable commands:
Ludovic Chabant <ludovic@chabant.com>
parents:
98
diff
changeset
|
3 import io |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
4 import sys |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
5 import time |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
6 import logging |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
7 import argparse |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
8 import colorama |
69
cb1ed436642c
Always use version generated by `setup.py`. Better version generation.
Ludovic Chabant <ludovic@chabant.com>
parents:
61
diff
changeset
|
9 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
|
10 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
|
11 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
|
12 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
|
13 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
|
14 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
|
15 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
|
16 from piecrust.plugins.base import PluginLoader |
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 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
19 logger = logging.getLogger(__name__) |
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 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
22 class ColoredFormatter(logging.Formatter): |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
23 COLORS = { |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
24 '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
|
25 'INFO': '', |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
26 'WARNING': colorama.Fore.YELLOW, |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
27 'ERROR': colorama.Fore.RED, |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
28 '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
|
29 } |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
30 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
31 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
|
32 super(ColoredFormatter, self).__init__(fmt, datefmt) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
33 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
34 def format(self, record): |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
35 color = self.COLORS.get(record.levelname) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
36 res = super(ColoredFormatter, self).format(record) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
37 if color: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
38 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
|
39 return res |
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 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
42 class NullPieCrust: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
43 def __init__(self): |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
44 self.root_dir = None |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
45 self.debug = False |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
46 self.templates_dirs = [] |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
47 self.theme_dir = None |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
48 self.cache_dir = None |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
49 self.config = PieCrustConfiguration() |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
50 self.plugin_loader = PluginLoader(self) |
103
028df35a690e
Fix using `chef` outside of a website.
Ludovic Chabant <ludovic@chabant.com>
parents:
99
diff
changeset
|
51 self.env = None |
3
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 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
54 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
|
55 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
|
56 # 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
|
57 # 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
|
58 # 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
|
59 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
|
60 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
|
61 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
|
62 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
|
63 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
|
64 |
384
d241585412ad
internal: Make it possible to pass `argv` to the main Chef function.
Ludovic Chabant <ludovic@chabant.com>
parents:
371
diff
changeset
|
65 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
|
66 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
|
67 try: |
384
d241585412ad
internal: Make it possible to pass `argv` to the main Chef function.
Ludovic Chabant <ludovic@chabant.com>
parents:
371
diff
changeset
|
68 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
|
69 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
|
70 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
|
71 logger.exception(ex) |
091f99bfbe44
Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
72 else: |
39
2f717f961996
Better error reporting and cache validation.
Ludovic Chabant <ludovic@chabant.com>
parents:
38
diff
changeset
|
73 log_friendly_exception(logger, ex) |
98
5959a117a943
Exit with the proper code.
Ludovic Chabant <ludovic@chabant.com>
parents:
97
diff
changeset
|
74 exit_code = 1 |
5959a117a943
Exit with the proper code.
Ludovic Chabant <ludovic@chabant.com>
parents:
97
diff
changeset
|
75 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
|
76 |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
77 |
583
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
78 def _setup_main_parser_arguments(parser): |
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
79 parser.add_argument( |
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
80 '--version', |
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
81 action='version', |
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
82 version=('%(prog)s ' + APP_VERSION)) |
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
83 parser.add_argument( |
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
84 '--root', |
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
85 help="The root directory of the website.") |
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
86 parser.add_argument( |
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
87 '--config', |
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
88 dest='config_variant', |
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
89 help="The configuration variant to use for this command.") |
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
90 parser.add_argument( |
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
91 '--config-set', |
632
9dd2f68f243b
chef: Fix the `--config-set` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
612
diff
changeset
|
92 nargs=2, |
9dd2f68f243b
chef: Fix the `--config-set` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
612
diff
changeset
|
93 metavar=('NAME', 'VALUE'), |
9dd2f68f243b
chef: Fix the `--config-set` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
612
diff
changeset
|
94 action='append', |
583
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
95 dest='config_values', |
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
96 help="Sets a specific site configuration setting.") |
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
97 parser.add_argument( |
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
98 '--debug', |
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
99 help="Show debug information.", action='store_true') |
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
100 parser.add_argument( |
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
101 '--debug-only', |
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
102 nargs='*', |
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
103 help="Only show debug information for the given categories.") |
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
104 parser.add_argument( |
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
105 '--no-cache', |
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
106 help="When applicable, disable caching.", |
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
107 action='store_true') |
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
108 parser.add_argument( |
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
109 '--quiet', |
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
110 help="Print only important information.", |
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
111 action='store_true') |
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
112 parser.add_argument( |
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
113 '--log', |
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
114 dest='log_file', |
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
115 help="Send log messages to the specified file.") |
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
116 parser.add_argument( |
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
117 '--log-debug', |
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
118 help="Log debug messages to the log file.", |
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
119 action='store_true') |
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
120 parser.add_argument( |
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
121 '--no-color', |
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
122 help="Don't use colorized output.", |
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
123 action='store_true') |
612
2edaefcb82cd
chef: Add `--pid-file` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
583
diff
changeset
|
124 parser.add_argument( |
2edaefcb82cd
chef: Add `--pid-file` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
583
diff
changeset
|
125 '--pid-file', |
2edaefcb82cd
chef: Add `--pid-file` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
583
diff
changeset
|
126 dest='pid_file', |
2edaefcb82cd
chef: Add `--pid-file` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
583
diff
changeset
|
127 help="Write a PID file for the current process.") |
38
091f99bfbe44
Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
128 |
091f99bfbe44
Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
129 |
091f99bfbe44
Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
130 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
|
131 # 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
|
132 # 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
|
133 # 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
|
134 # from the beginning. |
583
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
135 parser = argparse.ArgumentParser() |
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
136 _setup_main_parser_arguments(parser) |
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
137 parser.add_argument('args', nargs=argparse.REMAINDER) |
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
138 res, _ = parser.parse_known_args(argv) |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
139 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
140 # 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
|
141 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
|
142 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
|
143 |
579
42a5e78e782a
cli: Add `--no-color` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
564
diff
changeset
|
144 strip_colors = None |
42a5e78e782a
cli: Add `--no-color` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
564
diff
changeset
|
145 if res.no_color: |
42a5e78e782a
cli: Add `--no-color` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
564
diff
changeset
|
146 strip_colors = True |
42a5e78e782a
cli: Add `--no-color` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
564
diff
changeset
|
147 |
42a5e78e782a
cli: Add `--no-color` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
564
diff
changeset
|
148 colorama.init(strip=strip_colors) |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
149 root_logger = logging.getLogger() |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
150 root_logger.setLevel(logging.INFO) |
97
00a9b24ca944
Add `--log-debug` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
69
diff
changeset
|
151 if res.debug or res.log_debug: |
00a9b24ca944
Add `--log-debug` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
69
diff
changeset
|
152 root_logger.setLevel(logging.DEBUG) |
00a9b24ca944
Add `--log-debug` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
69
diff
changeset
|
153 |
583
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
154 if res.debug_only: |
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
155 for n in res.debug_only: |
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
156 logging.getLogger(n).setLevel(logging.DEBUG) |
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
|
157 |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
158 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
|
159 if res.debug or res.debug_only: |
97
00a9b24ca944
Add `--log-debug` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
69
diff
changeset
|
160 log_handler.setLevel(logging.DEBUG) |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
161 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
|
162 else: |
38
091f99bfbe44
Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
163 if res.quiet: |
97
00a9b24ca944
Add `--log-debug` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
69
diff
changeset
|
164 log_handler.setLevel(logging.WARNING) |
00a9b24ca944
Add `--log-debug` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
69
diff
changeset
|
165 else: |
00a9b24ca944
Add `--log-debug` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
69
diff
changeset
|
166 log_handler.setLevel(logging.INFO) |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
167 log_handler.setFormatter(ColoredFormatter("%(message)s")) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
168 root_logger.addHandler(log_handler) |
97
00a9b24ca944
Add `--log-debug` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
69
diff
changeset
|
169 |
38
091f99bfbe44
Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
170 if res.log_file: |
97
00a9b24ca944
Add `--log-debug` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
69
diff
changeset
|
171 file_handler = logging.FileHandler(res.log_file, mode='w') |
00a9b24ca944
Add `--log-debug` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
69
diff
changeset
|
172 root_logger.addHandler(file_handler) |
00a9b24ca944
Add `--log-debug` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
69
diff
changeset
|
173 if res.log_debug: |
00a9b24ca944
Add `--log-debug` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
69
diff
changeset
|
174 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
|
175 |
612
2edaefcb82cd
chef: Add `--pid-file` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
583
diff
changeset
|
176 # PID file. |
2edaefcb82cd
chef: Add `--pid-file` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
583
diff
changeset
|
177 if res.pid_file: |
2edaefcb82cd
chef: Add `--pid-file` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
583
diff
changeset
|
178 try: |
2edaefcb82cd
chef: Add `--pid-file` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
583
diff
changeset
|
179 pid_file_dir = os.path.dirname(res.pid_file) |
2edaefcb82cd
chef: Add `--pid-file` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
583
diff
changeset
|
180 if pid_file_dir and not os.path.isdir(pid_file_dir): |
2edaefcb82cd
chef: Add `--pid-file` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
583
diff
changeset
|
181 os.makedirs(pid_file_dir) |
2edaefcb82cd
chef: Add `--pid-file` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
583
diff
changeset
|
182 |
2edaefcb82cd
chef: Add `--pid-file` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
583
diff
changeset
|
183 with open(res.pid_file, 'w') as fp: |
2edaefcb82cd
chef: Add `--pid-file` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
583
diff
changeset
|
184 fp.write(str(os.getpid())) |
2edaefcb82cd
chef: Add `--pid-file` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
583
diff
changeset
|
185 except OSError as ex: |
2edaefcb82cd
chef: Add `--pid-file` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
583
diff
changeset
|
186 raise Exception("Can't write PID file: %s" % res.pid_file) from ex |
2edaefcb82cd
chef: Add `--pid-file` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
583
diff
changeset
|
187 |
38
091f99bfbe44
Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
188 return res |
091f99bfbe44
Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
189 |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
190 |
384
d241585412ad
internal: Make it possible to pass `argv` to the main Chef function.
Ludovic Chabant <ludovic@chabant.com>
parents:
371
diff
changeset
|
191 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
|
192 # Setup the app. |
489
186a29f61ddc
internal: Fix timing info.
Ludovic Chabant <ludovic@chabant.com>
parents:
467
diff
changeset
|
193 start_time = time.perf_counter() |
583
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
194 root = None |
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
195 if pre_args.root: |
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
196 root = os.path.expanduser(pre_args.root) |
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
197 else: |
38
091f99bfbe44
Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
198 try: |
091f99bfbe44
Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
199 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
|
200 except SiteNotFoundError: |
091f99bfbe44
Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
201 root = None |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
202 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
203 if not root: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
204 app = NullPieCrust() |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
205 else: |
583
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
206 app = PieCrust(root, cache=(not pre_args.no_cache), |
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
207 debug=pre_args.debug) |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
208 |
371
c2ca72fb7f0b
caching: Use separate caches for config variants and other contexts.
Ludovic Chabant <ludovic@chabant.com>
parents:
342
diff
changeset
|
209 # 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
|
210 cache_key = 'default' |
c2ca72fb7f0b
caching: Use separate caches for config variants and other contexts.
Ludovic Chabant <ludovic@chabant.com>
parents:
342
diff
changeset
|
211 |
466
456db44dcc53
bake: Pass the config variants and values from the CLI to the baker.
Ludovic Chabant <ludovic@chabant.com>
parents:
384
diff
changeset
|
212 # Handle custom configurations. |
632
9dd2f68f243b
chef: Fix the `--config-set` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
612
diff
changeset
|
213 if (pre_args.config_variant or pre_args.config_values) and not root: |
9dd2f68f243b
chef: Fix the `--config-set` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
612
diff
changeset
|
214 raise SiteNotFoundError( |
9dd2f68f243b
chef: Fix the `--config-set` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
612
diff
changeset
|
215 "Can't apply any configuration variant or value overrides, " |
9dd2f68f243b
chef: Fix the `--config-set` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
612
diff
changeset
|
216 "there is no website here.") |
467
a8028bf329a2
bug: Fix CLI crash caused by configuration variants.
Ludovic Chabant <ludovic@chabant.com>
parents:
466
diff
changeset
|
217 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
|
218 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
|
219 |
456db44dcc53
bake: Pass the config variants and values from the CLI to the baker.
Ludovic Chabant <ludovic@chabant.com>
parents:
384
diff
changeset
|
220 # 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
|
221 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
|
222 cache_key += ',variant=%s' % pre_args.config_variant |
583
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
223 if pre_args.config_values: |
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
224 for name, value in pre_args.config_values: |
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
225 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
|
226 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
227 # Setup the arg parser. |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
228 parser = argparse.ArgumentParser( |
57
c8c522dacfea
Add `help` function, cleanup argument handling.
Ludovic Chabant <ludovic@chabant.com>
parents:
44
diff
changeset
|
229 prog='chef', |
99
8703be118430
Changes to `help` command and extendable commands:
Ludovic Chabant <ludovic@chabant.com>
parents:
98
diff
changeset
|
230 description="The PieCrust chef manages your website.", |
8703be118430
Changes to `help` command and extendable commands:
Ludovic Chabant <ludovic@chabant.com>
parents:
98
diff
changeset
|
231 formatter_class=argparse.RawDescriptionHelpFormatter) |
583
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
232 _setup_main_parser_arguments(parser) |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
233 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
234 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
|
235 key=lambda c: c.name) |
99
8703be118430
Changes to `help` command and extendable commands:
Ludovic Chabant <ludovic@chabant.com>
parents:
98
diff
changeset
|
236 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
|
237 for c in commands: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
238 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
|
239 c.setupParser(p, app) |
57
c8c522dacfea
Add `help` function, cleanup argument handling.
Ludovic Chabant <ludovic@chabant.com>
parents:
44
diff
changeset
|
240 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
|
241 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
|
242 |
57
c8c522dacfea
Add `help` function, cleanup argument handling.
Ludovic Chabant <ludovic@chabant.com>
parents:
44
diff
changeset
|
243 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
|
244 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
|
245 with io.StringIO() as epilog: |
8703be118430
Changes to `help` command and extendable commands:
Ludovic Chabant <ludovic@chabant.com>
parents:
98
diff
changeset
|
246 epilog.write("additional help topics:\n") |
8703be118430
Changes to `help` command and extendable commands:
Ludovic Chabant <ludovic@chabant.com>
parents:
98
diff
changeset
|
247 for name, desc in help_cmd.getTopics(): |
8703be118430
Changes to `help` command and extendable commands:
Ludovic Chabant <ludovic@chabant.com>
parents:
98
diff
changeset
|
248 print_help_item(epilog, name, desc) |
8703be118430
Changes to `help` command and extendable commands:
Ludovic Chabant <ludovic@chabant.com>
parents:
98
diff
changeset
|
249 parser.epilog = epilog.getvalue() |
57
c8c522dacfea
Add `help` function, cleanup argument handling.
Ludovic Chabant <ludovic@chabant.com>
parents:
44
diff
changeset
|
250 |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
251 # 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
|
252 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
|
253 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
|
254 colored=False)) |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
255 |
61
64f37c4cce68
Print the help by default when running `chef` with no command.
Ludovic Chabant <ludovic@chabant.com>
parents:
57
diff
changeset
|
256 # 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
|
257 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
|
258 parser.print_help() |
64f37c4cce68
Print the help by default when running `chef` with no command.
Ludovic Chabant <ludovic@chabant.com>
parents:
57
diff
changeset
|
259 return 0 |
64f37c4cce68
Print the help by default when running `chef` with no command.
Ludovic Chabant <ludovic@chabant.com>
parents:
57
diff
changeset
|
260 |
371
c2ca72fb7f0b
caching: Use separate caches for config variants and other contexts.
Ludovic Chabant <ludovic@chabant.com>
parents:
342
diff
changeset
|
261 # 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
|
262 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
|
263 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
|
264 |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
265 # Run the command! |
57
c8c522dacfea
Add `help` function, cleanup argument handling.
Ludovic Chabant <ludovic@chabant.com>
parents:
44
diff
changeset
|
266 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
|
267 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
|
268 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
|
269 |
57
c8c522dacfea
Add `help` function, cleanup argument handling.
Ludovic Chabant <ludovic@chabant.com>
parents:
44
diff
changeset
|
270 exit_code = result.func(ctx) |
98
5959a117a943
Exit with the proper code.
Ludovic Chabant <ludovic@chabant.com>
parents:
97
diff
changeset
|
271 if exit_code is None: |
5959a117a943
Exit with the proper code.
Ludovic Chabant <ludovic@chabant.com>
parents:
97
diff
changeset
|
272 return 0 |
5959a117a943
Exit with the proper code.
Ludovic Chabant <ludovic@chabant.com>
parents:
97
diff
changeset
|
273 if not isinstance(exit_code, int): |
5959a117a943
Exit with the proper code.
Ludovic Chabant <ludovic@chabant.com>
parents:
97
diff
changeset
|
274 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
|
275 return -1 |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
276 return exit_code |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
277 |