�
܍~c� � �� � d Z ddlZddlZddlmZ ddlmZ ddlmZ ddl m
Z
ddlmZ dZ
ej e
� � Zd Zd
� Zd� Zdd�Zdd�Zd� Zd� Zedk r ej e� � � � dS dS )z0Generate multi-part mime messages for user-data.� N)�
MIMEMultipart)�MIMEText)�log)�addLogHandlerCLI)�INCLUSION_TYPES_MAPz make-mimezUExample: make-mime -a config.yaml:cloud-config -a script.sh:x-shellscript > user-datac �
� g }g }t | � � D ]�\ }\ }}}|� � � }t ||t j � � � � }|� dd|z � � |� � � � � � } | t � � vr!d| �d|dz �d�}
|� |
� � |� |� � ��t � � }|D ]}
|� |
� � �||fS )NzContent-Dispositionzattachment; filename="%s"z
content type z for attachment � z may be incorrect!)� enumerate�readr �sys�getdefaultencoding�
add_header�get_content_type�lower�get_content_types�appendr �attach)�files�sub_messages�errors�i�fh�filename�format_type�contents�sub_message�content_type�msg�combined_messages �?/usr/lib/python3/dist-packages/cloudinit/cmd/devel/make_mime.py�create_mime_messager! s- � ��L�
�F�*3�E�*:�*:� )� )�&��&�B��+��7�7�9�9���x��c�6L�6N�6N�O�O�����!�#>�(�#K�
�
�
� #�3�3�5�5�;�;�=�=���0�2�2�2�2�2� ���q�1�u�u�u�&�C�
�M�M�#�������K�(�(�(�(�$����� %� %������$�$�$�$��f�%�%� c �� � | � dd� � \ }}t |d� � ||� � � fS # t $ r}t j | d| z � � |�d}~ww xY w)z@Return file content type by reading the first line of the input.�:r �rzInvalid value for %rN)�split�open�strip�
ValueError�argparse�
ArgumentError)�textr r �es r �file_content_typer. . s� � ��!%���C��!3�!3���,��X�s�#�#�X�|�/A�/A�/C�/C�D�D��� � � ��$��(�D�1�
�
�� ��������s �=A �
A(�
A#�#A(c
� � | st j � � } t | _ t | _ | � dddt dg dd�� � | � dd d
dd�
� � | � ddd
dd�
� � | S )a Build or extend and arg parser for make-mime utility.
@param parser: Optional existing ArgumentParser instance representing the
subcommand which will be extended to support the args of this utility.
@returns: ArgumentParser with proper argument configuration.
z-az--attachr r z<file>:<content-type>z3attach the given file as the specified content-type)�dest�type�action�default�metavar�helpz-lz--list-types�
store_trueFz&List support cloud-init content types.)r2 r3 r5 z-fz--forcez$Ignore unknown content-type warnings)r* �ArgumentParser�__doc__�description�EPILOG�epilog�add_argumentr. )�parsers r �
get_parserr> |