
    Vh=                         d dl mZmZmZ eZdZdZd dlZd dl	Z	d dl
mZ  G d d      Z G d d	e      Zd
 Zedk(  r e        yy)    )absolute_importdivisionprint_functiona  
module: alternatives
short_description: Manages alternative programs for common commands
description:
  - Manages symbolic links using the C(update-alternatives) tool.
  - Useful when multiple programs are installed but provide similar functionality (for example, different editors).
author:
  - Marius Rieder (@jiuka)
  - David Wittman (@DavidWittman)
  - Gabe Mulley (@mulby)
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: full
  diff_mode:
    support: full
options:
  name:
    description:
      - The generic name of the link.
    type: str
    required: true
  path:
    description:
      - The path to the real executable that the link should point to.
    type: path
  family:
    description:
      - The family groups similar alternatives. This option is available only on RHEL-based distributions.
    type: str
    version_added: 10.1.0
  link:
    description:
      - The path to the symbolic link that should point to the real executable.
      - This option is always required on RHEL-based distributions. On Debian-based distributions this option is required
        when the alternative O(name) is unknown to the system.
    type: path
  priority:
    description:
      - The priority of the alternative. If no priority is given for creation V(50) is used as a fallback.
    type: int
  state:
    description:
      - V(present) - install the alternative (if not already installed), but do not set it as the currently selected alternative
        for the group.
      - V(selected) - install the alternative (if not already installed), and set it as the currently selected alternative
        for the group.
      - V(auto) - install the alternative (if not already installed), and set the group to auto mode. Added in community.general
        5.1.0.
      - V(absent) - removes the alternative. Added in community.general 5.1.0.
    choices: [present, selected, auto, absent]
    default: selected
    type: str
    version_added: 4.8.0
  subcommands:
    description:
      - A list of subcommands.
      - Each subcommand needs a name, a link and a path parameter.
      - Subcommands are also named C(slaves) or C(followers), depending on the version of C(alternatives).
    type: list
    elements: dict
    aliases: ['slaves']
    suboptions:
      name:
        description:
          - The generic name of the subcommand.
        type: str
        required: true
      path:
        description:
          - The path to the real executable that the subcommand should point to.
        type: path
        required: true
      link:
        description:
          - The path to the symbolic link that should point to the real subcommand executable.
        type: path
        required: true
    version_added: 5.1.0
requirements: [update-alternatives]
a  
- name: Correct java version selected
  community.general.alternatives:
    name: java
    path: /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java

- name: Select java-11-openjdk.x86_64 family
  community.general.alternatives:
    name: java
    family: java-11-openjdk.x86_64
  when: ansible_os_family == 'RedHat'

- name: Alternatives link created
  community.general.alternatives:
    name: hadoop-conf
    link: /etc/hadoop/conf
    path: /etc/hadoop/conf.ansible

- name: Make java 32 bit an alternative with low priority
  community.general.alternatives:
    name: java
    path: /usr/lib/jvm/java-7-openjdk-i386/jre/bin/java
    priority: -10

- name: Install Python 3.5 but do not select it
  community.general.alternatives:
    name: python
    path: /usr/bin/python3.5
    link: /usr/bin/python
    state: present

- name: Install Python 3.5 and reset selection to auto
  community.general.alternatives:
    name: python
    path: /usr/bin/python3.5
    link: /usr/bin/python
    state: auto

- name: keytool is a subcommand of java
  community.general.alternatives:
    name: java
    link: /usr/bin/java
    path: /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java
    subcommands:
      - name: keytool
        link: /usr/bin/keytool
        path: /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/keytool
N)AnsibleModulec                   ,    e Zd ZdZdZdZdZed        Zy)AlternativeStatepresentselectedabsentautoc                 ^    | j                   | j                  | j                  | j                  gS N)PRESENTSELECTEDABSENTAUTO)clss    r/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/alternatives.pyto_listzAlternativeState.to_list   s!    S\\3::sxx@@    N)	__name__
__module____qualname__r   r   r   r   classmethodr    r   r   r   r      s*    GHFDA Ar   r   c                       e Zd ZdZd Zed        Zed        Zed        Zd Z	d Z
d Zd	 Zd
 Zed        Zed        Zed        Zed        Zed        Zed        Zed        Zd Zy)AlternativesModuleNc                     || _         t        dt        t               t                           | _        ddi| j                   _        g | _        | j                          y )NF)beforeafter)changeddiffLC_ALLC)moduledictresultrun_command_environ_updatemessagesrun)selfr%   s     r   __init__zAlternativesModule.__init__   sB    5t46/PQ2:C.
r   c                     | j                   j                  j                  d      t        j                  t        j
                  t        j                  fv S Nstate)r%   paramsgetr   r   r   r   r+   s    r   mode_presentzAlternativesModule.mode_present   s>    {{!!%%g.3C3K3KM]MfMfhxh}h}2~~~r   c                 n    | j                   j                  j                  d      t        j                  k(  S r.   )r%   r0   r1   r   r   r2   s    r   mode_selectedz AlternativesModule.mode_selected   s)    {{!!%%g.2B2K2KKKr   c                 n    | j                   j                  j                  d      t        j                  k(  S r.   )r%   r0   r1   r   r   r2   s    r   	mode_autozAlternativesModule.mode_auto   s)    {{!!%%g.2B2G2GGGr   c                      j                           j                  r j                  j                  d    j                  j                  d   } j                   j                   j
                  vs}|+ j
                   j                     j                  d      |k7  sP^t        fd j
                   j                     j                  d      D              rt         fdD              s j                           j                  d uxr  j                   j                  k(  }d} j                  O j                   j
                  v r7 j
                   j                     }|j                  d       j                  k(  } j                  r|s|s j                           j                  rH j                  dk(  r9 j                          n( j                   j
                  v r j!                          dj#                   j$                         j&                  d	<     j                  j(                  d
i  j&                   y )Nsubcommandspriorityc              3   &   K   | ]  }|v  
 y wr   r   ).0ssubcommands_parameters     r   	<genexpr>z)AlternativesModule.run.<locals>.<genexpr>   s     |q%: :|s   c              3   r   K   | ].  }|j                   j                     j                  d       v  0 yw)r9   N)current_alternativespathr1   )r<   r=   r+   s     r   r?   z)AlternativesModule.run.<locals>.<genexpr>   s1     |abT%>%>tyy%I%M%Mm%\ \|s   47Ffamilymanual msgr   )parser3   r%   r0   rB   rA   r1   allinstallcurrent_pathrC   r5   setr7   current_moder   removejoinr)   r'   	exit_json)r+   priority_parameteris_same_pathis_same_familycurrent_alternativer>   s   `    @r   r*   zAlternativesModule.run   s   

$(KK$6$6}$E!!%!3!3J!?		%IIT%>%>>'38Q8QRVR[R[8\8`8`ak8l  qC  9C*6|DD]D]^b^g^gDhDlDlmzD{|||f{||   99D0ST5F5F$))5SL"N  ,1B1BdF_F_1_&*&?&?@Q@Q&R#!4!8!8!Bdkk!Q!!<>
 ~~$"3"3x"?		 yyD555 XXdmm4E,,r   c                    t         j                  j                  | j                        s)| j                  j	                  d| j                  z         | j
                  s| j                  j	                  d       | j                  d| j
                  | j                  | j                  t        | j                        g}| j                  |j                  d| j                  g       | j                  j                  d   B| j                  D cg c]  }d|d   |d	   |d
   g }}||D cg c]  }|D ]  }|  c}}z  }d| j                  d<   | j                  j!                  d| j                  d| j                  d       | j                  j"                  s| j                  j%                  |d       | j                  j&                  rt)        t*        j,                  | j                  | j                  | j                  | j
                        | j                  d   d<   | j                  r6| j                  d   d   j/                  t)        | j                               y y y c c}w c c}}w )Nz Specified path %s does not exist)rF   zQNeeded to install the alternative, but unable to do so as we are missing the linkz	--installz--familyr9   z--slavelinknamerB   Tr!   zInstall alternative '' for ''.check_rc)r/   rB   rC   r:   rU   r"   r    r9   )osrB   existsr%   	fail_jsonrU   UPDATE_ALTERNATIVESrV   strr:   rC   extendr0   r9   r'   r)   append
check_moderun_command_diffr&   r   r   update)r+   cmdsubcmdr9   sublistitems         r   rI   zAlternativesModule.install   s   ww~~dii(KK!!&H499&T!UyyKK!!&y!z''diiDIIWZ[_[h[hWij;;"JJ
DKK01;;m,8eieueuv[aIvf~vf~vf~VvKvHWHDHDHHC!%ITYYPTPYPYZ[{{%%KK##C$#7;;+/&..YY{{YY,DKK( F#G,33D $ 0 05     wHs   I*,I/c                    | j                   d| j                  | j                  g}d| j                  d<   | j                  j                  d| j                  d| j                  d       | j                  j                  s| j                  j                  |d       | j                  j                  r+t        t        j                        | j                  d	   d
<   y y )Nz--removeTr!   zRemove alternative 'z' from 'rX   rY   r/   r"   r    )r_   rV   rB   r'   r)   rb   r%   rc   rd   re   r&   r   r   r+   rg   s     r   rM   zAlternativesModule.remove  s    ''TYY		J!%ITYYPTPYPYZ[{{%%KK##C$#7;;+/6F6M6M+NDKK( r   c                    | j                   | j                  }n| j                   }| j                  d| j                  |g}d| j                  d<   | j
                  j                  d|d| j                  d       | j                  j                  s| j                  j                  |d       | j                  j                  r$t        j                  | j                  d   d	   d
<   y y )Nz--setTr!   zSet alternative 'rW   rX   rY   r"   r    r/   )rB   rC   r_   rV   r'   r)   rb   r%   rc   rd   re   r   r   )r+   argrg   s      r   rK   zAlternativesModule.set  s    99++C))C''$))SA!%IdiiPQ{{%%KK##C$#7;;4D4M4MDKK(1 r   c                 ~   | j                   d| j                  g}| j                  j                  d| j                  z         d| j                  d<   | j
                  j                  s| j
                  j                  |d       | j
                  j                  r$t        j                  | j                  d   d   d<   y y )	Nz--autoz!Set alternative to auto for '%s'.Tr!   rY   r"   r    r/   )r_   rV   r)   rb   r'   r%   rc   rd   re   r   r   rm   s     r   r   zAlternativesModule.auto  s    ''499=@DIINO!%I{{%%KK##C$#7;;4D4L4LDKK(1 r   c                 L    | j                   j                  j                  d      S )NrV   r%   r0   r1   r2   s    r   rV   zAlternativesModule.name)      {{!!%%f--r   c                 L    | j                   j                  j                  d      S )NrB   rr   r2   s    r   rB   zAlternativesModule.path-  rs   r   c                 L    | j                   j                  j                  d      S )NrC   rr   r2   s    r   rC   zAlternativesModule.family1  s    {{!!%%h//r   c                 h    | j                   j                  j                  d      xs | j                  S )NrU   )r%   r0   r1   current_linkr2   s    r   rU   zAlternativesModule.link5  s(    {{!!%%f-B1B1BBr   c                    | j                   j                  j                  d      %| j                   j                  j                  d      S | j                  j                  | j                  i       j                  dd      S )Nr:   2   )r%   r0   r1   rA   rB   r2   s    r   r:   zAlternativesModule.priority9  sb    ;;!!*-9;;%%))*55((,,TYY;??
BOOr   c                 h   | j                   j                  j                  d      %| j                   j                  j                  d      S | j                  | j                  v rP| j                  | j                     j                  d      r(| j                  | j                     j                  d      S y )Nr9   )r%   r0   r1   rB   rA   r2   s    r   r9   zAlternativesModule.subcommands?  s    ;;!!-0<;;%%))-88YY$3338Q8QRVR[R[8\8`8`an8o,,TYY7;;MJJr   c                 t    | j                   !| j                  j                  dd      | _         | j                   S )Nzupdate-alternativesT)_UPDATE_ALTERNATIVESr%   get_bin_pathr2   s    r   r_   z&AlternativesModule.UPDATE_ALTERNATIVESG  s5    $$,(,(@(@AVX\(]D%(((r   c                 z	   d | _         d | _        d | _        i | _        | j                  j                  | j                  d| j                  g      \  }}}|dk7  r,| j                  j                  d| j                  d|       y t        j                  dt        j                        }t        j                  dt        j                        }t        j                  dt        j                        }t        j                  dt        j                        }t        j                  d	t        j                        }t        j                  d
t        j                        }	|j                  |      }
|
s| j                  j                  d       y |
j                  d      | _         |j                  |      }
|
s| j                  j                  d       n|
j                  d      | _        |j                  |      }
|
s| j                  j                  d       n|
j                  d      | _        t        |j                  |            }|s,| j                   r | j                   D ci c]  }|d   |d    }}|j                  |      D ]m  \  }}}}t        t#        |      ||	j                  |      D cg c]'  \  }}|dk7  rt        |||j%                  |            ) c}}      | j                  |<   o | j                  j&                  r| j(                  | j                  v rS| j*                  d   d   j-                  t        t.        j0                  | j(                  | j                  | j(                     j%                  d      | j                               | j                  | j(                     j%                  d      rQ| j*                  d   d   j-                  t        | j                  | j(                     j%                  d                   | j                   dk(  rT| j                  | j(                  k7  r:| j*                  d   d   j-                  t        t.        j2                               y y y | j*                  d   d   j-                  t        t.        j4                               y y c c}w c c}}w )Nz	--displayr   zNo current alternative found. 'z' exited with z(\s-\s(?:status\sis\s)?(\w*)(?:\smode|.)$z"^\s*link currently points to (.*)$z^\s*link \w+ is (.*)$z%^\s*(?:slave|follower) (\S+) is (.*)$zM^(\/.*)\s-\s(?:family\s(\S+)\s)?priority\s(\d+)((?:\s+(?:slave|follower).*)*)z"^\s+(?:slave|follower) (.*): (.*)$zNo current mode found in output   zNo current path found in outputzNo current link found in outputrV   rU   z(null)rV   rB   rU   )r:   rC   r9   r"   r   r:   )r/   rB   r:   rU   r9   r[   rD   rl   )rL   rJ   rw   rA   r%   rd   r_   rV   debugrecompile	MULTILINEsearchgroupr&   findallr9   intr1   re   rB   r'   rf   r   r   r   r   )r+   rcdisplay_outputdummycurrent_mode_regexcurrent_path_regexcurrent_link_regexsubcmd_path_link_regexalternative_regexsubcmd_regexmatchsubcmd_path_mapr=   rB   rC   priorh   rV   spaths                      r   rG   zAlternativesModule.parseM  s      $&! '+kk&=&=%%{DII>'
#^U 7KKUYUmUmoqrsZZ(SUWUaUabZZ(Mr||\ZZ(@",,O!#,TVXVbVb!cJJ'wy{  zF  zF  Gzz"GV")).9KK?@!KKN")).9KK?@ %AD")).9KK?@ %AD5==nMN4#3#3=A=M=MNqy!F)3NON*;*C*CN*S 		&D&$.2T
 &2%9%9&%A	X "dEUhEV	 "(,,T2 X/D%%d+		 ;;yyD555F#H-44T*22!66tyyAEEjQ**	6  ,,TYY7;;MJKK'188$($=$=dii$H$L$L]$[:  $$0T5F5F$))5SKK'188.77:  6T0
 F#H-44T*116 #  OXs   !R20,R7)r   r   r   r|   r,   propertyr3   r5   r7   r*   rI   rM   rK   r   rV   rB   rC   rU   r:   r9   r_   rG   r   r   r   r   r      s      L L H H&-PB	ON"	M . . . . 0 0 C C P P
   ) )
Kr   r   c                     t        t        t        dd      t        d      t        d      t        d      t        d      t        dt        j                         t        j                        t        dd	d
gt        t        dd      t        dd      t        dd                        ddg      } t        |        y )Nr`   T)typerequiredrB   )r   r   )r   choicesdefaultlistr&   slavesr   )r   elementsaliasesoptions)rV   rB   rC   rU   r:   r/   r9   )rB   rC   )argument_specsupports_check_moderequired_one_of)r   r&   r   r   r   r   )r%   s    r   mainr     s    5406"U#6"u%(002(11
 &6H:W[ut4v5v5X 
" !+,'F, vr   __main__)
__future__r   r   r   r   __metaclass__DOCUMENTATIONEXAMPLESr\   r   ansible.module_utils.basicr   r   objectr   r   r   r   r   r   <module>r      sc    A @Qf/b 
 	 4A Au up6 zF r   