From d589450200c1ce9e14eefad63dc099767af64ada Mon Sep 17 00:00:00 2001 From: tBunnyMan Date: Wed, 4 Jul 2012 12:32:20 -0700 Subject: [PATCH 1/4] Change behavior of DELETE_OUTPUT_DIRECTORY to purge contents of the directory, not the directory itself. Added Debug level logging for each deletion Added error level logging when a delete has an exception Built a test case for clean_output_directory in tests.utils.py Updated `settings` in documentation to reflect new behavior Removed the tip from the bottom of getting started since this setting should no longer break web servers pointing to the output directory. --- docs/getting_started.rst | 2 -- docs/settings.rst | 4 ++-- pelican/utils.py | 20 ++++++++++++++++---- tests/test_utils.py | 11 +++++++++++ 4 files changed, 29 insertions(+), 8 deletions(-) diff --git a/docs/getting_started.rst b/docs/getting_started.rst index cd186a1c..a0975bd5 100644 --- a/docs/getting_started.rst +++ b/docs/getting_started.rst @@ -234,5 +234,3 @@ Or run a simple web server using Python:: cd output && python -m SimpleHTTPServer -(Tip: If using the latter method in conjunction with the auto-reload feature, -ensure that ``DELETE_OUTPUT_DIRECTORY`` is set to ``False`` in your settings file.) diff --git a/docs/settings.rst b/docs/settings.rst index 65561d5d..c5aa7f4e 100644 --- a/docs/settings.rst +++ b/docs/settings.rst @@ -37,8 +37,8 @@ Setting name (default value) What doe timestamp information (mtime) if it can't get date information from the metadata. `JINJA_EXTENSIONS` (``[]``) A list of any Jinja2 extensions you want to use. -`DELETE_OUTPUT_DIRECTORY` (``False``) Delete the output directory as well as - the generated files. +`DELETE_OUTPUT_DIRECTORY` (``False``) Delete the contents of the output directory before + generating new files. `LOCALE` (''[#]_) Change the locale. A list of locales can be provided here or a single string representing one locale. When providing a list, all the locales will be tried diff --git a/pelican/utils.py b/pelican/utils.py index 0940bf72..a1c40077 100644 --- a/pelican/utils.py +++ b/pelican/utils.py @@ -92,10 +92,22 @@ def clean_output_dir(path): """Remove all the files from the output directory""" # remove all the existing content from the output folder - try: - shutil.rmtree(path) - except Exception: - pass + for filename in os.listdir(path): + file = os.path.join(path, filename) + if os.path.isdir(file): + logger.debug("Deleting directory %s" % file) + try: + shutil.rmtree(file) + except Exception, e: + logger.error("Unable to delete directory %s; %e" % file, e) + elif os.path.isfile(file) or os.path.islink(file): + logger.debug("Deleting file/link %s" % file) + try: + os.remove(file) + except Exception, e: + logger.error("Unable to delete file %s; %e" % file, e) + else: + logger.error("Unable to delete %s, file type unknown" % file) def get_relative_path(filename): diff --git a/tests/test_utils.py b/tests/test_utils.py index e738e295..d2f44131 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +import shutil import os import datetime import time @@ -86,3 +87,13 @@ class TestUtils(unittest.TestCase): changed = utils.files_changed(path, 'rst') self.assertEquals(changed, True) self.assertAlmostEqual(utils.LAST_MTIME, t, delta=1) + + def test_clean_output_dir(self): + test_directory = os.path.join(os.path.dirname(__file__), 'clean_output') + content = os.path.join(os.path.dirname(__file__), 'content') + shutil.copytree(content, test_directory) + utils.clean_output_dir(test_directory) + self.assertTrue(os.path.isdir(test_directory)) + self.assertListEqual([], os.listdir(test_directory)) + shutil.rmtree(test_directory) + From 2b062ce384c5e75e4af0d137351d97aea8c5ed39 Mon Sep 17 00:00:00 2001 From: tBunnyMan Date: Wed, 4 Jul 2012 14:55:39 -0700 Subject: [PATCH 2/4] Documentation typo --- docs/settings.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/settings.rst b/docs/settings.rst index c5aa7f4e..78ede790 100644 --- a/docs/settings.rst +++ b/docs/settings.rst @@ -37,7 +37,7 @@ Setting name (default value) What doe timestamp information (mtime) if it can't get date information from the metadata. `JINJA_EXTENSIONS` (``[]``) A list of any Jinja2 extensions you want to use. -`DELETE_OUTPUT_DIRECTORY` (``False``) Delete the contents of the output directory before +`DELETE_OUTPUT_DIRECTORY` (``False``) Delete the content of the output directory before generating new files. `LOCALE` (''[#]_) Change the locale. A list of locales can be provided here or a single string representing one locale. From c07821e8a210c70eef91067223cebe388481ad71 Mon Sep 17 00:00:00 2001 From: tBunnyMan Date: Wed, 4 Jul 2012 14:56:52 -0700 Subject: [PATCH 3/4] Too many empty lines at end of file --- tests/test_utils.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/test_utils.py b/tests/test_utils.py index d2f44131..2e3f714b 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -96,4 +96,3 @@ class TestUtils(unittest.TestCase): self.assertTrue(os.path.isdir(test_directory)) self.assertListEqual([], os.listdir(test_directory)) shutil.rmtree(test_directory) - From 4427424db39c95890a30d2a6293d129002e2dc04 Mon Sep 17 00:00:00 2001 From: tBunnyMan Date: Wed, 4 Jul 2012 15:20:15 -0700 Subject: [PATCH 4/4] Changed debugging reporting to post file deletion instead of pre file deletion per request. --- pelican/utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pelican/utils.py b/pelican/utils.py index a1c40077..f0f742db 100644 --- a/pelican/utils.py +++ b/pelican/utils.py @@ -95,15 +95,15 @@ def clean_output_dir(path): for filename in os.listdir(path): file = os.path.join(path, filename) if os.path.isdir(file): - logger.debug("Deleting directory %s" % file) try: shutil.rmtree(file) + logger.debug("Deleted directory %s" % file) except Exception, e: logger.error("Unable to delete directory %s; %e" % file, e) elif os.path.isfile(file) or os.path.islink(file): - logger.debug("Deleting file/link %s" % file) try: os.remove(file) + logger.debug("Deleted file/link %s" % file) except Exception, e: logger.error("Unable to delete file %s; %e" % file, e) else: