From 86da6d1f2e954ae575f0b3a78d39eb3106a11c46 Mon Sep 17 00:00:00 2001 From: tBunnyMan Date: Thu, 23 Aug 2012 11:05:07 -0700 Subject: [PATCH 1/3] Check for value error caused by no valid files found with files_changed This causes an infinite loop when in auto-reload Fix #467 Fix #451 Fix #443 --- pelican/utils.py | 12 ++++++++---- tests/test_utils.py | 15 ++++++++++++++- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/pelican/utils.py b/pelican/utils.py index 53e6e52b..c79c7cee 100644 --- a/pelican/utils.py +++ b/pelican/utils.py @@ -241,10 +241,14 @@ def files_changed(path, extensions): yield os.stat(os.path.join(root, f)).st_mtime global LAST_MTIME - mtime = max(file_times(path)) - if mtime > LAST_MTIME: - LAST_MTIME = mtime - return True + try: + mtime = max(file_times(path)) + if mtime > LAST_MTIME: + LAST_MTIME = mtime + return True + except ValueError: + logger.info("No files found in path") + return False return False diff --git a/tests/test_utils.py b/tests/test_utils.py index 2ea756dc..7ea0cf23 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -74,7 +74,8 @@ class TestUtils(unittest.TestCase): self.assertNotIn(fr_article1, index) def test_files_changed(self): - "Test if file changes are correctly detected" + """Test if file changes are correctly detected + Make sure to handle not getting any files correctly""" path = os.path.join(os.path.dirname(__file__), 'content') filename = os.path.join(path, 'article_with_metadata.rst') @@ -90,6 +91,18 @@ class TestUtils(unittest.TestCase): self.assertEquals(changed, True) self.assertAlmostEqual(utils.LAST_MTIME, t, delta=1) + empty_path = os.path.join(os.path.dirname(__file__), 'empty') + try: + os.mkdir(empty_path) + os.mkdir(os.path.join(empty_path, "empty_folder")) + shutil.copy(__file__, empty_path) + changed = utils.files_changed(empty_path, 'rst') + self.assertFalse(changed) + except OSError: + self.fail("OSError Exception in test_files_changed test") + finally: + shutil.rmtree(empty_path, True) + 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') From a37ba369ef49ae1643a5b7602860c6be804cf8ad Mon Sep 17 00:00:00 2001 From: tBunnyMan Date: Thu, 23 Aug 2012 12:44:22 -0700 Subject: [PATCH 2/3] Implemented better "valid files not found" behavior. Used an exception so show error state. Used a bool flag to make sure the error is only shown once PER error. Updated tests to check for the correct Exception raised --- pelican/__init__.py | 9 ++++++++- pelican/utils.py | 6 ++++-- tests/test_utils.py | 4 ++-- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/pelican/__init__.py b/pelican/__init__.py index ba48c4c7..64e334d4 100644 --- a/pelican/__init__.py +++ b/pelican/__init__.py @@ -11,7 +11,7 @@ from pelican.generators import (ArticlesGenerator, PagesGenerator, StaticGenerator, PdfGenerator, LessCSSGenerator) from pelican.log import init from pelican.settings import read_settings, _DEFAULT_CONFIG -from pelican.utils import clean_output_dir, files_changed, file_changed +from pelican.utils import clean_output_dir, files_changed, file_changed, NoFilesError from pelican.writers import Writer __major__ = 3 @@ -265,6 +265,7 @@ def main(): try: if args.autoreload: + files_found_error = True while True: try: # Check source dir for changed files ending with the given @@ -274,6 +275,8 @@ def main(): # have. if files_changed(pelican.path, pelican.markup) or \ files_changed(pelican.theme, ['']): + if files_found_error == False: + files_found_error = True pelican.run() # reload also if settings.py changed @@ -287,6 +290,10 @@ def main(): except KeyboardInterrupt: logger.warning("Keyboard interrupt, quitting.") break + except NoFilesError: + if files_found_error == True: + logger.warning("No valid files found in content. Nothing to generate.") + files_found_error = False except Exception, e: logger.warning( "Caught exception \"{}\". Reloading.".format(e) diff --git a/pelican/utils.py b/pelican/utils.py index c79c7cee..ca3015ce 100644 --- a/pelican/utils.py +++ b/pelican/utils.py @@ -14,6 +14,9 @@ from operator import attrgetter logger = logging.getLogger(__name__) +class NoFilesError(Exception): + pass + def get_date(string): """Return a datetime object from a string. @@ -247,8 +250,7 @@ def files_changed(path, extensions): LAST_MTIME = mtime return True except ValueError: - logger.info("No files found in path") - return False + raise NoFilesError("No files with the given extension(s) found.") return False diff --git a/tests/test_utils.py b/tests/test_utils.py index 7ea0cf23..0ebaf346 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -96,8 +96,8 @@ class TestUtils(unittest.TestCase): os.mkdir(empty_path) os.mkdir(os.path.join(empty_path, "empty_folder")) shutil.copy(__file__, empty_path) - changed = utils.files_changed(empty_path, 'rst') - self.assertFalse(changed) + with self.assertRaises(NoFilesError): + utils.files_changed(empty_path, 'rst') except OSError: self.fail("OSError Exception in test_files_changed test") finally: From 95af2e46ec95647a055a029bc81b531ecca86bb6 Mon Sep 17 00:00:00 2001 From: tBunnyMan Date: Thu, 23 Aug 2012 13:13:41 -0700 Subject: [PATCH 3/3] Missed a line in commit. --- tests/test_utils.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_utils.py b/tests/test_utils.py index 0ebaf346..148e322a 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -6,6 +6,7 @@ import time from pelican import utils from .support import get_article, unittest +from pelican.utils import NoFilesError class TestUtils(unittest.TestCase):