death to stderr and print statements!
authorCameron Patrick <[email protected]>
Sun, 1 Aug 2004 08:21:00 +0000 (08:21 +0000)
committerCameron Patrick <[email protected]>
Sun, 1 Aug 2004 08:21:00 +0000 (08:21 +0000)
sql-edition/servers/LATClient.py
sql-edition/servers/VendingMachine.py

index ce77970..4d17643 100644 (file)
@@ -2,6 +2,7 @@ from socket import *
 from select import select
 from os import popen4
 from time import sleep
+import logging
 
 LATCP_SOCKET = '/var/run/latlogin'
 
@@ -26,14 +27,14 @@ def read_for_a_bit(rfh):
                        message = message + ch
                else:
                        break
-       print "Received message: ", repr(message)
+       logging.debug("Received message: ", repr(message))
        return message
 
 def write_and_get_response(rfh, wfh, message, expect_echo=True):
-       print "Writing message:", repr(message)
+       logging.debug("Writing message:", repr(message))
        wfh.write(message+'\r\n')
        wfh.flush()
-       print "  --> Sent"
+       logging.debug("  --> Sent")
        response = read_for_a_bit(rfh)
        if response.find(message) == -1 and expect_echo:
                raise LATClientException("Talking to DEC server, expected to find original message in echo but didn't")
@@ -80,7 +81,7 @@ class LATClient:
                if r:
                        l = self.rfh.readline()
                        if l.find('Service in use') >= 0:
-                               print "Service in use, apparently: restarting DEC server"
+                               logging.warning("Service in use, apparently: restarting DEC server")
                                self.reboot_server()
 
        def __del__(self):
@@ -97,35 +98,47 @@ class LATClient:
        def reboot_server(self):
                self.sock.shutdown(2)
                self.sock.close()
+               
+               logging.info('Logging into DEC server')
                mopw, mopr = popen4('/usr/sbin/moprc '+self.server_name)
                write_and_get_response(mopr, mopw, '')
+
+               logging.info('Sending password')
                r = write_and_get_response(mopr, mopw, self.connect_password, False)
                if r.find('Enter username> ') == -1:
-                       print "Expected username prompt, got ", repr(r)
+                       logging.warning("Expected username prompt, got " + repr(r))
                        raise LATClientException('failed to reboot server')
+
+               logging.info('Sending username')
                r = write_and_get_response(mopr, mopw, 'grim reaper')
                if r.find('Local> ') == -1:
-                       print "Expected DEC server prompt, got ", repr(r)
+                       logging.warning("Expected DEC server prompt, got " + repr(r))
                        raise LATClientException('failed to reboot server')
+
+               logging.info('Requesting privileges')
                r = write_and_get_response(mopr, mopw, 'set priv')
                if r.find('Password> ') == -1:
-                       print "Expected priv password prompt, got ", repr(r)
+                       logging.warning("Expected priv password prompt, got " + repr(r))
                        raise LATClientException('failed to reboot server')
+
+               logging.info('Sending password')
                r = write_and_get_response(mopr, mopw, self.priv_password, False)
                if r.find('Local> ') == -1:
-                       print "Expected DEC server prompt, got ", repr(r)
+                       logging.warning("Expected DEC server prompt, got " + repr(r))
                        raise LATClientException('failed to reboot server')
+
+               logging.info('Sending reboot request')
                r = write_and_get_response(mopr, mopw, 'init del 0')
                if r.find('Target does not respond') == -1:
-                       print "Expected DEC server to die, got ", repr(r)
+                       logging.warning("Expected DEC server to die, got " + repr(r))
                        raise LATClientException('failed to reboot server')
+
+               logging.info('Closed connection to server')
                mopr.close()
                mopw.close()
-               print
-               print "Waiting 10 seconds for DEC server to come back to life..."
+               logging.info("Waiting 10 seconds for DEC server to come back to life...")
                sleep(10)
-               print "Rightyo, back to vending!"
-               print
+               logging.info("Rightyo, back to vending!")
                raise LATClientException('needed to reboot server')
 
        def read_reply(self):
index 9506daf..4a40922 100644 (file)
@@ -2,7 +2,7 @@
 import re
 from CRC import do_crc
 from select import select
-import socket
+import socket, logging
 
 asynchronous_responses = [     '400', '401', # door open/closed
                                '610',        # switches changed
@@ -42,7 +42,6 @@ class VendingMachine:
                        try:
                                s = self.rfh.read(1)
                        except socket.error:
-                               print "Blah, seems DEC server has fallen over"
                                raise VendingException('failed to read input from vending machine')
                        if s == '': raise VendingException('nothing read!')
                        if s == '\n' or s == '\r':
@@ -97,7 +96,7 @@ class VendingMachine:
                elif code[0] == '2':
                        self.events.append((KEY, int(code[1:3])))
                else:
-                       sys.stderr.write('WARNING: Unhandled event! (%s %s)\n'%(code,text))
+                       logging.warning('Unhandled event! (%s %s)\n'%(code,text))
 
        def authed_message(self, message):
                if self.challenge == None:

UCC git Repository :: git.ucc.asn.au