annotate piecrust/sources/mixins.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 13e8b50a2113
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
242
f130365568ff internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1 import os.path
f130365568ff internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2 import logging
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 711
diff changeset
3 from piecrust import osutil
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 711
diff changeset
4 from piecrust.sources.base import ContentItem
242
f130365568ff internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
5
f130365568ff internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
6
f130365568ff internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
7 logger = logging.getLogger(__name__)
f130365568ff internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
8
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 711
diff changeset
9 assets_suffix = '-assets'
242
f130365568ff internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
10
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 711
diff changeset
11
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 711
diff changeset
12 class SimpleAssetsSubDirMixin:
862
fddaf43424e2 refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents: 853
diff changeset
13 """ A content source mixin for sources that are file-system-based,
fddaf43424e2 refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents: 853
diff changeset
14 and have item assets stored in a sub-folder that is named after
fddaf43424e2 refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents: 853
diff changeset
15 the item.
fddaf43424e2 refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents: 853
diff changeset
16
fddaf43424e2 refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents: 853
diff changeset
17 More specifically, assets are stored in a sub-folder named:
fddaf43424e2 refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents: 853
diff changeset
18 `<item_path>-assets`
fddaf43424e2 refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents: 853
diff changeset
19 """
fddaf43424e2 refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents: 853
diff changeset
20 def _getRelatedAssetsContents(self, item):
fddaf43424e2 refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents: 853
diff changeset
21 spec_no_ext, _ = os.path.splitext(item.spec)
fddaf43424e2 refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents: 853
diff changeset
22 assets_dir = spec_no_ext + assets_suffix
fddaf43424e2 refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents: 853
diff changeset
23 try:
885
13e8b50a2113 publish: Fix publishers API and add a simple "copy" publisher.
Ludovic Chabant <ludovic@chabant.com>
parents: 862
diff changeset
24 asset_files = list(osutil.listdir(assets_dir))
13e8b50a2113 publish: Fix publishers API and add a simple "copy" publisher.
Ludovic Chabant <ludovic@chabant.com>
parents: 862
diff changeset
25 except (OSError, FileNotFoundError):
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 711
diff changeset
26 return None
242
f130365568ff internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
27
862
fddaf43424e2 refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents: 853
diff changeset
28 assets = []
fddaf43424e2 refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents: 853
diff changeset
29 for f in asset_files:
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 711
diff changeset
30 fpath = os.path.join(assets_dir, f)
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 711
diff changeset
31 name, _ = os.path.splitext(f)
862
fddaf43424e2 refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents: 853
diff changeset
32 assets.append(ContentItem(
fddaf43424e2 refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents: 853
diff changeset
33 fpath,
fddaf43424e2 refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents: 853
diff changeset
34 {'name': name,
fddaf43424e2 refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents: 853
diff changeset
35 'filename': f,
fddaf43424e2 refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents: 853
diff changeset
36 '__is_asset': True}))
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 711
diff changeset
37 return assets
242
f130365568ff internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
38
862
fddaf43424e2 refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents: 853
diff changeset
39 def _removeAssetGroups(self, groups):
fddaf43424e2 refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents: 853
diff changeset
40 asset_groups = [g for g in groups
fddaf43424e2 refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents: 853
diff changeset
41 if g.spec.endswith(assets_suffix)]
fddaf43424e2 refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents: 853
diff changeset
42 for g in asset_groups:
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 711
diff changeset
43 groups.remove(g)