implement utils.mtime
authorTero Marttila <terom@fixme.fi>
Wed, 11 Feb 2009 02:21:43 +0200
changeset 95 ebdbda3dd5d0
parent 94 6673de9bc911
child 96 d30c88e89a7e
implement utils.mtime
scripts/search-index
utils.py
--- a/scripts/search-index	Wed Feb 11 02:16:11 2009 +0200
+++ b/scripts/search-index	Wed Feb 11 02:21:43 2009 +0200
@@ -277,19 +277,14 @@
                 print "reloading all:",
 
         # stat for mtime
-        # XXX: replace with single utils.mtime()
-        elif os.path.exists(statefile_path) :
-            # get last update date for channel
-            mtime = utils.from_utc_timestamp(os.stat(statefile_path).st_mtime)
-            
-            if not options.quiet :
+        else :
+            # stat for mtime, None if unknown
+            mtime = utils.mtime(statefile_path, ignore_missing=True)
+
+            if mtime and not options.quiet :
                 print "last load=%s:" % mtime,
 
-        else :
-            # unknown, load all
-            mtime = None
-            
-            if not options.quiet :
+            elif not options.quiet :
                 print "no previous load state:",
  
         # only after some specific date?
--- a/utils.py	Wed Feb 11 02:16:11 2009 +0200
+++ b/utils.py	Wed Feb 11 02:21:43 2009 +0200
@@ -3,6 +3,7 @@
 """
 
 import datetime, calendar, pytz
+import os, errno
 
 from qmsk.web.urltree import URLType
 
@@ -91,3 +92,25 @@
 
     return calendar.timegm(dt.utctimetuple())
 
+def mtime (path, ignore_missing=False) :
+    """
+        Gets the mtime for the given path as an UTC datetime, or None, if the file doesn't exist and ignore_missing
+    """
+
+    try :
+        # stat
+        st = os.stat(path)
+    
+    # trap IOError
+    except os.error, e :
+        # ENOENT?
+        if ignore_missing and e.errno == errno.ENOENT :
+            return None
+
+        else :
+            raise
+
+    else :
+        # decode
+        return from_utc_timestamp(st.st_mtime)
+