From 565058df3e98ba45945b6254b5c69c3de399bcb7 Mon Sep 17 00:00:00 2001 From: Michael Merickel Date: Wed, 28 Oct 2015 12:15:07 -0500 Subject: avoid spamming by outputting state only once when a file changes --- pyramid/scripts/pserve.py | 46 ++++++++++++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/pyramid/scripts/pserve.py b/pyramid/scripts/pserve.py index 1184d7faa..4a6e917a7 100644 --- a/pyramid/scripts/pserve.py +++ b/pyramid/scripts/pserve.py @@ -844,7 +844,8 @@ class Monitor(object): # pragma: no cover self.poll_interval = poll_interval self.extra_files = list(self.global_extra_files) self.instances.append(self) - self.syntax_error_files = [] + self.syntax_error_files = set() + self.pending_reload = False self.file_callbacks = list(self.global_file_callbacks) def _exit(self): @@ -892,25 +893,34 @@ class Monitor(object): # pragma: no cover pyc = True else: pyc = False - if filename not in self.module_mtimes: - self.module_mtimes[filename] = mtime - elif self.module_mtimes[filename] < mtime: + old_mtime = self.module_mtimes.get(filename) + self.module_mtimes[filename] = mtime + if old_mtime is not None and old_mtime < mtime: + self.pending_reload = True if pyc: filename = filename[:-1] - # check if a file has syntax errors. If so, track it until it's fixed - for filename_promise in self.syntax_error_files + [filename]: - try: - py_compile.compile(filename_promise, doraise=True) - except py_compile.PyCompileError: - print("%s has a SyntaxError; NOT reloading." % filename_promise) - if filename_promise not in self.syntax_error_files: - self.syntax_error_files.append(filename_promise) - else: - if filename_promise in self.syntax_error_files: - self.syntax_error_files.remove(filename_promise) - if not self.syntax_error_files: - print("%s changed; reloading..." % filename) - return False + is_valid = True + if filename.endswith('.py'): + is_valid = self.check_syntax(filename) + if is_valid: + print("%s changed ..." % filename) + if self.pending_reload and not self.syntax_error_files: + self.pending_reload = False + return False + return True + + def check_syntax(self, filename): + # check if a file has syntax errors. + # If so, track it until it's fixed. + try: + py_compile.compile(filename, doraise=True) + except py_compile.PyCompileError: + print("%s has a SyntaxError; NOT reloading." % filename) + self.syntax_error_files.add(filename) + return False + else: + if filename in self.syntax_error_files: + self.syntax_error_files.remove(filename) return True def watch_file(self, cls, filename): -- cgit v1.2.3 From 93e886be234fd187c4ddc5e376fd6c51060500a7 Mon Sep 17 00:00:00 2001 From: Michael Merickel Date: Fri, 13 Nov 2015 12:51:00 -0600 Subject: print out every file that has a syntax error --- pyramid/scripts/pserve.py | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/pyramid/scripts/pserve.py b/pyramid/scripts/pserve.py index 4a6e917a7..1e580d897 100644 --- a/pyramid/scripts/pserve.py +++ b/pyramid/scripts/pserve.py @@ -18,6 +18,7 @@ import py_compile import re import subprocess import sys +import tempfile import textwrap import threading import time @@ -847,8 +848,16 @@ class Monitor(object): # pragma: no cover self.syntax_error_files = set() self.pending_reload = False self.file_callbacks = list(self.global_file_callbacks) + temp_pyc_fp = tempfile.NamedTemporaryFile(delete=False) + self.temp_pyc = temp_pyc_fp.name + temp_pyc_fp.close() def _exit(self): + try: + os.unlink(self.temp_pyc) + except IOError: + # not worried if the tempfile can't be removed + pass # use os._exit() here and not sys.exit() since within a # thread sys.exit() just closes the given thread and # won't kill the process; note os._exit does not call @@ -879,6 +888,7 @@ class Monitor(object): # pragma: no cover continue if filename is not None: filenames.append(filename) + new_changes = False for filename in filenames: try: stat = os.stat(filename) @@ -896,7 +906,7 @@ class Monitor(object): # pragma: no cover old_mtime = self.module_mtimes.get(filename) self.module_mtimes[filename] = mtime if old_mtime is not None and old_mtime < mtime: - self.pending_reload = True + new_changes = True if pyc: filename = filename[:-1] is_valid = True @@ -904,6 +914,11 @@ class Monitor(object): # pragma: no cover is_valid = self.check_syntax(filename) if is_valid: print("%s changed ..." % filename) + if new_changes: + self.pending_reload = True + if self.syntax_error_files: + for filename in sorted(self.syntax_error_files): + print("%s has a SyntaxError; NOT reloading." % filename) if self.pending_reload and not self.syntax_error_files: self.pending_reload = False return False @@ -913,9 +928,9 @@ class Monitor(object): # pragma: no cover # check if a file has syntax errors. # If so, track it until it's fixed. try: - py_compile.compile(filename, doraise=True) - except py_compile.PyCompileError: - print("%s has a SyntaxError; NOT reloading." % filename) + py_compile.compile(filename, cfile=self.temp_pyc, doraise=True) + except py_compile.PyCompileError as ex: + print(ex.msg) self.syntax_error_files.add(filename) return False else: -- cgit v1.2.3