annotate piecrust/pathutil.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 40a40305c4e1
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
38
091f99bfbe44 Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents: 36
diff changeset
1 import re
0
a212a3f2e3ee Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2 import os
a212a3f2e3ee Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
3 import os.path
62
52e4d9a1f917 Simple importer for PieCrust 1 websites.
Ludovic Chabant <ludovic@chabant.com>
parents: 38
diff changeset
4 import fnmatch
663
3ceeca7bb71c themes: Add support for a `--theme` argument to `chef`.
Ludovic Chabant <ludovic@chabant.com>
parents: 115
diff changeset
5 from piecrust import CONFIG_PATH, THEME_CONFIG_PATH
0
a212a3f2e3ee Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
6
a212a3f2e3ee Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
7
115
9c074aec60a6 Fix search for root folder. Must have been drunk when I wrote this originally.
Ludovic Chabant <ludovic@chabant.com>
parents: 62
diff changeset
8 re_terminal_path = re.compile(r'^(\w\:)?[/\\]$')
38
091f99bfbe44 Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents: 36
diff changeset
9
091f99bfbe44 Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents: 36
diff changeset
10
0
a212a3f2e3ee Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
11 class SiteNotFoundError(Exception):
663
3ceeca7bb71c themes: Add support for a `--theme` argument to `chef`.
Ludovic Chabant <ludovic@chabant.com>
parents: 115
diff changeset
12 def __init__(self, root=None, msg=None, theme=False):
0
a212a3f2e3ee Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
13 if not root:
a212a3f2e3ee Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
14 root = os.getcwd()
663
3ceeca7bb71c themes: Add support for a `--theme` argument to `chef`.
Ludovic Chabant <ludovic@chabant.com>
parents: 115
diff changeset
15
3ceeca7bb71c themes: Add support for a `--theme` argument to `chef`.
Ludovic Chabant <ludovic@chabant.com>
parents: 115
diff changeset
16 cfg_name = CONFIG_PATH
3ceeca7bb71c themes: Add support for a `--theme` argument to `chef`.
Ludovic Chabant <ludovic@chabant.com>
parents: 115
diff changeset
17 if theme:
3ceeca7bb71c themes: Add support for a `--theme` argument to `chef`.
Ludovic Chabant <ludovic@chabant.com>
parents: 115
diff changeset
18 cfg_name = THEME_CONFIG_PATH
3ceeca7bb71c themes: Add support for a `--theme` argument to `chef`.
Ludovic Chabant <ludovic@chabant.com>
parents: 115
diff changeset
19
38
091f99bfbe44 Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents: 36
diff changeset
20 full_msg = ("No PieCrust website in '%s' "
663
3ceeca7bb71c themes: Add support for a `--theme` argument to `chef`.
Ludovic Chabant <ludovic@chabant.com>
parents: 115
diff changeset
21 "('%s' not found!)" % (root, cfg_name))
38
091f99bfbe44 Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents: 36
diff changeset
22 if msg:
091f99bfbe44 Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents: 36
diff changeset
23 full_msg += ": " + msg
091f99bfbe44 Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents: 36
diff changeset
24 else:
091f99bfbe44 Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents: 36
diff changeset
25 full_msg += "."
091f99bfbe44 Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents: 36
diff changeset
26 Exception.__init__(self, full_msg)
0
a212a3f2e3ee Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
27
a212a3f2e3ee Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
28
663
3ceeca7bb71c themes: Add support for a `--theme` argument to `chef`.
Ludovic Chabant <ludovic@chabant.com>
parents: 115
diff changeset
29 def find_app_root(cwd=None, theme=False):
0
a212a3f2e3ee Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
30 if cwd is None:
a212a3f2e3ee Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
31 cwd = os.getcwd()
a212a3f2e3ee Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
32
663
3ceeca7bb71c themes: Add support for a `--theme` argument to `chef`.
Ludovic Chabant <ludovic@chabant.com>
parents: 115
diff changeset
33 cfg_name = CONFIG_PATH
3ceeca7bb71c themes: Add support for a `--theme` argument to `chef`.
Ludovic Chabant <ludovic@chabant.com>
parents: 115
diff changeset
34 if theme:
3ceeca7bb71c themes: Add support for a `--theme` argument to `chef`.
Ludovic Chabant <ludovic@chabant.com>
parents: 115
diff changeset
35 cfg_name = THEME_CONFIG_PATH
3ceeca7bb71c themes: Add support for a `--theme` argument to `chef`.
Ludovic Chabant <ludovic@chabant.com>
parents: 115
diff changeset
36
3ceeca7bb71c themes: Add support for a `--theme` argument to `chef`.
Ludovic Chabant <ludovic@chabant.com>
parents: 115
diff changeset
37 while not os.path.isfile(os.path.join(cwd, cfg_name)):
0
a212a3f2e3ee Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
38 cwd = os.path.dirname(cwd)
38
091f99bfbe44 Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents: 36
diff changeset
39 if not cwd or re_terminal_path.match(cwd):
663
3ceeca7bb71c themes: Add support for a `--theme` argument to `chef`.
Ludovic Chabant <ludovic@chabant.com>
parents: 115
diff changeset
40 raise SiteNotFoundError(cwd, theme=theme)
0
a212a3f2e3ee Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
41 return cwd
a212a3f2e3ee Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
42
62
52e4d9a1f917 Simple importer for PieCrust 1 websites.
Ludovic Chabant <ludovic@chabant.com>
parents: 38
diff changeset
43
52e4d9a1f917 Simple importer for PieCrust 1 websites.
Ludovic Chabant <ludovic@chabant.com>
parents: 38
diff changeset
44 def multi_fnmatch_filter(names, patterns, modifier=None, inverse=True):
52e4d9a1f917 Simple importer for PieCrust 1 websites.
Ludovic Chabant <ludovic@chabant.com>
parents: 38
diff changeset
45 res = []
52e4d9a1f917 Simple importer for PieCrust 1 websites.
Ludovic Chabant <ludovic@chabant.com>
parents: 38
diff changeset
46 for n in names:
52e4d9a1f917 Simple importer for PieCrust 1 websites.
Ludovic Chabant <ludovic@chabant.com>
parents: 38
diff changeset
47 matches = False
52e4d9a1f917 Simple importer for PieCrust 1 websites.
Ludovic Chabant <ludovic@chabant.com>
parents: 38
diff changeset
48 test_n = modifier(n) if modifier else n
52e4d9a1f917 Simple importer for PieCrust 1 websites.
Ludovic Chabant <ludovic@chabant.com>
parents: 38
diff changeset
49 for p in patterns:
52e4d9a1f917 Simple importer for PieCrust 1 websites.
Ludovic Chabant <ludovic@chabant.com>
parents: 38
diff changeset
50 if fnmatch.fnmatch(test_n, p):
52e4d9a1f917 Simple importer for PieCrust 1 websites.
Ludovic Chabant <ludovic@chabant.com>
parents: 38
diff changeset
51 matches = True
52e4d9a1f917 Simple importer for PieCrust 1 websites.
Ludovic Chabant <ludovic@chabant.com>
parents: 38
diff changeset
52 break
52e4d9a1f917 Simple importer for PieCrust 1 websites.
Ludovic Chabant <ludovic@chabant.com>
parents: 38
diff changeset
53 if matches and not inverse:
52e4d9a1f917 Simple importer for PieCrust 1 websites.
Ludovic Chabant <ludovic@chabant.com>
parents: 38
diff changeset
54 res.append(n)
52e4d9a1f917 Simple importer for PieCrust 1 websites.
Ludovic Chabant <ludovic@chabant.com>
parents: 38
diff changeset
55 elif not matches and inverse:
52e4d9a1f917 Simple importer for PieCrust 1 websites.
Ludovic Chabant <ludovic@chabant.com>
parents: 38
diff changeset
56 res.append(n)
52e4d9a1f917 Simple importer for PieCrust 1 websites.
Ludovic Chabant <ludovic@chabant.com>
parents: 38
diff changeset
57 return res
52e4d9a1f917 Simple importer for PieCrust 1 websites.
Ludovic Chabant <ludovic@chabant.com>
parents: 38
diff changeset
58
1114
8af2ea1f5c34 tasks: Add new `tasks` command and infrastructure, with `mention` task.
Ludovic Chabant <ludovic@chabant.com>
parents: 663
diff changeset
59
8af2ea1f5c34 tasks: Add new `tasks` command and infrastructure, with `mention` task.
Ludovic Chabant <ludovic@chabant.com>
parents: 663
diff changeset
60 def ensure_dir(path, mode=0o755):
8af2ea1f5c34 tasks: Add new `tasks` command and infrastructure, with `mention` task.
Ludovic Chabant <ludovic@chabant.com>
parents: 663
diff changeset
61 try:
8af2ea1f5c34 tasks: Add new `tasks` command and infrastructure, with `mention` task.
Ludovic Chabant <ludovic@chabant.com>
parents: 663
diff changeset
62 os.makedirs(path, mode=mode, exist_ok=True)
8af2ea1f5c34 tasks: Add new `tasks` command and infrastructure, with `mention` task.
Ludovic Chabant <ludovic@chabant.com>
parents: 663
diff changeset
63 except OSError:
8af2ea1f5c34 tasks: Add new `tasks` command and infrastructure, with `mention` task.
Ludovic Chabant <ludovic@chabant.com>
parents: 663
diff changeset
64 pass
1157
40a40305c4e1 config: Support environment variables in theme directories.
Ludovic Chabant <ludovic@chabant.com>
parents: 1114
diff changeset
65
40a40305c4e1 config: Support environment variables in theme directories.
Ludovic Chabant <ludovic@chabant.com>
parents: 1114
diff changeset
66
40a40305c4e1 config: Support environment variables in theme directories.
Ludovic Chabant <ludovic@chabant.com>
parents: 1114
diff changeset
67 def expandall(path):
40a40305c4e1 config: Support environment variables in theme directories.
Ludovic Chabant <ludovic@chabant.com>
parents: 1114
diff changeset
68 path = os.path.expandvars(path)
40a40305c4e1 config: Support environment variables in theme directories.
Ludovic Chabant <ludovic@chabant.com>
parents: 1114
diff changeset
69 path = os.path.expanduser(path)
40a40305c4e1 config: Support environment variables in theme directories.
Ludovic Chabant <ludovic@chabant.com>
parents: 1114
diff changeset
70 return path