Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(82287)

Side by Side Diff: Lib/shutil.py

Issue 12442: shutil.disk_usage()
Patch Set: Created 8 years, 1 month ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 """Utility functions for copying and archiving files and directory trees. 1 """Utility functions for copying and archiving files and directory trees.
2 2
3 XXX The functions here don't copy the resource fork or other metadata on Mac. 3 XXX The functions here don't copy the resource fork or other metadata on Mac.
4 4
5 """ 5 """
6 6
7 import os 7 import os
8 import sys 8 import sys
9 import stat 9 import stat
10 from os.path import abspath 10 from os.path import abspath
11 import fnmatch 11 import fnmatch
12 import collections 12 import collections
13 import errno 13 import errno
14 import tarfile 14 import tarfile
15 from collections import namedtuple
15 16
16 try: 17 try:
17 import bz2 18 import bz2
18 _BZ2_SUPPORTED = True 19 _BZ2_SUPPORTED = True
19 except ImportError: 20 except ImportError:
20 _BZ2_SUPPORTED = False 21 _BZ2_SUPPORTED = False
21 22
22 try: 23 try:
23 from pwd import getpwnam 24 from pwd import getpwnam
24 except ImportError: 25 except ImportError:
(...skipping 722 matching lines...) Expand 10 before | Expand all | Expand 10 after
747 func(filename, extract_dir, **dict(format_info[2])) 748 func(filename, extract_dir, **dict(format_info[2]))
748 else: 749 else:
749 # we need to look at the registered unpackers supported extensions 750 # we need to look at the registered unpackers supported extensions
750 format = _find_unpack_format(filename) 751 format = _find_unpack_format(filename)
751 if format is None: 752 if format is None:
752 raise ReadError("Unknown archive format '{0}'".format(filename)) 753 raise ReadError("Unknown archive format '{0}'".format(filename))
753 754
754 func = _UNPACK_FORMATS[format][1] 755 func = _UNPACK_FORMATS[format][1]
755 kwargs = dict(_UNPACK_FORMATS[format][2]) 756 kwargs = dict(_UNPACK_FORMATS[format][2])
756 func(filename, extract_dir, **kwargs) 757 func(filename, extract_dir, **kwargs)
758
759 if hasattr(os, "statvfs") or os.name == 'nt':
760 _ntuple_diskusage = namedtuple('usage', 'total used free percent')
761
762 def disk_usage(path):
763 """Return disk usage statistics about the given path as a namedtuple
764 including total, used and free space expressed in bytes plus the
765 percentage usage.
766 """
767 if hasattr(os, "statvfs"):
768 st = os.statvfs(path)
769 free = (st.f_bavail * st.f_frsize)
770 total = (st.f_blocks * st.f_frsize)
771 used = (st.f_blocks - st.f_bfree) * st.f_frsize
772 else:
773 import nt
774 total, free = nt._getdiskusage(path)
775 used = total - free
776 # NB: on UNIX the percentage might different than what shown
eric.araujo 2011/06/30 17:24:22 Maybe this would be more useful as a note in the d
777 # by df due to reserved blocks that we are currently not
778 # considering:
779 # http://larsmichelsen.com/open-source/answer-why-does-df-k-show-wrong-p ercentage-usage/
780 try:
781 percent = (float(used) / total) * 100
782 except ZeroDivisionError:
783 percent = 0
784 return _ntuple_diskusage(total, used, free, round(percent, 1))
OLDNEW
« no previous file with comments | « Doc/library/shutil.rst ('k') | Lib/test/test_shutil.py » ('j') | Lib/test/test_shutil.py » ('J')

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+