
    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 d dlmZ d dlmZ  G d d	e      Zd
 ZddZddZd Zedk(  r e        yy)    )absolute_importdivisionprint_functionaZ	  
---
module: patch
author:
    - Jakub Jirutka (@jirutka)
    - Luis Alberto Perez Lazaro (@luisperlaz)
description:
    - Apply patch files using the GNU patch tool.
short_description: Apply patch files using the GNU patch tool
version_added: "1.0.0"
options:
  basedir:
    description:
      - Path of a base directory in which the patch file will be applied.
      - May be omitted when O(dest) option is specified, otherwise required.
    type: path
  dest:
    description:
      - Path of the file on the remote machine to be patched.
      - The names of the files to be patched are usually taken from the patch
        file, but if there's just one file to be patched it can specified with
        this option.
    type: path
    aliases: [ originalfile ]
  src:
    description:
      - Path of the patch file as accepted by the GNU patch tool. If
        O(remote_src=false), the patch source file is looked up from the
        module's I(files) directory.
    type: path
    required: true
    aliases: [ patchfile ]
  state:
    description:
      - Whether the patch should be applied or reverted.
    type: str
    choices: [ absent, present ]
    default: present
  remote_src:
    description:
      - If V(false), it will search for src at originating/controller machine,
      - If C(true), it will go to the remote/target machine for the O(src).
    type: bool
    default: false
  strip:
    description:
      - Number that indicates the smallest prefix containing leading slashes
        that will be stripped from each file name found in the patch file.
      - For more information see the strip parameter of the GNU patch tool.
    type: int
    default: 0
  backup:
    description:
      - Passes C(--backup --version-control=numbered) to patch, producing numbered backup copies.
    type: bool
    default: false
  binary:
    description:
      - Setting to V(true) will disable patch's heuristic for transforming CRLF
        line endings into LF.
      - Line endings of O(src) and O(dest) must match.
      - If set to V(false), C(patch) will replace CRLF in O(src) files on POSIX.
    type: bool
    default: false
  ignore_whitespace:
    description:
      - Setting to V(true) will ignore white space changes between patch and input.
    type: bool
    default: false
notes:
  - This module requires GNU I(patch) utility to be installed on the remote host.
a  
- name: Apply patch to one file
  ansible.posix.patch:
    src: /tmp/index.html.patch
    dest: /var/www/index.html

- name: Apply patch to multiple files under basedir
  ansible.posix.patch:
    src: /tmp/customize.patch
    basedir: /var/www
    strip: 1

- name: Revert patch to one file
  ansible.posix.patch:
    src: /tmp/index.html.patch
    dest: /var/www/index.html
    state: absent
N)
format_exc)AnsibleModule)	to_nativec                       e Zd Zy)
PatchErrorN)__name__
__module____qualname__     g/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/ansible/posix/plugins/modules/patch.pyr
   r
   p   s    r   r
   c                     t        j                         j                         dv r| j                  d       y | j                  d       y )N)openbsdnetbsdfreebsdz--checkz	--dry-run)platformsystemlowerappend)optss    r   add_dry_run_optionr   t   s3     $DDIK r   c                     ddd|z  d|z  d|z  g}t        |       |r|j                  d       |r|j                  d       |r|j                  d|z         |d	k(  r|j                  d
        | |      \  }	}
}|	dk(  S )N--quiet	--forward
--strip=%s--directory='%s'--input='%s'--binary--ignore-whitespace'%s'present	--reverser   )r   r   )
patch_func
patch_filebasedir	dest_filebinaryignore_whitespacestripstater   rcvar1var2s               r   is_already_appliedr1   |   s    {5 "4w">Z')D tJ)*FY&'	K !$'Rt7Nr   c
                 P   ddddd|z  d|z  d|z  g}
|rt        |
       |r|
j                  d       |r|
j                  d	       |r|
j                  d
|z         |r|
j                  d       |	dk(  r|
j                  d        | |
      \  }}}|dk7  r|xs |}t        |      y )Nr   r   z--batchz--reject-file=-r   r   r    r!   r"   r#   z#--backup --version-control=numberedabsentr%   r   )r   r   r
   )r&   r'   r(   r)   r*   r+   r,   dry_runbackupr-   r   r.   outerrmsgs                  r   apply_patchr9      s    {I/@5 "4w">Z')D 4 J)*FY&'9:K %NRc	QwjSo r   c                     t        t        t        dddg      t        ddg      t        d      t        dd	
      t        dd
      t        dd
      t        dd
      t        dd
      t        ddddg      	      ddggd      t        ddj                        } t	        j
                  | j                  t        j                        sj                  d| j                  z         | j                  rMt	        j
                  | j                  t        j                        sj                  d| j                  z         | j                  rHt        j                  j                  | j                        sj                  d| j                  z         | j                  s.t        j                  j                  | j                        | _        j                  d      j                  d       fd}t        j                  j!                  | j                        | _        d}t#        || j                  | j                  | j                  | j$                  | j&                  | j(                  | j*                        sr	 t-        || j                  | j                  | j                  | j$                  | j&                  | j(                  j.                  | j0                  | j*                  
       d}j9                  |!       y # t2        $ r.}j                  t5        |      t7                       Y d }~Ed }~ww xY w)"NpathT	patchfile)typerequiredaliasesoriginalfile)r=   r?   )r=   intr   )r=   defaultboolFstrr$   r3   )r=   rB   choices)	srcdestr(   r,   
remote_srcr5   r*   r+   r-   rG   r(   )argument_specrequired_one_ofsupports_check_modeParamsr   z$src %s doesn't exist or not readable)r8   z%dest %s doesn't exist or not writablezbasedir %s doesn't existpatchzpatch command not foundc                 N    j                  ddj                  |             S )N )run_commandjoin)r   module	patch_bins    r   r&   zmain.<locals>.patch_func   s!    !!Y"GHHr   )r)   r*   r+   r,   r-   )r)   r*   r+   r,   r4   r5   r-   )r8   	exception)changed)r   dictr=   paramsosaccessrF   R_OK	fail_jsonrG   W_OKr(   r;   existsdirnameget_bin_pathabspathr1   r*   r+   r,   r-   r9   
check_moder5   r
   r   r   	exit_json)pr&   rU   erR   rS   s       @@r   mainre      s   &4+G6N+;<f%E1-7 VU3VU3">E9x>ST
 !),- F& 	Xr6==)A99QUUBGG$CquuMNvvbii0DOPyy		27199EF99GGOOAFF+	##G,I67I GGOOAEE"AEGj!%%affUVU]U]010C0C177Z[ZaZac	G
AEE199qxxklk~k~  GH  GN  GN & 1 1!((!''SG W%  	G1FF	Gs   A1L 	M	$MM	__main__)NFFr   r$   )NFFr   FFr$   )
__future__r   r   r   r=   __metaclass__DOCUMENTATIONEXAMPLESrX   r   	tracebackr   ansible.module_utils.basicr   ansible.module_utils._textr   	Exceptionr
   r   r1   r9   re   r   r   r   r   <module>ro      si    A @GR& 
    4 0	 	!$.6&r zF r   