00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 import os
00024
00025
00026
00027 mtab_mtime = None
00028 mtab_map = []
00029
00030 class MntEntObj(object):
00031 mnt_fsname = None
00032 mnt_dir = None
00033 mnt_type = None
00034 mnt_opts = None
00035 mnt_freq = 0
00036 mnt_passno = 0
00037
00038 def __init__(self,input=None):
00039 if input and isinstance(input, str):
00040 (self.mnt_fsname, self.mnt_dir, self.mnt_type, self.mnt_opts, \
00041 self.mnt_freq, self.mnt_passno) = input.split()
00042 def __dict__(self):
00043 return {"mnt_fsname": self.mnt_fsname, "mnt_dir": self.mnt_dir, \
00044 "mnt_type": self.mnt_type, "mnt_opts": self.mnt_opts, \
00045 "mnt_freq": self.mnt_freq, "mnt_passno": self.mnt_passno}
00046 def __str__(self):
00047 return "%s %s %s %s %s %s" % (self.mnt_fsname, self.mnt_dir, self.mnt_type, \
00048 self.mnt_opts, self.mnt_freq, self.mnt_passno)
00049
00050 class FileSystem(object):
00051 def __init__(self, mntent):
00052 self.mnt_dev = mntent.mnt_fsname
00053 self.mnt_pnt = mntent.mnt_dir
00054 self.fs_type = mntent.mnt_type
00055 try:
00056 stat_res = os.statvfs('%s' % self.mnt_pnt)
00057 except OSError:
00058 stat_res = self.f_bsize = self.f_frsize = self.f_blocks = self.f_blocks = self.f_bfree = self.f_bavail = self.f_files = self.f_ffree = self.f_favail = "UNKNOWN"
00059 else:
00060 self.f_bsize = stat_res.f_bsize
00061 self.f_frsize = stat_res.f_frsize
00062 self.f_blocks = stat_res.f_blocks
00063 self.f_bfree = stat_res.f_bfree
00064 self.f_bavail = stat_res.f_bavail
00065 self.f_files = stat_res.f_files
00066 self.f_ffree = stat_res.f_ffree
00067 self.f_favail = stat_res.f_favail
00068
00069 def to_dict(self):
00070 return dict(mnt_pnt=self.mnt_pnt, fs_type=self.fs_type, f_favail=self.f_favail,
00071 f_bsize=self.f_bsize, f_frsize=self.f_frsize, f_blocks=self.f_blocks,
00072 f_bfree=self.f_bfree, f_bavail=self.f_bavail, f_files=self.f_files,
00073 f_ffree=self.f_ffree)
00074
00075 def __str__(self):
00076 return "%s %s %s %s %s %s %s %s %s %s %s" % \
00077 (self.mnt_dev, self.mnt_pnt, self.fs_type,
00078 self.f_bsize, self.f_frsize, self.f_blocks,
00079 self.f_bfree, self.f_bavail, self.f_files,
00080 self.f_ffree, self.f_favail)
00081
00082
00083
00084 def get_mtab(mtab="/proc/mounts", vfstype=None):
00085 global mtab_mtime, mtab_map
00086
00087 mtab_stat = os.stat(mtab)
00088 if mtab_stat.st_mtime != mtab_mtime:
00089
00090 mtab_mtime = mtab_stat.st_mtime
00091 mtab_map = __cache_mtab__(mtab)
00092
00093
00094 if vfstype:
00095 return [ent for ent in mtab_map if ent.mnt_type == vfstype]
00096
00097 return mtab_map
00098
00099 def get_fslist():
00100 return [FileSystem(mnt) for mnt in get_mtab()]
00101
00102 def __cache_mtab__(mtab="/proc/mounts"):
00103 global mtab_mtime
00104
00105 f = open(mtab)
00106
00107 mtab = [MntEntObj(line) for line in f.read().split('\n') if len(line) > 0]
00108 f.close()
00109
00110 return mtab
00111
00112 def get_file_device_path(fname):
00113 '''What this function attempts to do is take a file and return:
00114 - the device the file is on
00115 - the path of the file relative to the device.
00116 For example:
00117 /boot/vmlinuz -> (/dev/sda3, /vmlinuz)
00118 /boot/efi/efi/redhat/elilo.conf -> (/dev/cciss0, /elilo.conf)
00119 /etc/fstab -> (/dev/sda4, /etc/fstab)
00120 '''
00121
00122
00123 fname = os.path.realpath(fname)
00124
00125
00126 mtab_dict = {}
00127 for ent in get_mtab():
00128 mtab_dict[ent.mnt_dir] = ent.mnt_fsname
00129
00130
00131 fdir = os.path.dirname(fname)
00132 match = mtab_dict.has_key(fdir)
00133 while not match:
00134 fdir = os.path.realpath(os.path.join(fdir, os.path.pardir))
00135 match = mtab_dict.has_key(fdir)
00136
00137
00138 if fdir != os.path.sep:
00139 fname = fname[len(fdir):]
00140
00141 return (mtab_dict[fdir], fname)
00142