diff options
author | 2008-12-21 15:08:06 +0530 | |
---|---|---|
committer | 2008-12-21 15:08:06 +0530 | |
commit | 058627dd559a984439302270d0c59464ed8210af (patch) | |
tree | b767c46b07b0a06529b5e5302212cc2d3640d068 | |
parent | Trivial cleanups to check.py (diff) | |
download | gard-058627dd559a984439302270d0c59464ed8210af.tar.gz gard-058627dd559a984439302270d0c59464ed8210af.tar.bz2 gard-058627dd559a984439302270d0c59464ed8210af.zip |
Improve check.py
* Put each mirror class (distfiles, releases) in it's own, well, class
* Introduce a GardCheck.lag() function to just get the lag
* Other assorted cleanups
-rw-r--r-- | check.py | 108 |
1 files changed, 70 insertions, 38 deletions
@@ -9,22 +9,16 @@ class GardCheck: def __init__(self, url): self.url = url - # Overridde in child classes - def check(self): - return True - - def humanize_time(self, secs): - mins, secs = divmod(secs, 60) - hours, mins = divmod(mins, 60) - days, hours = divmod(hours, 24) - return '%02d d %02dh %02d m %02d s' % (days, hours, mins, secs) - - def timestamp_to_secs(self, ts): - return rfc822.mktime_tz(rfc822.parsedate_tz(ts)) + def check_file_exists(self, url): + ret = True + try: + f = urllib2.urlopen(url) + if len(f.read()) == 0: + raise IOError + except: + ret = False -class DistCheck(GardCheck): - # can only be used from Python 2.6 onwards - HTTP_TIMEOUT = 30 + return ret # Takes the URL to a timestamp.{chk|x} file and returns the # corresponding time stamp in seconds @@ -42,48 +36,86 @@ class DistCheck(GardCheck): # timestamp.x format? ts = float(date.split(' ')[0]) except: - print 'ERROR: Could not get timestamp from ' + url return None return ts - def _check_file_exists(self, url): - ret = True - try: - f = urllib2.urlopen(url) - if len(f.read()) == 0: - raise IOError - except: - ret = False + def get_lag(self, path, verbose=False): + ts = self._get_timestamp_from_url(self.url + path) + now = time.mktime(time.gmtime()) + if ts is None or now < ts: + return None + return now - ts - return ret + def humanize_time(self, secs): + mins, secs = divmod(secs, 60) + hours, mins = divmod(mins, 60) + days, hours = divmod(hours, 24) + return '%02d d %02dh %02d m %02d s' % (days, hours, mins, secs) + + def timestamp_to_secs(self, ts): + return rfc822.mktime_tz(rfc822.parsedate_tz(ts)) + + # Override these in child classes + + def check(self): + return True - def check(self, maxdlag, maxrlag, verbose=False): + def lag(self): + return None + +# Check distfiles mirrors +class DistfilesCheck(GardCheck): + def lag(self): + path = '/distfiles/timestamp.chk' + return self.get_lag(path) + + def check(self, maxlag, verbose=False): # XXX: Replace 'verbose' with a logger object - ret = True - # Verify that distfiles aren't lagging - now = time.mktime(time.gmtime()) - ts = self._get_timestamp_from_url(self.url+'/distfiles/timestamp.chk') - if ts is not None and ((now - ts) > maxdlag or (now - ts) < 0): + lag = self.lag() + + if lag is None: + print 'ERROR: Could not get distfiles timestamp for ' + self.url + ret = False + elif lag > maxlag: if verbose is True: - print 'ERROR: distfiles at %s is lagging - delta is %s' \ - % (self.url, self.humanize_time(now - ts)) + print 'ERROR: distfiles at %s is lagging\n\tdelta is %s' \ + % (self.url, self.humanize_time(lag)) ret = False + else: + ret = True + return ret + +# Check releases mirrors +class ReleasesCheck(GardCheck): + def lag(self): + path = '/releases/.test/timestamp.x' + return self.get_lag(path) + + def check(self, maxlag, verbose=False): # Verify that releases aren't lagging - ts = self._get_timestamp_from_url(self.url+'/releases/.test/timestamp.x') - if ts is not None and ((now - ts) > maxrlag or (now - ts) < 0): + + lag = self.lag() + + if lag is None: + print 'ERROR: Could not get releases timestamp for ' + self.url + ret = False + elif lag > maxlag: if verbose is True: - print 'ERROR: releases at %s is lagging - delta is %s' \ - % (self.url, self.humanize_time(now - ts)) + print 'ERROR: releases at %s is lagging\n\tdelta is %s' \ + % (self.url, self.humanize_time(lag)) ret = False + else: + ret = True # Verify that releases/.test/THIS-FILE-SHOULD-NOT-BE-PUBLIC.txt # is not world readable - if self._check_file_exists(self.url+'releases/.test/THIS-FILE-SHOULD-NOT-BE-PUBLIC.txt'): + if self.check_file_exists(self.url+'releases/.test/THIS-FILE-SHOULD-NOT-BE-PUBLIC.txt'): if verbose is True: print 'ERROR: THIS-FILE-SHOULD-NOT-BE-PUBLIC.txt is visible on %s' % self.url ret = False return ret + |