annotate piecrust/commands/builtin/util.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 58ae026b4c31
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
aaa8fb7c8918 Re-arranged modules to reduce dependencies to builtin stuff.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1 import os
aaa8fb7c8918 Re-arranged modules to reduce dependencies to builtin stuff.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2 import os.path
aaa8fb7c8918 Re-arranged modules to reduce dependencies to builtin stuff.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
3 import logging
748
6db54bed2417 init: Use a better config template when creating websites.
Ludovic Chabant <ludovic@chabant.com>
parents: 726
diff changeset
4 from piecrust import CACHE_DIR, RESOURCES_DIR
663
3ceeca7bb71c themes: Add support for a `--theme` argument to `chef`.
Ludovic Chabant <ludovic@chabant.com>
parents: 371
diff changeset
5 from piecrust.app import CONFIG_PATH, THEME_CONFIG_PATH
1
aaa8fb7c8918 Re-arranged modules to reduce dependencies to builtin stuff.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
6 from piecrust.commands.base import ChefCommand
aaa8fb7c8918 Re-arranged modules to reduce dependencies to builtin stuff.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
7
aaa8fb7c8918 Re-arranged modules to reduce dependencies to builtin stuff.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
8
aaa8fb7c8918 Re-arranged modules to reduce dependencies to builtin stuff.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
9 logger = logging.getLogger(__name__)
aaa8fb7c8918 Re-arranged modules to reduce dependencies to builtin stuff.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
10
aaa8fb7c8918 Re-arranged modules to reduce dependencies to builtin stuff.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
11
aaa8fb7c8918 Re-arranged modules to reduce dependencies to builtin stuff.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
12 class InitCommand(ChefCommand):
aaa8fb7c8918 Re-arranged modules to reduce dependencies to builtin stuff.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
13 def __init__(self):
aaa8fb7c8918 Re-arranged modules to reduce dependencies to builtin stuff.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
14 super(InitCommand, self).__init__()
aaa8fb7c8918 Re-arranged modules to reduce dependencies to builtin stuff.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
15 self.name = 'init'
aaa8fb7c8918 Re-arranged modules to reduce dependencies to builtin stuff.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
16 self.description = "Creates a new empty PieCrust website."
aaa8fb7c8918 Re-arranged modules to reduce dependencies to builtin stuff.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
17 self.requires_website = False
aaa8fb7c8918 Re-arranged modules to reduce dependencies to builtin stuff.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
18
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 1
diff changeset
19 def setupParser(self, parser, app):
663
3ceeca7bb71c themes: Add support for a `--theme` argument to `chef`.
Ludovic Chabant <ludovic@chabant.com>
parents: 371
diff changeset
20 parser.add_argument(
879
58ae026b4c31 chef: Optimize startup time.
Ludovic Chabant <ludovic@chabant.com>
parents: 748
diff changeset
21 'destination',
58ae026b4c31 chef: Optimize startup time.
Ludovic Chabant <ludovic@chabant.com>
parents: 748
diff changeset
22 help="The destination directory in which to create the website.")
1
aaa8fb7c8918 Re-arranged modules to reduce dependencies to builtin stuff.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
23
aaa8fb7c8918 Re-arranged modules to reduce dependencies to builtin stuff.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
24 def run(self, ctx):
aaa8fb7c8918 Re-arranged modules to reduce dependencies to builtin stuff.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
25 destination = ctx.args.destination
aaa8fb7c8918 Re-arranged modules to reduce dependencies to builtin stuff.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
26 if destination is None:
aaa8fb7c8918 Re-arranged modules to reduce dependencies to builtin stuff.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
27 destination = os.getcwd()
aaa8fb7c8918 Re-arranged modules to reduce dependencies to builtin stuff.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
28
aaa8fb7c8918 Re-arranged modules to reduce dependencies to builtin stuff.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
29 if not os.path.isdir(destination):
5
474c9882decf Upgrade to Python 3.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
30 os.makedirs(destination, 0o755)
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 1
diff changeset
31
1
aaa8fb7c8918 Re-arranged modules to reduce dependencies to builtin stuff.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
32 config_path = os.path.join(destination, CONFIG_PATH)
663
3ceeca7bb71c themes: Add support for a `--theme` argument to `chef`.
Ludovic Chabant <ludovic@chabant.com>
parents: 371
diff changeset
33 if ctx.args.theme:
3ceeca7bb71c themes: Add support for a `--theme` argument to `chef`.
Ludovic Chabant <ludovic@chabant.com>
parents: 371
diff changeset
34 config_path = os.path.join(destination, THEME_CONFIG_PATH)
3ceeca7bb71c themes: Add support for a `--theme` argument to `chef`.
Ludovic Chabant <ludovic@chabant.com>
parents: 371
diff changeset
35
1
aaa8fb7c8918 Re-arranged modules to reduce dependencies to builtin stuff.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
36 if not os.path.isdir(os.path.dirname(config_path)):
5
474c9882decf Upgrade to Python 3.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
37 os.makedirs(os.path.dirname(config_path), 0o755)
1
aaa8fb7c8918 Re-arranged modules to reduce dependencies to builtin stuff.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
38
748
6db54bed2417 init: Use a better config template when creating websites.
Ludovic Chabant <ludovic@chabant.com>
parents: 726
diff changeset
39 tpl_path = os.path.join(RESOURCES_DIR, 'webinit', CONFIG_PATH)
6db54bed2417 init: Use a better config template when creating websites.
Ludovic Chabant <ludovic@chabant.com>
parents: 726
diff changeset
40 if ctx.args.theme:
879
58ae026b4c31 chef: Optimize startup time.
Ludovic Chabant <ludovic@chabant.com>
parents: 748
diff changeset
41 tpl_path = os.path.join(RESOURCES_DIR, 'webinit',
58ae026b4c31 chef: Optimize startup time.
Ludovic Chabant <ludovic@chabant.com>
parents: 748
diff changeset
42 THEME_CONFIG_PATH)
748
6db54bed2417 init: Use a better config template when creating websites.
Ludovic Chabant <ludovic@chabant.com>
parents: 726
diff changeset
43 with open(tpl_path, 'r', encoding='utf-8') as fp:
6db54bed2417 init: Use a better config template when creating websites.
Ludovic Chabant <ludovic@chabant.com>
parents: 726
diff changeset
44 config_text = fp.read()
6db54bed2417 init: Use a better config template when creating websites.
Ludovic Chabant <ludovic@chabant.com>
parents: 726
diff changeset
45
6db54bed2417 init: Use a better config template when creating websites.
Ludovic Chabant <ludovic@chabant.com>
parents: 726
diff changeset
46 with open(config_path, 'w', encoding='utf-8') as fp:
1
aaa8fb7c8918 Re-arranged modules to reduce dependencies to builtin stuff.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
47 fp.write(config_text)
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 1
diff changeset
48
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 1
diff changeset
49
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 1
diff changeset
50 class PurgeCommand(ChefCommand):
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 1
diff changeset
51 def __init__(self):
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 1
diff changeset
52 super(PurgeCommand, self).__init__()
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 1
diff changeset
53 self.name = 'purge'
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 1
diff changeset
54 self.description = "Purges the website's cache."
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 1
diff changeset
55
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 1
diff changeset
56 def setupParser(self, parser, app):
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 1
diff changeset
57 pass
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 1
diff changeset
58
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 1
diff changeset
59 def run(self, ctx):
879
58ae026b4c31 chef: Optimize startup time.
Ludovic Chabant <ludovic@chabant.com>
parents: 748
diff changeset
60 import shutil
58ae026b4c31 chef: Optimize startup time.
Ludovic Chabant <ludovic@chabant.com>
parents: 748
diff changeset
61
726
6e92b5cd0163 purge: Delete the whole cache directory, not just the current sub-cache.
Ludovic Chabant <ludovic@chabant.com>
parents: 666
diff changeset
62 cache_dir = os.path.join(ctx.app.root_dir, CACHE_DIR)
371
c2ca72fb7f0b caching: Use separate caches for config variants and other contexts.
Ludovic Chabant <ludovic@chabant.com>
parents: 304
diff changeset
63 if cache_dir and os.path.isdir(cache_dir):
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 1
diff changeset
64 logger.info("Purging cache: %s" % cache_dir)
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 1
diff changeset
65 shutil.rmtree(cache_dir)
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 1
diff changeset
66
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 1
diff changeset
67
60
6e60e0fef2be Add `import` command, Jekyll importer.
Ludovic Chabant <ludovic@chabant.com>
parents: 51
diff changeset
68 class ImportCommand(ChefCommand):
6e60e0fef2be Add `import` command, Jekyll importer.
Ludovic Chabant <ludovic@chabant.com>
parents: 51
diff changeset
69 def __init__(self):
6e60e0fef2be Add `import` command, Jekyll importer.
Ludovic Chabant <ludovic@chabant.com>
parents: 51
diff changeset
70 super(ImportCommand, self).__init__()
6e60e0fef2be Add `import` command, Jekyll importer.
Ludovic Chabant <ludovic@chabant.com>
parents: 51
diff changeset
71 self.name = 'import'
6e60e0fef2be Add `import` command, Jekyll importer.
Ludovic Chabant <ludovic@chabant.com>
parents: 51
diff changeset
72 self.description = "Imports content from another CMS into PieCrust."
6e60e0fef2be Add `import` command, Jekyll importer.
Ludovic Chabant <ludovic@chabant.com>
parents: 51
diff changeset
73
6e60e0fef2be Add `import` command, Jekyll importer.
Ludovic Chabant <ludovic@chabant.com>
parents: 51
diff changeset
74 def setupParser(self, parser, app):
6e60e0fef2be Add `import` command, Jekyll importer.
Ludovic Chabant <ludovic@chabant.com>
parents: 51
diff changeset
75 subparsers = parser.add_subparsers()
6e60e0fef2be Add `import` command, Jekyll importer.
Ludovic Chabant <ludovic@chabant.com>
parents: 51
diff changeset
76 for i in app.plugin_loader.getImporters():
297
2823ea40cfac import: Put importer metadata on the class, and allow return values.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
77 if not i.__class__.name:
2823ea40cfac import: Put importer metadata on the class, and allow return values.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
78 raise Exception("Importer '%s' has no name set." % type(i))
60
6e60e0fef2be Add `import` command, Jekyll importer.
Ludovic Chabant <ludovic@chabant.com>
parents: 51
diff changeset
79 p = subparsers.add_parser(i.name, help=i.description)
6e60e0fef2be Add `import` command, Jekyll importer.
Ludovic Chabant <ludovic@chabant.com>
parents: 51
diff changeset
80 i.setupParser(p, app)
6e60e0fef2be Add `import` command, Jekyll importer.
Ludovic Chabant <ludovic@chabant.com>
parents: 51
diff changeset
81 p.set_defaults(sub_func=i.checkedImportWebsite)
63
28958565a17b In-place upgrade for PieCrust 1 sites.
Ludovic Chabant <ludovic@chabant.com>
parents: 60
diff changeset
82 p.set_defaults(sub_requires_website=i.requires_website)
60
6e60e0fef2be Add `import` command, Jekyll importer.
Ludovic Chabant <ludovic@chabant.com>
parents: 51
diff changeset
83
63
28958565a17b In-place upgrade for PieCrust 1 sites.
Ludovic Chabant <ludovic@chabant.com>
parents: 60
diff changeset
84 def checkedRun(self, ctx):
304
34ef6a2a0c97 import: Show help if no sub-command was specified.
Ludovic Chabant <ludovic@chabant.com>
parents: 297
diff changeset
85 if not hasattr(ctx.args, 'sub_func'):
34ef6a2a0c97 import: Show help if no sub-command was specified.
Ludovic Chabant <ludovic@chabant.com>
parents: 297
diff changeset
86 ctx.parser.parse_args(['import', '--help'])
34ef6a2a0c97 import: Show help if no sub-command was specified.
Ludovic Chabant <ludovic@chabant.com>
parents: 297
diff changeset
87 return
60
6e60e0fef2be Add `import` command, Jekyll importer.
Ludovic Chabant <ludovic@chabant.com>
parents: 51
diff changeset
88 ctx.args.sub_func(ctx)
6e60e0fef2be Add `import` command, Jekyll importer.
Ludovic Chabant <ludovic@chabant.com>
parents: 51
diff changeset
89