o
    i                  
   @   s   U d Z ddlZddlZddlZddlmZ ddlmZ ddlm	Z	 ddl
mZ ddlmZ dg d	ed
gdZeed< eeZdd Zdd Zdd Zdd Zdd Zdede	dededdf
ddZdS )z6zypper_add_repo: Add zypper repositories to the system    N)util)Cloud)Config)
MetaSchema)
PER_ALWAYScc_zypper_add_repo)opensusezopensuse-microoszopensuse-tumbleweedzopensuse-leapsle_hpcz	sle-microsleszypper)iddistros	frequencyactivate_by_schema_keysmetac                 C   s   |  dd} | S )N _)replace)repo_id r   E/usr/lib/python3/dist-packages/cloudinit/config/cc_zypper_add_repo.py_canonicalize_id%   s   r   c                 C   sN   t | tr| r	dS dS t | ttfrddd | D S t | ts%t| S | S )N   r   z
    c                 S   s   g | ]}t |qS r   )_format_repo_value).0vr   r   r   
<listcomp>/   s    z&_format_repo_value.<locals>.<listcomp>)
isinstanceboollisttuplejoinstr)valr   r   r   r   *   s   

r   c                 C   sD   t  }i || < | D ]\}}t|||  |< q| }d|S )N
)	configobj	ConfigObjitemsr   writer!   )r   repo_configto_bekr   linesr   r   r   _format_repository_config5   s   
r-   c                 C   s  | sdS i }t | D ]f\}}tddgt|}|r&td|d| q
|d}t|}tj	|d| }tj	
|rFtd|| q
||v rRtd|| q
td	d
 | D }	dD ]
}
|
|	vrid|	|
< q_||	f||< q
| D ]\}}t||d }t|d | qudS )zWrite the user-provided repo definition files
    @param repos: A list of repo dictionary objects provided by the user's
        cloud config.
    @param repo_base_path: The directory path to which repo definitions are
        written.
    Nr   baseurlz;Repo config at index %d is missing required config keys: %s,z%s.repoz)Skipping repo %s, file %s already exists!z*Skipping repo %s, file %s already pending!c                 s   s8    | ]\}}|r|d kr|   dd|fV  qdS )r   -r   N)lowerstripr   )r   r+   r   r   r   r   	<genexpr>i   s    z_write_repos.<locals>.<genexpr>)enabledautorefresh1r   )	enumerateset
differenceLOGwarningr!   getr   ospathexistsinfodictr'   r-   r   
write_file)reposrepo_base_pathvalid_reposindexuser_repo_configmissing_keysr   canon_repo_idrepo_fn_pthr)   field	repo_data	repo_blobr   r   r   _write_reposA   sR   
rO   c                 C   s   | sdS d}t |}dg}|  D ]\}}|dkr&d}|d7 }t| q|r1|d||f  qt|dkr@|d	| }n|}t || dS )
z@Write to the default zypp configuration file /etc/zypp/zypp.confNz/etc/zypp/zypp.confz# Added via cloud.cfg	configdirz5Changing the location of the zypper configuration is z+not supported, skipping "configdir" settingz%s=%sr   r$   )	r   load_text_filer'   r;   r<   appendlenr!   rC   )zypper_configzypp_configzypp_conf_contentnew_settingssettingvaluemsg
new_configr   r   r   _write_zypp_config{   s$   

r\   namecfgcloudargsreturnc                 C   sj   | d}|std|  d S | d}|std|  d S | di }| dd}t| t|| d S )Nr   zBSkipping module named %s, no 'zypper' relevant configuration foundrD   z8Skipping module named %s, no 'repos' configuration foundconfigreposdirz/etc/zypp/repos.d/)r=   r;   debugr\   rO   )r]   r^   r_   r`   zypper_sectionrD   rT   rE   r   r   r   handle   s"   

rf   )__doc__loggingr>   r%   	cloudinitr   cloudinit.cloudr   cloudinit.configr   cloudinit.config.schemar   cloudinit.settingsr   r   __annotations__	getLogger__name__r;   r   r   r-   rO   r\   r"   r   rf   r   r   r   r   <module>   s*   	
:"