~ubuntu-branches/ubuntu/karmic/landscape-client/karmic-updates

« back to all changes in this revision

Viewing changes to landscape/lib/disk.py

  • Committer: Bazaar Package Importer
  • Author(s): Free Ekanayaka
  • Date: 2010-06-28 18:07:18 UTC
  • mfrom: (1.2.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20100628180718-g9l55c9c5bnch03b
Tags: 1.5.2.1-0ubuntu0.9.10.0
Filter duplicate network interfaces in get_active_interfaces (LP: #597000)

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
 
7
7
def get_mount_info(mounts_file, statvfs_, filesystems_whitelist=None):
8
8
    """
9
 
    Given a mounts file (e.g., /proc/mounts), generate dicts with the following
10
 
    keys:
11
 
 
12
 
    @param filesystems_whitelist: if provided, the list of file systems which
13
 
        we're allowed to stat.
14
 
 
15
 
     - device: The device file which is mounted.
16
 
     - mount-point: The path at which the filesystem is mounted.
17
 
     - filesystem: The filesystem type.
18
 
     - total-space: The capacity of the filesystem in megabytes.
19
 
     - free-space: The amount of space available in megabytes.
 
9
    This is a generator that yields information about mounted filesystems.
 
10
 
 
11
    @param mounts_file: A file with information about mounted filesystems,
 
12
        such as C{/proc/mounts}.
 
13
    @param statvfs_: A function to get file status information.
 
14
    @param filesystems_whitelist: Optionally, a list of which filesystems to
 
15
        stat.
 
16
    @return: A C{dict} with C{device}, C{mount-point}, C{filesystem},
 
17
        C{total-space} and C{free-space} keys. If the filesystem information
 
18
        is not available, C{None} is returned. Both C{total-space} and
 
19
        C{free-space} are in megabytes.
20
20
    """
21
21
    for line in open(mounts_file):
22
22
        try:
28
28
            filesystem not in filesystems_whitelist):
29
29
            continue
30
30
        megabytes = 1024 * 1024
31
 
        stats = statvfs_(mount_point)
 
31
        try:
 
32
            stats = statvfs_(mount_point)
 
33
        except OSError:
 
34
            continue
32
35
        block_size = stats[statvfs.F_BSIZE]
33
36
        total_space = (stats[statvfs.F_BLOCKS] * block_size) // megabytes
34
37
        free_space = (stats[statvfs.F_BFREE] * block_size) // megabytes
39
42
 
40
43
def get_filesystem_for_path(path, mounts_file, statvfs_,
41
44
                            filesystems_whitelist=None):
 
45
    """
 
46
    Tries to determine to which of the mounted filesystem C{path} belongs to,
 
47
    and then returns information about that filesystem or C{None} if it
 
48
    couldn't be determined.
 
49
 
 
50
    @param path: The path we want filesystem information about.
 
51
    @param mounts_file: A file with information about mounted filesystems,
 
52
        such as C{/proc/mounts}.
 
53
    @param statvfs_: A function to get file status information.
 
54
    @param filesystems_whitelist: Optionally, a list of which filesystems to
 
55
        stat.
 
56
    @return: A C{dict} with C{device}, C{mount-point}, C{filesystem},
 
57
        C{total-space} and C{free-space} keys. If the filesystem information
 
58
        is not available, C{None} is returned. Both C{total-space} and
 
59
        C{free-space} are in megabytes.
 
60
    """
42
61
    candidate = None
43
62
    path = os.path.realpath(path)
44
63
    path_segments = path.split("/")