o
    ?È^)#  ã                   @   sÖ   d dl mZ d dlZd dlZd dlZd dlZd dlZg d¢Zg d¢Zg d¢Z	dZ
dd„ Zejd	k r6d
d„ Zndd„ Zedfdd„Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd d!„ Zd"d#„ ZdS )$é    )ÚdeepcopyN)ÚaddressÚnetmaskÚ	broadcastÚnetworkÚmetricÚgatewayÚpointtopointÚmediaÚmtuÚhostnameÚ
leasehoursÚ	leasetimeÚvendorÚclientÚbootfileÚserverÚhwaddrÚproviderÚframeÚnetnumÚendpointÚlocalÚttl)zpre-upÚupzpost-upÚdownzpre-downz	post-down)Úbridge_ageingÚbridge_bridgeprioÚ	bridge_fdÚbridge_gcinitÚbridge_helloÚbridge_maxageÚbridge_maxwaitÚ
bridge_stpi   c                 C   s®   t | ƒ}t| tƒrt|tƒr| |¡ |S | ¡ D ];\}}||v rPt|| tƒr7t|tƒr7t|| |ƒ||< qt|| tƒrOt|tƒrOtt|| | ƒƒ||< q|||< q|S ©N)r   Ú
isinstanceÚlistÚextendÚitemsÚdictÚ
dict_mergeÚset)ÚontoÚsourceÚtargetÚkeyÚvalue© r1   ú//usr/lib/python3/dist-packages/probert/utils.pyr*      s   
€
r*   )r   é   c                 C   s>   i }| j D ]}| j  |¡}t|tƒr| dd¡}|||< q|S ©Nzutf-8Úreplace)Ú
attributesÚgetr%   ÚbytesÚdecode©ÚdeviceÚrr/   Úvalr1   r1   r2   Úudev_get_attributes1   s   


r>   c                 C   s@   i }| j jD ]}| j  |¡}t|tƒr| dd¡}|||< q|S r4   )r6   Úavailable_attributesr7   r%   r8   r9   r:   r1   r1   r2   r>   :   s   

é   c                    s(   dd„ t t ‡ fdd„| D ƒ|¡ƒD ƒS )Nc                 s   s"   � | ]\}}d d„ ||ƒV  qdS )c                    s   ‡ fdd„|D ƒS )Nc                 3   s    � | ]\}}|ˆ kr|V  qd S r$   r1   )Ú.0ÚpredÚitem©Úir1   r2   Ú	<genexpr>F   s   € z:partitionn2.<locals>.<genexpr>.<lambda>.<locals>.<genexpr>r1   )rE   Úteer1   rD   r2   Ú<lambda>F   s    z'partitionn2.<locals>.<genexpr>.<lambda>Nr1   )rA   ÚxÚtr1   r1   r2   rF   F   s   € ÿzpartitionn2.<locals>.<genexpr>c                 3   s   � | ]	}ˆ |ƒ|fV  qd S r$   r1   )rA   rC   ©Ú	predicater1   r2   rF   G   s   € ÿ)Ú	enumerateÚ	itertoolsrG   ©r(   rL   Únr1   rK   r2   Úpartitionn2E   s   ÿÿÿrQ   c                    s>   |   ¡ ‰ tˆ ‡ fdd„dd�}dd„ |D ƒ}|\\}}||fS )z{Unpack a partition into a tuple of (first partition, second partition)

    param: partition iterator from partitionn2
    c                    s   ˆ   | ¡dkS )Nr   )Úindex)rI   ©r(   r1   r2   rH   U   s    z#partition_to_pair.<locals>.<lambda>r@   rO   c                 S   ó   g | ]}t |ƒ‘qS r1   )r&   )rA   Úpr1   r1   r2   Ú
<listcomp>W   ó    z%partition_to_pair.<locals>.<listcomp>)ÚsplitrQ   )ÚinputÚ
partitionsÚdatar/   r0   r1   rS   r2   Úpartition_to_pairN   s   
þ
r\   c                 C   s   dd„ |   d¡D ƒS )Nc                 S   s&   g | ]}t |ƒr| d ¡d  ¡ ‘qS )ú;r   )ÚlenrX   Ústrip)rA   rI   r1   r1   r2   rV   ^   s    ÿz3disentagle_data_from_whitespace.<locals>.<listcomp>Ú
)rX   )r[   r1   r1   r2   Údisentagle_data_from_whitespace\   s   ra   c                 C   sz   i }i }t | ƒD ]+}t|ƒdkrqt|ƒ\}}|dkr'| |d |d i¡ qd |¡}| ||i¡ q| d|i¡ |S )zuTransform lease string into dictionary of attributes

    params: lease: string if a dhcp lease structure { to }
    r   Úoptioné   ú Úoptions)ra   r^   r\   ÚupdateÚjoin)ÚleaseÚ
lease_dictre   Úliner/   r0   r1   r1   r2   Údictify_leaseb   s   
rk   c                 C   s   dd„ t  d|  dd¡¡D ƒS )zˆParses dhclient leases file data, returning dictionary of leases

    :param leasesdata: string of lease data read from leases file
    c                 S   rT   r1   )rk   )rA   rh   r1   r1   r2   rV   }   rW   z.parse_dhclient_leases_file.<locals>.<listcomp>z
{([^{}]*)}ú"Ú )ÚreÚfindallr5   )Ú	leasedatar1   r1   r2   Úparse_dhclient_leases_filex   s   ÿrq   c                 C   sN   i }|   d¡D ]}| d¡st|ƒdk rq|  d¡}|d ||d  ¡ < q|S )z6Parses systemd/networkd/netif lease data, returns dictr`   ú#rc   ú=r   )rX   Ú
startswithr^   Úlower)rp   rh   rj   Úkeyvaluer1   r1   r2   Úparse_networkd_lease_file�   s   
rw   c                  C   s*   ddg} | D ]}t j |¡r|  S qd S )Nz/var/lib/dhcpz/var/lib/dhclient)ÚosÚpathÚexists)Úsupported_dirsÚdr1   r1   r2   Úget_dhclient_dŒ   s   ÿr}   c              
   C   s   d}|}|  ¡ D �]ï}| ¡ }| d¡rq| d¡}|d }|dkr,tj ||d ¡}q|dkrktj ||d ¡}t |¡D ]+}	t|	dƒ�}
|
 	¡  ¡ }W d  ƒ n1 sVw   Y  t
| |tj tj |	¡¡ƒ q>q|d	kr‡|dd… D ]}|| vri | |< d
| | d	< quq|dkr¶|dd… \}}}|| vr�i | |< n
d| | v r§tdƒ‚|| | d< || | d< |}q|dkrÃ|d | | d< q|tv rÐ|d | | |< q|tv rñ|| | vràg | | |< | | |  d |dd… ¡¡ q| d¡�rJd| | v�ri | | d< |dk�r'g | | d d< |dd… D ]}| | d d  |¡ �qq|dk�rIg | | d d< |dd… D ]}| | d d  |¡ �q:q| d¡�rød| | v�r]i | | d< |tv �rs| dd¡}|d | | d |< q|dk�r–g | | d d< |dd… D ]}| | d d  |¡ �q†q|dk�r¯|d  ¡ dk�r¯|d | | d d< q|dk�rÔd | | d v�rÅi | | d d < |d | | d d  |d < q|d!k�rød"| | d v�rêi | | d d"< |d | | d d" |d < q|  ¡ D ]}d	| | v�rd#| | d	< �qýdS )$zËParses the file contents, placing result into ifaces.

    :param ifaces: interface dictionary
    :param contents: contents of interfaces file
    :param path: directory interfaces file was located
    Nrr   rd   r   zsource-directoryrc   r-   r<   ÚautoTÚifaceé   Úfamilyz!Cannot define %s interface again.ÚmethodÚ	hwaddresszdns-Údnsz
dns-searchÚsearchzdns-nameserversÚnameserversÚbridge_Úbridgerm   Úbridge_portsÚportsÚ	bridge_hwÚmacr@   Úbridge_pathcostÚpathcostÚbridge_portprioÚportprioF)Ú
splitlinesr_   rt   rX   rx   ry   rg   ÚglobÚopenÚreadÚparse_etc_network_interfacesÚdirnameÚabspathÚ	ExceptionÚNET_CONFIG_OPTIONSÚNET_CONFIG_COMMANDSÚappendÚNET_CONFIG_BRIDGE_OPTIONSr5   ru   Úkeys)ÚifacesÚcontentsry   ÚcurrifÚsrc_dirrj   rX   rb   Úsrc_pathÚsrc_fileÚfpÚsrc_datar   r�   r‚   Údomainr   Úbridge_optionr1   r1   r2   r•   •   s¨   

ÿþýý
"
ÿ
€

ÿ

€€þr•   c                 C   sf   t j dt j | ¡¡}t j |d¡}t|ƒ�}t| ¡  ¡ ƒt }W d  ƒ |S 1 s,w   Y  |S )zA /sys/class/block/<device>/size and return integer value in bytesú/sys/class/blockÚsizeN)	rx   ry   rg   Úbasenamer“   Úintr”   r_   ÚSECTOR_SIZE_BYTES)r;   Ú
device_dirÚblockdev_sizer|   r©   r1   r1   r2   Úread_sys_block_size_bytesè   s   

ÿýr¯   c                 C   s*   t j dt j | ¡¡}t  t j |d¡¡S )Nr¨   Úslaves)rx   ry   rg   rª   Úlistdir)r;   r­   r1   r1   r2   Úread_sys_block_slavesò   s   r²   c                 c   s(   � | j dd�D ]
}d|vrq|V  qd S )NÚblock)Ú	subsystemÚMAJOR)Úlist_devices)Úcontextr;   r1   r1   r2   Úsane_block_devices÷   s   €ür¸   )Úcopyr   r’   rN   rx   rn   Úpyudevr™   rš   rœ   r¬   r*   Ú__version_info__r>   r«   rQ   r\   ra   rk   rq   rw   r}   r•   r¯   r²   r¸   r1   r1   r1   r2   Ú<module>   s2    

				S
