
    /I_H                         S SK r S SKrS SKrS SKrS SKJr   S SK7  S SKJr  S SK	J
s  Jr  S SKJ
s  Jr  S SKJ
s  Jr   " S S\5      r " S S\5      rg! \\4 a     S SK7  S SKJr  S SKJr  S SKJr  S SKJr   N>f = f)	    N)reduce)*)utilsc                       \ rS rSrSrg)NotSupported    N)__name__
__module____qualname____firstlineno____static_attributes__r	       1/usr/share/ifupdown2/ifupdownaddons/modulebase.pyr   r      s    r   r   c                       \ rS rSrSrS rS rS%S jrS&S jrS r	S	 r
S%S
 jrS rS rS%S jrS rS rS rS rS rS rS r S'S jr S%S jrS rS rS rS rS rS rS rS rS r S(S  jr!S! r"S" r#S# r$S$r%g))
moduleBase    zdBase class for ifupdown addon modules

Provides common infrastructure methods for all addon modules c                    U R                   R                  U l        [        R                  " SU R                  -   5      U l        [        R                  R                  SSS9U l	        [        R                  " S5      [        R                  " S5      [        R                  " S5      /U l        S U l        U R                  5         g )Nz	ifupdown.vrfzvrf-exec-cmd-prefix)attrzD([A-Za-z0-9\-]+)\[(\d+)\-(\d+)\]([A-Za-z0-9\-]+)\[(\d+)\-(\d+)\](.*)z(([A-Za-z0-9\-]+[A-Za-z])(\d+)\-(\d+)(.*)z$([A-Za-z0-9\-]+)\[(\d+)\-(\d+)\](.*))	__class__r
   
modulenamelogging	getLoggerloggerpolicymanagerpolicymanager_apiget_module_globalsvrf_exec_cmd_prefixrecompileglob_regexs_bridge_stp_user_spacemerge_modinfo_with_policy_files)selfargskargss      r   __init__moduleBase.__init__%   s    ..11''doo(EF
 $1#B#B#U#UV[bw#U#x  JJ'noJJ'RSJJ'NOQ '+#,,.r   c                    [        U R                  5       R                  S0 5      5      nU(       d  gSn[        [        R
                  R                  U R                  5      R                  5       5       HC  u  p4X1;  a2  U R                  R                  U R                  < SU< SU< 35        M<  XAU   S'   ME     [        R
                  R                  U R                  S5      nU(       a  UR                  S0 5      n[        5       n[        UR                  5       5       H@  u  pX;  a2  U R                  R                  U R                  < SU< SU< 35        M<  XU'   MB     UR                  U5        U$ )a%  
update addons modinfo dictionary with system/user defined values in policy files
Any value can be updated except the module help "mhelp"

We also check if the policy attributes really exist to make sure someone is not
trying to "inject" new attributes to prevent breakages and security issue
attrsNz/this attribute doesn't exist or isn't supported: default_modinfo)dictget_modinfogetlistr   r   get_module_defaultsr   itemsr   warningr   update)
r%   r+   	error_msgkeyvaluepolicy_modinfopolicy_attrsupdate_attrs	attr_nameattr_descriptions
             r   r$   *moduleBase.merge_modinfo_with_policy_files7   s+    T%%'++GR89G	 }>>RRSWSbSbciiklJC<##DOOS)$TU$)#Jy!	 m '88KKDOO]gh)--gr:L6L/3L4F4F4H/I+	)KK''T](^_.>+	 0J LL&r   Nc                    U R                  U5      (       d  [        R                  R                  (       d  U R                  R                  5       [        R                  :X  a*  [        R                  " 5         [        R                  " 5         U R                  R                  U5        U(       a  UR                  [        R                  5        g)z>log a warning if err str is not one of which we should ignore N)ignore_errorifupdownflagsflagsIGNORE_ERRORSr   getEffectiveLevelr   DEBUG	tracebackprint_stack	print_excr5   
set_statusifaceStatusWARNING)r%   strifaceobjs      r   log_warnmoduleBase.log_warn]   s~      %%m.A.A.O.O{{,,.'--?%%'##%KK$##K$7$78r   c                    U R                  U5      (       d  [        R                  R                  (       d  U R                  R                  5       [        R                  :X  a*  [        R                  " 5         [        R                  " 5         U(       a1  U(       a  UR                  [        R                  5        [        U5      eU R                  R                  U5        gg)zRlog an err if err str is not one of which we should ignore and raise an exception N)rA   rB   rC   rD   r   rE   r   rF   rG   rH   rI   rJ   rK   ERROR	Exceptionerror)r%   rM   rN   raise_errors       r   	log_errormoduleBase.log_errorh   s      %%m.A.A.O.O{{,,.'--?%%'##%''(9(9:n$!!#&r   c                 |     [         R                  " [         R                  < SU< 35        g! [         a     gf = f)Nz -x TF)r   exec_command	pidof_cmdrS   )r%   procNames     r   is_process_runningmoduleBase.is_process_runningw   s<    	 % ; <
   		s   +. 
;;c                    / n[        S5       n UR                  5       nUSS   H4  nUR                  UR                  5       S   R	                  S5      5        M6      S S S 5        U$ ! [
         a    e f = f! , (       d  f       U$ = f)Nz/proc/net/dev   r   r,   )open	readlinesappendsplitstriprS   )r%   
ifacenamesflineslines        r   get_ifaces_from_procmoduleBase.get_ifaces_from_proc   s    
/"a!!"ID%%djjl1o&;&;D&AB & #    #" s   A7AA((A44A77
Bc              #   .  #     U R                  5       nW H)  n [        R
                  " US-   U5      (       a  Uv   M)  M+     U(       d  g U H)  n [        R
                  " US-   U5      (       a  Uv   M)  M+     g ! [         a!    U R                  R                  SU-  5         Nf = f! [         a)  n[        U< SU< SU< S[        U5      < S35      eS nAff = f! [         a)  n[        U< SU< SU< S[        U5      < S35      eS nAff = f7f)Nz"%s: error reading ifaces from proc$z: error searching regex 'z' in z ())ri   rS   r   r5   r    searchrM   )r%   	ifacenameexprre   proc_ifacenamesproc_ifacenamees          r   parse_regexmoduleBase.parse_regex   s    	Q"779O .NL99TCZ88(( 9 . #IG99TCZ33#O 4 $  	QKK Di OP	Q  L"+T>3q6!K L LL  G"+T9c!f!F G GGsm   DA; D#B)D#C4D;(B&#D%B&&D)
C3$CCD
D)$DDDc                     U R                   S   R                  U5      (       dF  U R                   S   R                  U5      (       d#  U R                   S   R                  U5      (       a  gg)z;
Used by iface where ifname could be swp7 or swp[1-10].300
r      r_   TF)r"   match)r%   ifnames     r   ifname_is_globmoduleBase.ifname_is_glob   s^     Q%%f--Q%%f--Q%%f--r   c              #     #    SU-  S-   S-   nSU;   a'  U R                   R                  U< SU< 35        Uv   g U R                  nUS   R                  U5      (       a  US   R                  U5      nUR	                  5       n[        U5      S:  a  [        U< SU< S	U< 35      eUS   nUS
   n[        US   5      n	[        US   5      n
US   n[        US   5      n[        US   5      n[        XS-   5       H'  n[        XS-   5       H  nUSXU4-  -   U-   v   M     M)     g US   R                  U5      (       d  US   R                  U5      (       a  Sn	Sn
US   R                  U5      (       a  US   R                  U5      nOUS   R                  U5      nUR	                  5       n[        U5      S:w  a  [        SU-  U-   S-   5      eUS   nUS   n[        US   5      n	[        US   5      n
[        XS-   5       H  nUSU-  -   U-   v   M     g U R                   R                  U< SU< 35        Uv   g 7f)Nz"error parsing glob expression '%s'z5 (supported glob syntax: swp1-10.300 or swp[1-10].300z  or swp[1-10]sub[0-4].300,z#: comma are not supported in glob: r      z#: error: unhandled glob expression 
   rw   r_            z%d%s%dz%s: z(unexpected len)z%dr,   )	r   r5   r"   rx   groupslenrS   intrange)r%   ro   rp   errmsgregexsmmlistprefixsuffixstart_index	end_index
sub_string	start_subend_subijs                   r   
parse_globmoduleBase.parse_glob   sA    8$>KL01 $;KKYX^ _`J!!!9??4   q	%AHHJE5zA~QZ\`ag hii1XF1XFeAh-KE!HIqJE!HI%(mG;A6yA+6A 8qA.>#>>GG 7 7 AY__T""fQiood&;&; KIayt$$1IOOD)1IOOD)HHJE5zQ	 1F :=O OPP1XF1XFeAh-KE!HI;A6tQw&// 7
 KK9f =>Js   IIc                    SnSn/ nU(       d  g[         R                  " SU5      nU R                  R                  U< S[	        U5      < S35        U H  nUS:X  a  SnM  US:X  a  SnM  US:X  a  S	nM!  US
:X  a  S	nM+  U(       a5  U R                  XU5       H  n	X;  d  M
  UR                  U	5        M     SnMg  U(       a-  U R                  X5       H  n	UR                  U	5        M     SnM  UR                  U5        M     U(       d  gU$ )zparse port list containing glob and regex

Args:
    port_expr (str): expression
    ifacenames (list): list of interface names. This needs to be specified if the expression has a regular expression
r   Nz	[\s\t]\s*z: evaluating port expr ''noregexnoglobregexrw   glob)r    rc   r   debugrM   rt   rb   r   )
r%   ro   	port_exprre   r   r   portlistexprsrp   ports
             r   parse_port_listmoduleBase.parse_port_list   s     y1$c%j2 	3Dy ! ,,YjID+ - J  OOI<DOOD) =%' ( r   c                     [         R                  R                  (       d<  [        R                  " SU[        R
                  [        R                  -  5      (       a  gg)NexistsTF)rB   rC   FORCEr    rn   
IGNORECASE	MULTILINE)r%   r   s     r   rA   moduleBase.ignore_error  s;    %%9fMMBLL(** **r   c                     U R                   R                  SU-  SU-  -   5        [        R                  R                  (       a  g[        US5       nUR                  U5        SSS5        g! , (       d  f       g= f! [         a:  nU R                   R                  SU-  S-   [        U5      -   S-   5         SnAg	SnAff = f)
zwrites string to a file zwriting '%s'z to file %sr   wNzerror writing to file %s(rm   )
r   inforB   rC   DRYRUNr`   writeIOErrorr5   rM   )r%   filenamestrexprrf   rs   s        r   
write_filemoduleBase.write_file  s    
	KK-w6x'( )""))h$  %  %$ 	  	KK :!! "%a&!)+.!/ 0	s<   AA> A> A-$A> -
A;7A> ;A> >
C0B==Cc                      U R                   R                  SU-  5        [        US5       nUR                  5       sSSS5        $ ! , (       d  f       g= f! [         a     gf = f)z)read file and return lines from the file reading '%s'rN)r   r   r`   ra   rS   r%   r   rf   s      r   	read_filemoduleBase.read_file!  sX    	KK-x78h${{} %$$   		s-   *A A	A 
AA A 
A$#A$c                      U R                   R                  SU-  5        [        US5       nUR                  5       R	                  S5      sSSS5        $ ! , (       d  f       g= f! [
         a     gf = f)z+reads and returns first line from the file r   r   r   N)r   r   r`   readlinerd   rS   r   s      r   read_file_onelinemoduleBase.read_file_oneline+  sc    	KK-x78h$zz|))$/ %$$   		s.   *A& A	A& 
A#A& #A& &
A32A3c                 b    [         R                  " [         R                  < SU< SU< 35        g)z0set sysctl variable to value passed as argument  =N)r   rY   
sysctl_cmd)r%   variabler9   s      r   
sysctl_setmoduleBase.sysctl_set5  s#    !,,h? 	@r   c                     [         R                  " [         R                  < SU< 35      nUR                  S5      n[	        U5      S:  a  US   R                  5       $ g)zget value of sysctl variable r   r   rw   N)r   rY   r   rc   r   rd   )r%   r   outputrc   s       r   
sysctl_getmoduleBase.sysctl_get:  sQ    ##%*%5%5x%A BS!u:>8>>##r   c                     U R                   (       a  U R                   $  U R                  S5      U l         U R                   $ ! [         a    SU l          U R                   $ f = f)Nz net.bridge.bridge-stp-user-spacer   )r#   r   rS   r%   s    r   $systcl_get_net_bridge_stp_user_space/moduleBase.systcl_get_net_bridge_stp_user_spaceC  sa    &&...	,*.//:\*]D' ***  	,*+D'***	,s   A A A c                     UR                   nUR                  U5      nU(       a(  U(       a  U(       a	  U" U5        OU" U5        U" Xg5        g g N)nameget_attr_value_first)r%   rN   r=   attr_valsetfuncprehookprehookargsro   	attrvalues           r   set_iface_attrmoduleBase.set_iface_attrM  sA    MM	11)<	K(I&I1 r   c                     UR                  U5      nU(       d  g U(       a  U" UR                  U5      nOU" UR                  5      nU(       a  Xv:w  a  UR                  UUS5        g UR                  UUS5        g )Nrw   r   )r   r   update_config_with_status)r%   rN   ifaceobjcurrr=   attr_valgetfuncattr_valgetextraargr   runningattrvalues           r    query_n_update_ifaceobjcurr_attr+moduleBase.query_n_update_ifaceobjcurr_attrY  sx     11)<	.x}}-@ B  /x}}= *229 !% 229 !%r   c                 @    U Vs/ s H  o3U;   d  M
  UPM     sn$ s  snf )z!returns a list of differing keys r	   )r%   abxs       r   dict_key_subsetmoduleBase.dict_key_subsetl  s    '1aQ1'''s   	c                 ,    / nU R                   R                  S5      nUR                  5        HQ  u  p4U(       a  UR                  S5      (       a  M$  UR                  U5        SU;   d  M=  UR	                  US   5        MS     U$ ! [
         a     gf = f)zEreturns list of all module attrs defined in the module _modinfo
dict
r+   
deprecatedaliasesN)r.   r1   r4   rb   extendrS   )r%   retattrs	attrsdictattrnameattrvalss        r   get_mod_attrsmoduleBase.get_mod_attrsp  s    	H))'2I&/oo&7"8<<#=#=)(OOHY$78 '8 O 		s   A&B ,B 
BBc                 z     U R                   R                  S0 5      R                  U5      $ ! [         a     gf = f)zreturns module attr info r+   N)r.   r1   rS   r%   r   s     r   get_mod_attrmoduleBase.get_mod_attr  s;    	==$$Wb155h?? 		s   *- 
::c                 X     [        S SX/U R                  5      $ ! [         a     gf = f)z8returns module attrs defined in the module _modinfo dictc                 
    X   $ r   r	   )dks     r   <lambda>,moduleBase.get_mod_subattr.<locals>.<lambda>  s    qtr   r+   N)r   r.   rS   )r%   r   subattrnames      r   get_mod_subattrmoduleBase.get_mod_subattr  s7    	+gx-M( ( 		s    
))c                 @     U R                   $ ! [         a    0 s $ f = f)zreturn module info )r.   rS   r   s    r   r0   moduleBase.get_modinfo  s%    	==  	I	    c                     U R                  5       R                  S0 5      R                  U0 5      R                  S5      $ )Nr+   r-   )r0   r1   r   s     r   get_attr_default_value!moduleBase.get_attr_default_value  s6    !%%gr266xDHHSSr   c                 @     U R                   $ ! [         a    / s $ f = f)z;return the ifupdown scripts replaced by the current module )overrides_ifupdown_scriptsrS   r   s    r   get_overrides_ifupdown_scripts)moduleBase.get_overrides_ifupdown_scripts  s'    	222 	I	r   c                    S=pSn[         R                  R                  U5      (       d  X4$  [        R                  " U5      R                  S5      R                  S5      u  pE[        U5      n[        U5      nX4$ ! [         a8  nU R                  R                  U< S[        U5      < S35         S nAX4$ S nAff = f)Nr   z3/var/lib/ifupdown2/hooks/get_reserved_vlan_range.shr   -z	 failed (rm   )ospathr   r   rY   rd   rc   r   rS   r   r   rM   )r%   startendget_resvvlansrs   s         r   _get_reserved_vlan_range#moduleBase._get_reserved_vlan_range  s    Lww~~l++<	''5;;DAGGLFQFEa&C
 |	  	KKs1vFG|		s   AA= =
B?,B::B?c                     Sn [         R                  " S5      R                  5       nU$ ! [         a4  nU R                  R                  S[        U5      -  5        S n S nAU$ S nAff = f)Nr-   z/usr/sbin/ip vrf idzfailed to get vrf id (%s))r   rY   rd   rS   r   r   rM   )r%   vrfidrs   s      r   _get_vrf_contextmoduleBase._get_vrf_context  sh    	&&'<=CCEE   	KK93q6ABE	s   $* 
A()A##A(c                    SnUnU R                   S   Us=::  a  U R                   S   ::  a  O  OSnOjUS:  ad  U R                   S   Us=::  a  U R                   S   ::  a  O  OSnUnO5XR                   S   :  a#  X0R                   S   :  a  SnU R                   S   nU(       a6  [        R                  " SX%U R                   S   U R                   S   4-  5      eU$ )zQHelper function to check and warn if the vlanid falls in the
reserved vlan range Fr   rw   Tz;%s: reserved vlan %d being used (reserved vlan range %d-%d))_resv_vlan_range
exceptionsReservedVlanException)r%   vlanid	logprefixr  rT   invalid_vlans         r   _handle_reserved_vlan moduleBase._handle_reserved_vlan  s       #vI1F1Fq1IIE1W$$Q'3J$2G2G2JJ"//22s=R=RST=U7U#44Q7223p6?tOdOdefOgimi~i~  @A  jB  6C4C D D r   c                 L    U(       a  [         R                  " SU5      (       a  gg)z&Check if address is 00:00:00:00:00:00 z00:00:00:00:00:00FT)r    rx   )r%   ethaddrs     r   _valid_ethaddrmoduleBase._valid_ethaddr  s    "((#6@@r   c                     SU;   a5  UR                  SS5      n[        U5      nUS:X  a  US   nO)US:X  a  US   nOUR                  S5      (       a  USS  nOg [        U5      nU$ ! [         a     gf = f)N.r_   rw   r   vlanr   r   )rc   r   
startswithr   rS   )r%   ro   vid_strvlenvids        r   _get_vlan_id_from_ifacename&moduleBase._get_vlan_id_from_ifacename  s    )ooc1-Gw<Dqy!!*!!*!!&))mG	g,C 
  		s   A' '
A43A4c                     UR                  S5      n U(       a  [        U5      $  U R                  UR                  5      $ ! [         a     gf = f)zDerives vlanid from iface name

Example:
    Returns 1 for ifname vlan0001 returns 1
    Returns 1 for ifname vlan1
    Returns 1 for ifname eth0.1
    Returns 100 for ifname eth0.1.100
    Returns -1 if vlan id cannot be determined
zvlan-idr   )r   r   rS   r*  r   )r%   rN   r'  s      r   _get_vlan_idmoduleBase._get_vlan_id  sR     //	:	s7|+w //>>  		s   A 
AA)r#   r"   r   r   r   r   )NT)NN) r   )&r
   r   r   r   __doc__r(   r$   rO   rV   r\   ri   rt   rz   r   r   rA   r   r   r   r   r   r   r   r   r   r   r   r   r0   r  r  r  r  r  r!  r*  r-  r   r	   r   r   r   r       s    E/$$L		G.6p&P@
+ 26
2 <@%&("T	,$?r   r   )r
  r    r   rG   	functoolsr   ifupdown2.ifupdown.ifaceifupdown2.ifupdown.utilsr   ifupdown2.ifupdown.exceptionsifupdownr   ifupdown2.ifupdown.policymanagerr    ifupdown2.ifupdown.ifupdownflagsrB   ImportErrorModuleNotFoundErrorifupdown.ifaceifupdown.utilsifupdown.exceptionsifupdown.policymanagerifupdown.ifupdownflagsrS   r   objectr   r	   r   r   <module>r@     ss    
 	   3*.66<<<<	9 	Y? Y? 	() 3 $,223s   &A %A=<A=