HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux ip-10-0-8-47 6.8.0-1021-aws #23~22.04.1-Ubuntu SMP Tue Dec 10 16:31:58 UTC 2024 aarch64
User: ubuntu (1000)
PHP: 8.1.2-1ubuntu2.22
Disabled: NONE
Upload Files
File: //usr/lib/python3/dist-packages/cloudinit/config/__pycache__/schema.cpython-310.pyc
o

�Ad���@sLdZddlZddlZddlZddlZddlZddlZddlZddlm	Z	ddl
mZddlm
Z
ddlmZddlmZddlmZmZmZmZmZmZmZddlZdd	lmZmZdd
lmZddl m!Z!m"Z"m#Z#z
ddl$m%Z&e&Z%Wne'ye(Z%Ynwe�)e*�Z+d
Z,dZ-dddd�Z.dZ/dZ0dZ1dZ2dZ3dZ4dZ5dZ6dZ7er�ddlZddl8m9Z9m:Z:Gdd�de:�Z;ne<Z;Gdd �d e%�Z=Gd!d"�d"e�Z>ee>Z?dd#d$�d%e?d&ee@d'e@d(e@fd)d*�ZAGd+d,�d,eB�ZCd-d.�ZD	d�d0e<d(e@fd1d2�ZEe=fdeFd3e<d4e@d5ee(fd6d7�ZGeeGdd8�ZHeeGd9d8�ZIe=fd5ee(fd:d;�ZJe=fd5ee(fd<d=�ZKd>d?�ZLd�d3e<fdAdB�ZM		/	/	@	/d�d0e<d3ee<dCeFdDeFdEeFdFeFfdGdH�ZNGdIdJ�dJ�ZOdddK�dLe<dMePdNe<dOee?dPee?d(ee@ePffdQdR�ZQd�dSdT�ZRdUdV�ZSdWdX�ZTdYe<dZe@d(ee@fd[d\�ZUdYe<d]e<d(e@fd^d_�ZVd(e@fd`da�ZWdbe<d]e<fdcdd�ZXdbe<fdedf�ZYdge<d(e@fdhdi�ZZd�d3e<d]e<d(e@fdkdl�Z[dme;d(e@fdndo�Z\dme;d(e@fdpdq�Z]d�dme;d3ee<d(e@fdrds�Z^d(e<fdtdu�Z_dve`d(e@fdwdx�Zad(e@fdydz�Zbd(e<fd{d|�Zcd(e<fd}d~�Zdd�dd��Zed�d��Zfd�d��Zge*d�k�r$e�heg��dSdS)�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�true�false�null)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<defdd�ZdS)�
SchemaProblem�path�message�returncCs|j�d|j��S)N�: )r/r0��selfr+r+r,�formataszSchemaProblem.formatN)r%r&r'r(r)r5r+r+r+r,r.]s
r.�, ��prefix�	separator�schema_problemsr8r9r1cCs(|�tdd�|��}|r|�|��}|S)NcSs|��S�N)r5)�pr+r+r,�<lambda>nsz)_format_schema_problems.<locals>.<lambda>)�join�map)r:r8r9�	formattedr+r+r,�_format_schema_problemshsrAcsFeZdZdZ		d
deedeef�fdd�
Zdefdd	�Z�Z	S)�SchemaValidationErrorz<Raised when validating a cloud-config file against a schema.N�
schema_errors�schema_deprecationscsTd}|r|t|dd�7}|r|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: )r8�

�"Cloud config schema deprecations: N)rA�super�__init__rCrD)r4rCrDr0��	__class__r+r,rIws��
zSchemaValidationError.__init__r1cCs
t|j�Sr;)�boolrCr3r+r+r,�
has_errors�s
z SchemaValidationError.has_errors)NN)
r%r&r'�__doc__r
�SchemaProblemsrIrLrM�
__classcell__r+r+rJr,rBts���rBcCs@zddlm}Wn
tyYdSw|j�|d�pt|tf�S)zWTYPE_CHECKER override allowing bytes for string type

    For jsonschema v. 3.0.0+
    r)�Draft4ValidatorF�string)�
jsonschemarQ�ImportError�TYPE_CHECKER�is_type�
isinstance�bytes)�checker�instancerQr+r+r,�is_schema_byte_string�s��
�r[F�configcsPdtf��fdd�}|r|ngd�}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|��}�r1d|��Sd	|���d
�S)NrE�_description�_versionz	<missing z'_version key, please file a bug report>z in version �. � z

*�*)�get�
capitalize�strip)r]�key_description�v�msg��annotater\r+r,�format_message�s
�
z:_add_deprecated_changed_or_new_msg.<locals>.format_message)r�changed�newrEr )r(r>r?rc�rstrip)r\rj�
filter_keyrk�filter_keys�changed_new_deprecatedr r+rir,�"_add_deprecated_changed_or_new_msg�s	�rr�schemaro�
error_typeccs(�|rt|d|gd�}||�VdSdS)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)rjroN)rr)�
_validatorr�	_instancersrortrhr+r+r,ru�s�
��ru)rorlc
#s��ddlm}g}g}t|�D]2\}}	t|j||	|d��}
tt�fdd�|
��}tt�fdd�|
��}|s<|�|�n|�|�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��schema_pathc�t|��Sr;�rW��e�rtr+r,r=��z_anyOf.<locals>.<lambda>c�
t|��Sr;rzr{r}r+r,r=��
�.%r is not valid under any of the given schemas��contextN)rSr�	enumerate�list�descend�filter�extend)
�	validator�anyOfrZ�_schemartr�
all_errors�all_deprecations�index�	subschema�all_errs�errs�deprecationsr+r}r,�_anyOf�s*���
�r�c#s��ddlm}t|�}g}g}|D]4\}	}
t�j�|
|	d��}tt�fdd�|��}tt�fdd�|��}
|s@|
}|�|
�n|�|�q|d�f|d�V��fd	d
�|D�}|rv|�|�d�dd
�|D��}|d�|f�VdS|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`.
    rrrwcryr;rzr{r}r+r,r=r~z_oneOf.<locals>.<lambda>crr;rzr{r}r+r,r=r�r�r�cs g|]\}}���|�r|�qSr+)�is_valid)�.0�i�s)rZr�r+r,�
<listcomp>(s z_oneOf.<locals>.<listcomp>r6css�|]}t|�VqdSr;)�repr)r�rsr+r+r,�	<genexpr>+��z_oneOf.<locals>.<genexpr>z%r is valid under each of %sN)	rSrr�r�r�r�r��appendr>)r��oneOfrZr�rtr�
subschemasr�r�r�r�r�r�r��first_valid�
more_valid�reprsr+)rtrZr�r,�_oneOfs<���
�


�r�c
Cs�ddlm}m}ddlm}t|j�}d|d<ddi|dd	<i}t|d
�r2|j�	dt
�}d|i}n
|j}tt
f|d<d|i}t|j�}t|t<t|d
<t|d<t|d<|d||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)�createF�additionalProperties�typerR�
properties�labelrU�type_checker�
default_typesrlr�r��draft4)�meta_schema�
validators�versionNc[s$tdd�|�||��}t|d�duS)zgOverride version of `is_valid`.

        It does ignore instances of `SchemaDeprecationError`.
        cSst|t�Sr;)rWr-r{r+r+r,r=rs�z<get_jsonschema_validator.<locals>.is_valid.<locals>.<lambda>N)r��iter_errors�next)r4rZr��__�errorsr+r+r,r�ls

�z*get_jsonschema_validator.<locals>.is_validr+r;)rSrQr��jsonschema.validatorsr�r�META_SCHEMA�hasattrrU�redefiner[�
DEFAULT_TYPESr(rX�dict�
VALIDATORS�_validator_deprecated�DEPRECATED_KEY�_validator_changedr�r�r�)
rQr�r��strict_metaschema�validator_kwargsr��typesr��cloudinitValidatorr�r+r+r,�get_jsonschema_validator3s<

��
��

r�Tc
Cs|ddlm}z|�|�WdS|y=}z#|r,ttd�dd�|jD��|j�gd�|�t�	d|�WYd}~dSd}~ww)	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�.cS�g|]}t|��qSr+�r(�r�r<r+r+r,r���z3validate_cloudconfig_metaschema.<locals>.<listcomp>�rCzGMeta-schema validation failed, attempting to validate config anyway: %sN)
�jsonschema.exceptionsr��check_schemarBr.r>r/r0�LOG�warning)r�rs�throwr��errr+r+r,�validate_cloudconfig_metaschema~s&
�������r��strictr��log_details�log_deprecationscCs$|durt�}zt�\}}|rt||dd�Wnty&t�d�YdSw|||�d�}g}	g}
t|�|�dd�d�D]"}d	�d
d�|j	D��}t
||j�f}
t|t
�r[|
|
7}
q=|	|
7}	q=|rp|
rpt|
dd
d�}t�|�|r{|	sv|
r{t|	|
��|	r�|r�t|	dd
d�}nd}t�|�dSdS)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)�format_checkercSs|jSr;)r/r{r+r+r,r=�sz-validate_cloudconfig_schema.<locals>.<lambda>�r]r�cSr�r+r�r�r+r+r,r��r�z/validate_cloudconfig_schema.<locals>.<listcomp>z"Deprecated cloud-config provided:
�
r7zInvalid 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.r0rWr-rAr�rB)r\rsr�r�r�r�r�r�r�r�r��schema_errorr/�problemr0�detailsr+r+r,�validate_cloudconfig_schema�s\
��
�
��

�

���r�c@s�eZdZdededefdd�Zededeedefd	d
��Z	de
fdd
�Ze	d deedeedeedededefdd��Z
deedededeefdd�Zde
de
deeeffdd�ZdS)!�
_Annotator�cloudconfig�original_content�schemamarkscCs||_||_||_dSr;)�_cloudconfig�_original_content�_schemamarks)r4r�r�r�r+r+r,rI�s
z_Annotator.__init__r�contentr1cCsd�|�}d|�d|�d�S)Nr��# z: -------------
rF)r>)rr��bodyr+r+r,�
_build_footer�s
z_Annotator._build_footerr:cCsztt�}|D]4\}}t�d|�}|r"|��\}}|t|��|�nd}||j|�|�|dur:dj|||d�}q|S)Nz&format-l(?P<line>\d+)\.c(?P<col>\d+).*zLine {line} column {col}: {msg})�line�colrh)	rr��re�match�groups�intr�r�r5)r4r:�errors_by_liner/rhr�r�r�r+r+r,�_build_errors_by_lines��z _Annotator._build_errors_by_linerE�problems�labels�footerr��label_prefixcCsB|D]}|�|��}|�|�|�d|�d|���|d7}q|S)Nr�r2�)r�)r�r�r�r�r�r�r�r+r+r,�
_add_problemss

z_Annotator._add_problems�linesr��deprecations_by_linec	s�g}g}g}d}d}t|d�D]8\}	}
||	}||	}|s|rBg}
�j||
||dd�}�j||
||dd�}|�|
dd�|
��q|�|
�q|�t�fdd�td	d�d
|fd|ff���|S)Nr��E)r��Dz		# �,cs
�j|�Sr;)r���seqr3r+r,r=Ar�z._Annotator._annotate_content.<locals>.<lambda>cSst|d�S)Nr�)rLr�r+r+r,r=Cr~�Errors�Deprecations)r�r�r�r>r�r?r�)r4r�r�r��annotated_content�error_footer�deprecation_footer�error_index�deprecation_index�line_numberr�r�r�r�r+r3r,�_annotate_content!sD
��
����z_Annotator._annotate_contentrCrDcCsp|s|s|jS|j���d�}t|jt�s"d�||�ddg�g�S|�|�}|�|�}|�	|||�}d�|�S)Nr�r�z&# E1: Cloud-config is not a YAML dict.)
r��decode�splitrWr�r�r>r�r�r)r4rCrDr�r�r�r�r+r+r,rjMs$����

�
z_Annotator.annotateN)rE)r%r&r'r�rXrI�staticmethodr(rr�rOr�r�r�rrrjr+r+r+r,r��sT��
�
����������
�,��
�r��rCrDr�r�r�rCrDcCst|||��|p	g|pg�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�rj)r�r�r�rCrDr+r+r,�annotated_cloudconfig_filegs�r
c
Cs�t|dd�}|�t�s*tdd�|t����g}t|�}|r(tti|i|j	d��|�z|r5t
�|�\}}nt
�|�}i}Wnct
jy�}zVd}	}
d}t|d�r\t|d�r\t|d�}nt|d	�rkt|d	�rkt|d	�}|rw|jd}	|jd}
td
j|	|
d�d�|t|���g}t|�}|r�tti|i|j	d��||�d}~wwt|t�s�|s�td
��zt||ddd�WdSty�}z+|r�tt||||j	|jd��n|jr�t|jddd�}t|�|��r�WYd}~dSd}~ww)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)rzformat-l1.c1z"File {0} needs to begin with "{1}"r�r�N�context_mark�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�r	rGr6r7)r�
startswith�CLOUD_CONFIG_HEADERr.r5rrB�printr
rCr�load_with_marks�load�yaml�	YAMLErrorr��getattrr��columnr(rWr��RuntimeErrorr�rDrArM)
�config_pathrsrjr�r�rr��marksr|r�r�markr0r+r+r,�validate_cloudconfig_files�
���
��
�


��
����
���	����rcCs|dkrdS|dkrdSdS)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.
    �array��objectr�rr+)�valuer+r+r,�_sort_property_order�s
rccs>�|D]}t|t�rt|ttf�st|�EdHq|VqdSr;)rWrr(rX�_flatten)�xs�xr+r+r,r �s��r �
property_dict�	multi_keycCsng}|�|i�D](}|�t�rq|�d�r$|�dd�|�dg�D��q|�d�r0|�|d�qtt|��S)N�enumcS� g|]}dt�||��d��qS��``��	_YAML_MAPrc)r��
enum_valuer+r+r,r��s��z,_collect_subschema_types.<locals>.<listcomp>r�)rcr�r�r�r�r )r#r$�property_typesr�r+r+r,�_collect_subschema_types�s


��
�r-�defsc
Csnt||�|�dg�}t|t�s|g}|�d�r"dd�|dD�}n|�d�r0|�t|d��n
|�d�r=|�t|d��t|�dkrH|d}n|jtd	�d
�	|�}|�di�}|�dg�}t|t�sg|g}t
|�}t|�di�|�di��D]}t||�}|r�|dkr�qx|�
|�qx|r�t|�dkr�|�d
|d��S|jtd	�dd
�	|��d�}	|�d
|	��S|p�dS)zNReturn a string representing a property type from a given
    jsonschema.
    r�r%cSr&r'r)�r��kr+r+r,r�	s�z&_get_property_type.<locals>.<listcomp>r�r�r�rr��/�items�	UNDEFINEDz of �(�))�_flatten_schema_refsrcrWr�r�r-�len�sortrr>rLr�_get_property_typer�)
r#r.r,�
property_typer2�sub_property_types�prune_undefined�sub_item�sub_type�sub_property_docr+r+r,r9�sB


�





r9cCsB|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��subr5)r r8�list_paragraphr+r+r,�_parse_description+s�rC�src_cfgcCsd|vr|�d��dd�}|�||�d|vrUd|dvr2|d�d��dd�}|d�||�d|dvrU|ddD]}d|vrT|�d��dd�}|�||�q>t|�dg�|�dg�|�dg��D]}d|vr}|�d��dd�}|�||�qgdS)	zEFlatten schema: replace $refs in src_cfg with definitions from $defs.z$refz#/$defs/rEr2r�r��allOfN)�pop�replace�updaterrc)rDr.�	reference�
sub_schemar+r+r,r6As.�


���r6cCs,|�dd�}|s
dS|D]}|�|�qdS)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.
    rEN)rFrH)rD�sub_schemasrJr+r+r,�_flatten_schema_all_of[s�rL�prop_configcCs�dtdtdtfdd�}|�di�}|�di�}g}g}||||�t||�D]}||||�q&d�t||��}|r>d	|��}|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 ":".
    r\�descriptions�deprecated_descriptionscSsBtt|jd��r|�t|��dS|�d�r|�t|��dSdS)N)�deprecated_version�changed_version�new_versionr )�anyr?rcr�rr)r\rNrOr+r+r,�assign_descriptionsss���
�z6_get_property_description.<locals>.assign_descriptionsr�r�r`ra)r�r�rcrr>)rMrTr�r�rNrOr=r r+r+r,�_get_property_descriptionhs$��
�
rU�    cs�|d}g}��d�durdS�fdd�dD�}�fdd�|D�}|D]�}|��D]�\}}	t|	|�t|	�|	�d�dur>q)t|	�}
|	�d	|�}|�tj||t|
|�t	|	|�d
��|	�d�}|r�t||�|�d�sp|�d
�r�|�t
j||d��|d7}|�t|||d��|�dg�D]$}
|
�d�s�|
�d
�r�|�t
j||d��|d7}|�t|
||d��q�d|	vs�d
|	vr�|�t|	||d��q)q#d�|�S)zDReturn restructured text describing the supported schema properties.rV�hiddenTrEcs$g|]}d�vs|�dvr|�qS)rWr+�r�r]�rsr+r,r��s�
��z%_get_property_doc.<locals>.<listcomp>)r��patternPropertiescsg|]}��|i��qSr+)rcrXrYr+r,r��sr�)r8�	prop_namer �	prop_typer2r�rZ)r8r[)r.r8r�rF)
rcr2r6rLrUr��SCHEMA_PROPERTY_TMPLr5rCr9�SCHEMA_LIST_ITEM_TMPL�_get_property_docr>)rsr.r8�
new_prefixr��
property_keys�property_schemas�prop_schema�prop_keyrMr r�r2�
alt_schemar+rYr,r_�s�
�
��

���������������
7r_�metacCsj|�d�}|s	dSt}t|�D]#\}}t�|d��d�}|tkr+|�dt�|d��|d�	|�7}q|S)zAReturn restructured text describing the meta examples if present.r"rErVr�rr�)
rc�SCHEMA_EXAMPLES_HEADERr��textwrap�indentr�insert�SCHEMA_EXAMPLES_SPACER_TEMPLATEr5r>)rfr"�rst_content�count�example�indented_linesr+r+r,�
_get_examples�s
�rpcCs2|�d�sdSd�dd�|dD��}d|�d�S)Nr$rEr6css�|]	}d|�d�VqdS)r(Nr+r/r+r+r,r��s�
�z3_get_activate_by_schema_keys_doc.<locals>.<genexpr>z**Activate only on keys:** rF)rcr>)rf�schema_keysr+r+r,� _get_activate_by_schema_keys_doc�s

�rrc	Cs`|durt�}|r|std��t|���}hd�}dh}d}||r*d�||�}n
|||r7d�||�}|r=t|��tt|��}d|d<|�d	i�}|�|d
�ra|�|d
i�}t	t|�}z
t
||d�|d<Wnty}t�
d
�d|d<Ynw|dr�t|d<t|�|d<d�|d�|d<t�dd|d�|d<t|�|d<tjdi|��}|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 schema>rrrr!r"r#r r$rEz(Missing required keys in module meta: {}z3Additional unexpected keys found in module meta: {}�property_header�$defsr)r.�property_docz3Unable to render property_doc due to invalid schemar"r6r!r��-r�title_underbarr+)r��
ValueError�set�keysr5�KeyErrorr�rrcr
r_�AttributeErrorr�r��SCHEMA_PROPERTY_HEADERrpr>r�rArr�SCHEMA_DOC_TMPL)	rfrsrz�
required_keys�
optional_keys�
error_message�	meta_copyr.�templater+r+r,�get_meta_doc�sR	���

��r�cCstj�tj�t��}t|�Sr;)�osr/�dirname�abspath�__file__r)�configs_dirr+r+r,�get_modules1sr��requested_modulescCs�d}tt����dg}t|��t|��}|r&td�t|�d�|��dd�|D]%}d|vs2||vrMt�	|dgdg�\}}|rMt�
|d	�}||jpKd7}q(|S)
zaLoad module docstrings

    Docstrings are generated on module load. Reduce, reuse, recycle.
    rE�allz+Invalid --docs value {}. Must be one of: {}r6T��sys_exit�cloudinit.configrfr)r�r��valuesry�
differencerr5r>r�find_module�
import_modulerN)r��docs�all_modules�invalid_docs�mod_name�mod_locs�_�modr+r+r,�load_doc6s*��
��r�cCstj�tj�tj�t��d�S)N�schemas)r�r/r>r�r�r�r+r+r+r,�get_schema_dirQsr�c
Csztj�t�t�}d}z	t�t|��}Wnty,}z
t	�
d||�WYd}~nd}~ww|s;t	�
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#)rtz$schemarE)r�r/r>r��USERDATA_SCHEMA_FILE�json�loadsr�	Exceptionr�r�)�schema_file�full_schemar|r+r+r,r�Us$����r�cCsVt�}t���D] \}}t�|dgdg�\}}|r(t�|d�}|j||jd<q|S)z<Return metadata coalesced from all cc_* cloud-config module.r�rfrr)r�r�r2rr�r�rf)�	full_metar�r�r�r�r+r+r,�get_metats
��r�cCs\|s	tjddd�}|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)�progr z-cz
--config-filez.Path of the cloud-config yaml file to validate)�helpz--system�
store_trueFz)Validate the system cloud-config userdata)�action�defaultr�z-dz--docs�+zCPrint schema module docs. Choices: all or space-delimited cc_names.)�nargsr�z
--annotatez/Annotate existing cloud-config file with errors)�argparse�ArgumentParser�add_argument��parserr+r+r,�
get_parser�s:����	�r�cCs�|j|j|jg}tdd�|D��dkrtddd�|jr%|jr%tddd�t�}|jr4tt|j��dS|jr>d	|jff}nRt	�
�d
krJtddd�tgd�}|jd
d�|j
�d�}|setddd�dSd	|ff}d|j
�d�fd|j
�d�ff}|D]\}}	|	r�t	j�|	�r�|||	ff7}q|t	j�|d
d�s�td|d
d�d�ddd�d}
tt|�dk�}|r�tdd�dd�|D���d}
g}t|d�D]�\}
\}}|r�td|
�d |�d!|�d"��z	t|||j�WnYt�y}z"|j�st|
�d#|���tt|�|
d$d%�|�|�WYd}~q�d}~wt�yD}zt|
�d#|���tt|�|
d$d%�|�|�WYd}~q�d}~ww|j�rK|n|}t|
�d&|���q�|�rktd�d'd�|D��d(dd�dSdS))z@Handle provided schema args and perform the appropriate actions.cSsg|]}|r|�qSr+r+)r��argr+r+r,r��r�z&handle_schema_args.<locals>.<listcomp>r�z;Expected one of --config-file, --system or --docs argumentsTr�z;Invalid flag combination. Cannot use --annotate with --docsNz	user-datarzNUnable to read system userdata or vendordata as non-root user. Try using sudo.)�ds_deps�trust)�existing�cloud_configz;Unable to obtain user data file. No instance data availablezvendor-data�vendor_cloud_configzvendor2-data�vendor2_cloud_configzConfig file z does not existz	Error: {})�fmtr�rEz!Found cloud-config data types: %sr6css�|]\}}|VqdSr;r+)r��cfg_typer�r+r+r,r��r�z%handle_schema_args.<locals>.<genexpr>z  r�r`z at �:zInvalid cloud-config z
Error: {}
)r�zValid cloud-config: css�|]}|VqdSr;r+)r�rtr+r+r,r��s�z'Error: Invalid cloud-config schema: {}
)�config_filer��systemr7rrjr�rr�r��getuidr�fetch�paths�	get_ipathr/�existsrLr>r�rrBr(r�r)r�args�exclusive_argsr��config_files�init�
userdata_file�vendor_config_filesr��vendor_file�nested_output_prefix�multi_config_output�error_types�idx�cfg_filer|�cfgr+r+r,�handle_schema_args�s����
�
������
���
��r�cCst�}td|���dS)zDTool to validate schema of a cloud-config file or print schema docs.r�r)r�r��
parse_argsr�r+r+r,�main�sr��__main__)FN)T)NFFTF)F)rVr;)irNr�r��loggingr�r��sysrh�collectionsr�collections.abcr�copyr�	functoolsr�	itertoolsrr*rrr	r
rrr
r�	cloudinitrr�cloudinit.stagesr�cloudinit.utilrrrrSr�_ValidationErrorrTr��	getLoggerr%r��VERSIONED_USERDATA_SCHEMA_FILEr�r*rr~r}r]r^rgrkr��DEPRECATED_PREFIX�typing_extensionsrrrr�r-r.rOr(rArxrBr[rrrLrur�r�r�r�r�r�r�r�rXr
rrr r-r9rCr6rLrUr_rprrr�r�r�r�r�r�r�r�r�r��exitr+r+r+r,�<module>s"$�
�����
�#��
�)����
��
�)�
�/K%������
�Qz������

�
Z
,
*H	>

$X
�