U
    Ad                     @   s   d 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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ree  dS )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                 C   s   g }g }t | D ]p\}\}}}| }t||t }|dd|  |  }	|	t krvd|	|d f }
|	|
 |	| qt
 }|D ]}
||
 q||fS )NzContent-Dispositionzattachment; filename="%s"z3content type %r for attachment %s may be incorrect!   )	enumeratereadr   sysgetdefaultencodingZ
add_headerZget_content_typelowerget_content_typesappendr   Zattach)filesZsub_messageserrorsiZfhfilenameZformat_typecontentsZ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   s`   z&|  dd\}}t|d|| fW S  tk
rZ } zt| d|  |W 5 d}~X Y nX dS )z@Return file content type by reading the first line of the input.:r   rzInvalid value for %rN)splitopenstrip
ValueErrorargparseZ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__ZdescriptionEPILOGZepilogadd_argumentr#   )parserr   r   r   
get_parser9   s:    
r0   Fc                    s   t  fddt D S )zReturn 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).0ZctypeZstrip_prefixr   r   
<listcomp>f   s   z%get_content_types.<locals>.<listcomp>)sortedr   valuesr4   r   r4   r   r   a   s
    
r   c                 C   s   t t|jrtjntj |jr6tdt	dd dS t
|j\}}|r|jrRdnd}|D ]}tj| d| d qZtjd |jsd	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.
    
Tr4   r   WARNINGZERRORz: z-Invalid content-types, override with --force
r   )r   LOGdebugr   DEBUGr9   Z
list_typesprintjoinr   r   r   Zforcer
   stderrwrite)nameargsr   r   levelerrorr   r   r   handle_argsm   s    	rE   c                  C   s   t   } tt| S )N)r0   
parse_argsrE   NAME)rB   r   r   r   main   s    
rH   __main__)N)F)r,   r    r
   Zemail.mime.multipartr   Zemail.mime.textr   Z	cloudinitr   Zcloudinit.cmd.develr   Zcloudinit.handlersr   rG   Z	getLoggerr:   r-   r   r#   r0   r   rE   rH   __name__exitr   r   r   r   <module>   s&   

(
