VULN_CHECK_I913V6 9*bO,dZddlZddlZddlZddlZddlmZddlmZdZ Gdde Z Gdd e Z Gd d e Z Gd d e ZGdde ZGdde Zy)z pyudev.discover =============== Tools to discover a device given limited information. .. moduleauthor:: mulhern NDeviceNotFoundError)DevicescBtjfd}|S)z\ Allow Device discovery methods to return None instead of raising an exception. c4 |i|S#t$rYywxYw)z Returns result of calling ``func`` on ``args``, ``kwargs``. Returns None if ``func`` raises :exc:`DeviceNotFoundError`. Nr)argskwargsfuncs 4/usr/lib/python3.12/site-packages/pyudev/discover.pythe_funcz wrap_exception..the_func+s*  (( ("  s  ) functoolswraps)r r s` r wrap_exceptionr%s' __T OceZdZdZej ZeejdZ eejdZ edZ edZ y) HypothesiszM Represents a hypothesis about the meaning of the device identifier. ct)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 NotImplementedErrorclsvalues r matchzHypothesis.match@s "##rct)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` r)rcontextkeys r lookupzHypothesis.lookupQs "##rcy)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 Nrrs r setupzHypothesis.setup_s rc`|j|}||j||StS)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` )rr frozenset)rrrrs r get_deviceszHypothesis.get_devicesjs.ii+.?szz'3'K KrN) __name__ __module__ __qualname____doc__abcABCMeta __metaclass__ classmethodabstractmethodrrr!r$rrr rr9s~KKM $ $ $ $   L Lrrc`eZdZdZedZedZedZedZedZ y)DeviceNumberHypothesisz Represents the hypothesis that the device is a device number. The device may be separated into major/minor number or a composite number. ctjd}|j|}|xrFtjt |j dt |j dS)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\d+)(\D+)(?P\d+)$majorminor)recompilerosmakedevintgroup)rrmajor_minor_rers r _match_major_minorz)DeviceNumberHypothesis._match_major_minors\$JK$$U+   G$ %s5;;w+?'@  rctjd}|j|}|xrt|j dS)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\d+)$number)r3r4rr7r8)rr number_rers r _match_numberz$DeviceNumberHypothesis._match_numbers:JJ34 &3U[[233rcJ|j|xs|j|S)z Match the number under the assumption that it is a device number. :returns: the device number or None :rtype: int or NoneType )r:r>rs r rzDeviceNumberHypothesis.matchs%%%e,H0A0A%0HHrc|j}tjtjj |dS)z Find subsystems in /sys/dev. :param Context context: the context :returns: a lis of available subsystems :rtype: list of str dev)sys_pathr5listdirpathjoin)rrrBs r find_subsystemsz&DeviceNumberHypothesis.find_subsystemss-##zz"'',,x788rcttjfd|jD}t d|DS)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` c32K|]}|ywNr.0srr rs r z0DeviceNumberHypothesis.lookup..K.JtGQ$.Jc3&K|] }|| ywrIrrKrs r rMz0DeviceNumberHypothesis.lookup..9Cq1=C)rrfrom_device_numberrFr#rrrresr s `` @r rzDeviceNumberHypothesis.lookups;g889Kc.A.A'.JK9C999rN) r%r&r'r(r,r:r>rrFrrrr r/r/xsm      4 4II 9 9 : :rr/c0eZdZdZedZedZy)DevicePathHypothesiszG Discover the device assuming the identifier is a device path. c|S)z Match ``value`` under the assumption that it is a device path. :returns: the device path or None :rtype: str or NoneType rrs r rzDevicePathHypothesis.match  rcrttj||}| t|fStS) 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` )rr from_pathr#)rrrrWs r rzDevicePathHypothesis.lookups50nW../=$'Oy# DDrN)r%r&r'r(r,rrrrr rYrYs1 E ErrYc@eZdZdZedZedZedZy)DeviceNameHypothesiszf Discover the device assuming the input is a device name. Try every available subsystem. cl|jd}fd|D}d|D}td|DS)z Find all subsystems in sysfs. :param Context context: the context :rtype: frozenset :returns: subsystems in sysfs )busclass subsystemc3^K|]$}tjj|&ywrIr5rDrE)rKnamerBs r rMz7DeviceNameHypothesis.find_subsystems..s!FXTBGGLL40X*-c3`K|]&}tjj|s#|(ywrI)r5rDisdirrKds r rMz7DeviceNameHypothesis.find_subsystems..s =1BGGMM!,.s!EIqrzz!}!}Is&()rBr#)rrdirnamesabsnames realnamesrBs @r rFz$DeviceNameHypothesis.find_subsystemss:##0FXF== EIEEErc|S)z Match ``value`` under the assumption that it is a device name. :returns: the device path or None :rtype: str or NoneType rrs r rzDeviceNameHypothesis.matchr[rcttjfd|jD}t d|DS)r]c32K|]}|ywrIrrJs r rMz.DeviceNameHypothesis.lookup..rNrOc3&K|] }|| ywrIrrQs r rMz.DeviceNameHypothesis.lookup.. rSrT)rr from_namerFr#rVs `` @r rzDeviceNameHypothesis.lookups;g//0Kc.A.A'.JK9C999rN)r%r&r'r(r,rFrrrrr r`r`sE  F F : :rr`cXeZdZdZgdZedZedZedZedZ y)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/vgc||j}d|D}d|D}ttd|DS)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 c3LK|]}t|js|ywrI)list device_linksrks r rMz5DeviceFileHypothesis.get_link_dirs...sIAD4Has$$c3BK|]}|jD]}|ywrI)r|)rKrlls r rMz5DeviceFileHypothesis.get_link_dirs../sG.q1.sc3ZK|]#}tjj|%ywrI)r5rDdirname)rKr~s r rMz5DeviceFileHypothesis.get_link_dirs..0s&&(IIG.Gc.Ts!@2b#&rhc30K|] }|ywrIr)rKfrr s r rMz.DeviceFileHypothesis.lookup..Us3U4#Usc3&K|] }|| ywrIrrks r rMz.DeviceFileHypothesis.lookup..Vs=Gqq}GrT)rrfrom_device_filer#r)rrrdevicefilesrr s `` @r rzDeviceFileHypothesis.lookupAsjg667 #:'3'F+1+=9fY' N9; N@@3U3=G===rN) r%r&r'r(rr,rr!rrrrr rxrx s^ J>> 44>>rrxc.eZdZdZeeeegZdZ dZ dZ y) Discoveryz1 Provides discovery methods for devices. c&|j|_yrI) _HYPOTHESES _hypotheses)selfs r __init__zDiscovery.__init__fs++rcH|jD]}|j|y)z Set up individual hypotheses. May be an expensive call. :param Context context: the context N)rr!)rrhyps r r!zDiscovery.setupis ##C IIg $rcDtfd|jDS)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` c3PK|]}|jD]}|ywrI)r$)rKhrlrrs r rMz(Discovery.get_devices..}s* '!w1NAA1NA's#&)r#r)rrrs ``r r$zDiscovery.get_devicests% ''   rN) r%r&r'r(rxr`r/rYrrr!r$rrr rrYs+  K,   rr)r(r)r r5r3pyudev._errorsr pyudev.devicerrobjectrr/rYr`rxrrrr rsy"  /!(<L<L~E:ZE:PE:E:,::,:^J>:J>Z& & r