HEX
Server: Apache/2.4.65 (Debian)
System: Linux kubikelcreative 5.10.0-35-amd64 #1 SMP Debian 5.10.237-1 (2025-05-19) x86_64
User: www-data (33)
PHP: 8.4.13
Disabled: NONE
Upload Files
File: //usr/lib/python3/dist-packages/cloudinit/config/__pycache__/schema.cpython-39.pyc
a

��`6L�@sLdZddlmZddlmZddlmZmZddlZddl	m
Z
ddlmZddl
Z
ddlZddlZddlZddlZdd	d
d�ZdZd
ZdZdZdZdZdZGdd�de�Zdd�Zd7dd�Zdd�Zd8dd�Zdd�Z d d!�Z!d"d#�Z"d9d%d&�Z#d:d(d)�Z$d*d+�Z%da&d,d-�Z'd.d/�Z(d;d0d1�Z)d2d3�Z*d4d5�Z+e,d6k�rHe�-e+��dS)<zFschema.py: Set of module functions for processing cloud-config schema.�)�read_cfg_paths)�importer)�find_modules�	load_fileN)�defaultdict)�deepcopy�true�false�null)TFNs	UNDEFINEDs
#cloud-configz�
{name}
{title_underbar}
**Summary:** {title}

{description}

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

**Module frequency:** {frequency}

**Supported distros:** {distros}

**Config schema**:
{property_doc}
{examples}
z/{prefix}**{prop_name}:** ({type}) {description}zF{prefix}Each item in **{prop_name}** list supports the following keys:z
**Examples**::

z
    # --- Example{0} ---cs"eZdZdZd�fdd�	Z�ZS)�SchemaValidationErrorz<Raised when validating a cloud-config file against a schema.�cs8||_dd�|D�}d�d�|��}tt|��|�dS)z�Init the exception an n-tuple of schema errors.

        @param schema_errors: An n-tuple of the format:
            ((flat.config.key, msg),)
        cSsg|]\}}d�||��qS�z{0}: {1}��format)�.0Z
config_key�messagerr�9/usr/lib/python3/dist-packages/cloudinit/config/schema.py�
<listcomp>6s�z2SchemaValidationError.__init__.<locals>.<listcomp>zCloud config schema errors: {0}�, N)�
schema_errorsr�join�superr�__init__)�selfrZerror_messagesr��	__class__rrr/s��zSchemaValidationError.__init__)r)�__name__�
__module__�__qualname__�__doc__r�
__classcell__rrrrr,srcCs@zddlm}Wnty$YdS0|j�|d�p>t|tf�S)zWTYPE_CHECKER override allowing bytes for string type

    For jsonschema v. 3.0.0+
    r)�Draft4ValidatorF�string)�
jsonschemar!�ImportError�TYPE_CHECKERZis_type�
isinstance�bytes)Zchecker�instancer!rrr�is_schema_byte_string>s
�r)FcCsz$ddlm}m}ddlm}m}WntyBt�d�YdS0t	|d�rj|j
�dt�}|||d�}n&|j
}	ttf|	d<||j|jd	|	d
�}|||�d�}
d}t|
�|�d
d�d�D]*}d�dd�|jD��}
||
|jff7}q�|�r|r�t|��n dd�|D�}t�dd�|��dS)a�Validate provided config meets the schema definition.

    @param config: Dict of cloud configuration settings validated against
        schema.
    @param schema: jsonschema dict describing the supported schema definition
       for the cloud config module (config.cc_*).
    @param strict: Boolean, when True raise SchemaValidationErrors instead of
       logging warnings.

    @raises: SchemaValidationError when provided config does not validate
        against the provided schema.
    r)r!�
FormatChecker)�create�extendz<Ignoring schema validation. python-jsonschema is not presentNr%r")�type_checkerZdraft4)Zmeta_schemaZ
validators�versionZ
default_types)Zformat_checkerrcSs|jS)N)�path)�errr�<lambda>t�z-validate_cloudconfig_schema.<locals>.<lambda>)�key�.cSsg|]}t|��qSr)�str)r�prrrrur2z/validate_cloudconfig_schema.<locals>.<listcomp>cSsg|]\}}d�||��qSr
r)r�k�msgrrrr{r2zInvalid config:
%s�
)r#r!r*Zjsonschema.validatorsr+r,r$�logging�debug�hasattrr%Zredefiner)Z
DEFAULT_TYPESr5r'ZMETA_SCHEMAZ
VALIDATORS�sortedZiter_errorsrr/rr�warning)�config�schema�strictr!r*r+r,r-ZcloudinitValidator�types�	validator�errors�errorr/�messagesrrr�validate_cloudconfig_schemaKs>
�
��
rGcCsF|s|Si}|rt||�}tt�}g}g}|D]f\}}t�d|�}	|	rf|	��\}
}|t|
��|�nd}|||�|�|dur.dj|
||d�}q.|�	��
d�}d}
t|d�D]r\}}
||}|�rg}|D].}|�d�|
��|�d�|
|��|
d7}
q�|�|
d	d
�|��q�|�|
�q�|�d�d�|���d�|�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 schema_errors: List of tuples from a JSONSchemaValidationError. The
        tuples consist of (schemapath, error_message).
    z&format-l(?P<line>\d+)\.c(?P<col>\d+).*NzLine {line} column {col}: {msg})�line�colr8r9�zE{0}z# E{0}: {1}z		# �,z# Errors: -------------
{0}

)
�_schemapath_for_cloudconfigr�list�re�match�groups�int�appendr�decode�split�	enumerater)�cloudconfig�original_contentrZschemapathsZerrors_by_lineZerror_footerZannotated_contentr/r8rOrHrI�linesZerror_index�line_numberrDZerror_labelrErrr�annotated_cloudconfig_filesJ	��
�rZc

Cs�|dur:t��dkrtd��t�}|�d�}t|dd�}n&tj�|�sTtd�|���t|dd�}|�	t
�s�dd	�|t
���ff}t|�}|r�t
ti||j��|�zt�|�}Wn�tj�y�}	z�d
}
}d}t|	d�r�t|	d�r�t|	d�}n"t|	d��rt|	d��rt|	d�}|�r,|jd
}
|jd
}d
j|
|d�d�|t|	��ff}t|�}|�rlt
ti||j��||	�WYd}	~	n
d}	~	00zt||dd�Wn@t�y�}	z&|�r�t
t|||	j���WYd}	~	n
d}	~	00dS)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.
    Nrz?Unable to read system userdata as non-root user. Try using sudo�userdata_rawF)rSzConfigfile {0} does not existzformat-l1.c1z"File {0} needs to begin with "{1}"rJ�context_mark�problem_markzformat-l{line}.c{col})rHrIzFile {0} is not valid yaml. {1}T)rA)�os�getuid�RuntimeErrorr�
get_ipath_currr/�existsr�
startswith�CLOUD_CONFIG_HEADERrSr�printrZr�yaml�	safe_load�	YAMLErrorr<�getattrrH�columnr5rG)
Zconfig_pathr@�annotate�pathsZuser_data_file�contentrDrErVr0rHrj�markrrr�validate_cloudconfig_file�sn�
��
��


���
�rocCs|���d�}i}d}d}g}t|d�D�]�\}}tt�||���d�}	|��}|r(|�d�rbq(|rt|d\}
}nd}
d}|�d��rd	|d}|r�|�	|�r�|d
t|��}t
|�}
|||
<tt�||dd
����d�}|d7}|	}
|	|7}	||d
�}|d7}nd}|�dd�\}
}|�r4|d|
}
|	|
k�r�|�r�|��\}
}|dk�r�|	|
k�r�d�|�d�d
d��}�q�nd}
d}�q4|�
|	|
f�|�r|��}|�d
��r|�
|	d|
df�tdtt�|���D]}|
dt
|�}|||<�q�|||
<q(|S)z�Return a dictionary mapping schemapath to original_content line number.

    @param config: The yaml.loaded config dictionary of a cloud-config file.
    @param original_content: The simple file content of the cloud-config file
    r9rz^(\s*)rJ�#����z- z.%dN�:r4�[�z.0)rSrTrU�lenrNrOrP�striprc�endswithr5�poprrR�rangerfrg)r?rWZ
content_linesZschema_line_numbersZ
list_indexZRE_YAML_INDENTZscopesrYrHZindent_depthZprevious_depthZpath_prefixZprevious_list_idxr3Zitem_indent�valueZinner_list_indexZlist_keyrrrrL�s` 


rLcCs�|�dt�}|tkr0|�d�r0dd�|dD�}t|t�rDd�|�}|�di�}|�dd�}|�di�D]$}|rx|d7}|d	t|�d
7}qh|r�d�||�S|S)zEReturn a string representing a property type from a given jsonschema.�type�enumcSsg|]}tt�||���qSr)r5�	_YAML_MAP�get)rr7rrrr4sz&_get_property_type.<locals>.<listcomp>�/�itemsrr�oneOf�(�)z
{0} of {1})r�SCHEMA_UNDEFINEDr&rMr�_get_property_typer)Z
property_dictZ
property_typer�Zsub_property_typeZsub_itemrrrr�0s �

r�cCsB|d}t�dd|�}t�dd�|�|�}t�dd�|�|�}|S)a�Parse description from the schema 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 schema.
    @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{}-)rN�subr)�description�prefixZlist_paragraphrrr�_parse_descriptionDs��r��    c	
Cs�|d}g}|�di���D]�\}}|�dd�}|�tj||t|�t||�d��|�d�}|r�t|t�r�|D]}|�t	||d��qnnBt|t
�r�|�d�r�|�tj||d��|d7}|�t	||d��d|vr|�t	||d��qd	�|�S)
zDReturn restructured text describing the supported schema properties.r��
propertiesr�rr)r��	prop_namer|r�r�)r�)r�r�z

)
rr�rR�SCHEMA_PROPERTY_TMPLrr�r�r&rM�_get_property_doc�dict�SCHEMA_LIST_ITEM_TMPLr)	r@r��
new_prefixr�Zprop_keyZprop_configr�r��itemrrrr�Zs8�


��
�r�rrcCsp|�d�}|sdSt}t|�D]L\}}|�d�}dd�|D�}|tkr\|�dt�|d��|d�|�7}q|S)zCReturn restructured text describing the schema examples if present.�examplesrrr9cSsg|]}d�|��qS)z    {0}r)rrHrrrr�r2z(_get_schema_examples.<locals>.<listcomp>rrJ)r�SCHEMA_EXAMPLES_HEADERrUrT�insert�SCHEMA_EXAMPLES_SPACER_TEMPLATErr)r@r�r�Zrst_content�countZexamplerXZindented_linesrrr�_get_schema_examplesxs

�r�cCsXt|�}t|�|d<t|�|d<d�|d�|d<t�dd|d�|d<tjfi|��S)	z�Return reStructured text rendering the provided jsonschema.

    @param schema: Dict of jsonschema to render.
    @raise KeyError: If schema lacks an expected key.
    Zproperty_docr�r�distrosr4�-�nameZtitle_underbar)rr�r�rrNr��SCHEMA_DOC_TMPLr)r@Zschema_copyrrr�get_schema_doc�sr�cCs�trtSddgd�}tj�tj�t��}t|�}|��D]@\}}t�	|dgdg�\}}|r8t�
|d�}|d�|j�q8|a|S)z>Return jsonschema coalesced from all cc_* cloud-config module.z'http://json-schema.org/draft-04/schema#zcloud-config-schema)z$schema�id�allOfzcloudinit.configr@rr�)
�FULL_SCHEMAr^r/�dirname�abspath�__file__rr�r�find_module�
import_modulerRr@)�full_schemaZconfigs_dir�potential_handlersZ_fname�mod_name�mod_locs�_looked_locs�modrrr�
get_schema�s �
�r�cCst|tjd�t�d�dS)N)�filerJ)re�sys�stderr�exit)rrrrrE�srEcCs\|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)�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�c	
Csp|j|j|jg}tdd�|D��dkr.td�t�}|js@|jr�zt|j||j�Wndty�}z|jsvtt	|��WYd}~nXd}~0t
y�}ztt	|��WYd}~n*d}~00|jdur�d}n|j}td|�n�|j�rldd�|d	D�}|d
g7}t|j��
t|��}|�r4td�t|�d�|���|d	D].}d
|jv�s\|d
|jv�r<tt|���q<dS)z@Handle provided schema args and perform the appropriate actions.cSsg|]}|r|�qSrr)r�argrrrr�r2z&handle_schema_args.<locals>.<listcomp>rJz;Expected one of --config-file, --system or --docs argumentsNzsystem userdatazValid cloud-config:cSsg|]}|d�qS)r�r)r�	subschemarrrr�r2r��allz-Invalid --docs value {0}. Must be one of: {1}rr�)Zconfig_fileZdocs�systemrvrEr�rorkrr5r`re�set�
differencerrMrr�)	r��argsZexclusive_argsr�r0Zcfg_nameZ
schema_idsZinvalid_docsr�r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__)F)F)r�)rr)N).rZcloudinit.cmd.develr�	cloudinitrZcloudinit.utilrrr��collectionsr�copyrr:r^rNr�rfr~r�rdr�r�r�r�r��
ValueErrorrr)rGrZrorLr�r�r�r�r�r�r�rEr�r�r�rr�rrrr�<module>sN�
4/
C?


!