From ba78707547ea3ab1fa3f73eecedf762368347450 Mon Sep 17 00:00:00 2001 From: Mike Hiretsky Date: Wed, 27 Jul 2011 15:23:24 +0400 Subject: [PATCH] Add FStab object for getting mount point information. --- pym/cl_utils.py | 63 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/pym/cl_utils.py b/pym/cl_utils.py index 7d2e281..fd9bae8 100644 --- a/pym/cl_utils.py +++ b/pym/cl_utils.py @@ -638,6 +638,69 @@ def isFstabMount(pathname,mapDevUuid={},listFstab=[]): filter(lambda x: absPath in x and x[1] != "none", listFstab),[""]))[0] +class SingletonParam(type): + def __init__(cls, name, bases, dict): + super(SingletonParam, cls).__init__(name, bases, dict) + cls.instance = {} + + def __call__(cls,*args,**kw): + keyarg = args[0] if args else "" + if not keyarg in cls.instance: + cls.instance[keyarg] = super(SingletonParam, cls).__call__(*args, **kw) + return cls.instance[keyarg] + +class FStab(object): + """Data reader for fstab""" + __metaclass__ = SingletonParam + fstab_file = '/etc/fstab' + NAME, DIR, TYPE, OPTS, FREQ, PASSNO = range(0,6) + + def __init__(self,fstab_file=None): + if fstab_file: + self.fstab_file = fstab_file + self.cache = [] + self.rotateCache = [] + self.dictUUID = getUUIDDict() + self.rebuildCache() + + def rebuildCache(self): + """Rebuild cache from fstab file""" + self.cache = \ + map(lambda x:map(lambda y:y.strip(),x.split()), + filter(lambda x:x and not x.lstrip().startswith("#"), + open(self.fstab_file,'r').read().split('\n'))) + for data in self.cache: + convertDev = lambda x: path.realpath(x) if x.startswith('/') else x + data[0] = getUdevDeviceInfo( + name=convertDev(self.dictUUID.get(data[0],data[0])) + ).get('DEVNAME','') + data[1] = data[1] if data[2] != "swap" else "swap" + self.rotateCache = zip(*self.cache) + + def getBy(self,what=DIR,where=NAME,eq=None,noteq=None,allentry=False): + """Get data from fstab""" + if not eq is None: + filterfunc = lambda x: x[where] == eq + else: + filterfunc = lambda x: x[where] != noteq + res = map(lambda x:x[what],filter(filterfunc,self.cache)) + if allentry: + return res + else: + return "" if not res else res[-1] + + def getFields(self,*fields): + """Get all data by specifie fields""" + return zip(*reduce(lambda x,y:x+[self.rotateCache[y]],fields,[])) + + def isExists(self,what=DIR,eq=None,noteq=None): + """Field with condition exist in fstab""" + if not eq is None: + filterfunc = lambda x: x[what] == eq + else: + filterfunc = lambda x: x[what] != noteq + return bool(filter(filterfunc,self.cache)) + def isMount(pathname): """В случае монтирования директории выдает другой примонтированный путь""" absPath = path.abspath(pathname)