[Tutor] Critique and a big thanks!

Shawhan, Doug (EM, ITS) Doug.Shawhan at ge.com
Fri Apr 2 14:16:40 EST 2004


Tutors, 

We are having trouble with failed hard drives on a series of newly deployed computers at the plant I work at. There are ~ 300 of these and ~10% have had failed drive so far! I have been working on a python script to back up the important outlook email database files on these machines. The script grew from a simple copy thang to this threaded beast that works way better than I ever thought it would! Thank you all so much for your help and suggestions for reading material!

My final question: What would you folks suggest to make this script more simple and easier to read? (...and if you see any horrible coding blemishes, I would like to know too!). Thanks again everyone! This list is one of the best things about python!

d

------------------------------script--------------------------------------------------


'''MailMover.py 

Copies *.pst and *.pab files from remote hosts to backup on remote hosts to the 
\\Server\shared\company\MailBackup directory. Gets list of hosts to 
back up from the hosts.csv file in \tmp\MailMover.
Our bunch has standardized on having mail folders in My Documents\EXCHANGE\,
your group will probably do something different - edit accordingly!

Usage: "python mailmover.py"

Caveats: one must have superuser privileges (domain opers) on the remote machines 
to connect and copy /-Y files. There is, as yet, _no_ error reporting.
'''



import glob
import string
import os
import time
from threading import Thread

#static info
destination_directory="\\\\Server\\shared\company\\MailBackup"
host_list="c:\\tmp\\MailMover\\hosts.csv"
MAX_THREADS=10

#create list for thread pool.
gathers=[]

#open the .csv file and strip out the linefeeds
hostguffer=open(host_list,"r")
hostholder=hostguffer.readlines()
hosts=[]
for host in hostholder:
	hosts.append(host[:-1])

class gather(Thread):
	def __init__(self, users, host, destination_directory):
		Thread.__init__(self)
		self.host=host
		self.users=users
		self.destination_directory=destination_directory
	def run(self):
		print "thread started"
		for user in self.users:
			#strip extraneous pathname info from username. 
			#Assumes all hostnames are the same length
			username=user[21:] 
			collection_path="%s\\MYDOCU~1\\Exchange\\"%user
			mailbox_list=[]
			mailboxes=glob.glob("%s*.pst"%collection_path)
			if mailboxes !=[]:
				#create pathname on storage host!
				storage_path="%s\\%s\\%s\\"%(self.destination_directory,self.host, username)
				os.system("mkdir %s"%storage_path)
				os.system("copy /Y %s*.pst %s"%(collection_path, storage_path))
				#we'll just copy the addressbooks too, for smarts
				os.system("copy /Y %s*.pab %s"%(collection_path, storage_path))
				
def count_active():
    #returns the number of living threads
    num_active = 0
    for g in gathers:
        if g.isAlive():
            num_active += 1
    print "%d alive" % num_active
    return num_active

for host in hosts:
	users=glob.glob("\\\\%s\\d$\\users\\*"%host)# collect the users on the host
	while count_active() >= MAX_THREADS:
		print "too many active"
		time.sleep(1)
	go=gather(users, host, destination_directory)
	gathers.append(go)
	go.start()




More information about the Tutor mailing list