Mercurial > piecrust2
annotate piecrust/main.py @ 1145:e94737572542
serve: Fix an issue where false positive matches were rendered as the requested page.
Now we try to render the page, but also try to detect for the most common "empty" pages.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Tue, 05 Jun 2018 22:08:51 -0700 |
parents | 957f7c972715 |
children |
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 |
666
81d9c3a3a0b5
internal: Get rid of the whole "sub cache" business.
Ludovic Chabant <ludovic@chabant.com>
parents:
663
diff
changeset
|
6 import hashlib |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
7 import logging |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
8 import argparse |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
9 import colorama |
69
cb1ed436642c
Always use version generated by `setup.py`. Better version generation.
Ludovic Chabant <ludovic@chabant.com>
parents:
61
diff
changeset
|
10 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
|
11 from piecrust.app import ( |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
689
diff
changeset
|
12 PieCrustFactory, PieCrustConfiguration) |
341
bd726306d4dc
chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents:
307
diff
changeset
|
13 from piecrust.chefutil import ( |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
689
diff
changeset
|
14 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
|
15 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
|
16 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
|
17 from piecrust.plugins.base import PluginLoader |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
18 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
19 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
20 logger = logging.getLogger(__name__) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
21 |
989
8adc27285d93
bake: Big pass on bake performance.
Ludovic Chabant <ludovic@chabant.com>
parents:
979
diff
changeset
|
22 _chef_start_time = time.perf_counter() |
8adc27285d93
bake: Big pass on bake performance.
Ludovic Chabant <ludovic@chabant.com>
parents:
979
diff
changeset
|
23 |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
24 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
25 class ColoredFormatter(logging.Formatter): |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
26 COLORS = { |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
689
diff
changeset
|
27 'DEBUG': colorama.Fore.BLACK + colorama.Style.BRIGHT, |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
689
diff
changeset
|
28 'INFO': '', |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
689
diff
changeset
|
29 'WARNING': colorama.Fore.YELLOW, |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
689
diff
changeset
|
30 'ERROR': colorama.Fore.RED, |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
689
diff
changeset
|
31 'CRITICAL': colorama.Back.RED + colorama.Fore.WHITE |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
689
diff
changeset
|
32 } |
3
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 __init__(self, fmt=None, datefmt=None): |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
35 super(ColoredFormatter, self).__init__(fmt, datefmt) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
36 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
37 def format(self, record): |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
38 color = self.COLORS.get(record.levelname) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
39 res = super(ColoredFormatter, self).format(record) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
40 if color: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
41 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
|
42 return res |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
43 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
44 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
45 class NullPieCrust: |
663
3ceeca7bb71c
themes: Add support for a `--theme` argument to `chef`.
Ludovic Chabant <ludovic@chabant.com>
parents:
632
diff
changeset
|
46 def __init__(self, theme_site=False): |
3ceeca7bb71c
themes: Add support for a `--theme` argument to `chef`.
Ludovic Chabant <ludovic@chabant.com>
parents:
632
diff
changeset
|
47 self.theme_site = theme_site |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
48 self.root_dir = None |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
49 self.debug = False |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
50 self.templates_dirs = [] |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
51 self.theme_dir = None |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
52 self.cache_dir = None |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
53 self.config = PieCrustConfiguration() |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
54 self.plugin_loader = PluginLoader(self) |
103
028df35a690e
Fix using `chef` outside of a website.
Ludovic Chabant <ludovic@chabant.com>
parents:
99
diff
changeset
|
55 self.env = None |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
56 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
57 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
58 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
|
59 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
|
60 # 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
|
61 # 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
|
62 # 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
|
63 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
|
64 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
|
65 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
|
66 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
|
67 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
|
68 |
384
d241585412ad
internal: Make it possible to pass `argv` to the main Chef function.
Ludovic Chabant <ludovic@chabant.com>
parents:
371
diff
changeset
|
69 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
|
70 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
|
71 try: |
384
d241585412ad
internal: Make it possible to pass `argv` to the main Chef function.
Ludovic Chabant <ludovic@chabant.com>
parents:
371
diff
changeset
|
72 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
|
73 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
|
74 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
|
75 logger.exception(ex) |
091f99bfbe44
Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
76 else: |
39
2f717f961996
Better error reporting and cache validation.
Ludovic Chabant <ludovic@chabant.com>
parents:
38
diff
changeset
|
77 log_friendly_exception(logger, ex) |
98
5959a117a943
Exit with the proper code.
Ludovic Chabant <ludovic@chabant.com>
parents:
97
diff
changeset
|
78 exit_code = 1 |
5959a117a943
Exit with the proper code.
Ludovic Chabant <ludovic@chabant.com>
parents:
97
diff
changeset
|
79 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
|
80 |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
81 |
583
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
82 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
|
83 parser.add_argument( |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
689
diff
changeset
|
84 '--version', |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
689
diff
changeset
|
85 action='version', |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
689
diff
changeset
|
86 version=('%(prog)s ' + APP_VERSION)) |
583
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
87 parser.add_argument( |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
689
diff
changeset
|
88 '--root', |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
689
diff
changeset
|
89 help="The root directory of the website.") |
583
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
90 parser.add_argument( |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
689
diff
changeset
|
91 '--theme', |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
689
diff
changeset
|
92 action='store_true', |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
689
diff
changeset
|
93 help="Makes the current command apply to a theme website.") |
663
3ceeca7bb71c
themes: Add support for a `--theme` argument to `chef`.
Ludovic Chabant <ludovic@chabant.com>
parents:
632
diff
changeset
|
94 parser.add_argument( |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
689
diff
changeset
|
95 '--config', |
909
eed19a80c00e
chef: Allow multiple config variants to be applied.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
96 action='append', |
eed19a80c00e
chef: Allow multiple config variants to be applied.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
97 dest='config_variants', |
eed19a80c00e
chef: Allow multiple config variants to be applied.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
98 help="The configuration variant(s) to use for this command.") |
583
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
99 parser.add_argument( |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
689
diff
changeset
|
100 '--config-set', |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
689
diff
changeset
|
101 nargs=2, |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
689
diff
changeset
|
102 metavar=('NAME', 'VALUE'), |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
689
diff
changeset
|
103 action='append', |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
689
diff
changeset
|
104 dest='config_values', |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
689
diff
changeset
|
105 help="Sets a specific site configuration setting.") |
583
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
106 parser.add_argument( |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
689
diff
changeset
|
107 '--debug', |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
689
diff
changeset
|
108 help="Show debug information.", action='store_true') |
583
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
109 parser.add_argument( |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
689
diff
changeset
|
110 '--debug-only', |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
689
diff
changeset
|
111 action='append', |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
689
diff
changeset
|
112 help="Only show debug information for the given categories.") |
583
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
113 parser.add_argument( |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
689
diff
changeset
|
114 '--no-cache', |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
689
diff
changeset
|
115 help="When applicable, disable caching.", |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
689
diff
changeset
|
116 action='store_true') |
583
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
117 parser.add_argument( |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
689
diff
changeset
|
118 '--quiet', |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
689
diff
changeset
|
119 help="Print only important information.", |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
689
diff
changeset
|
120 action='store_true') |
583
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
121 parser.add_argument( |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
689
diff
changeset
|
122 '--log', |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
689
diff
changeset
|
123 dest='log_file', |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
689
diff
changeset
|
124 help="Send log messages to the specified file.") |
583
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
125 parser.add_argument( |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
689
diff
changeset
|
126 '--log-debug', |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
689
diff
changeset
|
127 help="Log debug messages to the log file.", |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
689
diff
changeset
|
128 action='store_true') |
583
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
129 parser.add_argument( |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
689
diff
changeset
|
130 '--no-color', |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
689
diff
changeset
|
131 help="Don't use colorized output.", |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
689
diff
changeset
|
132 action='store_true') |
612
2edaefcb82cd
chef: Add `--pid-file` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
583
diff
changeset
|
133 parser.add_argument( |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
689
diff
changeset
|
134 '--pid-file', |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
689
diff
changeset
|
135 dest='pid_file', |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
689
diff
changeset
|
136 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
|
137 |
091f99bfbe44
Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
138 |
666
81d9c3a3a0b5
internal: Get rid of the whole "sub cache" business.
Ludovic Chabant <ludovic@chabant.com>
parents:
663
diff
changeset
|
139 """ Kinda hacky, but we want the `serve` command to use a different cache |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
689
diff
changeset
|
140 so that PieCrust doesn't need to re-render all the pages when going |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
689
diff
changeset
|
141 between `serve` and `bake` (or, worse, *not* re-render them all correctly |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
689
diff
changeset
|
142 and end up serving or baking the wrong version). |
666
81d9c3a3a0b5
internal: Get rid of the whole "sub cache" business.
Ludovic Chabant <ludovic@chabant.com>
parents:
663
diff
changeset
|
143 """ |
81d9c3a3a0b5
internal: Get rid of the whole "sub cache" business.
Ludovic Chabant <ludovic@chabant.com>
parents:
663
diff
changeset
|
144 _command_caches = { |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
689
diff
changeset
|
145 'serve': 'server'} |
666
81d9c3a3a0b5
internal: Get rid of the whole "sub cache" business.
Ludovic Chabant <ludovic@chabant.com>
parents:
663
diff
changeset
|
146 |
81d9c3a3a0b5
internal: Get rid of the whole "sub cache" business.
Ludovic Chabant <ludovic@chabant.com>
parents:
663
diff
changeset
|
147 |
979
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
909
diff
changeset
|
148 def _make_chef_state(): |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
909
diff
changeset
|
149 return [] |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
909
diff
changeset
|
150 |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
909
diff
changeset
|
151 |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
909
diff
changeset
|
152 def _recover_pre_chef_state(state): |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
909
diff
changeset
|
153 for s in state: |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
909
diff
changeset
|
154 s() |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
909
diff
changeset
|
155 |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
909
diff
changeset
|
156 |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
909
diff
changeset
|
157 def _pre_parse_chef_args(argv, *, bypass_setup=False, state=None): |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
158 # 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
|
159 # 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
|
160 # 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
|
161 # from the beginning. |
583
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
162 parser = argparse.ArgumentParser() |
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
163 _setup_main_parser_arguments(parser) |
666
81d9c3a3a0b5
internal: Get rid of the whole "sub cache" business.
Ludovic Chabant <ludovic@chabant.com>
parents:
663
diff
changeset
|
164 parser.add_argument('extra_args', nargs=argparse.REMAINDER) |
583
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
165 res, _ = parser.parse_known_args(argv) |
979
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
909
diff
changeset
|
166 if bypass_setup: |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
909
diff
changeset
|
167 return res |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
168 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
169 # 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
|
170 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
|
171 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
|
172 |
579
42a5e78e782a
cli: Add `--no-color` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
564
diff
changeset
|
173 strip_colors = None |
42a5e78e782a
cli: Add `--no-color` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
564
diff
changeset
|
174 if res.no_color: |
42a5e78e782a
cli: Add `--no-color` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
564
diff
changeset
|
175 strip_colors = True |
42a5e78e782a
cli: Add `--no-color` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
564
diff
changeset
|
176 |
42a5e78e782a
cli: Add `--no-color` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
564
diff
changeset
|
177 colorama.init(strip=strip_colors) |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
178 root_logger = logging.getLogger() |
979
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
909
diff
changeset
|
179 previous_level = root_logger.level |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
180 root_logger.setLevel(logging.INFO) |
97
00a9b24ca944
Add `--log-debug` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
69
diff
changeset
|
181 if res.debug or res.log_debug: |
00a9b24ca944
Add `--log-debug` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
69
diff
changeset
|
182 root_logger.setLevel(logging.DEBUG) |
979
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
909
diff
changeset
|
183 if state is not None: |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
909
diff
changeset
|
184 state.append(lambda: root_logger.setLevel(previous_level)) |
97
00a9b24ca944
Add `--log-debug` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
69
diff
changeset
|
185 |
583
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
186 if res.debug_only: |
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
187 for n in res.debug_only: |
979
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
909
diff
changeset
|
188 sub_logger = logging.getLogger(n) |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
909
diff
changeset
|
189 previous_level = sub_logger.level |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
909
diff
changeset
|
190 sub_logger.setLevel(logging.DEBUG) |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
909
diff
changeset
|
191 if state is not None: |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
909
diff
changeset
|
192 state.append(lambda: sub_logger.setLevel(previous_level)) |
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
|
193 |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
194 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
|
195 if res.debug or res.debug_only: |
97
00a9b24ca944
Add `--log-debug` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
69
diff
changeset
|
196 log_handler.setLevel(logging.DEBUG) |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
197 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
|
198 else: |
38
091f99bfbe44
Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
199 if res.quiet: |
97
00a9b24ca944
Add `--log-debug` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
69
diff
changeset
|
200 log_handler.setLevel(logging.WARNING) |
00a9b24ca944
Add `--log-debug` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
69
diff
changeset
|
201 else: |
00a9b24ca944
Add `--log-debug` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
69
diff
changeset
|
202 log_handler.setLevel(logging.INFO) |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
203 log_handler.setFormatter(ColoredFormatter("%(message)s")) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
204 root_logger.addHandler(log_handler) |
979
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
909
diff
changeset
|
205 if state is not None: |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
909
diff
changeset
|
206 state.append(lambda: root_logger.removeHandler(log_handler)) |
97
00a9b24ca944
Add `--log-debug` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
69
diff
changeset
|
207 |
38
091f99bfbe44
Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
208 if res.log_file: |
97
00a9b24ca944
Add `--log-debug` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
69
diff
changeset
|
209 file_handler = logging.FileHandler(res.log_file, mode='w') |
00a9b24ca944
Add `--log-debug` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
69
diff
changeset
|
210 root_logger.addHandler(file_handler) |
00a9b24ca944
Add `--log-debug` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
69
diff
changeset
|
211 if res.log_debug: |
00a9b24ca944
Add `--log-debug` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
69
diff
changeset
|
212 file_handler.setLevel(logging.DEBUG) |
979
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
909
diff
changeset
|
213 if state is not None: |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
909
diff
changeset
|
214 state.append(lambda: root_logger.removeHandler(file_handler)) |
38
091f99bfbe44
Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
215 |
612
2edaefcb82cd
chef: Add `--pid-file` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
583
diff
changeset
|
216 # PID file. |
2edaefcb82cd
chef: Add `--pid-file` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
583
diff
changeset
|
217 if res.pid_file: |
2edaefcb82cd
chef: Add `--pid-file` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
583
diff
changeset
|
218 try: |
2edaefcb82cd
chef: Add `--pid-file` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
583
diff
changeset
|
219 pid_file_dir = os.path.dirname(res.pid_file) |
2edaefcb82cd
chef: Add `--pid-file` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
583
diff
changeset
|
220 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
|
221 os.makedirs(pid_file_dir) |
2edaefcb82cd
chef: Add `--pid-file` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
583
diff
changeset
|
222 |
2edaefcb82cd
chef: Add `--pid-file` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
583
diff
changeset
|
223 with open(res.pid_file, 'w') as fp: |
2edaefcb82cd
chef: Add `--pid-file` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
583
diff
changeset
|
224 fp.write(str(os.getpid())) |
2edaefcb82cd
chef: Add `--pid-file` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
583
diff
changeset
|
225 except OSError as ex: |
2edaefcb82cd
chef: Add `--pid-file` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
583
diff
changeset
|
226 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
|
227 |
38
091f99bfbe44
Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
228 return res |
091f99bfbe44
Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
229 |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
230 |
666
81d9c3a3a0b5
internal: Get rid of the whole "sub cache" business.
Ludovic Chabant <ludovic@chabant.com>
parents:
663
diff
changeset
|
231 def _build_cache_key(pre_args): |
81d9c3a3a0b5
internal: Get rid of the whole "sub cache" business.
Ludovic Chabant <ludovic@chabant.com>
parents:
663
diff
changeset
|
232 cache_key_str = 'default' |
81d9c3a3a0b5
internal: Get rid of the whole "sub cache" business.
Ludovic Chabant <ludovic@chabant.com>
parents:
663
diff
changeset
|
233 if pre_args.extra_args: |
81d9c3a3a0b5
internal: Get rid of the whole "sub cache" business.
Ludovic Chabant <ludovic@chabant.com>
parents:
663
diff
changeset
|
234 cmd_name = pre_args.extra_args[0] |
81d9c3a3a0b5
internal: Get rid of the whole "sub cache" business.
Ludovic Chabant <ludovic@chabant.com>
parents:
663
diff
changeset
|
235 if cmd_name in _command_caches: |
81d9c3a3a0b5
internal: Get rid of the whole "sub cache" business.
Ludovic Chabant <ludovic@chabant.com>
parents:
663
diff
changeset
|
236 cache_key_str = _command_caches[cmd_name] |
909
eed19a80c00e
chef: Allow multiple config variants to be applied.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
237 if pre_args.config_variants: |
eed19a80c00e
chef: Allow multiple config variants to be applied.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
238 for value in pre_args.config_variants: |
eed19a80c00e
chef: Allow multiple config variants to be applied.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
239 cache_key_str += ',variant=%s' % value |
666
81d9c3a3a0b5
internal: Get rid of the whole "sub cache" business.
Ludovic Chabant <ludovic@chabant.com>
parents:
663
diff
changeset
|
240 if pre_args.config_values: |
81d9c3a3a0b5
internal: Get rid of the whole "sub cache" business.
Ludovic Chabant <ludovic@chabant.com>
parents:
663
diff
changeset
|
241 for name, value in pre_args.config_values: |
81d9c3a3a0b5
internal: Get rid of the whole "sub cache" business.
Ludovic Chabant <ludovic@chabant.com>
parents:
663
diff
changeset
|
242 cache_key_str += ',%s=%s' % (name, value) |
81d9c3a3a0b5
internal: Get rid of the whole "sub cache" business.
Ludovic Chabant <ludovic@chabant.com>
parents:
663
diff
changeset
|
243 |
81d9c3a3a0b5
internal: Get rid of the whole "sub cache" business.
Ludovic Chabant <ludovic@chabant.com>
parents:
663
diff
changeset
|
244 logger.debug("Using cache key: %s" % cache_key_str) |
81d9c3a3a0b5
internal: Get rid of the whole "sub cache" business.
Ludovic Chabant <ludovic@chabant.com>
parents:
663
diff
changeset
|
245 cache_key = hashlib.md5(cache_key_str.encode('utf8')).hexdigest() |
81d9c3a3a0b5
internal: Get rid of the whole "sub cache" business.
Ludovic Chabant <ludovic@chabant.com>
parents:
663
diff
changeset
|
246 return cache_key |
81d9c3a3a0b5
internal: Get rid of the whole "sub cache" business.
Ludovic Chabant <ludovic@chabant.com>
parents:
663
diff
changeset
|
247 |
81d9c3a3a0b5
internal: Get rid of the whole "sub cache" business.
Ludovic Chabant <ludovic@chabant.com>
parents:
663
diff
changeset
|
248 |
1129
957f7c972715
chef: Support special tokens in the `chef/env` setting.
Ludovic Chabant <ludovic@chabant.com>
parents:
1116
diff
changeset
|
249 def _setup_app_environment(app, env): |
957f7c972715
chef: Support special tokens in the `chef/env` setting.
Ludovic Chabant <ludovic@chabant.com>
parents:
1116
diff
changeset
|
250 from piecrust.uriutil import multi_replace |
957f7c972715
chef: Support special tokens in the `chef/env` setting.
Ludovic Chabant <ludovic@chabant.com>
parents:
1116
diff
changeset
|
251 |
957f7c972715
chef: Support special tokens in the `chef/env` setting.
Ludovic Chabant <ludovic@chabant.com>
parents:
1116
diff
changeset
|
252 tokens = { |
957f7c972715
chef: Support special tokens in the `chef/env` setting.
Ludovic Chabant <ludovic@chabant.com>
parents:
1116
diff
changeset
|
253 '%root_dir%': app.root_dir} |
957f7c972715
chef: Support special tokens in the `chef/env` setting.
Ludovic Chabant <ludovic@chabant.com>
parents:
1116
diff
changeset
|
254 |
1116
40228511d600
chef: Add new `chef/env` config section.
Ludovic Chabant <ludovic@chabant.com>
parents:
1016
diff
changeset
|
255 for k, v in env.items(): |
40228511d600
chef: Add new `chef/env` config section.
Ludovic Chabant <ludovic@chabant.com>
parents:
1016
diff
changeset
|
256 varname = k |
40228511d600
chef: Add new `chef/env` config section.
Ludovic Chabant <ludovic@chabant.com>
parents:
1016
diff
changeset
|
257 append = False |
40228511d600
chef: Add new `chef/env` config section.
Ludovic Chabant <ludovic@chabant.com>
parents:
1016
diff
changeset
|
258 if k.lower() == 'path': |
40228511d600
chef: Add new `chef/env` config section.
Ludovic Chabant <ludovic@chabant.com>
parents:
1016
diff
changeset
|
259 append = True |
40228511d600
chef: Add new `chef/env` config section.
Ludovic Chabant <ludovic@chabant.com>
parents:
1016
diff
changeset
|
260 v = os.pathsep + v |
40228511d600
chef: Add new `chef/env` config section.
Ludovic Chabant <ludovic@chabant.com>
parents:
1016
diff
changeset
|
261 elif k.endswith('+'): |
40228511d600
chef: Add new `chef/env` config section.
Ludovic Chabant <ludovic@chabant.com>
parents:
1016
diff
changeset
|
262 varname = k[:-1] |
40228511d600
chef: Add new `chef/env` config section.
Ludovic Chabant <ludovic@chabant.com>
parents:
1016
diff
changeset
|
263 append = True |
40228511d600
chef: Add new `chef/env` config section.
Ludovic Chabant <ludovic@chabant.com>
parents:
1016
diff
changeset
|
264 |
1129
957f7c972715
chef: Support special tokens in the `chef/env` setting.
Ludovic Chabant <ludovic@chabant.com>
parents:
1116
diff
changeset
|
265 v = multi_replace(v, tokens) |
957f7c972715
chef: Support special tokens in the `chef/env` setting.
Ludovic Chabant <ludovic@chabant.com>
parents:
1116
diff
changeset
|
266 |
1116
40228511d600
chef: Add new `chef/env` config section.
Ludovic Chabant <ludovic@chabant.com>
parents:
1016
diff
changeset
|
267 if append: |
40228511d600
chef: Add new `chef/env` config section.
Ludovic Chabant <ludovic@chabant.com>
parents:
1016
diff
changeset
|
268 logger.debug("Env: $%s += %s" % (varname, v)) |
40228511d600
chef: Add new `chef/env` config section.
Ludovic Chabant <ludovic@chabant.com>
parents:
1016
diff
changeset
|
269 os.environ[varname] += v |
40228511d600
chef: Add new `chef/env` config section.
Ludovic Chabant <ludovic@chabant.com>
parents:
1016
diff
changeset
|
270 else: |
40228511d600
chef: Add new `chef/env` config section.
Ludovic Chabant <ludovic@chabant.com>
parents:
1016
diff
changeset
|
271 logger.debug("Env: $%s = %s" % (varname, v)) |
40228511d600
chef: Add new `chef/env` config section.
Ludovic Chabant <ludovic@chabant.com>
parents:
1016
diff
changeset
|
272 os.environ[varname] = v |
40228511d600
chef: Add new `chef/env` config section.
Ludovic Chabant <ludovic@chabant.com>
parents:
1016
diff
changeset
|
273 |
40228511d600
chef: Add new `chef/env` config section.
Ludovic Chabant <ludovic@chabant.com>
parents:
1016
diff
changeset
|
274 |
384
d241585412ad
internal: Make it possible to pass `argv` to the main Chef function.
Ludovic Chabant <ludovic@chabant.com>
parents:
371
diff
changeset
|
275 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
|
276 # Setup the app. |
583
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
277 root = None |
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
278 if pre_args.root: |
1eda551ee681
cli: More proper argument parsing for the main/root arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
579
diff
changeset
|
279 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
|
280 else: |
38
091f99bfbe44
Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
281 try: |
663
3ceeca7bb71c
themes: Add support for a `--theme` argument to `chef`.
Ludovic Chabant <ludovic@chabant.com>
parents:
632
diff
changeset
|
282 root = find_app_root(theme=pre_args.theme) |
38
091f99bfbe44
Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
283 except SiteNotFoundError: |
091f99bfbe44
Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
284 root = None |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
285 |
666
81d9c3a3a0b5
internal: Get rid of the whole "sub cache" business.
Ludovic Chabant <ludovic@chabant.com>
parents:
663
diff
changeset
|
286 # Can't apply custom configuration stuff if there's no website. |
909
eed19a80c00e
chef: Allow multiple config variants to be applied.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
287 if (pre_args.config_variants or pre_args.config_values) and not root: |
666
81d9c3a3a0b5
internal: Get rid of the whole "sub cache" business.
Ludovic Chabant <ludovic@chabant.com>
parents:
663
diff
changeset
|
288 raise SiteNotFoundError( |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
689
diff
changeset
|
289 "Can't apply any configuration variant or value overrides, " |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
689
diff
changeset
|
290 "there is no website here.") |
666
81d9c3a3a0b5
internal: Get rid of the whole "sub cache" business.
Ludovic Chabant <ludovic@chabant.com>
parents:
663
diff
changeset
|
291 |
81d9c3a3a0b5
internal: Get rid of the whole "sub cache" business.
Ludovic Chabant <ludovic@chabant.com>
parents:
663
diff
changeset
|
292 if root: |
81d9c3a3a0b5
internal: Get rid of the whole "sub cache" business.
Ludovic Chabant <ludovic@chabant.com>
parents:
663
diff
changeset
|
293 cache_key = None |
81d9c3a3a0b5
internal: Get rid of the whole "sub cache" business.
Ludovic Chabant <ludovic@chabant.com>
parents:
663
diff
changeset
|
294 if not pre_args.no_cache: |
81d9c3a3a0b5
internal: Get rid of the whole "sub cache" business.
Ludovic Chabant <ludovic@chabant.com>
parents:
663
diff
changeset
|
295 cache_key = _build_cache_key(pre_args) |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
689
diff
changeset
|
296 appfactory = PieCrustFactory( |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
689
diff
changeset
|
297 root, |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
689
diff
changeset
|
298 theme_site=pre_args.theme, |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
689
diff
changeset
|
299 cache=(not pre_args.no_cache), |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
689
diff
changeset
|
300 cache_key=cache_key, |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
689
diff
changeset
|
301 debug=pre_args.debug, |
909
eed19a80c00e
chef: Allow multiple config variants to be applied.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
302 config_variants=pre_args.config_variants, |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
689
diff
changeset
|
303 config_values=pre_args.config_values) |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
689
diff
changeset
|
304 app = appfactory.create() |
666
81d9c3a3a0b5
internal: Get rid of the whole "sub cache" business.
Ludovic Chabant <ludovic@chabant.com>
parents:
663
diff
changeset
|
305 else: |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
689
diff
changeset
|
306 appfactory = None |
666
81d9c3a3a0b5
internal: Get rid of the whole "sub cache" business.
Ludovic Chabant <ludovic@chabant.com>
parents:
663
diff
changeset
|
307 app = NullPieCrust( |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
689
diff
changeset
|
308 theme_site=pre_args.theme) |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
309 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
310 # Setup the arg parser. |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
311 parser = argparse.ArgumentParser( |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
689
diff
changeset
|
312 prog='chef', |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
689
diff
changeset
|
313 description="The PieCrust chef manages your website.", |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
689
diff
changeset
|
314 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
|
315 _setup_main_parser_arguments(parser) |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
316 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
317 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
|
318 key=lambda c: c.name) |
99
8703be118430
Changes to `help` command and extendable commands:
Ludovic Chabant <ludovic@chabant.com>
parents:
98
diff
changeset
|
319 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
|
320 for c in commands: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
321 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
|
322 c.setupParser(p, app) |
57
c8c522dacfea
Add `help` function, cleanup argument handling.
Ludovic Chabant <ludovic@chabant.com>
parents:
44
diff
changeset
|
323 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
|
324 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
|
325 |
57
c8c522dacfea
Add `help` function, cleanup argument handling.
Ludovic Chabant <ludovic@chabant.com>
parents:
44
diff
changeset
|
326 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
|
327 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
|
328 with io.StringIO() as epilog: |
8703be118430
Changes to `help` command and extendable commands:
Ludovic Chabant <ludovic@chabant.com>
parents:
98
diff
changeset
|
329 epilog.write("additional help topics:\n") |
8703be118430
Changes to `help` command and extendable commands:
Ludovic Chabant <ludovic@chabant.com>
parents:
98
diff
changeset
|
330 for name, desc in help_cmd.getTopics(): |
8703be118430
Changes to `help` command and extendable commands:
Ludovic Chabant <ludovic@chabant.com>
parents:
98
diff
changeset
|
331 print_help_item(epilog, name, desc) |
8703be118430
Changes to `help` command and extendable commands:
Ludovic Chabant <ludovic@chabant.com>
parents:
98
diff
changeset
|
332 parser.epilog = epilog.getvalue() |
57
c8c522dacfea
Add `help` function, cleanup argument handling.
Ludovic Chabant <ludovic@chabant.com>
parents:
44
diff
changeset
|
333 |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
334 # 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
|
335 result = parser.parse_args(argv) |
989
8adc27285d93
bake: Big pass on bake performance.
Ludovic Chabant <ludovic@chabant.com>
parents:
979
diff
changeset
|
336 logger.debug(format_timed(_chef_start_time, 'initialized PieCrust', |
341
bd726306d4dc
chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents:
307
diff
changeset
|
337 colored=False)) |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
338 |
61
64f37c4cce68
Print the help by default when running `chef` with no command.
Ludovic Chabant <ludovic@chabant.com>
parents:
57
diff
changeset
|
339 # 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
|
340 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
|
341 parser.print_help() |
64f37c4cce68
Print the help by default when running `chef` with no command.
Ludovic Chabant <ludovic@chabant.com>
parents:
57
diff
changeset
|
342 return 0 |
64f37c4cce68
Print the help by default when running `chef` with no command.
Ludovic Chabant <ludovic@chabant.com>
parents:
57
diff
changeset
|
343 |
1116
40228511d600
chef: Add new `chef/env` config section.
Ludovic Chabant <ludovic@chabant.com>
parents:
1016
diff
changeset
|
344 # Do any custom setup the user wants. |
40228511d600
chef: Add new `chef/env` config section.
Ludovic Chabant <ludovic@chabant.com>
parents:
1016
diff
changeset
|
345 custom_env = app.config.get('chef/env') |
40228511d600
chef: Add new `chef/env` config section.
Ludovic Chabant <ludovic@chabant.com>
parents:
1016
diff
changeset
|
346 if custom_env: |
1129
957f7c972715
chef: Support special tokens in the `chef/env` setting.
Ludovic Chabant <ludovic@chabant.com>
parents:
1116
diff
changeset
|
347 _setup_app_environment(app, custom_env) |
1116
40228511d600
chef: Add new `chef/env` config section.
Ludovic Chabant <ludovic@chabant.com>
parents:
1016
diff
changeset
|
348 |
989
8adc27285d93
bake: Big pass on bake performance.
Ludovic Chabant <ludovic@chabant.com>
parents:
979
diff
changeset
|
349 # Add some timing information. |
1016
c4cfbbeed72e
chef: Fix crash for commands run outside of a website.
Ludovic Chabant <ludovic@chabant.com>
parents:
989
diff
changeset
|
350 if app.env: |
c4cfbbeed72e
chef: Fix crash for commands run outside of a website.
Ludovic Chabant <ludovic@chabant.com>
parents:
989
diff
changeset
|
351 app.env.stats.registerTimer('ChefStartup') |
c4cfbbeed72e
chef: Fix crash for commands run outside of a website.
Ludovic Chabant <ludovic@chabant.com>
parents:
989
diff
changeset
|
352 app.env.stats.stepTimerSince('ChefStartup', _chef_start_time) |
989
8adc27285d93
bake: Big pass on bake performance.
Ludovic Chabant <ludovic@chabant.com>
parents:
979
diff
changeset
|
353 |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
354 # Run the command! |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
689
diff
changeset
|
355 ctx = CommandContext(appfactory, app, parser, result) |
57
c8c522dacfea
Add `help` function, cleanup argument handling.
Ludovic Chabant <ludovic@chabant.com>
parents:
44
diff
changeset
|
356 exit_code = result.func(ctx) |
98
5959a117a943
Exit with the proper code.
Ludovic Chabant <ludovic@chabant.com>
parents:
97
diff
changeset
|
357 if exit_code is None: |
5959a117a943
Exit with the proper code.
Ludovic Chabant <ludovic@chabant.com>
parents:
97
diff
changeset
|
358 return 0 |
5959a117a943
Exit with the proper code.
Ludovic Chabant <ludovic@chabant.com>
parents:
97
diff
changeset
|
359 if not isinstance(exit_code, int): |
5959a117a943
Exit with the proper code.
Ludovic Chabant <ludovic@chabant.com>
parents:
97
diff
changeset
|
360 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
|
361 return -1 |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
362 return exit_code |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
363 |