
    Vh                     V   d dl mZmZmZ eZdZd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mZ d dlmZ d dlmZmZ d d	lmZ  ej0                  d
      5  ej2                  rd dlZnd dlmZ ddd       d Zd Zd Zd Z G d de      Z d Z!e"dk(  r e!        yy# 1 sw Y   1xY w)    )absolute_importdivisionprint_functiona  
module: decompress
short_description: Decompresses compressed files
version_added: 10.1.0
description:
  - Decompresses compressed files.
  - The source (compressed) file and destination (decompressed) files are on the remote host.
  - Source file can be deleted after decompression.
extends_documentation_fragment:
  - ansible.builtin.files
  - community.general.attributes
attributes:
  check_mode:
    support: full
  diff_mode:
    support: none
options:
  src:
    description:
      - Remote absolute path for the file to decompress.
    type: path
    required: true
  dest:
    description:
      - The file name of the destination file where the compressed file will be decompressed.
      - If the destination file exists, it will be truncated and overwritten.
      - If not specified, the destination filename will be derived from O(src) by removing the compression format extension.
        For example, if O(src) is V(/path/to/file.txt.gz) and O(format) is V(gz), O(dest) will be V(/path/to/file.txt). If
        the O(src) file does not have an extension for the current O(format), the O(dest) filename will be made by appending
        C(_decompressed) to the O(src) filename. For instance, if O(src) is V(/path/to/file.myextension), the (dest) filename
        will be V(/path/to/file.myextension_decompressed).
    type: path
  format:
    description:
      - The type of compression to use to decompress.
    type: str
    choices: [gz, bz2, xz]
    default: gz
  remove:
    description:
      - Remove original compressed file after decompression.
    type: bool
    default: false
requirements:
  - Requires C(lzma) (standard library of Python 3) or L(backports.lzma, https://pypi.org/project/backports.lzma/) (Python
    2) if using C(xz) format.
author:
  - Stanislav Shamilov (@shamilovstas)
a  
- name: Decompress file /path/to/file.txt.gz into /path/to/file.txt (gz compression is used by default)
  community.general.decompress:
    src: /path/to/file.txt.gz
    dest: /path/to/file.txt

- name: Decompress file /path/to/file.txt.gz into /path/to/file.txt
  community.general.decompress:
    src: /path/to/file.txt.gz

- name: Decompress file compressed with bzip2
  community.general.decompress:
    src: /path/to/file.txt.bz2
    dest: /path/to/file.bz2
    format: bz2

- name: Decompress file and delete the compressed file afterwards
  community.general.decompress:
    src: /path/to/file.txt.gz
    dest: /path/to/file.txt
    remove: true
zm
dest:
  description: Path to decompressed file.
  type: str
  returned: success
  sample: /path/to/file.txt
N)six)ModuleHelper)	to_nativeto_bytes)depslzma)r   c                 .    t        j                  | d      S Nrb)r   opensrcs    p/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/decompress.pylzma_decompressr   p       99S$    c                 z    t         j                  rt        j                  | d      S t        j                  | d      S r   )r   PY3bz2r   BZ2Filer   s    r   bz2_decompressr   t   s+    
wwxxT""{{3%%r   c                 .    t        j                  | d      S r   )gzipr   r   s    r   gzip_decompressr   {   r   r   c                      ||       5 }t        |d      5 }t        j                  ||       d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   y xY w)Nwb)r   shutilcopyfileobj)b_srcb_desthandlersrc_file	dest_files        r   
decompressr'      sV    	 48&$ 	49x3	44 4	4 	44 4s   A
>A
A	A

Ac                       e Zd ZdZdZdZ e e edd       ed       edd	g d
       edd            dd      Zd Zd Z	d Z
d Zy)
Decompressz%s_decompressedFdestpathT)typerequired)r,   strgzr/   r   xz)r,   defaultchoicesbool)r,   r2   )r   r*   formatremove)argument_specadd_file_common_argssupports_check_modec                     t         t        t        d| _        | j                  j
                  | j                         | j                  _        t        j                  | j                         | j                          y )Nr0   )r   r   r   handlersvarsr*   get_destination_filenamer
   validatemodule	configure)selfs    r   __init_module__zDecompress.__init_module__   sL    .~_]99>>!!::<DIINdkk"r   c                 f   t        | j                  j                  d      }t        | j                  j                  d      }t        j
                  j                  |      sg| j                  j                  r<t        j
                  j                  |      r| j                  j                  d       n| j                  d|z         t        j
                  j                  |      r| j                  d|z         t        j
                  j                  |      r| j                  d|z         y y )	Nsurrogate_or_stricterrorsF)changedzPath does not exist: '%s'msgz Cannot decompress directory '%s'z3Destination is a directory, cannot decompress: '%s')r	   r<   r*   r   osr+   existsr6   r?   	exit_jsondo_raiseisdir)rA   r#   r"   s      r   r@   zDecompress.configure   s    $))..1FG/DEww~~e$yyBGGNN6$:%%e%4"="EF77==MM@5HMI77== MMSV\\M] !r   c                    t        | j                  j                  d      }t        | j                  j                  d      }| j                  j                  | j                  j                  | j                  j                        }| j                  | j                  j                     }	 t        j                  | j                  j                        \  }}| j                  j                  |       t        |d      }t        |||       t"        j$                  j'                  |      rt)        j*                  |d       | _        nd	| _        | j,                  r3| j                  j.                  s	 | j                  j1                  |       | j                  j2                  r!| j.                  st#        j2                  |       | j                  j5                  || j,                        | _        y # t        $ r)}| j                  dt!        |      z         Y d }~d }~ww xY w# t        $ r0 | j                  d
d| j                  j                  d       Y w xY w)NrD   rE   )r+   )dirz$Unable to create temporary file '%s'rH   F)shallowTzUnable to move temporary file 'z' to '')r	   r<   r*   r   r?   load_file_common_argumentsparamsr;   r5   tempfilemkstemptmpdiradd_cleanup_filer'   OSErrorrM   r   rJ   r+   rK   filecmpcmprG   
check_modeatomic_mover6   set_fs_attributes_if_different)	rA   r#   r"   	file_argsr$   tempfdtemppath
b_temppathes	            r   __run__zDecompress.__run__   s   $))..1FG/DEKK::4;;;M;MTXT]T]TbTb:c	--		 0 01	U'//DKK4F4FGFHKK((2!(3HIJuj'2 77>>&!&{{:vuMMDLDL<< 6 6o''
F; 99DOOIIe{{AA)T\\Z!  	UMMDyQR|SMTT	U  oR\^b^g^g^l^l"mnos+   ,A"G5 6H* 5	H'>H""H'*6I#"I#c                     | j                   j                  }d| j                   j                  z  }|j                  |      r(t	        |      t	        |      kD  r|d t	        |        }|S t
        j                  |z  }|S )Nz.%s)r<   r   r5   endswithlenr)   destination_filename_template)rA   r   fmt_extensionfilenames       r   r=   z#Decompress.get_destination_filename   sr    iimm		 0 00<<&3s8c-6H+H/S//0H  "??#EHr   N)__name__
__module____qualname__rh   use_old_vardictoutput_paramsdictr?   rB   r@   rd   r=    r   r   r)   r)      sk    $5!OM&406"UD:MNVU3	
 " 	F^[:r   r)   c                  ,    t         j                          y )N)r)   executerq   r   r   mainrt      s    r   __main__)#
__future__r   r   r   r,   __metaclass__DOCUMENTATIONEXAMPLESRETURNr   rZ   r   rJ   r    rU   ansible.module_utilsr   Kansible_collections.community.general.plugins.module_utils.mh.module_helperr   +ansible.module_utils.common.text.convertersr   r	   :ansible_collections.community.general.plugins.module_utilsr
   declarer   r   	backportsr   r   r   r'   r)   rt   rk   rq   r   r   <module>r      s    C B0d.
    	   $ d K KT\\& #
ww"	# & 4H HV zF W# #s   BB(