Add os_device* variables

netsetup
Mike Hiretsky 14 years ago
parent 87facedfd4
commit d488c975e5

@ -16,7 +16,10 @@
import os
import re
import cl_overriding
from cl_datavars import glob_attr
from os.path import join as pathjoin, exists as pathexists
from os import readlink,listdir,access,R_OK
class fillVars(object, glob_attr):
@ -27,13 +30,13 @@ class fillVars(object, glob_attr):
# Получена ли сеть по DHCP если нет to ip или off
for interfaces in netInterfaces:
fdhcpLeases = "/var/lib/dhcp/dhclient.leases"
if os.access(fdhcpLeases, os.R_OK) and\
if access(fdhcpLeases, R_OK) and\
interfaces in open(fdhcpLeases).read():
listInterfacesInfo.append((interfaces, "DHCP"))
continue
fdhcpInfo = "/var/lib/dhcpcd/dhcpcd-%s.info"%interfaces
fdhcpLease = "/var/lib/dhcpcd/dhcpcd-%s.lease"%interfaces
if os.path.exists(fdhcpInfo) or os.path.exists(fdhcpLease):
if pathexists(fdhcpInfo) or pathexists(fdhcpLease):
listInterfacesInfo.append((interfaces, "DHCP"))
# Если интерфейс без DHCP
if not (interfaces, "DHCP") in listInterfacesInfo:
@ -50,19 +53,36 @@ class fillVars(object, glob_attr):
listInterfacesInfo.append((interfaces, "Off"))
return ", ".join(map(lambda x:"%s (%s)"%(x[0],x[1]),listInterfacesInfo))
def get_os_device_hash(self):
diskIdPath = '/dev/disk/by-id'
usbdevices = \
map(lambda x: readlink(pathjoin(diskIdPath,x)).rpartition('/')[2],
filter(lambda x: x.startswith('usb-'),listdir(diskIdPath)))
reWrongDevice = re.compile("|".join(['sr','fd','ram','loop']))
devices = filter( lambda x: not reWrongDevice.search(x),
listdir('/sys/block'))
device_hash = {}
for mapnum,device in enumerate(sorted(devices)):
device_hash[device] = {}
device_hash[device]['map'] = mapnum
if device in usbdevices:
removablePath = '/sys/block/%s/removable'%device
if os.access(removablePath,R_OK) and \
open(removablePath,'r').read().strip() == "1":
devtype = "flash"
else:
devtype = "usb-hdd"
else:
devtype = "hdd"
device_hash[device]['type'] = devtype
return device_hash
def get_os_disk_hash(self):
reWrongDevice = re.compile("|".join(['ram','loop']))
reSdaPart = re.compile("^/dev/sd([a-z])(\d+)$")
rePartInfo = re.compile("""
^(?P<dev>/dev/[^:]+):\s*
(LABEL=\"(?P<name>[^\"]*)\")?\s*
(UUID=\"(?P<uuid>[^\"]*)\")?\s*
(TYPE=\"(?P<format>[^\"]*)\")?""", re.X)
devices = filter( lambda x: not reWrongDevice.search(x),
os.listdir('/sys/block'))
devices = self.Get('os_device_hash').keys()
disks = reduce( lambda x,y: x +
map( lambda x: "/dev/%s"%x,
filter(lambda x: y in x,os.listdir('/sys/block/%s'%y))),
filter(lambda x: y in x,listdir('/sys/block/%s'%y))),
devices, [] )
disk_hash = {}
# fill grub info
@ -72,14 +92,16 @@ class fillVars(object, glob_attr):
if match:
disk_hash[dev]['grub'] = "%d,%d" % \
(ord(match.groups()[0])-ord('a'),
int(match.groups()[1]))
int(match.groups()[1])-1)
curDevice = None
# parse all parted lines started with Disk and started with number
partedLines = \
filter(lambda x: x.startswith("Disk") or
x.strip()[:1].isdigit(),
self._runos('LANG=C /usr/sbin/parted -l',ret_list=True))
res = self._runos('LANG=C /usr/sbin/parted -l',ret_list=True)
if res is False:
cl_overriding.printERROR("Cann't execute /usr/sbin/parted")
cl_overriding.exit(1)
partedLines = filter(lambda x: x.startswith("Disk") or
x.strip()[:1].isdigit(), res )
for line in partedLines:
# split data
parts = filter(lambda x: x, line.strip().split(' '))
@ -96,70 +118,79 @@ class fillVars(object, glob_attr):
disk_hash[partition] = {}
disk_hash[partition]['part'] = parts[4]
disk_hash[partition]['size'] = parts[3]
# fill format, name and uuid
for line in self._runos('/sbin/blkid',ret_list=True):
partinfo = rePartInfo.search(line)
partinfo = partinfo.groupdict() if partinfo else None
if partinfo and partinfo['dev'] in disks:
dev = partinfo['dev']
disk_hash[dev]['format'] = partinfo['format'] or ""
disk_hash[dev]['name'] = partinfo['name'] or ""
disk_hash[dev]['uuid'] = partinfo['uuid'] or ""
res = self._runos('/sbin/blkid',ret_list=True)
if res is False:
cl_overriding.printERROR("Cann't execute /sbin/blkid")
cl_overriding.exit(1)
# map attribute name of blkid to disk_hash
blkid_hash = {'LABEL':'name',
'UUID':'uuid',
'TYPE':'format'}
for line in res:
# split line and discard empty elements
parts = filter(lambda x: x, line.strip().split(' '))
if len(parts)>1 and parts[0][:-1] in disks:
dev = parts[0][:-1]
for i in parts[1:]:
key,op,value = i.partition('=')
if key in blkid_hash:
key = blkid_hash[key]
disk_hash[dev][key] = value[1:-1]
return disk_hash
def get_os_disk_dev(self):
"""List of available partition devices"""
return sorted(self.Get('os_disk_hash').keys())
def getAttributeFromHash(self,attr):
hash = self.Get('os_disk_hash')
def getAttributeFromHash(self,var,attr):
hash = self.Get(var)
return map(lambda x: hash[x][attr] if attr in hash[x] else "",
sorted(hash.keys()))
def get_os_disk_uuid(self):
"""List uudi for partition devices"""
return self.getAttributeFromHash('uuid')
return self.getAttributeFromHash('os_disk_hash','uuid')
def get_os_disk_install(self):
"""List mounted points for installed system"""
return self.getAttributeFromHash('install')
return self.getAttributeFromHash('os_disk_hash','install')
def get_os_disk_load(self):
"""List mounted points for current operation system"""
return self.getAttributeFromHash('load')
return self.getAttributeFromHash('os_disk_hash','load')
def get_os_disk_format(self):
"""List filesystem for partition devices"""
return self.getAttributeFromHash('format')
return self.getAttributeFromHash('os_disk_hash','format')
def get_os_disk_grub(self):
"""List grub id for partition devices"""
return self.getAttributeFromHash('grub')
return self.getAttributeFromHash('os_disk_hash','grub')
def get_os_disk_part(self):
"""Type of partition devices (primary, extended or logical)"""
return self.getAttributeFromHash('part')
return self.getAttributeFromHash('os_disk_hash','part')
def get_os_disk_size(self):
"""Partition size"""
return self.getAttributeFromHash('size')
return self.getAttributeFromHash('os_disk_hash','size')
def get_os_disk_name(self):
"""Label of partitions"""
return self.getAttributeFromHash('name')
return self.getAttributeFromHash('os_disk_hash','name')
def get_os_device_dev(self):
"""Devices"""
pass
return sorted(self.Get('os_device_hash').keys())
def get_os_device_type(self):
"""Device type (hdd,cdrom,usb-flash)"""
pass
return self.getAttributeFromHash('os_device_hash','type')
def get_os_device_map(self):
"""Map number for grub"""
pass
return self.getAttributeFromHash('os_device_hash','map')
def get_os_grub_info(self):
"""Part of current grub.conf"""

@ -37,6 +37,9 @@ class Data:
# inforamation about net interfaces
os_net_interfaces_info = {}
# infomation about disk in hash
os_device_hash = {'official':True}
# infomation about disk in hash
os_disk_hash = {'official':True}

Loading…
Cancel
Save