All pastes #167652 Raw Edit

Peng

public python v1 · immutable
·published 2006-09-12 05:42 UTC
rendered paste body
#!/usr/bin/env python# -*- coding: UTF-8 -*-CACHE_FILE =  '../../mattnordhoff.com.data/junk/uptime.dat'LOG_FILE = '../../mattnordhoff.com.data/junk/uptime.log'ENABLE_LOG = 1from os import popen2from os.path import getmtime, isfilefrom time import gmtime, strftime, timedef getprocessoutput(e):   """   Executes e and returns its output.   """   ein, eout = popen2(e)   output = eout.read().rstrip()   ein.close()   eout.close()   return output# So, um, why do people put everything in a main() function?def main():   now = time()   if isfile(CACHE_FILE):      fresh = False      fh = open(CACHE_FILE, 'r')      uptime, servername = [line.rstrip() for line in fh.readlines()]      fh.close()      if getmtime(CACHE_FILE) <= (now - 60):         fresh = True         uptime = getprocessoutput('/usr/bin/uptime')         # I do not know how to only open the file once. :|         fh = open(CACHE_FILE, 'w')         fh.write(uptime + '\n' + servername)         fh.close()   else:      fresh = True      uptime = getprocessoutput('/usr/bin/uptime')      hostname = getprocessoutput('/bin/hostname')      domainname = getprocessoutput('/bin/dnsdomainname')      # Is that even possible?      if hostname == '':         servername = 'this server'      else:         if domainname == '':            servername = hostname         else:            servername = hostname + '.' + domainname      # The file could probably be screwed up if the script is accessed twice      # at the exact same time...      cfh = open(CACHE_FILE, 'w')      cfh.write(uptime + '\n' + servername)      cfh.close()      # The bit of code below is simply not being executed. I can make the if      # check for a nonexistant variable if I want to, and I won't get an      # error. If I put invalid syntax here, I get an error, so Python knows      # it exists (how could it not?). The code messing with cfh above works,      # but I can put a print or whatever I want in or around it, and it won't      # be executed. Is my sleepy brain missing something, or is Python      # completely insane?      if ENABLE_LOG:         lfh = open(LOG_FILE, 'a')         lfh.write(strftime('%Y-%m-%d %H:%M:%S: ') + uptime + '\n')         lfh.close()   mtime = getmtime(CACHE_FILE) # That getmtime() is called up in the if and here..   print 'Content-Type: text/plain;charset=us-ascii'   print 'Cache-Control: max-age=60, must-revalidate, private, ' \         'proxy-revalidate, s-maxage=60'   print 'Expires: ' + strftime('%a, %d %b %Y %H:%M:%S GMT', gmtime(now + 60))   print 'Last-Modified: ' + strftime('%a, %d %b %Y %H:%M:%S GMT', gmtime(mtime))   print   print '/usr/bin/uptime output on', servername, 'as of',   if fresh:      print 'right now:\n'   else:      timediff = int(now - mtime)      print timediff,      if timediff == 1:         print 'second ago:\n'      else:         print 'seconds ago:\n'   print uptimeif __name__ == '__main__':   main()