
    x[hZ                         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j                  e      ZdZd Zd	 Zdd
ZddZd Zd Zedk(  r ej*                   e              yy)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                    g }g }t        |       D ]  \  }\  }}}|j                         }t        ||t        j                               }|j                  dd|z         |j                         j                         }	|	t               vrd|	d|dz   d}
|j                  |
       |j                  |        t               }|D ]  }
|j                  |
        ||fS )NzContent-Dispositionzattachment; 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*3E*: )&&B+779xc6L6L6NO!#>(#K	
 #335;;=022AC MM#K() % %$%f%%    c                     	 | j                  dd      \  }}t        |d      ||j                         fS # t        $ r}t	        j
                  | d| z        |d}~ww xY 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   es       r   file_content_typer,   .   sj    !%C!3,Xs#X|/A/A/CDD $$(D1
	s   14 	AAAc           
          | st        j                         } t        | _        t        | _        | j                  dddt        dg dd       | j                  dd	d
dd       | j                  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.)r0   r1   r3   z-fz--forcez$Ignore unknown content-type warnings)r(   ArgumentParser__doc__descriptionEPILOGepilogadd_argumentr,   )parsers    r   
get_parserr<   9   s     ((* FFM
'B  	 5   3   Mr    c           	          t        t        j                         D cg c]  }| r|j                  dd      n| c}      S c c}w )zReturn a list of cloud-init supported content types.  Optionally
    strip out the leading 'text/' of the type if strip_prefix=True.
    ztext/ )sortedr   valuesreplace)strip_prefixctypes     r   r   r   a   sI      -335	
 +7EMM'2&EA	
 	
s   A c                    |j                   r%t        dj                  t        d                   yt	        |j
                        \  }}|rh|j                  rdnd}|D ]'  }t        j                  j                  | d| d       ) t        j                  j                  d       |j                  sy	t        |       y)
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.
    
T)rB   r   WARNINGERRORz: z-Invalid content-types, override with --force
r   )

list_typesprintjoinr   r   r   forcer
   stderrwrite)nameargsr   r   levelerrors         r   handle_argsrR   m   s     dii)t<=>24::>f!ZZ	W 	4EJJwbr23	4

IJzz	
r    c                  R    t               j                         } t        t        |       S N)r<   
parse_argsrR   NAME)rO   s    r   mainrW      s    <""$DtT""r    __main__rT   )F)r6   r(   loggingr
   email.mime.multipartr   email.mime.textr   cloudinit.handlersr   rV   	getLogger__name__LOGr8   r   r,   r<   r   rR   rW   exit r    r   <module>rb      s{   
 7   
 . $ 2g!- &.%P	2#
 zCHHTV r    