view 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
line wrap: on
line source

import re
import os
import os.path
import fnmatch
from piecrust import CONFIG_PATH, THEME_CONFIG_PATH


re_terminal_path = re.compile(r'^(\w\:)?[/\\]$')


class SiteNotFoundError(Exception):
    def __init__(self, root=None, msg=None, theme=False):
        if not root:
            root = os.getcwd()

        cfg_name = CONFIG_PATH
        if theme:
            cfg_name = THEME_CONFIG_PATH

        full_msg = ("No PieCrust website in '%s' "
                    "('%s' not found!)" % (root, cfg_name))
        if msg:
            full_msg += ": " + msg
        else:
            full_msg += "."
        Exception.__init__(self, full_msg)


def find_app_root(cwd=None, theme=False):
    if cwd is None:
        cwd = os.getcwd()

    cfg_name = CONFIG_PATH
    if theme:
        cfg_name = THEME_CONFIG_PATH

    while not os.path.isfile(os.path.join(cwd, cfg_name)):
        cwd = os.path.dirname(cwd)
        if not cwd or re_terminal_path.match(cwd):
            raise SiteNotFoundError(cwd, theme=theme)
    return cwd


def multi_fnmatch_filter(names, patterns, modifier=None, inverse=True):
    res = []
    for n in names:
        matches = False
        test_n = modifier(n) if modifier else n
        for p in patterns:
            if fnmatch.fnmatch(test_n, p):
                matches = True
                break
        if matches and not inverse:
            res.append(n)
        elif not matches and inverse:
            res.append(n)
    return res


def ensure_dir(path, mode=0o755):
    try:
        os.makedirs(path, mode=mode, exist_ok=True)
    except OSError:
        pass


def expandall(path):
    path = os.path.expandvars(path)
    path = os.path.expanduser(path)
    return path