
    Vh                         d dl mZ dZd dlZd dlZd dlZd dlZd dlZd dlZd dl	Z	d dl
Zd dlmZ d dlmZmZ d dlmZmZ d dlmZmZmZ d dlmZ d dlmZ d d	lmZ  e       Z G d
 de      Z y)    )annotationsa  
    name: local
    short_description: execute on controller
    description:
        - This connection plugin allows ansible to execute tasks on the Ansible 'controller' instead of on a remote host.
    author: ansible (@core)
    version_added: historical
    extends_documentation_fragment:
        - connection_pipelining
    notes:
        - The remote user is ignored, the user with which the ansible CLI was executed is used instead.
N)AnsibleErrorAnsibleFileNotFound)	text_typebinary_type)to_bytes	to_nativeto_text)ConnectionBase)Display)unfrackpathc                  b     e Zd ZdZdZdZd
 fdZddZdd fdZd fdZ	d fdZ
dd	Z xZS )
Connectionz Local based connections localTc                    t        t        | 
  |i | d | _        	 t	        j
                         | _        y # t        $ r4 t        j                  dt        j                         z         d| _        Y y w xY w)NzPCurrent user (uid=%s) does not seem to exist on this system, leaving user empty. )superr   __init__cwdgetpassgetuserdefault_userKeyErrordisplayvvosgetuid)selfargskwargs	__class__s      P/home/dcms/DCMS/lib/python3.12/site-packages/ansible/plugins/connection/local.pyr   zConnection.__init__.   se    j$($9&9	# ' 1D 	#JJilnlululwwx "D	#s   7 :A43A4c                    | j                   | j                  _        | j                  sUt        j                  dj                  | j                  j                        | j                  j                         d| _        | S )z/ connect to the local host; nothing to do here z(ESTABLISH LOCAL CONNECTION FOR USER: {0}hostT)r   _play_contextremote_user
_connectedr   vvvformatremote_addrr   s    r"   _connectzConnection._connect8   ss    
 *.):):&KKCJJ4K]K]KiKijqu  rD  rD  rP  rPK  Q"DO    c           	     B   t         t        |   |||       t        j	                  d       t
        j                  r!t
        j                  j                         d   nd}t        j                  j                  t        |d            st        d|z        t        j                  dj                  t        |            | j                   j"                  	       t        j	                  d
       t%        |t&        t(        f      rt        |      }nt+        t        |      }d}t,        j.                  }|rO| j0                  rC| j0                  j3                         r)| j5                  d      s	 t7        j8                         \  }}t-        j@                  |t%        |t&        t(        f      || jB                  |t,        j.                  t,        j.                        }|t        jD                  |       t        j	                  d       | j0                  rA| j0                  j3                         r&|r#tG        jF                  |jH                  tF        jJ                  tG        jF                  |jH                  tF        jL                        t        jN                  z         tG        jF                  |jP                  tF        jJ                  tG        jF                  |jP                  tF        jL                        t        jN                  z         tS        jT                         }	|	jW                  |jH                  tR        jX                         |	jW                  |jP                  tR        jX                         d}
	 | j0                  j[                  |
      sH| j0                  j]                  |
      s,|	j_                  | j                   j`                        }|s*|jc                         \  }}t        dt?        |
      z         |D ]m  \  }}|jd                  |jH                  k(  r|jH                  jg                         }:|jd                  |jP                  k(  sT|jP                  jg                         }o s*|jc                         \  }}t        dt?        |
      z         |
|z  }
| j0                  j[                  |
      s| j0                  j]                  |
      s,|	jE                          | j0                  j[                  |
      sw| j0                  j5                  d| j                         }|*|jh                  jk                  t        |d      dz          n$t        jj                  |t        |d      dz          tG        jF                  |jH                  tF        jJ                  tG        jF                  |jH                  tF        jL                        t        jN                   z         tG        jF                  |jP                  tF        jJ                  tG        jF                  |jP                  tF        jL                        t        jN                   z         t        j	                  d       |jc                  |      \  }}t        j	                  d       |rt        jD                  |       t        j	                  d       |jl                  ||fS # t:        t<        f$ r,}t        j	                  dt?        |      z         Y d}~d}~ww xY w# |	jE                          w xY w)z! run a command on the local host )in_datasudoablezin local.exec_command()r   Nsurrogate_or_stricterrorsz^failed to find the executable specified %s. Please verify if the executable exists and re-try.zEXEC {0}r$   zopening command with Popen()
pipeliningzUnable to open pty: %s)shell
executabler   stdinstdoutstderrz!done running command with Popen()r.   z:timeout waiting for privilege escalation password prompt:
z;privilege output closed while waiting for password prompt:
become_pass)playcontext   
z!getting output with communicate()zdone communicatingzdone with local.exec_command())7r   r   exec_commandr   debugCDEFAULT_EXECUTABLEsplitr   pathexistsr   r   r)   r*   r
   r&   r+   
isinstancer   r   map
subprocessPIPEbecomeexpect_prompt
get_optionptyopenptyIOErrorOSErrorr	   Popenr   closefcntlr9   F_SETFLF_GETFL
O_NONBLOCKr:   	selectorsDefaultSelectorregister
EVENT_READcheck_successcheck_password_promptselecttimeoutcommunicatefileobjreadr8   write
returncode)r   cmdr0   r1   r7   masterr8   epselectorbecome_outputeventsr9   r:   keyeventchunkr;   r!   s                     r"   r>   zConnection.exec_commandD   s    	j$,S'H,U/0898L8LQ))//1!4RV
ww~~hz:OPQ  UWa b c c 	K&&ws|44;M;M;Y;YZ45cI{343-Ch$C(A(A(CDOO\hLi
G # S9k":;!????
 HHUO9:;;4;;4468KK%--QXXu}}1UXZXeXe1efKK%--QXXu}}1UXZXeXe1ef 002Hahh	(<(<=ahh	(<(<=M!++33MB4;;KlKlmzK{%__T-?-?-G-GHF!)**+hkt  vC  lD  ,D  E  E&, 4
U;;!((2$%HHMMOE [[AHH4$%HHMMOE	4 !)**+ilu  wD  mE  ,E  F  F!U*M ++33MB4;;KlKlmzK{"  ;;,,];"kk44]PTPbPb4c>GGMM(;?T"UX]"]^HHVXkBW%X[`%`aKK%--QXXu}}1UY[YfYfXf1fgKK%--QXXu}}1UY[YfYfXf1fg9:w/*+ HHV67ff--E W% G61EFFGZ  s,   .[ 5C\ B\ \	!\\	\c           	        t         t        |   ||       t        || j                        }t        || j                        }t
        j                  dj                  ||      | j                  j                         t        j                  j                  t        |d            s#t        dj                  t        |                  	 t!        j"                  t        |d      t        |d             y	# t         j$                  $ r. t'        dj                  t        |      t        |                  t(        $ r2}t'        dj                  t        |      t        |                  d	}~ww xY w)
z% transfer a file from local to local )basedirzPUT {0} TO {1}r$   r2   r3   z"file or module does not exist: {0}z(failed to copy: {0} and {1} are the samez#failed to transfer file to {0}: {1}N)r   r   put_filer   r   r   r)   r*   r&   r+   r   rC   rD   r   r   r	   shutilcopyfileErrorr   rN   )r   in_pathout_pathre   r!   s       r"   ro   zConnection.put_file   s'    	j$((;gtxx8x:%,,Wh?dFXFXFdFdeww~~hw7LMN%&J&Q&QR[\cRd&eff	pOOHW5JKXV^g|M}~|| 	{IPPQZ[bQcenowexyzz 	pDKKIV^L_ajklamnoo	ps   ,C: :AE4-E//E4c                    t         t        |   ||       t        j	                  dj                  ||      | j                  j                         | j                  ||       y)z7 fetch a file from local to local -- for compatibility zFETCH {0} TO {1}r$   N)	r   r   
fetch_filer   r)   r*   r&   r+   ro   )r   rs   rt   r!   s      r"   rv   zConnection.fetch_file   sN     	j$*7H='..wAHZHZHfHfggx(r.   c                    d| _         y)z. terminate the connection; nothing to do here FN)r(   r,   s    r"   rQ   zConnection.close   s	    r.   )r   t.Anyr    rx   returnNone)ry   r   )NT)rc   strr0   zbytes | Noner1   boolry   ztuple[int, bytes, bytes])rs   r{   rt   r{   ry   rz   )ry   rz   )__name__
__module____qualname____doc__	transporthas_pipeliningr   r-   r>   ro   rv   rQ   __classcell__)r!   s   @r"   r   r   (   s1    #IN#
`.Dp$) r.   r   )!
__future__r   DOCUMENTATIONrR   r   r   rL   rV   rp   rG   typingtansible.constants	constantsr@   ansible.errorsr   r   ansible.module_utils.sixr   r   +ansible.module_utils.common.text.convertersr   r	   r
   ansible.plugins.connectionr   ansible.utils.displayr   ansible.utils.pathr   r   r    r.   r"   <module>r      sX   
 #   	 
      < ; T T 5 ) *
)Z  Z r.   