Circumstantial python script, need to use results from previous runs
Ok, I'm sure there may be plenty of ways to do this but I'm asking because I don't know where to start looking for a way to do this. I have a python script that runs some basic bash commands and based on the output will send an email to a given email address. I want to make it so that an email will only be sent if that information is different from the last output of the script.
So say I run an 'lpstat -p' and it sends an email because printer 'a' is disabled and cron runs the script again an hour later I only want another email sent if something other than 'a' requires an email notification.
I may have gone too into detail but really I just want a way for the script to know what happened in previous runs of the script - does that make any sense? I know I could run a "touch" on a file, but that seems rather primitive so I was wondering if python had a good built-in way of dealing with this without getting overly complex.
Here's a brief example of what I'm doing if my explanation doesn't make sense.
# The string used to find disabled pritners based on the stdout of lpstat
string_check = 'disabled'
stdout = commands.getoutput( 'lpstat -p' )
lpout = stdout.split( '\n' )
disabled = []
# Cycle through the output of lpstat which has been split into tokens based on each line of the output and save开发者_运维技巧 the lines that have a substring match with string_check
for line in lpout:
if string_check in line:
disabled.append( line )
# Initiate the required variables for constructing a basic email
new_message = ""
SERVER = ""
FROM = ""
TO = ""
SUBJECT = ""
TEXT = ""
message = ""
# Just some string manipulation - tries to remove useless, redundant information from the lpstat output
for line in disabled:
line = line.replace( "printer ", "" )
line = line.replace( " is ", "" )
line = line.replace( "idle.", "\t" )
line = line.replace( string_check, "\t" )
new_message += "\t" + line + "\n"
SERVER = "localhost"
FROM = "email"
TO = "email"
SUBJECT = "Printer unnexpectedly disabled"
TEXT = new_message
# Email template
message = """\
From: %s
To: %s
Subject: %s
Printers that seem to be disabled:
%s
""" % ( FROM, TO, SUBJECT, TEXT )
# if there ended up being some stuff in the disabled array then send the email
if len( disabled ) > 0:
server = smtplib.SMTP( SERVER )
server.sendmail ( FROM, TO, message )
server.quit( )
The shelve module offers a very simple persistent dictionary in Python.
http://docs.python.org/library/shelve.html
I suggest you go straight to using Pickle or JSON, you get a lot more flexibility than shelve. Note that this code would work nearly identically with Pickle, JSON or Yaml.
Basically you'd write out the object you wanted to at the end of the run:
json.dump(disabled, open('disabled_list.json', 'wb'))
Then at the beginning of the next run you'd load that json object back and filter the new lines based on it not being there before:
previously_disabled = json.load(open('disabled_list.json, 'rb'))
#...
if string_check in line and line not in previously_disabled:
disabled.append( line )
精彩评论