From c1d73107c7a492f261c6b3786427546387e25507 Mon Sep 17 00:00:00 2001 From: Arun Raghavan Date: Sun, 21 Dec 2008 14:00:55 +0530 Subject: Factor out distfiles/releases checking code. Portage checker en route. --- check.py | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 check.py diff --git a/check.py b/check.py new file mode 100644 index 0000000..1540627 --- /dev/null +++ b/check.py @@ -0,0 +1,90 @@ +#!/usr/bin/python + +import os +import urllib2 +import time +import rfc822 + +class GardCheck: + # Base class which provides some helper functions + 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)) + +class DistCheck(GardCheck): + # can only be used from Python 2.6 onwards + HTTP_TIMEOUT = 30 + + # Takes the URL to a timestamp.{chk|x} file and returns the + # corresponding time stamp in seconds + def _get_timestamp_from_url(self, url): + try: + f = urllib2.urlopen(url) + date = f.read() + f.close() + if date is None or len(date) == 0: + raise ValueError + try: + # timestamp.chk format + ts = self.timestamp_to_secs(date) + except: + # 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 + + return ret + + def check(self, maxdlag, maxrlag, 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): + if verbose is True: + print 'ERROR: distfiles at %s is lagging by %s' \ + % (self.url, self.humanize_time(now - ts)) + ret = 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): + if verbose is True: + print 'ERROR: releases at %s is lagging by %s seconds' \ + % (self.url, self.humanize_time(now - ts)) + ret = False + + # 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 verbose is True: + print 'ERROR: THIS-FILE-SHOULD-NOT-BE-PUBLIC.txt is visible to the outside world' + ret = False + + return ret -- cgit v1.2.3-65-gdbad