Attachment 'FreeSpace.py'

Download

   1 '''FreeSpace -- print free space on disk
   2 
   3 - Uses stat, works on Linux and probably other Unices
   4 - Arg is a path in the filesystem to be tested
   5 - With no arg, uses '/'
   6 
   7 Written by Michael Ashton <data@ieee.org>
   8 for MoinMoin 1.3.
   9 
  10 Public domain -- written 17 August 2005
  11 '''
  12 
  13 import commands
  14 Dependencies=[]
  15 
  16 def gigfmt(n):
  17 	n=float(n)
  18 	if (n>(1<<30)):
  19 		n=n/(1<<30)
  20 		suf="GiB"
  21 	elif (n>(1<<20)):
  22 		n=n/(1<<20)
  23 		suf="MiB"
  24 	elif (n>(1<<10)):
  25 		n=n/(1<<10)
  26 		suf="KiB"
  27 	else:
  28 		suf="B"
  29 	return "%.1f%s"%(n,suf)
  30 
  31 def execute(macro,args):
  32 	if not args is None and len(args)>0:
  33 			path=str(args)
  34 	else:
  35 		path="/"
  36 	res=commands.getstatusoutput(r"stat -f -c '(%f,%b,%s)' "+path)
  37 	if res[0]:
  38 		ret="(result "+str(res[0])+": "+res[1]+")"
  39 	else:
  40 		fsl=eval(res[1])
  41 		blksize=fsl[2]
  42 		freeblk=fsl[0]
  43 		totblk=fsl[1]
  44 		freebytes=freeblk*blksize
  45 		totbytes=totblk*blksize
  46 		ret=gigfmt(freebytes)+" of "+gigfmt(totbytes)
  47 	return ret
  48 
  49 if __name__=="__main__":
  50 	import sys
  51 	#print gigfmt(int(sys.argv[1]))
  52 	if len(sys.argv)>1:
  53 		print execute(0,(sys.argv[1],))
  54 	else:
  55 		print execute(0,())

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2005-08-17 20:52:45, 1.0 KB) [[attachment:FreeSpace.py]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.