Your IP : 216.73.217.13


Current Path : /usr/lib/python3/dist-packages/cloudinit/config/__pycache__/
Upload File :
Current File : //usr/lib/python3/dist-packages/cloudinit/config/__pycache__/schema.cpython-38.pyc

U

�Ad���	@sdZddlZddlZddlZddlZddlZddlZddlZddlm	Z	ddl
mZddlm
Z
ddlmZddlmZddlmZmZmZmZmZmZmZddlZdd	lmZmZdd
lmZddl m!Z!m"Z"m#Z#zddl$m%Z&e&Z%Wne'k
�re(Z%YnXe�)e*�Z+d
Z,dZ-dddd�Z.dZ/dZ0dZ1dZ2dZ3dZ4dZ5dZ6dZ7e�rvddlZddl8m9Z9m:Z:Gdd�de:�Z;ne<Z;Gdd �d e%�Z=Gd!d"�d"e�Z>ee>Z?dd#d$�e?ee@e@e@d%�d&d'�ZAGd(d)�d)eB�ZCd*d+�ZDde<e@d-�d.d/�ZEe=feFe<e@ee(d0�d1d2�ZGeeGdd3�ZHeeGd4d3�ZIe=fee(d5�d6d7�ZJe=fee(d5�d8d9�ZKd:d;�ZLd�e<d=�d>d?�ZMd�e<ee<eFeFeFeFd@�dAdB�ZNGdCdD�dD�ZOdddE�e<ePe<ee?ee?ee@ePfdF�dGdH�ZQd�dIdJ�ZRdKdL�ZSdMdN�ZTe<e@ee@dO�dPdQ�ZUe<e<e@dR�dSdT�ZVe@dU�dVdW�ZWe<e<dX�dYdZ�ZXe<d[�d\d]�ZYe<e@d^�d_d`�ZZd�e<e<e@db�dcdd�Z[e;e@de�dfdg�Z\e;e@de�dhdi�Z]d�e;ee<e@dj�dkdl�Z^e<dU�dmdn�Z_e`e@do�dpdq�Zae@dU�drds�Zbe<dU�dtdu�Zce<dU�dvdw�Zdd�dxdy�Zedzd{�Zfd|d}�Zge*d~k�re�heg��dS)�zFschema.py: Set of module functions for processing cloud-config schema.�N)�defaultdict)�Iterable)�deepcopy)�partial)�chain)�
TYPE_CHECKING�List�
NamedTuple�Optional�Type�Union�cast)�importer�safeyaml)�Init)�error�get_modules_from_dir�	load_file��ValidationErrorz!versions.schema.cloud-config.jsonzschema-cloud-config-v1.json�trueZfalseZnull)TFNs
#cloud-configz�
{name}
{title_underbar}
**Summary:** {title}

{description}

**Internal name:** ``{id}``

**Module frequency:** {frequency}

**Supported distros:** {distros}

{activate_by_schema_keys}{property_header}
{property_doc}

{examples}
z**Config schema**:z3{prefix}**{prop_name}:** ({prop_type}){description}zH{prefix}Each object in **{prop_name}** list supports the following keys:z**Examples**::

z
    # --- Example{0} ---�
deprecatedzDEPRECATED: )�NotRequired�	TypedDictc@sbeZdZUeed<eed<eed<eed<ejeed<ejeed<eed<eeeed<d	S)
�
MetaSchema�name�id�title�description�distros�examples�	frequency�activate_by_schema_keysN)�__name__�
__module__�__qualname__�str�__annotations__�typingrr�r)r)�9/usr/lib/python3/dist-packages/cloudinit/config/schema.pyrKs
rc@seZdZdS)�SchemaDeprecationErrorN)r#r$r%r)r)r)r*r+Ysr+c@s,eZdZUeed<eed<ed�dd�ZdS)�
SchemaProblem�path�message��returncCs|j�d|j��S)N�: )r-r.��selfr)r)r*�formataszSchemaProblem.formatN)r#r$r%r&r'r4r)r)r)r*r,]s
r,�, ��prefix�	separator)�schema_problemsr7r8r0cCs(|�tdd�|��}|r$|�|��}|S)NcSs|��S�N)r4)�pr)r)r*�<lambda>n�z)_format_schema_problems.<locals>.<lambda>)�join�map)r9r7r8Z	formattedr)r)r*�_format_schema_problemshsr@cs@eZdZdZd	eeeed��fdd�
Zed�dd�Z�Z	S)
�SchemaValidationErrorz<Raised when validating a cloud-config file against a schema.N��
schema_errors�schema_deprecationscsTd}|r|t|dd�7}|r8|r(|d7}|t|dd�7}t��|�||_||_dS)z�Init the exception an n-tuple of schema errors.

        @param schema_errors: An n-tuple of the format:
            ((flat.config.key, msg),)
        @param schema_deprecations: An n-tuple of the format:
            ((flat.config.key, msg),)
        �zCloud config schema errors: )r7�

�"Cloud config schema deprecations: N)r@�super�__init__rCrD)r3rCrDr.��	__class__r)r*rIws ��zSchemaValidationError.__init__r/cCs
t|j�Sr:)�boolrCr2r)r)r*�
has_errors�sz SchemaValidationError.has_errors)NN)
r#r$r%�__doc__r
�SchemaProblemsrIrLrM�
__classcell__r)r)rJr*rAts��rAcCsBzddlm}Wntk
r&YdSX|j�|d�p@t|tf�S)zWTYPE_CHECKER override allowing bytes for string type

    For jsonschema v. 3.0.0+
    r)�Draft4ValidatorF�string)�
jsonschemarQ�ImportError�TYPE_CHECKERZis_type�
isinstance�bytes)Zchecker�instancerQr)r)r*�is_schema_byte_string�s�
�rYF)�configr0csRtd���fdd�}|r|ndddg}d�t||��}��dd�}|�|����S)	z�combine description with new/changed/deprecated message

    deprecated/changed/new keys require a _version key (this is verified
    in a unittest), a _description key is optional
    ��keycsr��|��sdS��|�d�d�}��|�d�d|�d��}|���d|�d|��}�rbd|��Sd	|���d
�S)NrEZ_descriptionZ_versionz	<missing z'_version key, please file a bug report>z in version �. � z

*�*)�get�
capitalize�strip)r\Zkey_description�v�msg��annotaterZr)r*�format_message�s
�
z:_add_deprecated_changed_or_new_msg.<locals>.format_messager�changed�newrEr)r&r>r?r`�rstrip)rZrf�
filter_keyrgZfilter_keysZchanged_new_deprecatedrr)rer*�"_add_deprecated_changed_or_new_msg�s	�rl)r�schemark�
error_typeccs"|rt|d|gd�}||�VdS)z�Jsonschema validator for `deprecated` items.

    It raises a instance of `error_type` if deprecated that must be handled,
    otherwise the instance is consider faulty.
    T)rfrkN)rl)�
_validatorrZ	_instancermrkrnrdr)r)r*ro�s
�ro)rkrh�rnc
#s�ddlm}g}g}t|�D]d\}}	t|j||	|d��}
tt�fdd�|
��}tt�fdd�|
��}|sv|�|�q�|�|�q|d|f|d�V|Ed	Hd	S)
z�Jsonschema validator for `anyOf`.

    It treats occurrences of `error_type` as non-errors, but yield them for
    external processing. Useful to process schema annotations, as `deprecated`.
    rr�Zschema_pathcst|��Sr:�rV��erpr)r*r<�r=z_anyOf.<locals>.<lambda>cs
t|��Sr:rrrsrpr)r*r<�r=�.%r is not valid under any of the given schemas��contextN)rSr�	enumerate�list�descend�filter�extend)
�	validator�anyOfrX�_schemarnr�
all_errors�all_deprecations�index�	subschema�all_errs�errs�deprecationsr)rpr*�_anyOf�s(��
�r�c#s�ddlm}t|�}g}g}|D]h\}	}
t�j�|
|	d��}tt�fdd�|��}tt�fdd�|��}
|s~|
}|�|
�q�|�|�q |d�f|d�V��fd	d
�|D�}|r�|�|�d�dd
�|D��}|d�|f�Vn
|EdHdS)z�Jsonschema validator for `oneOf`.

    It treats occurrences of `error_type` as non-errors, but yield them for
    external processing. Useful to process schema annotations, as `deprecated`.
    rrrqcst|��Sr:rrrsrpr)r*r<r=z_oneOf.<locals>.<lambda>cs
t|��Sr:rrrsrpr)r*r<r=rurvcs g|]\}}���|�r|�qSr))�is_valid)�.0�i�s)rXr}r)r*�
<listcomp>(sz_oneOf.<locals>.<listcomp>r5css|]}t|�VqdSr:)�repr)r�rmr)r)r*�	<genexpr>+sz_oneOf.<locals>.<genexpr>z%r is valid under each of %sN)	rSrrxryrzr{r|�appendr>)r}�oneOfrXrrnrZ
subschemasr�r�r�r�r�r�r�Zfirst_validZ
more_validZreprsr))rnrXr}r*�_oneOfs:��
�

�r�c
Cs�ddlm}m}ddlm}t|j�}d|d<ddi|dd	<i}t|d
�rd|j�	dt
�}d|i}n|j}tt
f|d<d|i}t|j�}t|t<t|d
<t|d<t|d<|f||dd�|��}ddd�}	|	|_||fS)z�Get metaschema validator and format checker

    Older versions of jsonschema require some compatibility changes.

    @returns: Tuple: (jsonschema.Validator, FormatChecker)
    @raises: ImportError when jsonschema is not present
    r)rQ�
FormatChecker)�createFZadditionalProperties�typerR�
properties�labelrU�type_checkerZ
default_typesrhr�r~Zdraft4)Zmeta_schema�
validators�versionNc[s$tdd�|�||��}t|d�dkS)zgOverride version of `is_valid`.

        It does ignore instances of `SchemaDeprecationError`.
        cSst|t�Sr:)rVr+rsr)r)r*r<rs�z<get_jsonschema_validator.<locals>.is_valid.<locals>.<lambda>N)r{�iter_errors�next)r3rXr�__�errorsr)r)r*r�ls

�z*get_jsonschema_validator.<locals>.is_valid)N)rSrQr�Zjsonschema.validatorsr�rZMETA_SCHEMA�hasattrrUZredefinerYZ
DEFAULT_TYPESr&rW�dictZ
VALIDATORS�_validator_deprecated�DEPRECATED_KEY�_validator_changedr�r�r�)
rQr�r��strict_metaschemaZvalidator_kwargsr��typesr��cloudinitValidatorr�r)r)r*�get_jsonschema_validator3s@

��
��

r�T�rmc
Cszddlm}z|�|�WnZ|k
rt}z<|rXttd�dd�|jD��|j�gd�|�t�	d|�W5d}~XYnXdS)	a Validate provided schema meets the metaschema definition. Return strict
    Validator and FormatChecker for use in validation
    @param validator: Draft4Validator instance used to validate the schema
    @param schema: schema to validate
    @param throw: Sometimes the validator and checker are required, even if
        the schema is invalid. Toggle for whether to raise
        SchemaValidationError or log warnings.

    @raises: ImportError when jsonschema is not present
    @raises: SchemaValidationError when the schema is invalid
    r)�SchemaError�.cSsg|]}t|��qSr)�r&�r�r;r)r)r*r��sz3validate_cloudconfig_metaschema.<locals>.<listcomp>�rCzGMeta-schema validation failed, attempting to validate config anyway: %sN)
Zjsonschema.exceptionsr�Zcheck_schemarAr,r>r-r.�LOG�warning)r}rm�throwr��errr)r)r*�validate_cloudconfig_metaschema~s$
�����r�)rZrm�strictr��log_details�log_deprecationscCs&|dkrt�}z t�\}}|r,t||dd�Wn tk
rNt�d�YdSX|||�d�}g}	g}
t|�|�dd�d�D]D}d	�d
d�|j	D��}t
||j�f}
t|t
�r�|
|
7}
q||	|
7}	q||r�|
r�t|
dd
d�}t�|�|r�|	s�|
r�t|	|
��|	�r"|�rt|	dd
d�}nd}t�|�dS)aValidate provided config meets the schema definition.

    @param config: Dict of cloud configuration settings validated against
        schema. Ignored if strict_metaschema=True
    @param schema: jsonschema dict describing the supported schema definition
       for the cloud config module (config.cc_*). If None, validate against
       global schema.
    @param strict: Boolean, when True raise SchemaValidationErrors instead of
       logging warnings.
    @param strict_metaschema: Boolean, when True validates schema using strict
       metaschema definition at runtime (currently unused)
    @param log_details: Boolean, when True logs details of validation errors.
       If there are concerns about logging sensitive userdata, this should
       be set to False.
    @param log_deprecations: Controls whether to log deprecations or not.

    @raises: SchemaValidationError when provided config does not validate
        against the provided schema.
    @raises: RuntimeError when provided config sourced from YAML is not a dict.
    NF)r�z5Ignoring schema validation. jsonschema is not present)Zformat_checkercSs|jSr:)r-rsr)r)r*r<�r=z-validate_cloudconfig_schema.<locals>.<lambda>r[r�cSsg|]}t|��qSr)r�r�r)r)r*r��sz/validate_cloudconfig_schema.<locals>.<listcomp>z"Deprecated cloud-config provided:
�
r6zInvalid cloud-config provided:
zeInvalid cloud-config provided: Please run 'sudo cloud-init schema --system' to see the schema errors.)�
get_schemar�r�rTr��debug�sortedr�r>r-r,r.rVr+r@r�rA)rZrmr�r�r�r�r�r�r}r�r�Zschema_errorr-�problemr.Zdetailsr)r)r*�validate_cloudconfig_schema�s^
�

�
�

�

��r�c	@s�eZdZeeed�dd�Zeeeeed�dd��Z	e
d�dd	�Zedeeeeeeeeed�dd
��Z
eeeeeed�dd�Ze
e
eeefd�dd�ZdS)�
_Annotator)�cloudconfig�original_content�schemamarkscCs||_||_||_dSr:)�_cloudconfig�_original_content�_schemamarks)r3r�r�r�r)r)r*rI�sz_Annotator.__init__)r�contentr0cCsd�|�}d|�d|�d�S)Nr��# z: -------------
rF)r>)rr�Zbodyr)r)r*�
_build_footer�s
z_Annotator._build_footer)r9cCsztt�}|D]h\}}t�d|�}|rD|��\}}|t|��|�nd}||j|�|�|dk	rdj|||d�}q|S)Nz&format-l(?P<line>\d+)\.c(?P<col>\d+).*zLine {line} column {col}: {msg})�line�colrd)	rry�re�match�groups�intr�r�r4)r3r9�errors_by_liner-rdr�r�r�r)r)r*�_build_errors_by_lines�z _Annotator._build_errors_by_linerE)�problems�labels�footerr��label_prefixr0cCsB|D]8}|�|��}|�|�|�d|�d|���|d7}q|S)Nr�r1�)r�)r�r�r�r�r�r�r�r)r)r*�
_add_problemss

z_Annotator._add_problems)�linesr��deprecations_by_liner0c	s�g}g}g}d}d}t|d�D]p\}	}
||	}||	}|s>|r�g}
�j||
||dd�}�j||
||dd�}|�|
dd�|
��q|�|
�q|�t�fdd�td	d�d
|fd|ff���|S)Nr��E)r��Dz		# �,cs
�j|�Sr:)r���seqr2r)r*r<Ar=z._Annotator._annotate_content.<locals>.<lambda>cSst|d�S)Nr�)rLr�r)r)r*r<Cr=�ErrorsZDeprecations)rxr�r�r>r|r?r{)r3r�r�r��annotated_contentZerror_footerZdeprecation_footerZerror_indexZdeprecation_indexZline_numberr�r�r�r�r)r2r*�_annotate_content!sL��
����z_Annotator._annotate_content)rCrDr0cCsp|s|s|jS|j���d�}t|jt�sDd�||�ddg�g�S|�|�}|�|�}|�	|||�}d�|�S)Nr�r�z&# E1: Cloud-config is not a YAML dict.)
r��decode�splitrVr�r�r>r�r�r�)r3rCrDr�r�r�r�r)r)r*rfMs*����

�z_Annotator.annotateN)rE)r#r$r%r�rWrI�staticmethodr&rr�rOr�r�r�r�rrfr)r)r)r*r��s4�
���.
�r�rB)r�r�r�rCrDr0cCst|||��|pg|pg�S)a�Return contents of the cloud-config file annotated with schema errors.

    @param cloudconfig: YAML-loaded dict from the original_content or empty
        dict if unparseable.
    @param original_content: The contents of a cloud-config file
    @param schemamarks: Dict with schema marks.
    @param schema_errors: Instance of `SchemaProblems`.
    @param schema_deprecations: Instance of `SchemaProblems`.

    @return Annotated schema
    )r�rf)r�r�r�rCrDr)r)r*�annotated_cloudconfig_filegs�r�c
Cs�t|dd�}|�t�sTtdd�|t����g}t|�}|rPtti|i|j	d��|�z&|rjt
�|�\}}nt
�|�}i}Wn�t
jk
�rN}z�d}	}
d}t|d�r�t|d�r�t|d�}nt|d	�r�t|d	�r�t|d	�}|r�|jd}	|jd}
td
j|	|
d�d�|t|���g}t|�}|�r8tti|i|j	d��||�W5d}~XYnXt|t��sj|�sjtd
��zt||ddd�Wnrtk
�r�}zR|�r�tt||||j	|jd��n |j�r�t|jddd�}t|�|���r�W5d}~XYnXdS)aValidate cloudconfig file adheres to a specific jsonschema.

    @param config_path: Path to the yaml cloud-config file to parse, or None
        to default to system userdata from Paths object.
    @param schema: Dict describing a valid jsonschema to validate against.
    @param annotate: Boolean set True to print original config file with error
        annotations on the offending lines.

    @raises SchemaValidationError containing any of schema_errors encountered.
    @raises RuntimeError when config_path does not exist.
    F)r�zformat-l1.c1z"File {0} needs to begin with "{1}"r�r�NZcontext_markZproblem_markzformat-l{line}.c{col})r�r�zFile {0} is not valid yaml. {1}z Cloud-config is not a YAML dict.T)r�r�rBrGr5r6)r�
startswith�CLOUD_CONFIG_HEADERr,r4r�rA�printr�rCrZload_with_marks�load�yamlZ	YAMLErrorr��getattrr��columnr&rVr��RuntimeErrorr�rDr@rM)
Zconfig_pathrmrfr�r�rr�Zmarksrtr�r�Zmarkr.r)r)r*�validate_cloudconfig_files�
�����



�����
��	�
r�cCs|dkrdS|dkrdSdS)z�Provide a sorting weight for documentation of property types.

    Weight values ensure 'array' sorted after 'object' which is sorted
    after anything else which remains unsorted.
    Zarray��objectr�rr))�valuer)r)r*�_sort_property_order�s
r�ccs<|D]2}t|t�r0t|ttf�s0t|�EdHq|VqdSr:)rVrr&rW�_flatten)Zxs�xr)r)r*r��sr�)�
property_dict�	multi_keyr0cCsng}|�|i�D]P}|�t�r q|�d�rH|�dd�|�dg�D��q|�d�r|�|d�qtt|��S)N�enumcSs g|]}dt�||��d��qS��``��	_YAML_MAPr`)r�Z
enum_valuer)r)r*r��s�z,_collect_subschema_types.<locals>.<listcomp>r�)r`r�r|r�ryr�)r�r��property_typesr�r)r)r*�_collect_subschema_types�s


��
r�)r��defsr0c
Csxt||�|�dg�}t|t�s&|g}|�d�rDdd�|dD�}n6|�d�r`|�t|d��n|�d�rz|�t|d��t|�dkr�|d}n|jtd	�d
�	|�}|�di�}|�dg�}t|t�s�|g}t
|�}t|�di�|�di��D]*}t||�}|�r|dk�rq�|�
|�q�|�rnt|�dk�rB|�d
|d��S|jtd	�dd
�	|��d�}	|�d
|	��S|�pvdS)zNReturn a string representing a property type from a given
    jsonschema.
    r�r�cSs g|]}dt�||��d��qSr�r��r��kr)r)r*r�	sz&_get_property_type.<locals>.<listcomp>r�r~r�rr[�/�itemsZ	UNDEFINEDz of �(�))�_flatten_schema_refsr`rVryr|r��len�sortr�r>rLr�_get_property_typer�)
r�r�r�Z
property_typer�Zsub_property_typesZprune_undefined�sub_itemZsub_typeZsub_property_docr)r)r*r�sB


�





rr/cCsB|d}t�dd|�}t�dd�|�|�}t�dd�|�|�}|S)a�Parse description from the meta in a format that we can better
    display in our docs. This parser does three things:

    - Guarantee that a paragraph will be in a single line
    - Guarantee that each new paragraph will be aligned with
      the first paragraph
    - Proper align lists of items

    @param description: The original description in the meta.
    @param prefix: The number of spaces used to align the current description
    �z
(\S)\n(\S)z\1 \2z\n\nz\n\n{}z\n( +)-z\n{}-)r��subr4)rr7Zlist_paragraphr)r)r*�_parse_description+s�r)�src_cfgr�cCsd|kr(|�d��dd�}|�||�d|kr�d|dkrd|d�d��dd�}|d�||�d|dkr�|ddD],}d|kr||�d��dd�}|�||�q|t|�dg�|�dg�|�dg��D],}d|kr�|�d��dd�}|�||�q�dS)	zEFlatten schema: replace $refs in src_cfg with definitions from $defs.z$refz#/$defs/rEr�r�r~�allOfN)�pop�replace�updaterr`)rr�Z	reference�
sub_schemar)r)r*rAs(


�r)rcCs,|�dd�}|sdS|D]}|�|�qdS)z�Flatten schema: Merge allOf.

    If a schema as allOf, then all of the sub-schemas must hold. Therefore
    it is safe to merge them.
    r	N)r
r)rZsub_schemasr
r)r)r*�_flatten_schema_all_of[s
r)�prop_configr0cCs|tttd�dd�}|�di�}|�di�}g}g}||||�t||�D]}||||�qHd�t||��}|rxd|��}|S)aGReturn accumulated property description.

    Account for the following keys:
    - top-level description key
    - any description key present in each subitem under anyOf or allOf

    Order and deprecated property description after active descriptions.
    Add a trailing stop "." to any description not ending with ":".
    �rZ�descriptions�deprecated_descriptionscSs<tt|jd��r |�t|��n|�d�r8|�t|��dS)N)Zdeprecated_versionZchanged_versionZnew_versionr)�anyr?r`r�rlrr)r)r*�assign_descriptionsss���
z6_get_property_description.<locals>.assign_descriptionsr�r~r]r^)r�ryr`rr>)rrr�r~rrrrr)r)r*�_get_property_descriptionhs�
r�    )rmr�r0cs�|d}g}��d�dkrdS�fdd�dD�}�fdd�|D�}|D�]T}|��D�]D\}}	t|	|�t|	�|	�d�dkr�qTt|	�}
|	�d	|�}|�tj||t|
|�t	|	|�d
��|	�d�}|�rtt||�|�d�s�|�d
��r|�t
j||d��|d7}|�t|||d��|�dg�D]N}
|
�d��s@|
�d
��r$|�t
j||d��|d7}|�t|
||d���q$d|	k�s�d
|	krT|�t|	||d��qTqFd�|�S)zDReturn restructured text describing the supported schema properties.r�hiddenTrEcs$g|]}d�ks|�dkr|�qS)rr)�r�r\r�r)r*r��s�z%_get_property_doc.<locals>.<listcomp>)r��patternPropertiescsg|]}��|i��qSr))r`rr�r)r*r��sr�)r7�	prop_namerZ	prop_typer�r�r)r7r)r�r7r�rF)
r`r�rrrr��SCHEMA_PROPERTY_TMPLr4rr�SCHEMA_LIST_ITEM_TMPL�_get_property_docr>)rmr�r7Z
new_prefixr�Z
property_keysZproperty_schemasZprop_schemaZprop_keyrrr�r�Z
alt_schemar)r�r*r�s�
�

��

������������r)�metar0cCsj|�d�}|sdSt}t|�D]F\}}t�|d��d�}|tkrV|�dt�|d��|d�	|�7}q|S)zAReturn restructured text describing the meta examples if present.r rErr�rr�)
r`�SCHEMA_EXAMPLES_HEADERrx�textwrap�indentr��insert�SCHEMA_EXAMPLES_SPACER_TEMPLATEr4r>)rr Zrst_content�countZexampleZindented_linesr)r)r*�
_get_examples�s
�r%cCs2|�d�sdSd�dd�|dD��}d|�d�S)Nr"rEr5css|]}d|�d�VqdS)r�Nr)r�r)r)r*r��sz3_get_activate_by_schema_keys_doc.<locals>.<genexpr>z**Activate only on keys:** rF)r`r>)rZschema_keysr)r)r*� _get_activate_by_schema_keys_doc�s

�r&)rrmr0c	Csl|dkrt�}|r|std��t|���}ddddddd	h}d
h}d}||r^d�||�}n|||rxd
�||�}|r�t|��tt|��}d|d<|�di�}|�|d�r�|�|di�}t	t|�}zt
||d�|d<Wn(tk
�rt�
d�d|d<YnX|d�rt|d<t|�|d<d�|d�|d<t�dd|d	�|d<t|�|d
<tjf|�}|S)z�Return reStructured text rendering the provided metadata.

    @param meta: Dict of metadata to render.
    @param schema: Optional module schema, if absent, read global schema.
    @raise KeyError: If metadata lacks an expected key.
    Nz"Expected non-empty meta and schemarrr r!rrrr"rEz(Missing required keys in module meta: {}z3Additional unexpected keys found in module meta: {}Zproperty_header�$defs)r�Zproperty_docz3Unable to render property_doc due to invalid schemar5r��-Ztitle_underbar)r��
ValueError�set�keysr4�KeyErrorr�rr`r
r�AttributeErrorr�r��SCHEMA_PROPERTY_HEADERr%r>r�rr&�SCHEMA_DOC_TMPL)	rrmr+Z
required_keysZ
optional_keysZ
error_messageZ	meta_copyr��templater)r)r*�get_meta_doc�s^�	���


�r1cCstj�tj�t��}t|�Sr:)�osr-�dirname�abspath�__file__r)Zconfigs_dirr)r)r*�get_modules1sr6)�requested_modulesr0cCs�d}tt����dg}t|��t|��}|rLtd�t|�d�|��dd�|D]J}d|ksd||krPt�	|dgdg�\}}|rPt�
|d	�}||jp�d7}qP|S)
zaLoad module docstrings

    Docstrings are generated on module load. Reduce, reuse, recycle.
    rE�allz+Invalid --docs value {}. Must be one of: {}r5T��sys_exit�cloudinit.configrr)ryr6�valuesr*�
differencerr4r>r�find_module�
import_modulerN)r7�docsZall_modulesZinvalid_docs�mod_name�mod_locs�_�modr)r)r*�load_doc6s,���rEcCstj�tj�tj�t��d�S)NZschemas)r2r-r>r3r4r5r)r)r)r*�get_schema_dirQsrFc
Csxtj�t�t�}d}zt�t|��}Wn0tk
rV}zt	�
d||�W5d}~XYnX|stt	�
d|�idgd�}|S)z?Return jsonschema coalesced from all cc_* cloud-config modules.Nz$Cannot parse JSON schema file %s. %szCNo base JSON schema files found at %s. Setting default empty schemaz'http://json-schema.org/draft-04/schema#)r'z$schemar	)r2r-r>rF�USERDATA_SCHEMA_FILE�json�loadsr�	Exceptionr�r�)Zschema_file�full_schemartr)r)r*r�Us  ��r�cCsVt�}t���D]@\}}t�|dgdg�\}}|rt�|d�}|j||jd<q|S)z<Return metadata coalesced from all cc_* cloud-config module.r;rrr)r�r6r�rr>r?r)Z	full_metarCrArBrDr)r)r*�get_metats�rLcCs\|stjddd�}|jdddd�|jdd	d
dd�|jd
dddd�|jdd	d
dd�|S)z0Return a parser for supported cmdline arguments.�cloudconfig-schemaz.Validate cloud-config files or document schema)�progrz-cz
--config-filez.Path of the cloud-config yaml file to validate)�helpz--system�
store_trueFz)Validate the system cloud-config userdata)�action�defaultrOz-dz--docs�+zCPrint schema module docs. Choices: all or space-delimited cc_names.)�nargsrOz
--annotatez/Annotate existing cloud-config file with errors)�argparse�ArgumentParser�add_argument��parserr)r)r*�
get_parser�s:����	�rZcCs�|j|j|jg}tdd�|D��dkr2tddd�|jrJ|jrJtddd�t�}|jrhtt|j��dS|jr|d	|jff}n�t	�
�d
kr�tddd�tgd�}|jd
d�|j
�d�}|s�tddd�dSd	|ff}d|j
�d�fd|j
�d�ff}|D]&\}}	|	r�t	j�|	�r�|||	ff7}q�t	j�|d
d��sTtd|d
d�d�ddd�d}
tt|�dk�}|�r�tdd�dd�|D���d}
g}t|d�D�]\}
\}}|�r�td|
�d |�d!|�d"��zt|||j�Wn�tk
�r:}z<|j�s*t|
�d#|���tt|�|
d$d%�|�|�W5d}~XYnvtk
�r�}z4t|
�d#|���tt|�|
d$d%�|�|�W5d}~XYn$X|j�r�|n|}t|
�d&|����q�|�r�td�d'd�|D��d(dd�dS))z@Handle provided schema args and perform the appropriate actions.cSsg|]}|r|�qSr)r))r��argr)r)r*r��sz&handle_schema_args.<locals>.<listcomp>r�z;Expected one of --config-file, --system or --docs argumentsTr9z;Invalid flag combination. Cannot use --annotate with --docsNz	user-datarzNUnable to read system userdata or vendordata as non-root user. Try using sudo.)Zds_depsZtrust)�existingZcloud_configz;Unable to obtain user data file. No instance data availablezvendor-dataZvendor_cloud_configzvendor2-dataZvendor2_cloud_configzConfig file z does not existz	Error: {})�fmtr:rEz!Found cloud-config data types: %sr5css|]\}}|VqdSr:r))r��cfg_typerCr)r)r*r��sz%handle_schema_args.<locals>.<genexpr>z  r�r]z at �:zInvalid cloud-config z
Error: {}
)r]zValid cloud-config: css|]
}|VqdSr:r))r�rnr)r)r*r��sz'Error: Invalid cloud-config schema: {}
)Zconfig_filer@�systemrrrfr�r�rEr2�getuidrZfetch�pathsZ	get_ipathr-�existsrLr>rxr�rAr&r�r�)r�argsZexclusive_argsrKZconfig_filesZinitZ
userdata_fileZvendor_config_filesr^Zvendor_fileZnested_output_prefixZmulti_config_outputZerror_types�idxZcfg_filertZcfgr)r)r*�handle_schema_args�s����
�
������rfcCst�}td|���dS)zDTool to validate schema of a cloud-config file or print schema docs.rMr)rZrf�
parse_argsrXr)r)r*�main�srh�__main__)FN)T)NFFTF)F)r)N)N)irNrUrHZloggingr2r��sysr �collectionsrZcollections.abcr�copyr�	functoolsr�	itertoolsrr(rrr	r
rrr
r�Z	cloudinitrrZcloudinit.stagesrZcloudinit.utilrrrrSrZ_ValidationErrorrTrJZ	getLoggerr#r�ZVERSIONED_USERDATA_SCHEMA_FILErGr�r�r/r.rrrr#r�ZDEPRECATED_PREFIXZtyping_extensionsrrrr�r+r,rOr&r@r)rArYrlrLror�r�r�r�r�r�r�r�rWr�r�r�r�r�rrrrrrr%r&r1r6ryrErFr�rLrZrfrh�exitr)r)r)r*�<module>s�$

���#��)����)��/K%��Qz�
�
Z
,
*H	>
$X