
    th                     T   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	m
Z
 ddlmZ dZ ej                  e      ZdZd	 Zd
efdZ	 dde
ej*                     dej*                  fdZddede	e   fdZdedej2                  defdZd Zedk(  r ej:                   e              yy)z0Generate multi-part mime messages for user-data.    N)MIMEMultipart)MIMEText)ListOptional)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%%    textc                     	 | j                  dd      \  }}t        |d      ||j                         fS # t        $ r}t	        j
                  dd| z        |d}~ww xY w)z@Return file content type by reading the first line of the input.:r	   rNzInvalid value for %r)splitopenstrip
ValueErrorargparseArgumentError)r#   r   r   es       r    file_content_typer.   /   sj    !%C!3,Xs#X|/A/A/CDD $$(D1
	s   14 	AAAparserreturnc           
          | 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.)r4   r5   r7   z-fz--forcez$Ignore unknown content-type warnings)r+   ArgumentParser__doc__descriptionEPILOGepilogadd_argumentr.   )r/   s    r    
get_parserr?   :   s     ((* FFM
'B  	 5   3   Mr"   strip_prefixc           	          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)r@   ctypes     r    r   r   d   sI      -335	
 +7EMM'2&EA	
 	
s   A nameargsc                    |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)r@   r   WARNINGERRORz: z-Invalid content-types, override with --force
r	   )

list_typesprintjoinr   r!   r   forcer   stderrwrite)rG   rH   r   r   levelerrors         r    handle_argsrU   p   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_argsrU   NAME)rH   s    r    mainrZ      s    <""$DtT""r"   __main__rW   )F)r:   r+   loggingr   email.mime.multipartr   email.mime.textr   typingr   r   cloudinit.handlersr   rY   	getLogger__name__LOGr<   r!   strr.   r9   r?   boolr   	NamespaceintrU   rZ   exit r"   r    <module>rj      s   
 7   
 . $ ! 2g!- &.C  15'X,,-''T	D 	T#Y 	c !3!3  2#
 zCHHTV r"   