view piecrust/publishing/copy.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
line wrap: on
line source

import os
import os.path
import shutil
import logging
from piecrust.publishing.base import Publisher


logger = logging.getLogger(__name__)


class CopyPublisher(Publisher):
    PUBLISHER_NAME = 'copy'
    PUBLISHER_SCHEME = 'file'

    def parseUrlTarget(self, url):
        self.config = {'output': (url.netloc + url.path)}

    def run(self, ctx):
        dest = self.config.get('output')

        if ctx.was_baked:
            to_upload = list(self.getBakedFiles(ctx))
            to_delete = list(self.getDeletedFiles(ctx))
            if to_upload or to_delete:
                logger.info("Copying new/changed files...")
                for path in to_upload:
                    rel_path = os.path.relpath(path, ctx.bake_out_dir)
                    dest_path = os.path.join(dest, rel_path)
                    dest_dir = os.path.dirname(dest_path)
                    os.makedirs(dest_dir, exist_ok=True)
                    try:
                        dest_mtime = os.path.getmtime(dest_path)
                    except OSError:
                        dest_mtime = 0
                    if os.path.getmtime(path) >= dest_mtime:
                        logger.info(rel_path)
                        if not ctx.preview:
                            shutil.copyfile(path, dest_path)

                logger.info("Deleting removed files...")
                for path in self.getDeletedFiles(ctx):
                    rel_path = os.path.relpath(path, ctx.bake_out_dir)
                    logger.info("%s [DELETE]" % rel_path)
                    if not ctx.preview:
                        try:
                            os.remove(path)
                        except OSError:
                            pass
            else:
                logger.info("Nothing to copy to the output folder.")