Рефакторинг

* импорт целого модуля device, вместо его полей
master-3.5
parent d9e1f446a1
commit fa3b455d1f

@ -39,8 +39,9 @@ from calculate.lib.utils.files import (removeDir,
MAGIC_CONTINUE, makeDirectory,
isEmpty, check_rw, calculate_exclude,
PercentProgress, getProgPath, xztaropen)
import calculate.lib.utils.device as device
from calculate.lib.utils.device import (detectDeviceForPartition,
udev, countPartitions)
countPartitions)
from calculate.lib.utils.tools import classproperty, Signal, traverse
from calculate.lib.utils.text import _u8
from calculate.lib.variables.linux import LinuxDataVars, Linux
@ -1180,9 +1181,9 @@ class PartitionDistributive(Distributive):
elif deviceName == "":
return True
fdiskProg, gdiskProg = checkUtils('/sbin/fdisk', '/usr/sbin/gdisk')
partitionNumber = \
udev.get_device_info(name=dev).get('ID_PART_ENTRY_NUMBER', '') or \
udev.get_device_info(name=dev).get('UDISKS_PARTITION_NUMBER', '')
info = device.udev.get_device_info(name=dev)
partitionNumber = (info.get('ID_PART_ENTRY_NUMBER','') or
info.get('UDISKS_PARTITION_NUMBER', ''))
devicePartitionCount = countPartitions(deviceName)
if deviceName and not partitionNumber:
raise DistributiveError(

@ -31,8 +31,9 @@ from calculate.lib.utils.files import (pathJoin,
process, listDirectory,
checkUtils, readFile, find, copyWithPath,
readLinesFile, getProgPath)
from calculate.lib.utils.device import (detectDeviceForPartition, sysfs,
udev, lvm, countPartitions)
import calculate.lib.utils.device as device
from calculate.lib.utils.device import (detectDeviceForPartition,
countPartitions)
from datavars import DataVarsInstall
from calculate.install.variables.autopartition import AutoPartition
@ -59,9 +60,9 @@ class Install(MethodsInterface):
def __init__(self):
self.clVars = None
# refresh information about LVM
lvm.refresh()
device.lvm.refresh()
# refresh information about device in udevadm info
udev.refresh()
device.udev.refresh()
def initVars(self, datavars=None):
"""Primary initialization of variables"""
@ -113,10 +114,10 @@ class Install(MethodsInterface):
partition_table = self.clVars.Select('os_device_table',
where='os_device_dev',
eq=disk, limit=1)
info = device.udev.get_device_info(name=partition)
partition_number = (
udev.get_device_info(name=partition).get('ID_PART_ENTRY_NUMBER', '') or
udev.get_device_info(name=partition).get('UDISKS_PARTITION_NUMBER', '')
)
info.get('ID_PART_ENTRY_NUMBER', '') or
info.get('UDISKS_PARTITION_NUMBER', ''))
device_partition_count = countPartitions(device_name)
if device_name and not partition_number:
@ -288,7 +289,7 @@ class Install(MethodsInterface):
where="os_install_disk_mount",
eq="/boot/efi", limit=1)
if efi_disk:
efi_uuid = udev.get_device_info(
efi_uuid = device.udev.get_device_info(
name=efi_disk).get("ID_PART_ENTRY_UUID", "")
if efi_uuid:
p_efibootmgr = process(efi_boot_mgr, "-v")
@ -420,9 +421,10 @@ class Install(MethodsInterface):
where='os_disk_mount',
eq='/', limit=1)
try:
sysname = udev.get_syspath(name=root_dev)
if sysfs.exists(sysname, sysfs.Path.BlockScheduler):
sysfs.write(sysname, sysfs.Path.BlockScheduler, scheduler)
sysname = device.udev.get_syspath(name=root_dev)
if device.sysfs.exists(sysname, device.sysfs.Path.BlockScheduler):
device.sysfs.write(
sysname, device.sysfs.Path.BlockScheduler, scheduler)
except Exception:
raise InstallError(_("Unable to change the I/O scheduler"))
return True

@ -23,7 +23,7 @@ import glob
from calculate.lib.datavars import Variable, VariableError, ReadonlyVariable
from calculate.lib.utils.portage import isPkgInstalled
from calculate.lib.utils.files import readFile
from calculate.lib.utils.device import sysfs
import calculate.lib.utils.device as device
from calculate.lib.utils.common import (getVideoFromXorgLog,
getVideoFromXorgConf,
getVideoFromCmdLine,
@ -226,7 +226,8 @@ class VariableOsInstallX11VideoDrv(VideoVariable):
return self.default_video
else:
for drv in map(lambda x: x[0], self.choice()):
refcnt = sysfs.read(sysfs.Path.Module, drv, "refcnt").strip()
refcnt = device.sysfs.read(
device.sysfs.Path.Module, drv, "refcnt").strip()
if refcnt.isdigit() and int(refcnt) > 0:
return {'i915': 'intel'}.get(drv, drv)
else:

@ -21,7 +21,8 @@ from time import sleep
from calculate.lib.datavars import (Variable, VariableError,
VariableInterface,
ReadonlyVariable, ReadonlyTableVariable)
from calculate.lib.utils.device import (humanreadableSize, udev)
import calculate.lib.utils.device as device
from calculate.lib.utils.device import humanreadableSize
from calculate.lib.utils.files import (readLinesFile, process, getProgPath)
from calculate.install.fs_manager import FileSystemManager
from calculate.lib.utils.tools import Sizes
@ -98,7 +99,7 @@ class AutoPartition(object):
Autopartition maker
"""
def recreateSpace(self, table, device, data, lvm, vgname, bios_grub,
def recreateSpace(self, table, dev, data, lvm, vgname, bios_grub,
bios_grub_size):
"""
Recreate disk space by table device data lvm flag and vgname
@ -111,26 +112,26 @@ class AutoPartition(object):
vgname lvm Volume Group Name
"""
if lvm:
self.recreateLvm(table, device, data, vgname, bios_grub,
self.recreateLvm(table, dev, data, vgname, bios_grub,
bios_grub_size)
else:
self.recreatePartitionTable(table, device, data, bios_grub,
self.recreatePartitionTable(table, dev, data, bios_grub,
bios_grub_size)
udev.refresh()
device.udev.refresh()
def recreatePartitionTable(self, table, device, data, bios_grub,
def recreatePartitionTable(self, table, dev, data, bios_grub,
bios_grub_size):
"""
"""
mapDispatch = {'dos': self.recreatePartitionTableDos,
'gpt': self.recreatePartitionTableGpt}
if table in mapDispatch:
mapDispatch[table](device, data, bios_grub, bios_grub_size)
mapDispatch[table](dev, data, bios_grub, bios_grub_size)
else:
raise AutopartitionError(
_('Autopartitioning for %s is not supported') % table)
def recreatePartitionTableDos(self, device, data, bios_grub,
def recreatePartitionTableDos(self, dev, data, bios_grub,
bios_grub_size):
"""
Create DOS partition table by /sbin/fdisk
@ -143,7 +144,7 @@ class AutoPartition(object):
WRITE_AND_QUIT = "w\nq\n"
fdiskProg = getProgPath('/sbin/fdisk')
fdisk = process(fdiskProg, device[0])
fdisk = process(fdiskProg, dev[0])
fdisk.write(NEW_PARTITION_TABLE)
num = 1
for size in map(lambda x: str(Sizes().to_K(int(x))) \
@ -161,11 +162,11 @@ class AutoPartition(object):
fdisk.write(WRITE_AND_QUIT)
fdisk.success()
# ожидаение появления устройств ({device}{partnum} или {device}p{partnum})
self._waitDevice("%s%s"%(device[0],str(num - 1)),
"%sp%s"%(device[0],str(num - 1)))
# ожидаение появления устройств ({dev}{partnum} или {dev}p{partnum})
self._waitDevice("%s%s"%(dev[0],str(num - 1)),
"%sp%s"%(dev[0],str(num - 1)))
def recreatePartitionTableGpt(self, device, data, bios_grub=True,
def recreatePartitionTableGpt(self, dev, data, bios_grub=True,
bios_grub_size=0):
"""
Create GPT partition table by /sbin/gdisk
@ -180,7 +181,7 @@ class AutoPartition(object):
BIOS_BOOT_PART_SIZE = "%dM" % (int(bios_grub_size) / Sizes.M)
fdiskProg = getProgPath('/usr/sbin/gdisk')
fdisk = process(fdiskProg, device[0])
fdisk = process(fdiskProg, dev[0])
fdisk.write(NEW_PARTITION_TABLE)
num = 1
biosBootCreated = not bios_grub
@ -205,9 +206,9 @@ class AutoPartition(object):
fdisk.write(NEW_BIOSBOOT_PARTITION % "")
fdisk.write(WRITE_AND_QUIT)
fdisk.success()
# ожидаение появления устройств ({device}{partnum} или {device}p{partnum})
self._waitDevice("%s%s"%(device[0],str(num - 1)),
"%sp%s"%(device[0],str(num - 1)))
# ожидаение появления устройств ({dev}{partnum} или {dev}p{partnum})
self._waitDevice("%s%s"%(dev[0],str(num - 1)),
"%sp%s"%(dev[0],str(num - 1)))
def _waitDevice(self, *devices):
"""

@ -29,10 +29,11 @@ from calculate.lib.datavars import (TableVariable, Variable, VariableError,
ReadonlyVariable, ReadonlyTableVariable,
SourceReadonlyVariable, VariableInterface,
HumanReadable)
import calculate.lib.utils.device as device
from calculate.lib.utils.device import (getDeviceType,
getPartitionType, getPartitionDevice,
raid, lvm, getPartitionSize,
humanreadableSize, udev, sysfs,
getPartitionSize,
humanreadableSize,
getUUIDDict, getCommonDeviceName)
from calculate.install.variables.autopartition import Sizes
from calculate.lib.utils.files import (listDirectory, pathJoin, readFile,
@ -53,27 +54,27 @@ class DeviceHelper(VariableInterface):
def getBlockDevices(self):
"""Get interest devices from sys block path"""
return filter(self.rePassDevice.search,
udev.get_block_devices())
device.udev.get_block_devices())
def separateDevice(self, device):
def separateDevice(self, dev):
"""
Separate device word and number on tuple
Using for sort. (Example: sda2 ("sda",2), md5p1 ("md",5,"p",1)
"""
return map(lambda x: int(x) if x.isdigit() else x,
re.findall('\d+|\D+', device))
re.findall('\d+|\D+', dev))
def mapUdevProperty(self, var, prop, default):
"""Get each element from var through udev [prop]"""
return map(lambda x: udev.get_device_info(name=x).get(prop, default),
self.Get(var))
return [device.udev.get_device_info(name=x).get(prop, default)
for x in self.Get(var)]
def getPerfectName(self, device, defaultValue=None):
def getPerfectName(self, dev, defaultValue=None):
"""
Get dev name or human-readable lvm name
"""
info = udev.get_device_info(name=device)
info = device.udev.get_device_info(name=dev)
if 'DM_VG_NAME' in info and 'DM_LV_NAME' in info:
lvmDeviceName = '/dev/{vg}/{lv}'.format(vg=info['DM_VG_NAME'],
lv=info['DM_LV_NAME'])
@ -84,11 +85,11 @@ class DeviceHelper(VariableInterface):
else:
return defaultValue
def getLvmName(self, device):
def getLvmName(self, dev):
"""
Get lvm name
"""
return self.getPerfectName(device, defaultValue="")
return self.getPerfectName(dev, defaultValue="")
#######################################################
@ -137,8 +138,9 @@ class VariableOsDeviceDev(DeviceHelper, ReadonlyVariable):
# get devices from block sys directories(discard mem,sr,loop and other)
devices = (x for x in self.getBlockDevices() if x.count('/') == 2)
devnames = udev.syspath_to_devname(
x for x in devices if udev.is_device(udev.get_device_info(x)))
devnames = device.udev.syspath_to_devname(
x for x in devices
if device.udev.is_device(device.udev.get_device_info(x)))
return list(sorted((x for x in devnames),
key=self.separateDevice))
@ -161,7 +163,7 @@ class VariableOsDeviceDev(DeviceHelper, ReadonlyVariable):
self.parent.Invalidate(self.name)
self.parent.Invalidate('os_install_disk_uuid')
self.parent.Invalidate('os_disk_dev')
udev.clear_cache()
device.udev.clear_cache()
except OSError as e:
pass
@ -172,11 +174,11 @@ class VariableOsDeviceType(ReadonlyVariable):
"""
type = "list"
def getType(self, device):
device_name = path.basename(device)
def getType(self, dev):
device_name = path.basename(dev)
if device_name in self.usbdevices:
if sysfs.read(sysfs.Path.Block,device_name,
"removable").strip() == 1:
if device.sysfs.read(device.sysfs.Path.Block,device_name,
"removable").strip() == 1:
return "flash"
else:
return "usb-hdd"
@ -247,10 +249,10 @@ class VariableOsDeviceSsdSet(ReadonlyVariable):
def get(self):
"""Get device partition table"""
def isSsd(device, name):
prop = udev.get_device_info(name=device)
def isSsd(dev, name):
prop = device.udev.get_device_info(name=dev)
rpm = prop.get(self.udev_property, None)
if (any(x in device for x in self.devnames)
if (any(x in dev for x in self.devnames)
or rpm == "0" or any(x in name for x in self.ssd_names)):
return "on"
else:
@ -271,8 +273,8 @@ class VariableOsDeviceSyspath(ReadonlyVariable):
def get(self):
"""Get device partition table"""
def getSysPath(device):
prop = udev.get_device_info(name=device)
def getSysPath(dev):
prop = device.udev.get_device_info(name=dev)
syspath = prop.get(self.udev_property, "")
return syspath
@ -310,12 +312,12 @@ class VariableOsDeviceTable(ReadonlyVariable):
"""
type = "list"
def getTableByChild(self, device):
def getTableByChild(self, dev):
"""Get table by child partitions"""
syspath = udev.get_syspath(name=device)
shortname = path.basename(device)
for child in sysfs.glob(syspath, "%s*" % shortname):
udevinfo = udev.get_device_info(path=child)
syspath = device.udev.get_syspath(name=dev)
shortname = path.basename(dev)
for child in device.sysfs.glob(syspath, "%s*" % shortname):
udevinfo = device.udev.get_device_info(path=child)
map_names = {'mbr': 'dos',
'msdos': 'dos'}
table = (udevinfo.get('ID_PART_ENTRY_SCHEME', '') or
@ -328,16 +330,16 @@ class VariableOsDeviceTable(ReadonlyVariable):
autopartition = self.Get('cl_autopartition_set') == 'on'
autoDevice = self.Get('cl_autopartition_device')
def getTable(device):
prop = udev.get_device_info(name=device)
def getTable(dev):
prop = device.udev.get_device_info(name=dev)
return prop.get('ID_PART_TABLE_TYPE',
self.getTableByChild(device))
self.getTableByChild(dev))
def getByAutopartition(device):
if autopartition and autoDevice == device:
def getByAutopartition(dev):
if autopartition and autoDevice == dev:
return self.Get('cl_autopartition_table')
else:
return getTable(device)
return getTable(dev)
return map(getByAutopartition,
self.Get('os_device_dev'))
@ -353,11 +355,11 @@ class VariableOsDeviceName(ReadonlyVariable):
'mmcblk': 'Multimedia Card'
}
def getName(self, device):
devicepath = udev.get_syspath(name=device)
def getName(self, dev):
devicepath = device.udev.get_syspath(name=dev)
if devicepath:
vendor = sysfs.read(devicepath, "device/vendor").strip()
model = sysfs.read(devicepath, "device/model").strip()
vendor = device.sysfs.read(devicepath, "device/vendor").strip()
model = device.sysfs.read(devicepath, "device/model").strip()
if vendor or model:
return ("%s %s" %
(vendor, model)).strip()
@ -424,7 +426,7 @@ class VariableOsDiskDev(DeviceHelper, ReadonlyVariable):
disks = self.getBlockDevices()
parents = {re_parent.search(x).group()
for x in disks if x.count("/") > 2}
dev_names = udev.syspath_to_devname(
dev_names = device.udev.syspath_to_devname(
(x for x in disks if x not in parents),
dropempty=False)
return list(sorted((x for x in dev_names), key=self.separateDevice))
@ -472,10 +474,10 @@ class VariableOsDiskFormat(ReadonlyVariable):
"""Get current disk filesystem"""
fstab = FStab('/etc/fstab', devs=self.Get('os_disk_dev'))
def getFormat(device):
prop = udev.get_device_info(name=device)
def getFormat(dev):
prop = device.udev.get_device_info(name=dev)
return prop.get('FSTAB_TYPE') or \
fstab.getBy(what=fstab.TYPE, eq=device) or \
fstab.getBy(what=fstab.TYPE, eq=dev) or \
prop.get('ID_FS_TYPE', '')
return map(getFormat,
@ -506,18 +508,18 @@ class VariableOsDiskType(ReadonlyVariable):
lvmUsedDisks[diskName]))
return diskName, diskType
for device, diskType in types:
prop = udev.get_device_info(name=device)
for dev, diskType in types:
prop = device.udev.get_device_info(name=dev)
if "raid" in diskType:
for x in raid.devices(prop.get('DEVPATH', '')):
raidUsedDisks[x] = device
for x in device.raid.devices(prop.get('DEVPATH', '')):
raidUsedDisks[x] = dev
if diskType.endswith("lvm"):
for x in lvm.used_partitions(prop.get('DM_VG_NAME', ''),
prop.get('DM_LV_NAME', '')):
for x in device.lvm.used_partitions(prop.get('DM_VG_NAME', ''),
prop.get('DM_LV_NAME', '')):
if x in lvmUsedDisks:
lvmUsedDisks[x].append(device)
lvmUsedDisks[x].append(dev)
else:
lvmUsedDisks[x] = [device]
lvmUsedDisks[x] = [dev]
return map(lambda x: x[1],
map(forMember,
types))
@ -571,9 +573,9 @@ class VariableOsDiskParent(ReadonlyVariable):
def get(self):
"""Get disk parent"""
return map(getPartitionDevice,
map(lambda x: udev.get_device_info(name=x).get('DEVPATH', ''),
self.Get('os_disk_dev')))
return [getPartitionDevice(x)
for x in (device.udev.get_device_info(name=y).get('DEVPATH', '')
for y in self.Get("os_disk_dev"))]
class VariableOsDiskId(DeviceHelper, ReadonlyVariable):
@ -608,8 +610,8 @@ class VariableOsDiskGrub(ReadonlyVariable):
self.Get('os_device_map')))
def getGrubMap(devParent):
device, parent = devParent
prop = udev.get_device_info(name=device)
dev, parent = devParent
prop = device.udev.get_device_info(name=dev)
partnum = int(prop.get('ID_PART_ENTRY_NUMBER', 0))
if parent in devicesMap.keys() and partnum:
return "%s,%d" % (devicesMap[parent], partnum - 1)
@ -633,7 +635,8 @@ class VariableOsDiskPart(ReadonlyVariable):
return \
map(lambda x: x[0] if x[1] else "",
map(lambda x: (
getPartitionType(udev.get_device_info(name=x[0])), x[1]),
getPartitionType(
device.udev.get_device_info(name=x[0])), x[1]),
self.ZipVars('os_disk_dev', 'os_disk_parent')))
@ -673,8 +676,8 @@ class VariableOsDiskOptions(ReadonlyVariable):
def get(self):
fstab = FStab('/etc/fstab', devs=self.Get('os_disk_dev'))
def getFormat(device):
return fstab.getBy(what=fstab.OPTS, eq=device)
def getFormat(dev):
return fstab.getBy(what=fstab.OPTS, eq=dev)
return map(getFormat,
self.Get('os_disk_dev'))
@ -885,9 +888,9 @@ class VariableOsLocationSource(LocationHelper, DeviceHelper, Variable):
###########################
disks = filter(lambda x: x.startswith('/dev/'), value)
# get original /dev names
cnDisks = map(lambda x: udev.get_device_info(name=x).get('DEVNAME', x),
disks)
wrongDevices = list(set(cnDisks) - \
cnDisks = (device.udev.get_device_info(name=x).get('DEVNAME', x)
for x in disks)
wrongDevices = list(set(cnDisks) -
set(self.fixOsDiskDev()))
if wrongDevices:
raise VariableError(_("Wrong device '%s'") % wrongDevices[0])
@ -2036,10 +2039,10 @@ class VariableOsInstallRootType(LocationHelper, Variable):
return "flash"
else:
rootdev = self.Get('os_install_root_dev')
device = getPartitionDevice(
udev.get_device_info(name=rootdev).get('DEVPATH', ''))
dev = getPartitionDevice(
device.udev.get_device_info(name=rootdev).get('DEVPATH', ''))
device_type = self.Select(
'os_device_type', where='os_device_dev', eq=device, limit=1)
'os_device_type', where='os_device_dev', eq=dev, limit=1)
if device_type in ("usb-hdd", "flash"):
return "usb-hdd"
return "hdd"

@ -29,7 +29,8 @@ from calculate.lib.utils.ip import (getInterfaces, getIp, getMask, getMac,
cidrToMask, maskToCidr, getIpNet, isDhcpIp,
checkIp, checkMask, isSlaveInterface,
getOperState, getPlugged)
from calculate.lib.utils.device import lspci, sysfs
import calculate.lib.utils.device as device
from calculate.lib.utils.device import lspci
from calculate.lib.utils.files import (listDirectory, readLinesFile)
from calculate.lib.utils import ip
from calculate.lib.utils.portage import isPkgInstalled
@ -234,7 +235,7 @@ class VariableOsInstallNetName(NetHelper, ReadonlyVariable):
def getPci(interface):
pathname = path.realpath(
sysfs.syspath(sysfs.Path.ClassNet, interface))
device.sysfs.syspath(device.sysfs.Path.ClassNet, interface))
pci = rePci.search(pathname)
if pci:

@ -29,7 +29,7 @@ from calculate.lib.utils.common import getPasswdUsers, getUserGroups, getGroups,
from calculate.lib.utils.common import getValueFromConfig, getValueFromCmdLine
from calculate.lib.utils.common import getUserPrimaryGroup
from calculate.lib.utils.portage import isPkgInstalled
from calculate.lib.utils.device import udev
import calculate.lib.utils.device as device
from calculate.lib.encrypt import sha256_crypt
from calculate.core.server.admin import Admins
from calculate.lib.variables.system import RootType
@ -650,7 +650,8 @@ class VariableClInstallDevFrom(Variable):
If device in calculate3.env dev_from not exists set ''
"""
if value:
value = udev.get_device_info(name=value).get('DEVNAME', value)
value = device.udev.get_device_info(
name=value).get('DEVNAME', value)
if value in self.Get('os_disk_dev'):
return value
else:

Loading…
Cancel
Save