
    Vh,                         d dl mZmZmZ eZdZdZdZd dl	Z	d dl
mZ d dlmZ d dlmZ  ej                   d	      5  d dlZddd       d
 Zd Zd Zd Zd Zd Zd Zedk(  r e        yy# 1 sw Y   ,xY w)    )absolute_importdivisionprint_functiona$  
module: iso_customize
short_description: Add/remove/change files in ISO file
description:
  - This module is used to add/remove/change files in ISO file.
  - The file inside ISO will be overwritten if it exists by option O(add_files).
author:
  - Yuhua Zou (@ZouYuhua) <zouy@vmware.com>
requirements:
  - "pycdlib"
version_added: '5.8.0'

extends_documentation_fragment:
  - community.general.attributes

attributes:
  check_mode:
    support: full
  diff_mode:
    support: none

options:
  src_iso:
    description:
      - This is the path of source ISO file.
    type: path
    required: true
  dest_iso:
    description:
      - The path of the customized ISO file.
    type: path
    required: true
  delete_files:
    description:
      - Absolute paths for files inside the ISO file that should be removed.
    type: list
    required: false
    elements: str
    default: []
  add_files:
    description:
      - Allows to add and replace files in the ISO file.
      - Will create intermediate folders inside the ISO file when they do not exist.
    type: list
    required: false
    elements: dict
    default: []
    suboptions:
      src_file:
        description:
          - The path with file name on the machine the module is executed on.
        type: path
        required: true
      dest_file:
        description:
          - The absolute path of the file inside the ISO file.
        type: str
        required: true
notes:
  - The C(pycdlib) library states it supports Python 2.7 and 3.4+.
  - The function C(add_file) in pycdlib will overwrite the existing file in ISO with type ISO9660 / Rock Ridge 1.12 / Joliet
    / UDF. But it will not overwrite the existing file in ISO with Rock Ridge 1.09 / 1.10. So we take workaround "delete the
    existing file and then add file for ISO with Rock Ridge".
a  
- name: "Customize ISO file"
  community.general.iso_customize:
    src_iso: "/path/to/ubuntu-22.04-desktop-amd64.iso"
    dest_iso: "/path/to/ubuntu-22.04-desktop-amd64-customized.iso"
    delete_files:
      - "/boot.catalog"
    add_files:
      - src_file: "/path/to/grub.cfg"
        dest_file: "/boot/grub/grub.cfg"
      - src_file: "/path/to/ubuntu.seed"
        dest_file: "/preseed/ubuntu.seed"
  register: customize_iso_result
z
src_iso:
  description: Path of source ISO file.
  returned: on success
  type: str
  sample: "/path/to/file.iso"
dest_iso:
  description: Path of the customized ISO file.
  returned: on success
  type: str
  sample: "/path/to/customized.iso"
N)deps)AnsibleModule)	to_nativepycdlibc                    |j                  dd      \  }}|j                         sd}|j                         }|j                  |j                               D ]  \  }}}||j                         k(  s|j                         |v r y |dk(  rd|z  }	n|d|}	|	j                         }
	 |dk(  r|j	                  |
       nJ|dk(  r|j	                  |
|       n1|dk(  r|j	                  |
|		       n|d
k(  r|j	                  |
|	        y # t
        $ r-}d|	dt        |      }| j                  |       Y d }~d }~ww xY w)N/   iso_path/%siso9660rr)rr_namejolietjoliet_pathudfudf_pathzFailed to create dir  with error: msg)rsplitstripwalkupperadd_directory	Exceptionr   	fail_json)module
opened_isoiso_typedir_path
parent_dircheck_dirnamedirnamedirlistdummy_filelistcurrent_dirpathcurrent_dirpath_uppererrr   s                s/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/iso_customize.pyiso_add_dirr0   t   sY    (Q 7J
!'')M,6OOZEUEUEWO,X *(.j&&((""$/S "'-"7-7"G$3$9$9$;!*y(,,-BC%,,-BM,Z),,-BP_,`&,,-B_,]'*(  *BQS\]`Sab  S ))*s   *A!D	E#D??Ec                     |j                         j                  d      }d}|D ]3  }|j                         s|dk(  rd|z  }n|d|}t        | |||       5 y )Nr   r   )r   splitr0   )r#   r$   r%   r&   dirnamesr,   items          r/   iso_add_dirsr5      sd    ~~%%c*HO Czz|c!#dlO)8$?OFJ/BC    c                    t         j                  j                  |      j                         }t         j                  j	                  |      }|j                         j                  d      }d}|D ]x  }|j                         s| j                  |j                               D ]0  \  }}}	||j                         k7  r n|j                         |vs/  y |dk(  rd|z  }r|d|}z d|vr|j                         dz   }
n|j                         dz   }
| j                  |j                               D ]4  \  }}}||j                         k7  r y|j                         |v xs |
|v c S  y )Nr   r   Fr   ..;1;1)ospathr)   r   basenamer2   r   r   )r$   	dest_filefile_dir	file_namer3   r'   r4   r)   r*   r+   file_in_iso_pathdummy_dirlistfilelists                r/   iso_check_file_existsrD      s_   wwy)//1H  +I~~%%c*HJ 6zz|0:IYIYI[0\ 	,GWn***,,zz|7*	 J$.5J6  )$??,u4$??,t3,6OOZEUEUEWO,X M(j&&(( H,L0@H0LL	Mr6   c                 0   |j                         }|d   dk7  rd|z  }|j                         }t        j                  j                  |      j                         }t        j                  j	                  |      }d|vr|j                         dz   }n|j                         dz   }|r|dk7  rt        | |||       	 |dk(  r|j                  ||       y |d	k(  r3t        ||      r|j                  |       |j                  |||
       y |dk(  r|j                  |||       y |dk(  r3t        ||      r|j                  |       |j                  |||       y y # t        $ r-}	d|dt        |	      }
| j                  |
       Y d }	~	y d }	~	ww xY w)Nr   r   r   r8   r9   r:   r   r   r   )r   r   r   )r   r   r   r   )r   r   zFailed to add local file z to ISO with error: r   )r   r;   r<   r)   r=   r   r5   add_filerD   rm_filer!   r   r"   )r#   r$   r%   src_filer>   
file_localr?   r@   rA   r.   r   s              r/   iso_add_filerJ      s   !I|sI%	!Jwwy)//1H  +I
)$??,u4$??,t3HOVZ8<"y 
5EF %Z;"",<"=
5EyY!
5ES\] %Z;""I"6
5EPYZ   "EOQZ[^Q_`S!!"s*   ;E 7E E &7E 	F(#FFc                 H   |j                         }|d   dk7  rd|z  }t        j                  j                  |      }t	        ||      s| j                  d|z         d|vr|j                         dz   }n|j                         dz   }	 |d	k(  r|j                  |
       y |dk(  r|j                  |
       y |dk(  r|j                  |       y |dk(  r|j                  |       y y # t        $ r-}d|dt        |      }| j                  |       Y d }~y d }~ww xY w)Nr   r   r   The file %s does not exist.r   r8   r9   r:   r   r   r   r   r   r   r   zFailed to delete iso file r   )
r   r;   r<   r=   rD   r"   r   rG   r!   r   )r#   r$   r%   r>   r@   rA   r.   r   s           r/   iso_delete_filerM      s(   !I|sI%	  +I Y7:YFG
)$??,u4$??,t3"y (89(89!95	2  "?H)TW.YS!!"s*   
C+ "C+ :C+ C+ +	D!4#DD!c           	         d }d}	 t        j                  d      }|j                  |       |j                         rd}n%|j	                         rd}n|j                         rd}|D ]  }t        | |||        |D ]  }t        | |||d   |d           |j                  |       |r|j                          y y # t        $ r-}d	|d
t        |      }	| j                  |	       Y d }~Ed }~ww xY w# |r|j                          w w xY w)Nr   T)always_consistentr   r   r   rH   r>   zFailed to rebuild ISO r   r   )r	   PyCdlibopenhas_rock_ridge
has_joliethas_udfrM   rJ   writer!   r   r"   close)
r#   src_isodest_isodelete_files_listadd_files_listisor%   r4   r.   r   s
             r/   iso_rebuildr\     s   
CHoo5H^^H[[]H% 	9DFC48	9 # 	UDhZ0@${BST	U 			(
 IIK 	  ";BIcNSS!!" IIK s*   B!B; ;	C1#C,'C4 ,C11C4 4D	c                     t        t        dd      t        dd      t        ddg       t        ddg t        t        dd      t        dd            	      
      } t        | dgd      }t        j                  |       |j                  d   }t
        j                  j                  |      s|j                  d|z         |j                  d   }t
        j                  j                  |      }|r4t
        j                  j                  |      s|j                  d|z         |j                  d   D cg c]  }|j                          }}|j                  d   }|rB|D ]=  }t
        j                  j                  |d         r&|j                  d|d   z         ? t        ||||d      }	|j                  st        |||||       d|	d<    |j                  di |	 y c c}w )Nr<   T)typerequiredliststr)r^   elementsdefaultdict)rH   r>   )r^   rb   rc   options)rW   rX   delete_files	add_files)rf   rg   )argument_specrequired_one_ofsupports_check_moderW   zISO file %s does not exist.r   rX   z$The dest directory %s does not existrf   rg   rH   rL   )rW   customized_isorf   rg   changedrl    )rd   r   r   validateparamsr;   r<   existsr"   r)   r   
check_moder\   	exit_json)
rh   r#   rW   rX   dest_iso_dirsrY   rZ   r4   results
             r/   mainrv   $  s   &406D1vrB&"6D9ED9
	M #69 F
 	MM&mmI&G77>>'":WDE}}Z(H77??8,LBGGNN<8ClRS,2MM.,IJqJJ]];/N" 	WD77>>$z"23  %BT*EU%U V	W & F FGX/@.QF9Fv' Ks   <G+__main__)
__future__r   r   r   r^   __metaclass__DOCUMENTATIONEXAMPLESRETURNr;   :ansible_collections.community.general.plugins.module_utilsr   ansible.module_utils.basicr   +ansible.module_utils.common.text.convertersr   declarer	   r0   r5   rD   rJ   rM   r\   rv   __name__rm   r6   r/   <module>r      s    A @?B
 
 K 4 AT\\) 
*>CMD$"N"8:0f zF Q s   A++A4