o edc@sdZddlZddlZddlZddlZddlmZddlmmZddddddd Z Gd d d ej Z Gd d d ej Z dS)znetplan ip command lineN)CalledProcessErrorz#run/systemd/netif/leases/{lease_id}ifindex)patternmethodzrzNetplanIpLeases.__init__cCs<|jjddd|jjddd|j|_||dS)N interfacez1Interface for which to display IP lease settings.)r --root-dirzBSearch for configuration files in this root directory instead of /)parser add_argumentcommand_ip_leasesrr&r'rrrrr%Cs zNetplanIpLeases.runc sjdkr fdd}tg}jr|djg7}|djg7}td|z tj|dd}Wnt yLt d jt j d t d Ynwi}|d }|D]}|d \}}|||<qV||dS)Nrc s(fdd}fdd}t|dd}t|dd}zQtd|}tjd jr2tjjnd |jj|d }tj |sH| d d }t |}| D]}t |qQWdWdS1sfwYWdSty} zt djt| ftjdtdWYd} ~ dSd} ~ ww)Nc stjdjd}z t|}|dWdWS1s"wYWdSty?}z t d|t |d}~ww)Nz/sys/class/netrrzCannot read file %s: %s) ospathjoinr-open readlinesstrip Exceptionloggingdebugstr) ifindex_fferrrlease_method_ifindexTs (zXNetplanIpLeases.command_ip_leases..find_lease_file..lease_method_ifindexc szHtddjg}|D]5}d|vrC|dd}tddd|g}|D]}d|vrB|ddWSq,qWtd ty\}ztd t|d}~ww) NdevshowzGENERAL.CONNECTION:conidzconnection.uuidz@Could not find a NetworkManager connection for the interface: %szrrrlease_method_nm_connection]s"    z^NetplanIpLeases.command_ip_leases..find_lease_file..lease_method_nm_connectionbackendrr lease_method_/)r-lease_idzNetworkManager/internal-zNetworkManager/dhclient-z%No lease found for interface '%s': %sfilerC) lease_pathevalr2r3r4root_dirabspathformatr-isfilereplacer5r6printrJr8r;sysstderrexit) mappingr?rO lease_pattern lease_methodrTr3r=rLr>rrrfind_lease_fileSs4      &z:NetplanIpLeases.command_ip_leases..find_lease_filer.z --mappingzcommand ip leases: running %sT)textz:No lease found for interface '%s' (not managed by Netplan)rUrC,=)r- print_usagerFget_generator_pathrYr9r: subprocess check_outputrr^r_r`rarIr7) rreargvoutrb mapping_skeyvaluekeyvaluerrrr1Ns(   4     z!NetplanIpLeases.command_ip_leases)r)r*r+rr%r1r,rrrrr <s  r ) __doc__r9r2r_rkrnetplan.cli.utilsclirFrWNetplanCommandr r rrrrs