Detect non-zero command exit codes
authorThomas Perl <thp@thpinfo.com>
Tue, 29 Jun 2010 23:53:22 +0000 (30 01:53 +0200)
committerThomas Perl <thp@thpinfo.com>
Tue, 29 Jun 2010 23:53:22 +0000 (30 01:53 +0200)
If a shell commands exists with a non-zero
status, urlwatch considers this as an error
and will optionally report the error to the
user (not overwriting the last output data).

lib/urlwatch/handler.py
urlwatch

index d02f6ee..1748a12 100755 (executable)
@@ -65,12 +65,25 @@ class JobBase(object):
     def retrieve(self, timestamp=None, filter=None, headers=None):
         raise Exception('Not implemented')
 
+class ShellError(Exception):
+    """Exception for shell commands with non-zero exit code"""
+
+    def __init__(self, result):
+        Exception.__init__(self)
+        self.result = result
+
+    def __str__(self):
+        return '%s: Exit status %d' % (self.__class__.__name__, self.result)
+
 class ShellJob(JobBase):
     def retrieve(self, timestamp=None, filter=None, headers=None):
         process = subprocess.Popen(self.location, \
                 stdout=subprocess.PIPE, \
                 shell=True)
         stdout_data, stderr_data = process.communicate()
+        result = process.wait()
+        if result != 0:
+            raise ShellError(result)
         return filter(self.location, stdout_data)
 
 class UrlJob(JobBase):
index 021012a..5eb1bb3 100755 (executable)
--- a/urlwatch
+++ b/urlwatch
@@ -260,6 +260,10 @@ if __name__ == '__main__':
                 log.error('got HTTPError while loading url: %s' % error)
                 if display_errors:
                     details += foutput('error', job, error, summary)
+        except handler.ShellError, error:
+            log.error('Shell returned %d' % error.result)
+            if display_errors:
+                details += foutput('error', job, error, summary)
         except urllib2.URLError, error:
             log.error('got URLError while loading url: %s' % error)
             if display_errors: