
    VhB*                         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Z	 d dlZdZd dlmZmZ d d	lmZ dd
ZddZd Zedk(  r e        yy# e$ r  e
j                         ZdZY Aw xY w)    )absolute_importdivisionprint_functiona
  
module: iso_create
short_description: Generate ISO file with specified files or folders
description:
  - This module is used to generate ISO file with specified path of files.
author:
  - Diane Wang (@Tomorrow9) <dianew@vmware.com>
requirements:
  - "pycdlib"
version_added: '0.2.0'

extends_documentation_fragment:
  - community.general.attributes

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

options:
  src_files:
    description:
      - This is a list of absolute paths of source files or folders which will be contained in the new generated ISO file.
      - Will fail if specified file or folder in O(src_files) does not exist on local machine.
      - 'Note: With all ISO9660 levels from 1 to 3, all file names are restricted to uppercase letters, numbers and underscores
        (_). File names are limited to 31 characters, directory nesting is limited to 8 levels, and path names are limited
        to 255 characters.'
    type: list
    required: true
    elements: path
  dest_iso:
    description:
      - The absolute path with file name of the new generated ISO file on local machine.
      - Will create intermediate folders when they does not exist.
    type: path
    required: true
  interchange_level:
    description:
      - The ISO9660 interchange level to use, it dictates the rules on the names of files.
      - Levels and valid values V(1), V(2), V(3), V(4) are supported.
      - The default value is level V(1), which is the most conservative, level V(3) is recommended.
      - ISO9660 file names at interchange level V(1) cannot have more than 8 characters or 3 characters in the extension.
    type: int
    default: 1
    choices: [1, 2, 3, 4]
  vol_ident:
    description:
      - The volume identification string to use on the new generated ISO image.
    type: str
  rock_ridge:
    description:
      - Whether to make this ISO have the Rock Ridge extensions or not.
      - Valid values are V(1.09), V(1.10) or V(1.12), means adding the specified Rock Ridge version to the ISO.
      - If unsure, set V(1.09) to ensure maximum compatibility.
      - If not specified, then not add Rock Ridge extension to the ISO.
    type: str
    choices: ['1.09', '1.10', '1.12']
  joliet:
    description:
      - Support levels and valid values are V(1), V(2), or V(3).
      - Level V(3) is by far the most common.
      - If not specified, then no Joliet support is added.
    type: int
    choices: [1, 2, 3]
  udf:
    description:
      - Whether to add UDF support to this ISO.
      - If set to V(true), then version 2.60 of the UDF spec is used.
      - If not specified or set to V(false), then no UDF support is added.
    type: bool
    default: false
af  
- name: Create an ISO file
  community.general.iso_create:
    src_files:
      - /root/testfile.yml
      - /root/testfolder
    dest_iso: /tmp/test.iso
    interchange_level: 3

- name: Create an ISO file with Rock Ridge extension
  community.general.iso_create:
    src_files:
      - /root/testfile.yml
      - /root/testfolder
    dest_iso: /tmp/test.iso
    rock_ridge: 1.09

- name: Create an ISO file with Joliet support
  community.general.iso_create:
    src_files:
      - ./windows_config/Autounattend.xml
    dest_iso: ./test.iso
    interchange_level: 3
    joliet: 3
    vol_ident: WIN_AUTOINSTALL
aC  
source_file:
  description: Configured source files or directories list.
  returned: on success
  type: list
  elements: path
  sample: ["/path/to/file.txt", "/path/to/folder"]
created_iso:
  description: Created iso file path.
  returned: on success
  type: str
  sample: "/path/to/test.iso"
interchange_level:
  description: Configured interchange level.
  returned: on success
  type: int
  sample: 3
vol_ident:
  description: Configured volume identification string.
  returned: on success
  type: str
  sample: "OEMDRV"
joliet:
  description: Configured Joliet support level.
  returned: on success
  type: int
  sample: 3
rock_ridge:
  description: Configured Rock Ridge version.
  returned: on success
  type: str
  sample: "1.09"
udf:
  description: Configured UDF support.
  returned: on success
  type: bool
  sample: false
NTF)AnsibleModulemissing_required_lib)	to_nativec           	      X   d }d }d }	t         j                  j                  |      }
d|
vr|j                         dz   }n|j                         dz   }|r|
}|r|}|r|}		 |j	                  |||||	       y # t
        $ r+}| j                  d|dt        |             Y d }~y d }~ww xY w)N.z.;1z;1iso_pathrr_namejoliet_pathudf_pathzFailed to add file  to ISO file due to msg)ospathbasenameupperadd_file	Exception	fail_jsonr   )moduleiso_filesrc_file	file_path
rock_ridge
use_jolietuse_udfr   r   r   	file_namefile_in_iso_patherrs                p/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/iso_create.pyr   r      s    GKH   +I
)$??,u4$??,t3i(-=w\grz{ ihXabeXfghhis   A5 5	B)>!B$$B)c           	         d }d }d }|j                         }	|rt        j                  j                  |      }|r|}|r|}	 |j	                  |	|||       y # t
        $ r+}
| j                  d|dt        |
             Y d }
~
y d }
~
ww xY w)Nr   zFailed to directory r   r   )r   r   r   r   add_directoryr   r   r   )r   r   dir_pathr   r   r    r   r   r   iso_dir_pathr#   s              r$   r&   r&      s    GKH>>#L''""8,jgS^iqr jxYbcfYghiijs   A 	B !BBc                  H	   t        t        ddd      t        dd      t        dg dd	      t        d
      t        d
g d      t        dg d      t        dd            } t        | d      }t        s |j                  t	        d      t
               |j                  j                  d      }|r t        |      dk(  r|j                  d       |D ]7  }t        j                  j                  |      r#|j                  d|z         9 |j                  j                  d      }|r t        |      dk(  r|j                  d       t        j                  j                  |      }|r5t        j                  j                  |      s	 t        j                  |       |j                  j                  d      }|d }|j                  j                  d!      }|j                  j                  d"      }	|j                  j                  d#      }
d }|j                  d$   rd%}t        d|||||	|
|&      }|j                   st#        j$                  d'      }|j'                  |||	|
|(       |D ]  }t        j                  j)                  |      rcg }g }|j+                  d)      }t        j                  j-                  |      }t/        ||d)|z   |	|
|*       t        j0                  |      D ]r  \  }}}|D ]1  }|j3                  t        j                  j5                  ||             3 |D ]1  }|j3                  t        j                  j5                  ||             3 t |D ]B  }t/        |||j7                  t        j                  j                  |            d   |	|
|*       D |D ]C  }t9        ||||j7                  t        j                  j                  |            d   |	|
|+       E t9        |||d)t        j                  j-                  |      z   |	|
|+        |j;                  |       |j=                          d|d,<    |j>                  d-i | y # t        $ r,}|j                  d|dt        |             Y d }~d }~ww xY w).NlistTr   )typerequiredelements)r+   r,   int)            r/   )r+   choicesdefaultstr)r+   )z1.09z1.10z1.12)r+   r3   )r/   r0   r1   boolF)r+   r4   )	src_filesdest_isointerchange_level	vol_identr   jolietudf)argument_specsupports_check_modepycdlib)	exceptionr7   r   zKPlease specify source file and/or directory list using src_files parameter.r   zHSpecified source file/directory path does not exist on local machine, %sr8   zVPlease specify the absolute path of the new created ISO file using dest_iso parameter.z&Exception caught when creating folder z, with error r:    r9   r   r;   r<   z2.60)changedsource_filecreated_isor9   r:   r   r;   r<   )always_consistent)r9   r:   r   r;   r<   /)r   r'   r   r   r    )r   r   r   r   r   r    rB    ) dictr   HAS_PYCDLIBr   r   PYCDLIB_IMP_ERRparamsgetlenr   r   existsdirnamemakedirsOSErrorr   
check_moder?   PyCdlibnewisdirrstripr   r&   walkappendjoinsplitr   writeclose	exit_json)r=   r   src_file_listr   r8   dest_iso_dirr#   	volume_idinter_levelr   r   r    resultr   dir_list	file_listdir_namer   dirsfilesfilenamedirnew_dirnew_files                           r$   mainrl      s?   FTFC6D1E<KE"U,DE	2fe,M # F -i8OTMM%%k2M]+q0jk! xww~~h'!knv!vwx }}  ,HCMQ&uv77??8,LBGGNN<8	}KK% !!+.I	--##$78K""<0J""8,JG}}U!%	F ??T:{iT^gqw~% 	XHww}}X&	#??3/77++H5fx#.]g)3WF *,): A%D$$) G!((dH)EFG# A T3(?@AA
  ( aG!&8gmmTVT[T[TcTcdlTmFnopFq-7JX_aa !* EHVh'/~~bggooh6O'PQR'S`j(2GEE (XQTWYW^W^WgWghpWqQq$.:wX3	X8 	x F9Fvw  	}^jluvylz!{||	}s   Q, ,	R!5!RR!__main__)NNNNNN)NNNNN)
__future__r   r   r   r+   __metaclass__DOCUMENTATIONEXAMPLESRETURNr   	tracebackrJ   r?   rI   ImportError
format_excansible.module_utils.basicr   r   +ansible.module_utils.common.text.convertersr   r   r&   rl   __name__rG       r$   <module>rz      s    A @HT6%
N 
 K
 K Ai.j"\~ zF _  *i**,OKs   A A&%A&