????
Your IP : 3.142.201.19
a
8g+^w,�@s�dZddlmZddlmZddlmZddlmZddlZddlZddlZddl Z ddl
Z
ddlmZddl
mZd d
�Ze
�ej�Gdd�de��ZGd
d�de�ZGdd�de�ZGdd�de�ZGdd�de�ZGdd�de�ZdS)z�
pyudev.discover
===============
Tools to discover a device given limited information.
.. moduleauthor:: mulhern <amulhern@redhat.com>
�)�absolute_import)�division)�print_function)�unicode_literalsN��DeviceNotFoundError)�Devicescst����fdd��}|S)z\
Allow Device discovery methods to return None instead of raising an
exception.
cs*z�|i|��WSty$YdS0dS)z�
Returns result of calling ``func`` on ``args``, ``kwargs``.
Returns None if ``func`` raises :exc:`DeviceNotFoundError`.
Nr)�args�kwargs��func��3/usr/lib/python3.9/site-packages/pyudev/discover.py�the_func0sz wrap_exception.<locals>.the_func)� functools�wraps)rrr
rr�wrap_exception*s
rc@sLeZdZdZeejdd���Zeejdd���Zedd��Z edd ��Z
d
S)�
HypothesiszM
Represents a hypothesis about the meaning of the device identifier.
cCs
t��dS)a�
Match the given string according to the hypothesis.
The purpose of this method is to obtain a value corresponding to
``value`` if that is possible. It may use a regular expression, but
in general it should just return ``value`` and let the lookup method
sort out the rest.
:param str value: the string to inspect
:returns: the matched thing or None if unmatched
:rtype: the type of lookup's key parameter or NoneType
N��NotImplementedError��cls�valuer
r
r�matchDszHypothesis.matchcCs
t��dS)aN
Lookup the given string according to the hypothesis.
:param Context context: the pyudev context
:param key: a key with which to lookup the device
:type key: the type of match's return value if not None
:returns: a list of Devices obtained
:rtype: frozenset of :class:`Device`
Nr)r�context�keyr
r
r�lookupUszHypothesis.lookupcCsdS)z�
A potentially expensive method that may allow an :class:`Hypothesis`
to find devices more rapidly or to find a device that it would
otherwise miss.
:param Context context: the pyudev context
Nr
�rrr
r
r�setupcs zHypothesis.setupcCs$|�|�}|dur|�||�St�S)a
Get any devices that may correspond to the given string.
:param Context context: the pyudev context
:param str value: the value to look for
:returns: a list of devices obtained
:rtype: set of :class:`Device`
N)rr� frozenset)rrrrr
r
r�get_devicesns
zHypothesis.get_devicesN)�__name__�
__module__�__qualname__�__doc__�classmethod�abc�abstractmethodrrrr r
r
r
rr>s
rc@sLeZdZdZedd��Zedd��Zedd��Zedd ��Zed
d��Z dS)
�DeviceNumberHypothesisz�
Represents the hypothesis that the device is a device number.
The device may be separated into major/minor number or a composite number.
cCs8t�d�}|�|�}|o6t�t|�d��t|�d���S)z�
Match the number under the assumption that it is a major,minor pair.
:param str value: value to match
:returns: the device number or None
:rtype: int or NoneType
z#^(?P<major>\d+)(\D+)(?P<minor>\d+)$�major�minor)�re�compiler�os�makedev�int�group)rrZmajor_minor_rerr
r
r�_match_major_minor�s
�z)DeviceNumberHypothesis._match_major_minorcCs&t�d�}|�|�}|o$t|�d��S)z�
Match the number under the assumption that it is a single number.
:param str value: value to match
:returns: the device number or None
:rtype: int or NoneType
z^(?P<number>\d+)$Znumber)r+r,rr/r0)rrZ number_rerr
r
r�
_match_number�s
z$DeviceNumberHypothesis._match_numbercCs|�|�p|�|�S)z�
Match the number under the assumption that it is a device number.
:returns: the device number or None
:rtype: int or NoneType
)r1r2rr
r
rr�szDeviceNumberHypothesis.matchcCs|j}t�tj�|d��S)z�
Find subsystems in /sys/dev.
:param Context context: the context
:returns: a lis of available subsystems
:rtype: list of str
�dev)�sys_pathr-�listdir�path�join)rrr4r
r
r�find_subsystems�s z&DeviceNumberHypothesis.find_subsystemscs8ttj�����fdd�|���D�}tdd�|D��S)z�
Lookup by the device number.
:param Context context: the context
:param int key: the device number
:returns: a list of matching devices
:rtype: frozenset of :class:`Device`
c3s|]}��|��VqdS�Nr
��.0�s�rrrr
r� <genexpr>��z0DeviceNumberHypothesis.lookup.<locals>.<genexpr>css|]}|dur|VqdSr9r
�r;�rr
r
rr>�r?)rrZfrom_device_numberr8r�rrr�resr
r=rr�s
zDeviceNumberHypothesis.lookupN)
r!r"r#r$r%r1r2rr8rr
r
r
rr(|s
r(c@s(eZdZdZedd��Zedd��ZdS)�DevicePathHypothesiszG
Discover the device assuming the identifier is a device path.
cCs|S)z�
Match ``value`` under the assumption that it is a device path.
:returns: the device path or None
:rtype: str or NoneType
r
rr
r
rr�szDevicePathHypothesis.matchcCs(ttj�||�}|dur"t|f�St�S)��
Lookup by the path.
:param Context context: the context
:param str key: the device path
:returns: a list of matching devices
:rtype: frozenset of :class:`Device`
N)rrZ from_pathrrBr
r
rr�s
zDevicePathHypothesis.lookupN)r!r"r#r$r%rrr
r
r
rrD�s
rDc@s4eZdZdZedd��Zedd��Zedd��ZdS) �DeviceNameHypothesiszf
Discover the device assuming the input is a device name.
Try every available subsystem.
cs<|j�d}�fdd�|D�}dd�|D�}tdd�|D��S)z�
Find all subsystems in sysfs.
:param Context context: the context
:rtype: frozenset
:returns: subsystems in sysfs
)Zbus�classZ subsystemc3s|]}tj��|�VqdSr9�r-r6r7)r;�name�r4r
rr>�r?z7DeviceNameHypothesis.find_subsystems.<locals>.<genexpr>css|]}tj�|�r|VqdSr9)r-r6�isdir�r;�dr
r
rr>�r?css"|]}t�|�D]
}|VqqdSr9)r-r5)r;rM�nr
r
rr>�r?)r4r)rr�dirnamesZabsnamesZ realnamesr
rJrr8�s
z$DeviceNameHypothesis.find_subsystemscCs|S)z�
Match ``value`` under the assumption that it is a device name.
:returns: the device path or None
:rtype: str or NoneType
r
rr
r
rr�szDeviceNameHypothesis.matchcs8ttj�����fdd�|���D�}tdd�|D��S)rEc3s|]}��|��VqdSr9r
r:r=r
rr>r?z.DeviceNameHypothesis.lookup.<locals>.<genexpr>css|]}|dur|VqdSr9r
r@r
r
rr>r?)rr� from_namer8rrBr
r=rrs
zDeviceNameHypothesis.lookupN)r!r"r#r$r%r8rrr
r
r
rrF�s
rFc@sHeZdZdZgd�Zedd��Zedd��Zedd��Zed d
��Z dS)�DeviceFileHypothesisz�
Discover the device assuming the value is some portion of a device file.
The device file may be a link to a device node.
)z/devz/dev/disk/by-idz/dev/disk/by-labelz/dev/disk/by-partlabelz/dev/disk/by-partuuidz/dev/disk/by-pathz/dev/disk/by-uuidz/dev/input/by-pathz/dev/mapperz/dev/mdz/dev/vgcCs:|��}dd�|D�}dd�|D�}ttdd�|D���S)a7
Get all directories that may contain links to device nodes.
This method checks the device links of every device, so it is very
expensive.
:param Context context: the context
:returns: a sorted list of directories that contain device links
:rtype: list
css|]}t|j�r|VqdSr9)�list�device_linksrLr
r
rr>*r?z5DeviceFileHypothesis.get_link_dirs.<locals>.<genexpr>css|]}|jD]
}|VqqdSr9)rS)r;rM�lr
r
rr>+r?css|]}tj�|�VqdSr9)r-r6�dirname)r;rTr
r
rr>,r?)Zlist_devices�sorted�set)rr�devicesZdevices_with_linksZlinksr
r
r�
get_link_dirssz"DeviceFileHypothesis.get_link_dirscCs|�|�|_dS)z�
Set the link directories to be used when discovering by file.
Uses `get_link_dirs`, so is as expensive as it is.
:param Context context: the context
N)rY�
_LINK_DIRSrr
r
rr.s zDeviceFileHypothesis.setupcCs|Sr9r
rr
r
rr9szDeviceFileHypothesis.matchcsnttj��d�vr4����}|dur.t|f�St�S�fdd�|jD�}��fdd�|D�}tdd�|D��S)a�
Lookup the device under the assumption that the key is part of
the name of a device file.
:param Context context: the context
:param str key: a portion of the device file name
It is assumed that either it is the whole name of the device file
or it is the basename.
A device file may be a device node or a device link.
�/Nc3s|]}tj�|��VqdSr9rH)r;Zld)rr
rr>Pr?z.DeviceFileHypothesis.lookup.<locals>.<genexpr>c3s|]}��|�VqdSr9r
)r;�f)rrr
rr>Qr?css|]}|dur|VqdSr9r
rLr
r
rr>Rr?)rrZfrom_device_filerrZ)rrrZdevice�filesrXr
r=rr=s
zDeviceFileHypothesis.lookupN)
r!r"r#r$rZr%rYrrrr
r
r
rrQs
rQc@s4eZdZdZeeeegZdd�Z dd�Z
dd�ZdS) � Discoveryz1
Provides discovery methods for devices.
cCs|j|_dSr9)�_HYPOTHESES�_hypotheses)�selfr
r
r�__init__`szDiscovery.__init__cCs|jD]}|�|�qdS)z
Set up individual hypotheses.
May be an expensive call.
:param Context context: the context
N)r`r)rarZhypr
r
rrcs
zDiscovery.setupcst��fdd�|jD��S)z�
Get the devices corresponding to value.
:param Context context: the context
:param str value: some identifier of the device
:returns: a list of corresponding devices
:rtype: frozenset of :class:`Device`
c3s$|]}|����D]
}|VqqdSr9)r )r;�hrM�rrr
rr>wsz(Discovery.get_devices.<locals>.<genexpr>)rr`)rarrr
rdrr ns �zDiscovery.get_devicesN)r!r"r#r$rQrFr(rDr_rbrr r
r
r
rr^Us�r^)r$Z
__future__rrrrr&rr-r+ZsixZpyudev._errorsrZ
pyudev.devicerrZ
add_metaclass�ABCMeta�objectrr(rDrFrQr^r
r
r
r�<module>s&
=G/F