annotate piecrust/commands/builtin/publishing.py @ 1188:a7c43131d871

bake: Fix file write flushing problem with Python 3.8+ Writing the cache files fails in Python 3.8 because it looks like flushing behaviour has changed. We need to explicitly flush. And even then, in very rare occurrences, it looks like it can still run into racing conditions, so we do a very hacky and ugly "retry" loop when fetching cached data :(
author Ludovic Chabant <ludovic@chabant.com>
date Tue, 15 Jun 2021 22:36:23 -0700
parents d709429f02eb
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
613
e2e955a3bb25 publish: Add publish command.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1 import logging
e2e955a3bb25 publish: Add publish command.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2 from piecrust.commands.base import ChefCommand
e2e955a3bb25 publish: Add publish command.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
3
e2e955a3bb25 publish: Add publish command.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
4
e2e955a3bb25 publish: Add publish command.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
5 logger = logging.getLogger(__name__)
e2e955a3bb25 publish: Add publish command.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
6
e2e955a3bb25 publish: Add publish command.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
7
e2e955a3bb25 publish: Add publish command.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
8 class PublishCommand(ChefCommand):
e2e955a3bb25 publish: Add publish command.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
9 """ Command for running publish targets for the current site.
e2e955a3bb25 publish: Add publish command.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
10 """
e2e955a3bb25 publish: Add publish command.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
11 def __init__(self):
e2e955a3bb25 publish: Add publish command.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
12 super(PublishCommand, self).__init__()
e2e955a3bb25 publish: Add publish command.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
13 self.name = 'publish'
e2e955a3bb25 publish: Add publish command.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
14 self.description = "Publishes you website to a specific target."
e2e955a3bb25 publish: Add publish command.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
15
e2e955a3bb25 publish: Add publish command.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
16 def setupParser(self, parser, app):
e2e955a3bb25 publish: Add publish command.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
17 parser.add_argument(
879
58ae026b4c31 chef: Optimize startup time.
Ludovic Chabant <ludovic@chabant.com>
parents: 781
diff changeset
18 '--log-publisher',
58ae026b4c31 chef: Optimize startup time.
Ludovic Chabant <ludovic@chabant.com>
parents: 781
diff changeset
19 metavar='LOG_FILE',
58ae026b4c31 chef: Optimize startup time.
Ludovic Chabant <ludovic@chabant.com>
parents: 781
diff changeset
20 help="Log the publisher's output to a given file.")
613
e2e955a3bb25 publish: Add publish command.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
21 parser.add_argument(
954
d709429f02eb publish: Add more options for logging, better feedback when it fails.
Ludovic Chabant <ludovic@chabant.com>
parents: 885
diff changeset
22 '--log-debug-info',
d709429f02eb publish: Add more options for logging, better feedback when it fails.
Ludovic Chabant <ludovic@chabant.com>
parents: 885
diff changeset
23 action='store_true',
d709429f02eb publish: Add more options for logging, better feedback when it fails.
Ludovic Chabant <ludovic@chabant.com>
parents: 885
diff changeset
24 help="Add some debug info as a preamble to the log file.")
d709429f02eb publish: Add more options for logging, better feedback when it fails.
Ludovic Chabant <ludovic@chabant.com>
parents: 885
diff changeset
25 parser.add_argument(
d709429f02eb publish: Add more options for logging, better feedback when it fails.
Ludovic Chabant <ludovic@chabant.com>
parents: 885
diff changeset
26 '--append-log',
d709429f02eb publish: Add more options for logging, better feedback when it fails.
Ludovic Chabant <ludovic@chabant.com>
parents: 885
diff changeset
27 action='store_true',
d709429f02eb publish: Add more options for logging, better feedback when it fails.
Ludovic Chabant <ludovic@chabant.com>
parents: 885
diff changeset
28 help="Append to the log file if it exists.")
d709429f02eb publish: Add more options for logging, better feedback when it fails.
Ludovic Chabant <ludovic@chabant.com>
parents: 885
diff changeset
29 parser.add_argument(
879
58ae026b4c31 chef: Optimize startup time.
Ludovic Chabant <ludovic@chabant.com>
parents: 781
diff changeset
30 '--preview',
58ae026b4c31 chef: Optimize startup time.
Ludovic Chabant <ludovic@chabant.com>
parents: 781
diff changeset
31 action='store_true',
58ae026b4c31 chef: Optimize startup time.
Ludovic Chabant <ludovic@chabant.com>
parents: 781
diff changeset
32 help="Only preview what the publisher would do.")
613
e2e955a3bb25 publish: Add publish command.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
33
781
71a755512eb8 chef: Don't crash when running `chef` outside of a website.
Ludovic Chabant <ludovic@chabant.com>
parents: 758
diff changeset
34 # Don't setup anything for a null app.
71a755512eb8 chef: Don't crash when running `chef` outside of a website.
Ludovic Chabant <ludovic@chabant.com>
parents: 758
diff changeset
35 if app.root_dir is None:
71a755512eb8 chef: Don't crash when running `chef` outside of a website.
Ludovic Chabant <ludovic@chabant.com>
parents: 758
diff changeset
36 return
71a755512eb8 chef: Don't crash when running `chef` outside of a website.
Ludovic Chabant <ludovic@chabant.com>
parents: 758
diff changeset
37
758
6abb436fea5b publish: Make publisher more powerful and better exposed on the command line.
Ludovic Chabant <ludovic@chabant.com>
parents: 621
diff changeset
38 subparsers = parser.add_subparsers()
6abb436fea5b publish: Make publisher more powerful and better exposed on the command line.
Ludovic Chabant <ludovic@chabant.com>
parents: 621
diff changeset
39 for pub in app.publishers:
6abb436fea5b publish: Make publisher more powerful and better exposed on the command line.
Ludovic Chabant <ludovic@chabant.com>
parents: 621
diff changeset
40 p = subparsers.add_parser(
879
58ae026b4c31 chef: Optimize startup time.
Ludovic Chabant <ludovic@chabant.com>
parents: 781
diff changeset
41 pub.target,
58ae026b4c31 chef: Optimize startup time.
Ludovic Chabant <ludovic@chabant.com>
parents: 781
diff changeset
42 help="Publish using target '%s'." % pub.target)
758
6abb436fea5b publish: Make publisher more powerful and better exposed on the command line.
Ludovic Chabant <ludovic@chabant.com>
parents: 621
diff changeset
43 pub.setupPublishParser(p, app)
6abb436fea5b publish: Make publisher more powerful and better exposed on the command line.
Ludovic Chabant <ludovic@chabant.com>
parents: 621
diff changeset
44 p.set_defaults(sub_func=self._doPublish)
6abb436fea5b publish: Make publisher more powerful and better exposed on the command line.
Ludovic Chabant <ludovic@chabant.com>
parents: 621
diff changeset
45 p.set_defaults(target=pub.target)
613
e2e955a3bb25 publish: Add publish command.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
46
758
6abb436fea5b publish: Make publisher more powerful and better exposed on the command line.
Ludovic Chabant <ludovic@chabant.com>
parents: 621
diff changeset
47 if not app.publishers:
6abb436fea5b publish: Make publisher more powerful and better exposed on the command line.
Ludovic Chabant <ludovic@chabant.com>
parents: 621
diff changeset
48 parser.epilog = (
6abb436fea5b publish: Make publisher more powerful and better exposed on the command line.
Ludovic Chabant <ludovic@chabant.com>
parents: 621
diff changeset
49 "No publishers have been defined. You can define publishers "
6abb436fea5b publish: Make publisher more powerful and better exposed on the command line.
Ludovic Chabant <ludovic@chabant.com>
parents: 621
diff changeset
50 "through the `publish` configuration settings. "
6abb436fea5b publish: Make publisher more powerful and better exposed on the command line.
Ludovic Chabant <ludovic@chabant.com>
parents: 621
diff changeset
51 "For more information see: "
6abb436fea5b publish: Make publisher more powerful and better exposed on the command line.
Ludovic Chabant <ludovic@chabant.com>
parents: 621
diff changeset
52 "https://bolt80.com/piecrust/en/latest/docs/publishing/")
6abb436fea5b publish: Make publisher more powerful and better exposed on the command line.
Ludovic Chabant <ludovic@chabant.com>
parents: 621
diff changeset
53
6abb436fea5b publish: Make publisher more powerful and better exposed on the command line.
Ludovic Chabant <ludovic@chabant.com>
parents: 621
diff changeset
54 def checkedRun(self, ctx):
879
58ae026b4c31 chef: Optimize startup time.
Ludovic Chabant <ludovic@chabant.com>
parents: 781
diff changeset
55 from piecrust.pathutil import SiteNotFoundError
58ae026b4c31 chef: Optimize startup time.
Ludovic Chabant <ludovic@chabant.com>
parents: 781
diff changeset
56
758
6abb436fea5b publish: Make publisher more powerful and better exposed on the command line.
Ludovic Chabant <ludovic@chabant.com>
parents: 621
diff changeset
57 if ctx.app.root_dir is None:
6abb436fea5b publish: Make publisher more powerful and better exposed on the command line.
Ludovic Chabant <ludovic@chabant.com>
parents: 621
diff changeset
58 raise SiteNotFoundError(theme=ctx.app.theme_site)
6abb436fea5b publish: Make publisher more powerful and better exposed on the command line.
Ludovic Chabant <ludovic@chabant.com>
parents: 621
diff changeset
59
6abb436fea5b publish: Make publisher more powerful and better exposed on the command line.
Ludovic Chabant <ludovic@chabant.com>
parents: 621
diff changeset
60 if not hasattr(ctx.args, 'sub_func'):
6abb436fea5b publish: Make publisher more powerful and better exposed on the command line.
Ludovic Chabant <ludovic@chabant.com>
parents: 621
diff changeset
61 ctx.parser.parse_args(['publish', '--help'])
613
e2e955a3bb25 publish: Add publish command.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
62 return
758
6abb436fea5b publish: Make publisher more powerful and better exposed on the command line.
Ludovic Chabant <ludovic@chabant.com>
parents: 621
diff changeset
63 ctx.args.sub_func(ctx)
613
e2e955a3bb25 publish: Add publish command.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
64
758
6abb436fea5b publish: Make publisher more powerful and better exposed on the command line.
Ludovic Chabant <ludovic@chabant.com>
parents: 621
diff changeset
65 def _doPublish(self, ctx):
885
13e8b50a2113 publish: Fix publishers API and add a simple "copy" publisher.
Ludovic Chabant <ludovic@chabant.com>
parents: 879
diff changeset
66 from piecrust.publishing.base import PublishingManager
879
58ae026b4c31 chef: Optimize startup time.
Ludovic Chabant <ludovic@chabant.com>
parents: 781
diff changeset
67
885
13e8b50a2113 publish: Fix publishers API and add a simple "copy" publisher.
Ludovic Chabant <ludovic@chabant.com>
parents: 879
diff changeset
68 pub = PublishingManager(ctx.appfactory, ctx.app)
621
8f9c0bdb3724 publish: Polish/refactor the publishing workflows.
Ludovic Chabant <ludovic@chabant.com>
parents: 613
diff changeset
69 pub.run(
879
58ae026b4c31 chef: Optimize startup time.
Ludovic Chabant <ludovic@chabant.com>
parents: 781
diff changeset
70 ctx.args.target,
58ae026b4c31 chef: Optimize startup time.
Ludovic Chabant <ludovic@chabant.com>
parents: 781
diff changeset
71 preview=ctx.args.preview,
58ae026b4c31 chef: Optimize startup time.
Ludovic Chabant <ludovic@chabant.com>
parents: 781
diff changeset
72 extra_args=ctx.args,
954
d709429f02eb publish: Add more options for logging, better feedback when it fails.
Ludovic Chabant <ludovic@chabant.com>
parents: 885
diff changeset
73 log_file=ctx.args.log_publisher,
d709429f02eb publish: Add more options for logging, better feedback when it fails.
Ludovic Chabant <ludovic@chabant.com>
parents: 885
diff changeset
74 log_debug_info=ctx.args.log_debug_info,
d709429f02eb publish: Add more options for logging, better feedback when it fails.
Ludovic Chabant <ludovic@chabant.com>
parents: 885
diff changeset
75 append_log_file=ctx.args.append_log)
613
e2e955a3bb25 publish: Add publish command.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
76