
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
3
\me|&                 @   s  d dl Z d dlZd dlZd dlmZmZmZ d dlmZm	Z	m
Z
 ejeZdZdZdZdZeegZdgZd	Zd
ZdddddddddddddZeeej dd dZG dd de jdZdd Zdd  Zd!d" Zd#d$ Z d%d& Z!d'd( Z"d)d* Z#d+d, Z$e
fd-d.Z%d1d/d0Z&dS )2    N)importer
type_utilsutil)FREQUENCIES
PER_ALWAYSPER_INSTANCEztext/x-not-multipartzapplication/octet-streamZ__end__Z	__begin__ztext/part-handlerzpart-handler-%03dz	part-%03dztext/x-include-urlztext/x-include-once-urlztext/x-shellscriptztext/cloud-configztext/cloud-boothookztext/cloud-config-archiveztext/cloud-config-jsonpztext/jinja2ztext/x-shellscript-per-bootztext/x-shellscript-per-instanceztext/x-shellscript-per-once)z#includez#include-oncez#!z#cloud-configz#part-handlerz#cloud-boothookz#cloud-config-archivez#cloud-config-jsonpz## template: jinjaztext/x-shellscript-per-bootztext/x-shellscript-per-instanceztext/x-shellscript-per-oncec             C   s   dt |  S )Nr   )len)e r
   /usr/lib/python3.6/__init__.py<lambda>>   s    r   )keyc               @   s4   e Zd ZdddZdd Zdd Zejdd	 Zd
S )Handler   c             C   s   || _ || _d S )N)handler_version	frequency)selfr   versionr
   r
   r   __init__C   s    zHandler.__init__c             C   s   dt j| | j f S )Nz%s: [%s])r   Zobj_name
list_types)r   r
   r
   r   __repr__G   s    zHandler.__repr__c             C   s,   t | dstdndd t| dD S d S )Nprefixesz#Missing prefixes subclass attributec             S   s   g | ]}t | qS r
   )INCLUSION_TYPES_MAP).0prefixr
   r
   r   
<listcomp>P   s   z&Handler.list_types.<locals>.<listcomp>)hasattrNotImplementedErrorgetattr)r   r
   r
   r   r   J   s    

zHandler.list_typesc             O   s
   t  d S )N)r   )r   argskwargsr
   r
   r   handle_partT   s    zHandler.handle_partN)r   )	__name__
__module____qualname__r   r   r   abcabstractmethodr!   r
   r
   r
   r   r   B   s   

r   )	metaclassc       	      C   s  | j }|tkp|tko|tks"d S y| j}t|}W n tttfk
rR   d}Y nX |d }yxtj	d| |||| |dkr| j
|||||| nB|dkr| j
||||| n&|dkr| j
|||| ntd| W n, tk
r    tjtd| |||| Y nX d S )N   zContent-Typez1Calling handler %s (%s, %s, %s) with frequency %s   r   zUnknown module version %sz8Failed calling handler %s (%s, %s, %s) with frequency %s)r   r   r   r   int	TypeError
ValueErrorAttributeErrorLOGdebugr!   	Exceptionr   logexc)	moddatafilenamepayloadr   headersZmod_freqZmod_vercontent_typer
   r
   r   run_partY   sF    
r8   c             C   s   dt i}t| |d d || d S )NzContent-Type)CONTENT_STARTr8   )r2   r3   r   r6   r
   r
   r   
call_begin   s    r:   c             C   s   dt i}t| |d d || d S )NzContent-Type)CONTENT_ENDr8   )r2   r3   r   r6   r
   r
   r   call_end   s    r<   c       
      C   s   | d }t | }| d }tjj| d d| }|jds@d| }tj||d | d }y<ttj	|}	t
|	| d	 | |j|	d
d |d | d< W n$ tk
r   tjtd|| Y nX d S )NZhandlercountr   Z
handlerdirz%sz.pyz%s.pyi  handlersr3   T)Zinitializedr(   z7Failed at registering python file: %s (part handler %s))PART_HANDLER_FN_TMPLospathjoinendswithr   Z
write_filefixup_handlerr   import_moduler:   registerr0   r1   r.   )
ZpdataZ_ctypeZ	_filenamer5   Zcurcountmodnamer   Zmodfnamer=   r2   r
   r
   r   walker_handle_handler   s&    
rG   c             C   s   y\t | tr| jddd }n$| d d|  jddjddd }t||krZ|d | }W n  tk
r|   | d| }Y nX |S )N
r(   r      ignore)errors)
isinstancestrsplitdecoder   UnicodeDecodeError)Zblobsizestartr
   r
   r   _extract_first_or_bytes   s    
$rS   c             C   sZ   y
| j dS  ttfk
rB   y
| j dS  tk
r<   Y nX Y n tk
rT   Y nX | S )NZstring_escapeZunicode_escape)encodeLookupErrorr+   r-   )textr
   r
   r   _escape_string   s    


rW   c             C   s   |d }|| j dkr&tjd| d S |tkr@t| ||| d S | d }||krpt|| | d ||| d | nN|rt|d}dt| }|tkrtj	d	|| qtj	d
|| ntjd| d S )NzContent-TypeZexcludedzcontent_type "%s" is excludedr=   r3   r      z'%s...'z)Unhandled non-multipart (%s) userdata: %sz0Unhandled unknown content-type (%s) userdata: %szEmpty payload of type %s)
getr.   r/   PART_CONTENT_TYPESrG   r8   rS   rW   NOT_MULTIPART_TYPEwarning)r3   r4   r5   r6   r7   r=   rR   Zdetailsr
   r
   r   walker_callback   s:    
r]   c       	      C   s   d}x| j  D ]t}|j dkr q|j }|d kr4t}|j }|sHt| }t|}tj| ||d< t	j
|}||||| |d }qW d S )Nr   Z	multipartzContent-Typer(   )walkZget_content_maintypeZget_content_type
OCTET_TYPEget_filenamePART_FN_TPLdictr.   r/   r   Zfully_decoded_payload)	msgcallbackr3   ZpartnumpartZctyper4   r6   r5   r
   r
   r   r^      s     

r^   c             C   sR   t | dst| dd t | ds.t| d| n | j}|rN|tkrNtjd| | | S )Nr   r(   r   z&Handler %s has an unknown frequency %s)r   setattrr   r   r.   r\   )r2   Zdef_freqZfreqr
   r
   r   rC     s    

rC   c             C   sT   yt j| j }W n tk
r&   |S X |j }xtD ]}|j|r6t| S q6W |S )N)r   Zdecode_binarylowerrP   lstripINCLUSION_SRCH
startswithr   )r5   defaultZ
payload_lcrV   r
   r
   r   type_from_starts_with#  s    

rl   )N)'r%   Zloggingr?   Z	cloudinitr   r   r   Zcloudinit.settingsr   r   r   Z	getLoggerr"   r.   r[   r_   r;   r9   ZCONTENT_SIGNALSrZ   r>   ra   r   sortedlistkeysri   ABCMetar   r8   r:   r<   rG   rS   rW   r]   r^   rC   rl   r
   r
   r
   r   <module>   sL   
0)