[Tutor] Looking up a value in a dictionary from user input problem

2009-08-06 Thread chase pettet
I am trying to write a script to work our LVS implementation.  I want to be
able to have  user do something like this "./script SITE SERVER" and have
the script look up the ip value of the site on that server and issue the
command to pull the status from LVS.  I am almost there but for some reason
when I try to pass the site parameter dynamically (the name of the
dictionary) I keep getting errors about value type.  I have two example that
work where the dictionary name is hard coded to to speak in the script, and
the value I want to pull is dynamic using sys.argv[1], and one where I'm
trying to pass the dictionary name and it does not work.  I tried to slim
thse examples down, so hopefully they are helpful:

works #1...

./script.py t

#!/usr/bin/env python
site = {"l":"10.1.1.1", "t":"10.1.1.2", "x":"10.1.1.3", "s1":"10.1.1.4",
"s2":"10.1.1.5", "s3":"10.1.1.6"}

def runBash(cmd):
  p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
  out = p.stdout.read().strip()
  return out

class LVS_Site:
  def show(self, site):
showsite = "ipvsadm -L -t %s:443" % (site)
showsiteresult = runBash(showsite)
return showsiteresult

a = LVS_Site()
b = site["%s" % (sys.argv[1])]
c = a.show(b)
print ""
print ""
print ""
print c

works #2...

./script.py t

#!/usr/bin/env python
site = {"l":"10.1.1.1", "t":"10.1.1.2", "x":"10.1.1.3", "s1":"10.1.1.4",
"s2":"10.1.1.5", "s3":"10.1.1.6"}

def runBash(cmd):
  p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
  out = p.stdout.read().strip()
  return out

class LVS_Site:
  def show(self, site):
showsite = "ipvsadm -L -t %s:443" % (site)
showsiteresult = runBash(showsite)
return showsiteresult

a = LVS_Site()
z = site
b = z["%s" % (sys.argv[1])]
c = a.show(b)
print ""
print ""
print ""
print c


Not working...

./script.py t site

#!/usr/bin/env python
site = {"l":"10.1.1.1", "t":"10.1.1.2", "x":"10.1.1.3", "s1":"10.1.1.4",
"s2":"10.1.1.5", "s3":"10.1.1.6"}

def runBash(cmd):
  p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
  out = p.stdout.read().strip()
  return out

class LVS_Site:
  def show(self, site):
showsite = "ipvsadm -L -t %s:443" % (site)
showsiteresult = runBash(showsite)
return showsiteresult

a = LVS_Site()
z = sys.argv[2]
b = b["%s" % (sys.argv[1])]
c = a.show(b)
print ""
print ""
print ""
print c

Error:

Traceback (most recent call last):
  File "./python2.py", line 22, in ?
b = z["%s" % (sys.argv[1])]
TypeError: string indices must be integers


I don't understand why the third one does not work. Thanks for any help!
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Removing files based upon time stamps

2008-06-25 Thread chase pettet
I'm trying to create a basic script that will remove old backup files (more
than 30 days) from a directory based upon timestamp.  The system it will run
on is Windows XP.  I created this and ran it on one box and it seemed to
work fine, when I ported it to the actual box it needs to run on it is not
removing the files.  I ran the script with "python -i" so it dumped me into
interactive mode and I confirmed that the current object was ok, I confirmed
it is in the correct directory, I confirmed I could so a manual
os.remove("file") and it would actually delete the file.  This makes me
think it is not a permissions issue.  But when I run the script it is a no
go.  I'm basically at a loss as to what to try for troubleshooting next.
Backstory, this directory is an iis ftp directory if that makes a
difference.

import os, time, sys
current = time.time()
os.chdir("c:\BACKUPS\DEV1")

for f in os.listdir('.'):
  modtime = os.path.getmtime('.')
  if modtime < current - 30 * 86400:
os.remove(f)


This is my first list post.  Thanks for any help!
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor