a 'DgZ@sdZddlZddlZddlZddlmZddlmZddlm Z dZ e e Z dZdd Zd d Zdd d ZdddZddZddZe dkreedS)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 Csg}g}t|D]p\}\}}}|}t||t}|dd||} | tvrvd| |df} | | | |qt } |D]} | | q| |fS)NzContent-Dispositionzattachment; filename="%s"z3content type %r for attachment %s may be incorrect!) enumeratereadrsysgetdefaultencodingZ add_headerZget_content_typelowerget_content_typesappendrZattach) filesZ sub_messageserrorsiZfhfilenameZ format_typecontentsZ sub_message content_typemsgcombined_messagerA/usr/lib/python3.9/site-packages/cloudinit/cmd/devel/make_mime.pycreate_mime_messages(     rc Csbz&|dd\}}t|d||fWSty\}zt|d||WYd}~n d}~00dS)z@Return file content type by reading the first line of the input.:rrzInvalid value for %rN)splitopenstrip ValueErrorargparse ArgumentError)textrrerrrfile_content_type.sr"c Cs^|s t}t|_t|_|jdddtdgddd|jdd d d d d |jddd d dd |S)aBuild 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:z3attach the given file as the specified content-type)desttypeactiondefaultmetavarhelpz-lz --list-types store_trueFz&List support cloud-init content types.)r%r&r(z-fz--forcez$Ignore unknown content-type warnings)rArgumentParser__doc__ descriptionEPILOGepilog add_argumentr")parserrrr get_parser9s: r1FcstfddtDS)zReturn a list of cloud-init supported content types. Optionally strip out the leading 'text/' of the type if strip_prefix=True. cs g|]}r|ddn|qS)ztext/)replace).0ZctypeZ strip_prefixrr fsz%get_content_types..)sortedrvaluesr5rr5rr as  r cCs|jrtdtdddSt|j\}}|rv|jr:dnd}|D]}tj |d|dqBtj d|jsvd St|dS) 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.  Tr5rZWARNINGZERRORz: z-Invalid content-types, override with --force r) Z list_typesprintjoinr rr forcerstderrwrite)nameargsrrlevelerrorrrr handle_argsms  rCcCst}tt|S)N)r1 parse_argsrCNAME)r@rrrmains rF__main__)N)F)r+rZloggingrZemail.mime.multipartrZemail.mime.textrZcloudinit.handlersrrEZ getLogger__name__ZLOGr-rr"r1r rCrFexitrrrrs$     (