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 )zParses 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ri | |< n
d| | v rtd|| | d< || | d< |}q|dkr|d | | d< q|tv r|d | | |< q|tv r|| | vrg | | |< | | | 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rd| | vr]i | | d< |tv rs|dd}|d | | d |< q|dkrg | | d d< |dd D ]}| | d d | qq|dkr|d  dkr|d | | d d< q|dkrd | | d vri | | d d < |d | | d d  |d < q|d!krd"| | d vri | | d d"< |d | | d d" |d < q|  D ]}d	| | vrd#| | d	< qdS )$zParses 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
