lock.py

Go to the documentation of this file.
00001 ##
00002 # 
00003 # ping a location every once in a while to keep an Internet connection 
00004 # open.  Pass the time in hours you want it to stay connected in on 
00005 # the command line; defaults to 1 hour.
00006 # 
00007 # Copyright (C) 2002 GDS Software
00008 # 
00009 # This program is free software; you can redistribute it and/or
00010 # modify it under the terms of the GNU General Public License as
00011 # published by the Free Software Foundation; either version 2 of
00012 # the License, or (at your option) any later version.
00013 # 
00014 # This program is distributed in the hope that it will be useful,
00015 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 # GNU General Public License for more details.
00018 # 
00019 # You should have received a copy of the GNU General Public
00020 # License along with this program; if not, write to the Free
00021 # Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
00022 # MA  02111-1307  USA
00023 # 
00024 # See http://www.gnu.org/licenses/licenses.html for more details.
00025 # 
00026 
00027 import os, time, sys, string
00028 __version__ = "$Id: lock.py,v 1.3 2002/08/21 12:41:48 donp Exp $"
00029 
00030 # Global variables
00031 site = "www.hcfa.gov"
00032 ping = "c:/winnt/system32/ping"
00033 cmd = ping + " " + site + " " + ">nul 2>&1"
00034 bad_count = 0
00035 
00036 def ConvertHours(numstr):
00037     numstr = string.replace(numstr, "h", "")
00038     return int(float(numstr) * 3600)
00039 
00040 def ConvertMinutes(numstr):
00041     numstr = string.replace(numstr, "m", "")
00042     return int(float(numstr) * 60)
00043 
00044 def ConvertDays(numstr):
00045     numstr = string.replace(numstr, "d", "")
00046     return int(float(numstr) * 3600 * 24)
00047 
00048 def Initialize():
00049     time_in_sec = 3600     # Default is 1 hour
00050     if len(sys.argv) >= 2:
00051         numstr = sys.argv[1]
00052         if string.find(numstr, "h") != -1:
00053             time_in_sec = ConvertHours(numstr)
00054             print "Locking for %.1f hours" % (time_in_sec/3600.)
00055         elif string.find(numstr, "m") != -1:
00056             time_in_sec = ConvertMinutes(numstr)
00057             print "Locking for %.1f minutes" % (time_in_sec/60.)
00058         elif string.find(numstr, "d") != -1:
00059             time_in_sec = ConvertDays(numstr)
00060             print "Locking for %.2f days" % (time_in_sec/(3600.*24))
00061         else:
00062             time_in_sec = ConvertHours(numstr + "h")
00063             print "Locking for %.1f hours" % (time_in_sec/3600.)
00064     else:
00065         time_in_sec = 3600
00066         print "Locking for %.1f hours" % (time_in_sec/3600.)
00067     # Perform an initial ping after the typical connect time in case this
00068     # forces a modem to connect.
00069     for ix in xrange(10):
00070         time.sleep(1)
00071     if os.system(cmd) != 0:
00072         print "'cmd' failed. Apparently not connected."
00073         sys.exit(1)
00074     return time_in_sec
00075 
00076 
00077 def PrintTimeLeft(seconds_left):
00078     days = seconds_left/(3600.*24)
00079     hours = seconds_left/(3600.)
00080     minutes = seconds_left/(60.)
00081     if days >= 1.0:
00082         print "%.1f days left (%.1f minutes)" % (days, minutes)
00083     elif hours > 1.0:
00084         print "%.1f hours left (%.1f minutes)" % (hours, minutes)
00085     else:
00086         if minutes > 0.0:
00087             print "%.1f minutes left" % minutes
00088 
00089 def main():
00090     sleep_time = 60  # How many seconds to sleep between pings
00091     time_in_sec = Initialize()
00092     start = time.time()
00093     while time.time() - start < time_in_sec:
00094         startloop = time.time()
00095         while time.time() - startloop < sleep_time:
00096             # Loop on short delays to avoid waiting a long time for Ctrl-C to
00097             # take effect.
00098             time.sleep(1)
00099         if os.system(cmd) != 0:
00100             bad_count = bad_count + 1
00101             if bad_count > 5:
00102                 print "More than 5 pings returned nonzero status"
00103                 sys.exit(0)
00104 
00105         seconds_left = time_in_sec - (time.time() - start)
00106         PrintTimeLeft(seconds_left)
00107 
00108 main()
00109 
00110 

© Copyright 2008-2009 Vyper Logix Corp., All Right Reserved; If you reference this document or any part of this document you must use the citation verbatim (including the link) "© Copyright 2008-2009 Vyper Logix Corp., All Right Reserved."

Notice: This source code contained in this document is NOT open source and is NOT being distributed as open source.

122,241 lines of code and growing...