From 9d583961e700667b7f1e78c4f2e0c88954becf15 Mon Sep 17 00:00:00 2001 From: bmcorser Date: Wed, 21 Aug 2013 19:57:55 +0100 Subject: [PATCH] Fix for THEME_STATIC_PATHS by copying sensitively If more than one path is defined in THEME_STATIC_PATHS, the theme's static directory in output is deleted and replaced by the following path's files. Using `shutil.rmtree` to remove the entire destination tree if overwrite is `True` assumes that we didn't want anything at all that was there. We should recurse through the directory and their subdirs instead, leaving things put there by the previous path where they were. I lazily copied almost verbatim the solution for recursively copying a diectory from http://stackoverflow.com/a/1994840. The reason for this is patch is that without it, my plugin is broken! It also makes my code a lot less crazy: https://github.com/bmcorser/pelicanfly/commit/a83f066 --- pelican/utils.py | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/pelican/utils.py b/pelican/utils.py index 054c1f40..e028c84c 100644 --- a/pelican/utils.py +++ b/pelican/utils.py @@ -275,16 +275,24 @@ def copy(path, source, destination, destination_path=None, overwrite=False): destination_ = os.path.abspath( os.path.expanduser(os.path.join(destination, destination_path))) + def recurse(source, destination): + for entry in os.listdir(source): + entry_path = os.path.join(source, entry) + if os.path.isdir(entry_path): + entry_dest = os.path.join(destination, entry) + if os.path.exists(entry_dest): + if not os.path.isdir(entry_dest): + raise IOError('Failed to copy {0} a directory.' + .format(entry_dest)) + recurse(entry_path, entry_dest) + else: + shutil.copytree(entry_path, entry_dest) + else: + shutil.copy(entry_path, destination) + + if os.path.isdir(source_): - try: - shutil.copytree(source_, destination_) - logger.info('copying %s to %s' % (source_, destination_)) - except OSError: - if overwrite: - shutil.rmtree(destination_) - shutil.copytree(source_, destination_) - logger.info('replacement of %s with %s' % (source_, - destination_)) + recurse(source_, destination_) elif os.path.isfile(source_): dest_dir = os.path.dirname(destination_)