o
    x[hZ  ã                   @   s¢   d 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	 dZ
e e¡ZdZdd	„ Zd
d„ Zddd„Zddd„Zdd„ Zdd„ ZedkrOe eƒ ¡ dS dS )z0Generate multi-part mime messages for user-data.é    N)ÚMIMEMultipart)ÚMIMEText)ÚINCLUSION_TYPES_MAPz	make-mimezUExample: make-mime -a config.yaml:cloud-config -a script.sh:x-shellscript > user-datac                 C   s¤   g }g }t | ƒD ]8\}\}}}| ¡ }t||t ¡ ƒ}| dd| ¡ | ¡  ¡ }	|	tƒ vr;d|	|d f }
| 	|
¡ | 	|¡ qt
ƒ }|D ]}
| |
¡ qF||fS )NzContent-Dispositionzattachment; filename="%s"z3content type %r for attachment %s 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_message© r   ú?/usr/lib/python3/dist-packages/cloudinit/cmd/devel/make_mime.pyÚcreate_mime_message   s(   ÿ
þ
r   c              
   C   sR   z|   dd¡\}}t|dƒ|| ¡ fW S  ty( } z	t | d|  ¡|‚d}~ww )z@Return file content type by reading the first line of the input.ú:r   ÚrzInvalid value for %rN)ÚsplitÚopenÚstripÚ
ValueErrorÚargparseÚArgumentError)Útextr   r   Úer   r   r   Úfile_content_type.   s   ÿþ€ÿr)   c              
   C   s^   | st  ¡ } t| _t| _| jdddtdg ddd� | jdd	d
ddd� | jddd
ddd� | 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.)r,   r-   r/   z-fz--forcez$Ignore unknown content-type warnings)r%   ÚArgumentParserÚ__doc__ÚdescriptionÚEPILOGÚepilogÚadd_argumentr)   )Úparserr   r   r   Ú
get_parser9   s:   ø
ûûr8   Fc                    s   t ‡ fdd„t ¡ D ƒƒS )z‰Return a list of cloud-init supported content types.  Optionally
    strip out the leading 'text/' of the type if strip_prefix=True.
    c                    s    g | ]}ˆ r|  d d¡n|‘qS )ztext/Ú )Úreplace)Ú.0Úctype©Ústrip_prefixr   r   Ú
<listcomp>f   s    ÿÿz%get_content_types.<locals>.<listcomp>)Úsortedr   Úvaluesr=   r   r=   r   r   a   s
   
þÿr   c                 C   s‚   |j rtd tdd�¡ƒ dS t|jƒ\}}|r;|jrdnd}|D ]}tj 	|› d|› d�¡ q!tj 	d¡ |js;d	S t|ƒ dS )
a<  Create a multi-part MIME archive for use as user-data.  Optionally
       print out the list of supported content types of cloud-init.

    Also setup CLI log handlers to report to stderr since this is a development
    utility which should be run by a human on the CLI.

    @return 0 on success, 1 on failure.
    Ú
Tr=   r   ÚWARNINGÚERRORz: z-Invalid content-types, override with --force
r   )
Ú
list_typesÚprintÚjoinr   r   r   Úforcer   ÚstderrÚwrite)ÚnameÚargsr   r   ÚlevelÚerrorr   r   r   Úhandle_argsm   s   	rO   c                  C   s   t ƒ  ¡ } tt| ƒS ©N)r8   Ú
parse_argsrO   ÚNAME)rL   r   r   r   Úmain†   s   

rS   Ú__main__rP   )F)r2   r%   Úloggingr   Úemail.mime.multipartr   Úemail.mime.textr   Úcloudinit.handlersr   rR   Ú	getLoggerÚ__name__ÚLOGr4   r   r)   r8   r   rO   rS   Úexitr   r   r   r   Ú<module>   s(   
ÿ

(ÿ