Author: beorn
Date: Sun Dec  7 00:05:12 2008
New Revision: 10025

Modified:
   toys/stbr/src-builder-status.py
Log:
- Complete rewrite in a more "python-way" spirit
- Builder address checked only in headers, not entire
  message


Modified: toys/stbr/src-builder-status.py
==============================================================================
--- toys/stbr/src-builder-status.py     (original)
+++ toys/stbr/src-builder-status.py     Sun Dec  7 00:05:12 2008
@@ -1,44 +1,99 @@
 #!/usr/bin/python
+#-*- coding: utf8 -*-
+#
+# This script uses stbr mailbox to fetch the build status of a SPEC file.
+# It is used by the PLD Neatty Web Builder Monitor And Such to display
+# the current state of the package. Hopefully.
+#
+# TODO:
+#   - DEBUG mode fired from the commandline (getopt?)
+#   - Status determined from the message sent date, not from the position
+#     in a mailbox file
+#   - Python 2.6 detection and proper message parsing in case of that
+#     version (see BIG FAT WARNING below)
+#
+# !!! BIG FAT WARNING !!!
+# The mailbox module has changed in Python 2.6, so this code will stop
+# working FOR SURE. If the Python interpreter on this machine has been
+# upgraded, this script stopped working and you see this big fat warning,
+# then nobody (me?) cared to clean some very important point on the TODO
+# list above. Please send a few grave words to me at [EMAIL PROTECTED] and
+# revert script to the 10018 SVN release.
+# !!! BIG FAT WARNING !!!
+#
+# $Id:$
 
+import email
+import email.Errors
+import mailbox
 import os
 import sys
-import re
-import readline
 
-mailbox = "/var/mail/stbr"
-#f_log = "/home/users/stbr/ApHeX/scripts/"
-f_log = "./"
-
-lines = []
-builder_l = []
-status_l = []
-
-def parseMailbox():
-       try:
-               pld = sys.argv[1]
-               spec = sys.argv[2]
-       except(IndexError):
-               print "Usage: %s th|ti spec" %  sys.argv[0]
-               return
-       srcbuilder = pld + "-src"
-       f = open(mailbox, 'r')
-       read = f.xreadlines()
-       for l in read:
-               l = l.strip()
-               lines.append(l)
-       i = 0
-       while(i < len(lines)):
-               if re.match("^From:", lines[i]):
-                       Rbuilder = lines[i].split(" ")
-                       if re.match(srcbuilder, Rbuilder[2]):
-                               builder_l.append(Rbuilder[2])
-                               i = i + 9
-                               if re.match(spec, lines[i]):
-                                       status = lines[i].split(" ")
-                                       status_l.append(status[2])
-               i = i + 1
-       for i in range(len(status_l)):
-               if re.match(srcbuilder, builder_l[i]):
-                       print status_l[i]
 
-parseMailbox()
+MAILBOX = '/var/mail/stbr'
+ALLOWED_DISTVER = ('th', 'ti')
+ALLOWED_STATUS = ('OK', 'FAILED')
+DEBUG = False  # Change this to True in case of disaster to see what is going 
on
+
+
+
+def email_reader_factory(fh):
+    try:
+       return email.message_from_file(fh)
+    except email.Errors.MessageParseError:
+       return ''
+
+
+try:
+    (distver, spec) = sys.argv[1:3]
+    distver = distver.lower()
+    if distver not in ALLOWED_DISTVER:
+       raise Exception()
+except:
+    sys.stderr.write('Usage: %s { %s } <spec>\n' % 
(os.path.basename(__file__), ' | '.join(ALLOWED_DISTVER)))
+    sys.exit(1)
+
+#result = 'UNKNOWN'
+result = None
+
+try:
+    fh = open(MAILBOX, 'rb')
+    mbox = mailbox.UnixMailbox(fh, email_reader_factory)
+    for message in mbox:
+       if not message:
+           continue
+       elif DEBUG:
+           sys.stderr.write('DEBUG: processing message %s\n' % 
(message['Message-ID'],))
+       
+       if not message['From'].endswith('[EMAIL PROTECTED]>' % (distver,)):
+           continue
+
+       for line in message.get_payload().split('\n'):
+           if line.startswith(spec):
+               try:
+                   (junk, branch, status) = line.split(' ', 2)
+               except:
+                   if DEBUG:
+                       sys.stderr.write('DEBUG:\tDismantle error at line 
"%s"\n' % (line,))
+                   continue
+               if status not in ALLOWED_STATUS:
+                   continue
+               else:
+                   result = status
+                   break
+    fh.close()
+except Exception, e:
+    if DEBUG:
+       raise
+    try:
+       fh.close()
+    except:
+       pass
+    sys.stdout.write('Script Error\n')
+    sys.exit(2)
+
+if result:
+    sys.stdout.write('%s\n' % (result,))
+
+# vim: sts=4 noai nocp indentexpr=
+
_______________________________________________
pld-cvs-commit mailing list
[email protected]
http://lists.pld-linux.org/mailman/listinfo/pld-cvs-commit

Reply via email to