[PATCH 1/5] Replace our pyudev with the package python-pyudev.

Vratislav Podzimek vpodzime at redhat.com
Wed Jul 30 07:18:46 UTC 2014


On Tue, 2014-07-29 at 13:10 -0500, David Lehman wrote:

>  
>      def backupConfigs(self, restore=False):
> @@ -2154,18 +2154,19 @@ class DeviceTree(object):
>          # blocks or since previous iterations.
>          while True:
>              devices = []
> -            new_devices = udev.get_block_devices()
> +            new_devices = udev.get_devices()
>  
>              for new_device in new_devices:
> -                if new_device['name'] not in old_devices:
> -                    old_devices[new_device['name']] = new_device
> +                new_name = udev.device_get_name(new_device)
> +                if not old_devices.has_key(new_name):
This is not valid in Python3, please use 'new_name in old_devices'
instead of has_key()



> diff --git a/blivet/udev.py b/blivet/udev.py
> index b66e30b..b87295b 100644
> --- a/blivet/udev.py
> +++ b/blivet/udev.py
> @@ -27,45 +27,28 @@ import re
>  from . import util
>  from .size import Size
>  
> -from . import pyudev
> -global_udev = pyudev.Udev()
> +import pyudev
> +global_udev = pyudev.Context()
>  
>  import logging
>  log = logging.getLogger("blivet")
>  
> -def enumerate_devices(deviceClass="block"):
> -    devices = global_udev.enumerate_devices(subsystem=deviceClass)
> -    return [path[4:] for path in devices]
> -
>  def get_device(sysfs_path):
> -    if not os.path.exists("/sys%s" % sysfs_path):
> -        log.debug("%s does not exist", sysfs_path)
> -        return None
> -
> -    # XXX we remove the /sys part when enumerating devices,
> -    # so we have to prepend it when creating the device
> -    dev = global_udev.create_device("/sys" + sysfs_path)
> -
> -    if dev:
> -        dev["name"] = dev.sysname
> -        dev["sysfs_path"] = sysfs_path
> -
> -        # now add in the contents of the uevent file since they're handy
> -        dev = parse_uevent_file(dev)
> +    try:
> +        dev = pyudev.Device.from_sys_path(global_udev, sysfs_path)
> +    except pyudev.DeviceNotFoundError as e:
> +        log.error(e)
> +        dev = None
>  
>      return dev
>  
> -def get_devices(deviceClass="block"):
> +def get_devices(subsystem="block"):
>      settle()
> -    entries = []
> -    for path in enumerate_devices(deviceClass):
> -        entry = get_device(path)
> -        if entry:
> -            entries.append(entry)
> -    return entries
> +    return [d for d in global_udev.list_devices(subsystem=subsystem)
> +                        if not __is_blacklisted_blockdev(d.sys_name)]
>  
>  def parse_uevent_file(dev):
> -    path = os.path.normpath("/sys/%s/uevent" % dev['sysfs_path'])
> +    path = os.path.normpath("%s/uevent" % device_get_sysfs_path(dev))
>      if not os.access(path, os.R_OK):
>          return dev
>  
> @@ -103,7 +86,7 @@ def resolve_devspec(devspec):
>      from . import devices
>  
>      ret = None
> -    for dev in get_block_devices():
> +    for dev in get_devices():
>          if devspec.startswith("LABEL="):
>              if device_get_label(dev) == devspec[6:]:
>                  ret = dev
> @@ -120,7 +103,7 @@ def resolve_devspec(devspec):
>              if not spec.startswith("/dev/"):
>                  spec = os.path.normpath("/dev/" + spec)
>  
> -            for link in dev["symlinks"]:
> +            for link in device_get_symlinks(dev):
>                  if spec == link:
>                      ret = dev
>                      break
> @@ -135,37 +118,18 @@ def resolve_glob(glob):
>      if not glob:
>          return ret
>  
> -    for dev in get_block_devices():
> +    for dev in get_devices():
>          name = device_get_name(dev)
>  
>          if fnmatch.fnmatch(name, glob):
>              ret.append(name)
>          else:
> -            for link in dev["symlinks"]:
> +            for link in device_get_symlinks(dev):
>                  if fnmatch.fnmatch(link, glob):
>                      ret.append(name)
>  
>      return ret
>  
> -def get_block_devices():
> -    settle()
> -    entries = []
> -    for path in enumerate_block_devices():
> -        entry = get_block_device(path)
> -        if entry:
> -            if entry["name"].startswith("md"):
> -                # mdraid is really braindead, when a device is stopped
> -                # it is no longer usefull in anyway (and we should not
> -                # probe it) yet it still sticks around, see bug rh523387
> -                state = None
> -                state_file = "/sys/%s/md/array_state" % entry["sysfs_path"]
> -                if os.access(state_file, os.R_OK):
> -                    state = open(state_file).read().strip()
> -                if state == "clear":
> -                    continue
> -            entries.append(entry)
> -    return entries
> -
>  def __is_blacklisted_blockdev(dev_name):
>      """Is this a blockdev we never want for an install?"""
>      if dev_name.startswith("ram") or dev_name.startswith("fd"):
> @@ -180,17 +144,6 @@ def __is_blacklisted_blockdev(dev_name):
>  
>      return False
>  
> -def enumerate_block_devices():
> -    return [d for d in enumerate_devices(deviceClass="block") if not __is_blacklisted_blockdev(os.path.basename(d))]
> -
> -def get_block_device(sysfs_path):
> -    dev = get_device(sysfs_path)
> -    if not dev or 'name' not in dev:
> -        return None
> -    else:
> -        return dev
> -
> -
>  # These are functions for retrieving specific pieces of information from
>  # udev database entries.
>  def device_get_name(udev_info):
> @@ -198,7 +151,7 @@ def device_get_name(udev_info):
>      if "DM_NAME" in udev_info:
>          name = udev_info["DM_NAME"]
>      else:
> -        name = udev_info["name"]
> +        name = udev_info.sys_name
>  
>      return name
>  
> @@ -236,7 +189,7 @@ def device_is_md(info):
>  
>      # The udev information keeps shifting around. Only md arrays have a
>      # /sys/class/block/<name>/md/ subdirectory.
> -    md_dir = "/sys" + device_get_sysfs_path(info) + "/md"
> +    md_dir = device_get_sysfs_path(info) + "/md"
>      return os.path.exists(md_dir)
>  
>  def device_is_cciss(info):
> @@ -256,7 +209,7 @@ def device_is_zfcp(info):
>      if info.get("DEVTYPE") != "disk":
>          return False
>  
> -    subsystem = "/sys" + info.get("sysfs_path")
> +    subsystem = device_get_sysfs_path(info)
>  
>      while True:
>          topdir = os.path.realpath(os.path.dirname(subsystem))
> @@ -284,7 +237,7 @@ def device_get_zfcp_attribute(info, attr=None):
>          log.debug("device_get_zfcp_attribute() called with attr=None")
>          return None
>  
> -    attribute = "/sys%s/device/%s" % (info.get("sysfs_path"), attr,)
> +    attribute = "%s/device/%s" % (device_get_sysfs_path(info), attr,)
You can remove the trailing comma here while changing this line.

-- 
Vratislav Podzimek

Anaconda Rider | RHCE | Red Hat, Inc. | Brno - Czech Republic




More information about the anaconda-patches mailing list