diff --git a/pym/cl_utils.py b/pym/cl_utils.py index 025b051..5d4d51a 100644 --- a/pym/cl_utils.py +++ b/pym/cl_utils.py @@ -1001,3 +1001,33 @@ def lspci(filtername=None,shortInfo=False): 'vendor':sfunc(device[2]),\ 'name':sfunc(device[3])} return retData + +def getUdevDeviceInfo(path="",name=""): + """Get device info by syspath of name""" + udevadmCmd = getProgPath('/sbin/udevadm') + typeQuery = "--path" if path else "--name" + value = path if path else name + return dict( + filter(lambda x:x[0], + map(lambda x:x.partition("=")[0::2], + process(udevadmCmd,"info","--query","property", + typeQuery,value).read().split("\n")))) + +def getPartitionSize(dev): + """Get partition size""" + SECTORSIZE=512 + syspath = getUdevDeviceInfo(name=dev).get('DEVPATH','') + if syspath: + sizeFile = pathJoin("/sys",syspath,"size") + if path.exists(sizeFile): + size = float(open(sizeFile,'r').read().strip())*SECTORSIZE + suffix = ((1024**0,""), + (1024**1,"KiB"), + (1024**2,"MiB"), + (1024**3,"GiB"), + (1024**4,"TiB"), + (1024**5,"PiB")) + suffix = filter(lambda x:size >=x[0],suffix)[-1] + if suffix: + return "%.1f %s"%(size/suffix[0],suffix[1]) + return "0"