a
    ©!cKT  ã                   @   s<  d dl mZ d dl mZ d dlmZ d dlmZmZ d dlm	Z	m
Z
 d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlZejjjZejjjZejjjZejjj Z!ejjj"Z#ejjj$Z%ejjj&Z'ejjj(Z)ejjj*Z+e ,d¡Z-G dd	„ d	e.ƒZ/G d
d„ de/ƒZ0G dd„ de/ƒZ1dS )é    )Úabsolute_import)Úunicode_literals)Úmisc)ÚucdÚ_)Ú
basestringÚurlparseNÚdnfc                       sŠ   e Zd ZdZddd„Zdd„ Z‡ fdd„Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
efdd„Zefdd„Zdd„ Zefdd„Zedd„ ƒZ‡  ZS )Ú
BaseConfigzlBase class for storing configuration definitions.

       Subclass when creating your own definitions.

    Nc                 C   s   || j d< || _d S )NÚ_config)Ú__dict__Ú_section)ÚselfÚconfigÚsectionÚparser© r   ú3/usr/lib/python3.9/site-packages/dnf/conf/config.pyÚ__init__<   s    
zBaseConfig.__init__c              
   C   s€   d| j vrtd | j|¡ƒ‚t| j|ƒ}|d u r4d S z|ƒ  ¡ }W n& tyh } zW Y d }~d S d }~0 0 t|t	ƒr|t
|ƒS |S )Nr   z!'{}' object has no attribute '{}')r   ÚAttributeErrorÚformatÚ	__class__Úgetattrr   ÚgetValueÚ	ExceptionÚ
isinstanceÚstrr   )r   ÚnameÚoptionÚvalueZexr   r   r   Ú__getattr__@   s    

zBaseConfig.__getattr__c                    s:   t | j|d ƒ}|d u r(tt| ƒ ||¡S |  ||t¡ d S ©N)r   r   Úsuperr
   Ú__setattr__Ú
_set_valueÚPRIO_RUNTIME)r   r   r   r   ©r   r   r   r#   N   s    zBaseConfig.__setattr__c              	   C   sn   g }|  d| j ¡ | jrd| j ¡ D ]>}z|j ¡ }W n tyL   d}Y n0 |  d|j|f ¡ q$d |¡S )Nú[%s]Ú z%s: %sÚ
)	Úappendr   r   ÚoptBindsÚsecondÚgetValueStringÚRuntimeErrorÚfirstÚjoin)r   ÚoutÚoptBindr   r   r   r   Ú__str__U   s    
zBaseConfig.__str__c                 C   s   t | j|d ƒ}|d uS r!   )r   r   ©r   r   Úmethodr   r   r   Ú_has_optiona   s    zBaseConfig._has_optionc                 C   s$   t | j|d ƒ}|d u rd S |ƒ  ¡ S r!   )r   r   r   r4   r   r   r   Ú
_get_valuee   s    zBaseConfig._get_valuec                 C   s$   t | j|d ƒ}|d u rd S |ƒ  ¡ S r!   )r   r   ZgetPriorityr4   r   r   r   Ú_get_priorityk   s    zBaseConfig._get_priorityc              
   C   s  t | j|dƒ}|du r&td| d ƒ‚|ƒ }|du rZz| ||¡ W n tyV   Y n0 n¾zrt|tƒspt|tƒr†| |tj 	|¡¡ nDt|tjj
ƒs¢t|tjjƒr¾t|tƒr¾| |t|ƒ¡ n| ||¡ W nJ ty } z0tjjtdƒ|t|ƒf t|ƒd‚W Y d}~n
d}~0 0 dS )zSSet option's value if priority is equal or higher
           than current priority.NzOption "z" does not existszError parsing '%s': %s©Z	raw_error)r   r   r   Úsetr   ÚlistÚtupleÚlibdnfÚconfÚVectorStringZ
OptionBoolZOptionChildBoolÚintÚboolr.   r	   Ú
exceptionsÚConfigErrorr   r   )r   r   r   Úpriorityr5   r   Úer   r   r   r$   q   s2    ÿþ
ÿþzBaseConfig._set_valuec                 C   sð   |  |¡rì| |¡D ]Ö}| ||¡}|r0|dkr4d}t| j|ƒrªz| j ¡  |¡ ||¡ W qê ty¦ } z2t	 
tdƒt|ƒt|ƒt|ƒt|ƒ¡ W Y d}~qêd}~0 0 q|dkrÊt| |ƒrÊt| ||ƒ qt	 tdƒt|ƒt|ƒt|ƒ¡ qdS )z+Set option values from an INI file section.ÚNoner(   z,Invalid configuration value: %s=%s in %s; %sNÚarchz+Unknown configuration option: %s = %s in %s)Ú
hasSectionÚoptionsZgetSubstitutedValueÚhasattrr   r+   ÚatÚ	newStringr.   ÚloggerÚerrorr   r   r   ÚsetattrÚdebug)r   r   r   ÚfilenamerD   r   r   rE   r   r   r   Ú	_populateŒ   s$    

ÿþzBaseConfig._populatec              	   C   sb   d| j  g}| jrT| j ¡ D ]6}z| d|j|j ¡ f ¡ W q tyP   Y q0 qd |¡d S )z]Return a string representing the values of all the
           configuration options.
        r'   z%s = %sr)   )	r   r   r+   r*   r/   r,   r-   r.   r0   )r   Úoutputr2   r   r   r   Údump¡   s    zBaseConfig.dumpc                 C   sn   | j  ¡ }z| |¡ ||¡ W nH tyh } z0tj tdƒ 	||t
|ƒ¡t
|ƒ¡‚W Y d}~n
d}~0 0 dS )aÔ  For standard options, sets the value of the option if the priority is equal to or higher
           than the current priority.
           For "append" options, appends the values parsed from value_string to the current list of values. If the first
           parsed element of the list of values is empty and the priority is equal to or higher than the current
           priority, the current list is replaced with the new values.
           If the priority is higher than the current priority, the current priority is increased to the priority.
           Raises dnf.exceptions.ConfigError if the option with the given name does not exist or value_string contains
           an invalid value or not allowed value.
        zCannot set "{}" to "{}": {}N)r   r+   rK   rL   r.   r	   rB   rC   r   r   r   )r   r   Zvalue_stringrD   Z	opt_bindsrE   r   r   r   Úset_or_append_opt_value²   s    
ÿz"BaseConfig.set_or_append_opt_valuec                 C   sˆ   t j ¡ }| | ¡ | |¡sD| ¡ D ]}t jj ||¡|kr&|}q&| ¡ D ]*\}}t|t	ƒrhd 
|¡}| |||¡ qL| | d¡ dS )zö
        filename   - name of config file (.conf or .repo)
        section_id - id of modified section (e.g. main, fedora, updates)
        substitutions - instance of base.conf.substitutions
        modify     - dict of modified options
        ú FN)r=   r>   ÚConfigParserÚreadrH   ZgetDataÚ
substituteÚitemsr   r;   r0   ZsetValueÚwrite)rQ   Z
section_idÚsubstitutionsZmodifyr   Zsectr   r   r   r   r   Úwrite_raw_configfileÄ   s    	




zBaseConfig.write_raw_configfile)NNN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r    r#   r3   r6   r7   r8   r%   r$   ÚPRIO_DEFAULTrR   rT   rU   Ústaticmethodr]   Ú__classcell__r   r   r&   r   r
   5   s   
r
   c                       sÖ   e Zd ZdZd%‡ fdd„	Zdd„ Ze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dd„ ƒZejdd„ ƒZedd„ ƒZejdd„ ƒZedd„ ƒZejd d„ ƒZdefd!d"„Zed#d$„ ƒZ‡  ZS )&ÚMainConfz?Configuration option definitions for dnf.conf's [main] section.ÚmainNc              
      s  t j ¡ }tt| ƒ |||¡ |  dtjj	gt
¡ |  dtjjgt
¡ tjj ¡ | _t ¡ | _| j ¡  t
tjj¡ tj ¡ rŒtjj}d}nXzt ¡  }}W nF ttfyâ } z*tdƒ t|ƒ¡}tj |¡‚W Y d }~n
d }~0 0 | j  ¡  t
|¡ | j !¡  t
|¡ g | _"d S )NZ
pluginpathZpluginconfpathz/var/logzCould not set cachedir: {})#r=   r>   Ú
ConfigMainr"   re   r   r$   r	   ÚconstZ
PLUGINPATHrb   ZPLUGINCONFPATHr\   ZSubstitutionsÚhawkeyZdetect_archrG   r   Zsystem_cachedirr:   ZSYSTEM_CACHEDIRÚutilZ	am_i_rootr   ZgetCacheDirÚIOErrorÚOSErrorr   r   r   rB   ÚErrorÚcachedirÚlogdirÚ	tempfiles)r   r   r   r   rn   ro   rE   Úmsgr&   r   r   r   â   s$    


"zMainConf.__init__c                 C   s   | j D ]}t |¡ qd S r!   )rp   ÚosÚunlink)r   Ú	file_namer   r   r   Ú__del__ý   s    
zMainConf.__del__c                 C   sJ   d}|   d¡D ]}tj |¡r|} q(q|sF|   d¡d }tj |¡ |S )zReturns the value of reposdirNZreposdirr   )r7   rr   ÚpathÚexistsr	   rj   Z
ensure_dir)r   Z	myrepodirZrdirr   r   r   Úget_reposdir  s    zMainConf.get_reposdirc           	   
   C   sî   |   |¡}|  |¡}t|tƒrêt |¡}|d dv rF|  ||j|¡ n¤tj 	¡ }t
jdd\}}| j |¡ zpz| d||¡ W n@ tyÀ } z(tj tdƒ |t|ƒ¡¡‚W Y d}~nd}~0 0 |  |||¡ W t |¡ nt |¡ 0 dS )zŽ
        In case the option value is a remote URL, download it to the temporary location
        and use this temporary file instead.
        r   )Úfiler(   zdnf-downloaded-config-)ÚprefixNz9Configuration file URL "{}" could not be downloaded:
  {})r8   r7   r   r   r   r$   rv   r=   ZrepoZ
DownloaderÚtempfileZmkstemprp   r*   ZdownloadURLr.   r	   rB   rC   r   r   r   rr   Úclose)	r   ÚoptnameÚprioÚvalÚlocationZ
downloaderZtemp_fdZ	temp_pathrE   r   r   r   Ú_check_remote_file  s&    




ÿÿzMainConf._check_remote_filec                    s¸   ˆ  d¡‰ ˆ dkrˆ S ˆ |¡}|tkr,ˆ S ˆ  |¡}t|tƒs€t‡ fdd„|D ƒƒr´ˆ |tj 	‡fdd„|D ƒ¡|¡ ˆ S n4t
j t
j ˆ | d¡¡¡r´ˆ |ˆ |¡|¡ ˆ S dS )zœ
        Return root used as prefix for option (installroot or "/"). When specified from commandline
        it returns value from conf.installroot
        Úinstallrootú/c              	   3   s*   | ]"}t j t j ˆ | d ¡¡¡V  qdS )rƒ   N)rr   rv   rw   r0   Úlstrip©Ú.0Úp)r‚   r   r   Ú	<genexpr><  ó    z6MainConf._search_inside_installroot.<locals>.<genexpr>c                    s   g | ]}ˆ   |¡‘qS r   )Ú_prepend_installroot_pathr…   ©r   r   r   Ú
<listcomp>?  r‰   z7MainConf._search_inside_installroot.<locals>.<listcomp>)r7   r8   ÚPRIO_COMMANDLINEr   r   Úanyr$   r=   r>   r?   rr   rv   rw   r0   r„   rŠ   )r   r}   r~   r   r   )r‚   r   r   Ú_search_inside_installroot,  s&    



ýz#MainConf._search_inside_installrootc                 C   s,   |   |¡}|  |  |¡¡}|  |||¡ d S r!   )r8   rŠ   r7   r$   )r   r}   r~   Únew_pathr   r   r   Úprepend_installrootH  s    
zMainConf.prepend_installrootc                 C   s,   t j |  d¡| d¡¡}tjj || j	¡S )Nr‚   rƒ   )
rr   rv   r0   r7   r„   r=   r>   rW   rY   r\   )r   rv   Z	root_pathr   r   r   rŠ   N  s    z"MainConf._prepend_installroot_pathc                 C   s  g d¢}|D ]
}t ||dƒ}|dur|g kr|  |¡räd}| jrnz| j ¡  |¡ ¡ }W n tyl   Y n0 |rÐtjj	}||  
|¡k r’|  
|¡}|D ]6}|rº|  ||  |¡|g |¡ q–|  |g tjj	¡ q–n|  ||tjj	¡ qt| |ƒ rþt| ||ƒ qt tdƒt|ƒt|ƒ¡ qt |ddƒdu r>|  ddtjj	¡ t|dƒr|j ¡ D ]¾\}}|D ]®}	t| j|ƒrÞz| j ¡  |¡ t	|	¡ W nL tyÚ }
 z2tjjtdƒ||	t|
ƒf t|
ƒd	‚W Y d}
~
n
d}
~
0 0 n.t| |ƒrøt| ||	ƒ ntd
ƒ}t ||¡ q`qTdS )z%Configure parts of CLI from the opts )ÚpluginsÚversionÚconfig_file_pathÚ
debuglevelZ
errorlevelr‚   ZbestZ	assumeyesZassumenoZclean_requirements_on_removeÚgpgcheckZshowdupesfromreposr’   Z
ip_resolveZrpmverbosityZdisable_excludesZcolorZdownloadonlyÚexcludeÚexcludepkgsÚskip_brokenZtsflagsrG   ÚbasearchZ
ignorearchZ	cacheonlyZcommentNFú%Unknown configuration option: %s = %sr–   Zlocalpkg_gpgcheckÚmain_setoptsz4Error parsing --setopt with key '%s', value '%s': %sr9   z1Main config did not have a %s attr. before setopt)r   r6   r   r+   rK   ZgetAddValuer.   r	   r>   r   r8   r$   r7   rJ   rO   rM   Úwarningr   r   rœ   rZ   rL   rB   rC   r   )r   ÚoptsZconfig_argsr   r   ZappendValueZadd_priorityÚitemÚvaluesr   rE   rq   r   r   r   Ú_configure_from_optionsR  sX    



ÿÿþz MainConf._configure_from_optionsc                 C   sP   d}|d urL|g krL|   |¡r2|  ||tjj¡ nt tdƒt|ƒt|ƒ¡ d S )Nr˜   r›   )	r6   r$   r	   r>   r   rM   r   r   r   )r   Zpkgsr   r   r   r   Úexclude_pkgs‘  s    

ÿzMainConf.exclude_pkgsc                 C   s(   |   d¡}|r$|  d| |  d¡¡ dS )z Adjust conf options interactionsr™   ÚstrictN)r7   r$   r8   )r   Zskip_broken_valr   r   r   Ú_adjust_conf_optionsœ  s    
zMainConf._adjust_conf_optionsc                 C   s   | j  d¡S ©NÚ
releasever©r\   Úgetr‹   r   r   r   r¦   £  s    zMainConf.releaseverc                 C   s,   |d u r| j  dd ¡ d S t|ƒ| j d< d S r¥   )r\   Úpopr   )r   r   r   r   r   r¦   ¨  s    c                 C   s   | j  d¡S )NrG   r§   r‹   r   r   r   rG   °  s    zMainConf.archc                 C   sb   |d u r| j  dd ¡ d S |tjj ¡ vrFtdƒ}tj | 	d|¡¡‚|| j d< tj 
|¡| _
d S )NrG   úIncorrect or unknown "{}": {})r\   r©   r	   ÚrpmÚ_BASEARCH_MAPÚkeysr   rB   rm   r   rš   ©r   r   rq   r   r   r   rG   µ  s    
c                 C   s   | j  d¡S )Nrš   r§   r‹   r   r   r   rš   Â  s    zMainConf.basearchc                 C   sT   |d u r| j  dd ¡ d S |tjj ¡ vrFtdƒ}tj | 	d|¡¡‚|| j d< d S )Nrš   rª   )
r\   r©   r	   r«   r¬   r    r   rB   rm   r   r®   r   r   r   rš   Ç  s    c              
   C   s¸   |d u r|   d¡}tj ¡ }z| |¡ W nh tyf } z$tj t	dƒ||f ¡‚W Y d }~n6d }~0  t
y’ } zt |¡ W Y d }~n
d }~0 0 |  || j||¡ |  d||¡ d S )Nr”   zParsing file "%s" failed: %s)r7   r=   r>   rW   rX   r.   r	   rB   rC   r   rk   rM   r   rR   r   r$   )r   rQ   rD   r   rE   r   r   r   rX   Ó  s    

, zMainConf.readc                 C   s   |   d¡tjjkS )Nr•   )r7   r	   rh   ZVERBOSE_LEVELr‹   r   r   r   Úverboseã  s    zMainConf.verbose)rf   N)r^   r_   r`   ra   r   ru   Úpropertyrx   r   r   r‘   rŠ   r¡   r¢   r¤   r¦   ÚsetterrG   rš   rb   rX   r¯   rd   r   r   r&   r   re   ß   s6   
?





re   c                       s*   e Zd ZdZd‡ fdd„	Zdd„ Z‡  ZS )ÚRepoConfz4Option definitions for repository INI file sections.Nc                    sP   |r
|j ntj ¡ }tt| ƒ tj |¡||¡ || _|rL| j  	¡  
t|¡ d S r!   )r   r=   r>   rg   r"   r²   r   Z
ConfigRepoZ_mainConfigRefHolderr   r:   rb   )r   Úparentr   r   Z
mainConfigr&   r   r   r   ë  s
    zRepoConf.__init__c                 C   s  t |ddƒdu r,dD ]}|  |dtjj¡ qt |di ƒ}| ¡ D ]Æ\}}t | j|¡sXq@| ¡ D ]¤\}}|D ]–}t| j	|ƒrêz| j	 
¡  |¡ t|¡ W nN tyæ }	 z6tjjtdƒ| j||t|	ƒf t|	ƒd‚W Y d}	~	n
d}	~	0 0 qltdƒ}
t |
| j|¡ qlq`q@dS )	zConfigure repos from the opts. r–   NF)r–   Zrepo_gpgcheckÚrepo_setoptsz7Error parsing --setopt with key '%s.%s', value '%s': %sr9   z-Repo %s did not have a %s attr. before setopt)r   r$   r	   r>   r   rZ   Úfnmatchr   rJ   r   r+   rK   rL   r.   rB   rC   r   r   rM   r   )r   rž   r}   r´   ZrepoidZsetoptsr   r    r   rE   rq   r   r   r   r¡   ô  s*    ÿþz RepoConf._configure_from_options)NN)r^   r_   r`   ra   r   r¡   rd   r   r   r&   r   r²   è  s   	r²   )2Z
__future__r   r   Zdnf.yumr   Zdnf.i18nr   r   Z
dnf.pycompr   r   rµ   Zdnf.conf.substitutionsr	   Z	dnf.constZdnf.exceptionsZdnf.utilri   Zloggingrr   Zlibdnf.confr=   Zlibdnf.repor{   r>   ZOptionZPriority_EMPTYZ
PRIO_EMPTYZPriority_DEFAULTrb   ZPriority_MAINCONFIGZPRIO_MAINCONFIGZPriority_AUTOMATICCONFIGZPRIO_AUTOMATICCONFIGZPriority_REPOCONFIGZPRIO_REPOCONFIGZPriority_PLUGINDEFAULTZPRIO_PLUGINDEFAULTZPriority_PLUGINCONFIGZPRIO_PLUGINCONFIGZPriority_COMMANDLINEr   ZPriority_RUNTIMEr%   Z	getLoggerrM   Úobjectr
   re   r²   r   r   r   r   Ú<module>   s@   









 +  