space.py

Go to the documentation of this file.
00001 ##
00002 # 
00003 # This module provides a function that constructs a list containing
00004 # the sizes of directories under a specified directory.
00005 # 
00006 # Copyright (C) 2002 GDS Software
00007 # 
00008 # This program is free software; you can redistribute it and/or
00009 # modify it under the terms of the GNU General Public License as
00010 # published by the Free Software Foundation; either version 2 of
00011 # the License, or (at your option) any later version.
00012 # 
00013 # This program is distributed in the hope that it will be useful,
00014 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 # GNU General Public License for more details.
00017 # 
00018 # You should have received a copy of the GNU General Public
00019 # License along with this program; if not, write to the Free
00020 # Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
00021 # MA  02111-1307  USA
00022 # 
00023 # See http://www.gnu.org/licenses/licenses.html for more details.
00024 # 
00025 
00026 import os
00027 __version__ = "$Id: space.py,v 1.4 2002/08/21 12:41:49 donp Exp $"
00028 
00029 listG = []
00030 
00031 ##
00032 # Given a list of files and the directory they're in, add the
00033 #     total size and directory name to the global list listG.
00034 #     
00035 def GetTotalFileSize(dummy_param, directory, list_of_files):
00036     global listG
00037     currdir = os.getcwd()
00038     os.chdir(directory)
00039     total_size = 0
00040     if len(list_of_files) != 0:
00041         for file in list_of_files:
00042             if file == ".." or file == ".":  continue
00043             size = os.stat(file)[6]
00044             total_size = total_size + size
00045     listG.append([total_size, directory])
00046     os.chdir(currdir)
00047 
00048 ##
00049 # Returns a list of the form [ [a, b], [c, d], ... ] where
00050 #     a, c, ... are the number of total bytes in the directory and
00051 #     b, d, ... are the directory names.  The indicated directory 
00052 #     is recursively descended and the results are sorted by directory 
00053 #     size with the largest directory at the beginning of the list.
00054 #     
00055 def GetSize(directory):
00056     import os
00057     global listG
00058     listG = []
00059     os.path.walk(directory, GetTotalFileSize, "")
00060     listG.sort()
00061     listG.reverse()
00062 
00063 def ShowBiggestDirectories(directory):
00064     import string
00065     GetSize(directory)
00066     # Get total number of bytes
00067     total_size = 0L
00068     for dir in listG:
00069         total_size = total_size + dir[0]
00070     if total_size != 0:
00071         print "For directory '%s':    " % directory,
00072         print "[total space = %.1f MB]" % (total_size / 1e6)
00073         print "   %     MB   Directory"
00074         print "------ -----  " + "-" * 50
00075         not_shown_count = 0
00076         for dir in listG:
00077             percent = 100.0 * dir[0] / total_size
00078             dir[1] = string.replace(dir[1], "\\\\", "/")
00079             if percent >= 0.1:
00080                 print "%6.1f %5d  %s" % (percent, int(dir[0]/1e6), dir[1])
00081             else:
00082                 not_shown_count = not_shown_count + 1
00083         if not_shown_count > 0:
00084             if not_shown_count > 1:
00085                 print "            [%d directories not shown]" % not_shown_count
00086             else:
00087                 print "            [%d directory not shown]" % not_shown_count
00088 
00089 if __name__ == '__main__':
00090     import sys
00091     name = sys.argv[0]
00092     sys.argv = sys.argv[1:]
00093     if len(sys.argv) == 0:
00094         sys.argv.append(".")
00095     ShowBiggestDirectories(sys.argv[0])
00096 
00097 

© 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...