3
# mmmm m m mmmm mmmm mmm mm#mm
4
# #" "# # # #" "# #" "# #" # #
5
# # # # # # # # # #"""" #
6
# ##m#" "mm"# ##m#" ##m#" "#mm" "mm
9
# This file is managed by puppet. Do not make local changes.
11
# Copyright (C) 2005, 2006, 2007, 2012 James Troup <james.troup@canonical.com>
20
################################################################################
22
class CriticalError(Exception):
23
"""This indicates a critical error."""
27
class WarnError(Exception):
28
"""This indicates a warning condition."""
32
class UnknownError(Exception):
33
"""This indicates a unknown error was encountered."""
37
def try_check(function, *args, **kwargs):
38
"""Perform a check with error/warn/unknown handling."""
40
function(*args, **kwargs)
41
except UnknownError, msg:
44
except CriticalError, msg:
47
except WarnError, msg:
51
print "%s raised unknown exception '%s'" % (function, sys.exc_info()[0])
53
traceback.print_exc(file=sys.stdout)
58
################################################################################
60
def check_file_freshness(filename, newer_than=600):
61
"""Check a file exists, is readable and is newer than <n> seconds (where <n> defaults to 600)."""
62
# First check the file exists and is readable
63
if not os.path.exists(filename):
64
raise CriticalError("%s: does not exist." % (filename))
65
if os.access(filename, os.R_OK) == 0:
66
raise CriticalError("%s: is not readable." % (filename))
68
# Then ensure the file is up-to-date enough
69
mtime = os.stat(filename)[stat.ST_MTIME]
70
last_modified = time.time() - mtime
71
if last_modified > newer_than:
72
raise CriticalError("%s: was last modified on %s and is too old (> %s seconds)."
73
% (filename, time.ctime(mtime), newer_than))
75
raise CriticalError("%s: was last modified on %s which is in the future."
76
% (filename, time.ctime(mtime)))
78
################################################################################