~free.ekanayaka/landscape-client/lucid-1.5.0-0ubuntu0.10.04.0

« back to all changes in this revision

Viewing changes to landscape/lib/disk.py

  • Committer: Bazaar Package Importer
  • Author(s): Rick Clark
  • Date: 2008-09-08 16:35:57 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080908163557-l3ixzj5dxz37wnw2
Tags: 1.0.18-0ubuntu1
New upstream release 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from __future__ import division
 
2
 
 
3
import os
 
4
import statvfs
 
5
 
 
6
 
 
7
def get_mount_info(mounts_file, statvfs_):
 
8
    """
 
9
    Given a mounts file (e.g., /proc/mounts), generate dicts with the following
 
10
    keys:
 
11
 
 
12
     - device: The device file which is mounted.
 
13
     - mount-point: The path at which the filesystem is mounted.
 
14
     - filesystem: The filesystem type.
 
15
     - total-space: The capacity of the filesystem in megabytes.
 
16
     - free-space: The amount of space available in megabytes.
 
17
    """
 
18
    for line in open(mounts_file):
 
19
        try:
 
20
            device, mount_point, filesystem = line.split()[:3]
 
21
            mount_point = mount_point.decode("string-escape")
 
22
        except ValueError:
 
23
            continue
 
24
        megabytes = 1024 * 1024
 
25
        stats = statvfs_(mount_point)
 
26
        block_size = stats[statvfs.F_BSIZE]
 
27
        total_space = (stats[statvfs.F_BLOCKS] * block_size) // megabytes
 
28
        free_space = (stats[statvfs.F_BFREE] * block_size) // megabytes
 
29
        yield {"device": device, "mount-point": mount_point,
 
30
               "filesystem": filesystem, "total-space": total_space,
 
31
               "free-space": free_space}
 
32
 
 
33
 
 
34
def get_filesystem_for_path(path, mounts_file, statvfs_):
 
35
    candidate = None
 
36
    path = os.path.realpath(path)
 
37
    path_segments = path.split("/")
 
38
    for info in get_mount_info(mounts_file, statvfs_):
 
39
        mount_segments = info["mount-point"].split("/")
 
40
        if path.startswith(info["mount-point"]):
 
41
            if ((not candidate)
 
42
                or path_segments[:len(mount_segments)] == mount_segments):
 
43
                candidate = info
 
44
    return candidate