python vs awk for simple sysamin tasks

Matthew Thorley ruach at chpc.utah.edu
Thu Jun 3 04:48:22 EDT 2004


Steve Lamb wrote:
> On 2004-06-03, Roy Smith <roy at panix.com> wrote:
> 
>>Neither of these are really tasks well suited to python at all.
> 
> 
>     Individually, no.  Combined, yes.
> 

So I went ahead and combined them and added a little extra heres the script:

#!/usr/bin/python

import os
from sys import argv, exit

class userFileStats:
	def __init__(self):
		self.path			= ''
		self.uid			= ''
		self.userName			= ''
		self.oserrors			= 0
		self.totalDirs 			= 0
		self.totalFiles			= 0
		self.totalFileSize		= 0
		
		self.totalUserDirs		= 0
		self.totalUserFiles		= 0
		self.totalUserFileSize	= 0
		self.smallestUserFile	= [100**100, 'name']
		self.largestUserFile	= [0, 'name']
	
	
	def walkPath(self, path, uid):
		self.path = path
		self.uid = int(uid)
		os.path.walk(path, self.tallyFiles, uid)
	
	
	def tallyFiles(self, uid, dir, names):		
		self.totalDirs = self.totalDirs + 1
		self.totalFiles = self.totalFiles + len(names)
		
		if os.stat(dir)[4] == self.uid:
			self.totalUserDirs = self.totalUserDirs + 1
		for name in names:
			try:
				stat = os.stat(dir+'/'+name)			
			except OSError:
				self.oserrors = self.oserrors + 1
				break
			
			self.totalFileSize = self.totalFileSize + stat[6]
			if stat[4] == self.uid:
				self.totalUserFiles = self.totalUserFiles + 1
				self.totalUserFileSize = self.totalUserFileSize + stat[6]				
				
				if stat[6] < self.smallestUserFile[0]:
					self.smallestUserFile[0] = stat[6]
					self.smallestUserFile[1] = dir+'/'+name
				
				if stat[6] > self.largestUserFile[0]:
					self.largestUserFile[0] = stat[6]
					self.largestUserFile[1] = dir+'/'+name
	
	
	def printResults(self):
		print "Results for path %s"\
		%(self.path)
		print "  Searched %s dirs"\
		%(self.totalDirs)
		print "  Searched %s files"\
		%(self.totalFiles)
		print "  Total disk use for all files = %s bytes"\
		%(self.totalFileSize/1024)
		print "  %s files returned errors"\
		%(self.oserrors)
		print "Results for user %s"\
		%(self.uid)
		print "  User owns %s dirs"\
		%(self.totalUserDirs)
		print "  User owns %s files"\
		%(self.totalUserFiles)
		print "  Total disk use for user = %s bytes"\
		%(self.totalUserFileSize/1024)
		print "  Users smallest file %s is %s bytes"\
		%(self.smallestUserFile[1], self.smallestUserFile[0]/1024)
		print "  Users largest file %s is %s bytes"\
		%(self.largestUserFile[1], self.largestUserFile[0]/1024)
		print "  Average user file size = %s bytes"\
		%( (self.totalUserFileSize/self.totalUserFiles)/1024 )


if __name__ == '__main__':
	if len(argv) == 2:
		user= argv[1]
		path = os.getcwd()
	elif len(argv) == 3:
		user = argv[1]
		path = argv[2]
	else:
		print 'Usage: userFileStats.py uid path\n'
		exit(1)
	
	userFileStats = userFileStats()
	userFileStats.walkPath(path, user)
	userFileStats.printResults()

It is A LOT longer than the one liners (obviously) but it has way more 
functionality. With a little tweaking you could easily do all sorts of 
other useful things. I'm sure utils like this already exist out there 
whether written in python or not.

Another question. The example my friend gave me takes the user name as 
an argument not the uid. Does any one know how to convert usernames to 
uids and vice versa in python ?  Please also comment on the script, any 
thoughts on simplification ?

thanks
-matthew



More information about the Python-list mailing list