o
    UÀ7e¹}  ã                   @   s¦  d dl mZmZ d dlZd dlZd dl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zd dlmZ W n eyF   ejZY nw d dlZzd dlZW n ey^   d dlZY nw d dlmZ d dlmZ zd dlmZ W n ey‚   d dlmZ Y nw d dlmZ d dlZd dlZd dlZd	d
lmZ d dl m!Z!m"Z" d dl#m$Z$ d dl%m&Z& d dl'm(Z( G dd„ de)ƒZ*e+dkrÑe*ƒ Z,e-e, .¡ ƒ e, /d ¡ dS dS )é    )Úabsolute_importÚprint_functionN)Úmd5)Ú	maketrans)ÚNamedTemporaryFile)Úescape)ÚConfigParser)Úgettexté   )ÚAptAuth)ÚSourcesListÚSourceEntry)ÚInvalidShortcutException)Úshortcut_handler)ÚGioc                   @   sì  e Zd ZdZdZeedƒƒ\ZZZ	edede	diZ
dud	d
„Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd „ Zd!d"„ Zd#d$„ Zd%d&„ Zd'd(„ Zd)d*„ Zd+d,„ Zd-d.„ Zd/d0„ Zd1d2„ Zd3d4„ Z d5d6„ Z!d7d8„ Z"d9d:„ Z#d;d<„ Z$d=d>„ Z%d?d@„ Z&dAdB„ Z'dCdD„ Z(dEdF„ Z)dGdH„ Z*dIdJ„ Z+dKdL„ Z,dMdN„ Z-dOdP„ Z.dQdR„ Z/dSdT„ Z0dUdV„ Z1dWdX„ Z2dYdZ„ Z3d[d\„ Z4d]d^„ Z5d_d`„ Z6dvdbdc„Z7dvddde„Z8dwdgdh„Z9didj„ Z:dkdl„ Z;dmdn„ Z<dodp„ Z=dqdr„ Z>e?dxdsdt„ƒZ@dS )yÚSoftwarePropertiesz /usr/share/app-install/channels/z$/etc/update-manager/release-upgradesé   ÚnormalÚltsÚneverNú/c                 C   s¬   |d | _ || _|dkrtj d|¡ |dkrd}|| _|| _tƒ | _t	j
 ¡ | _
g | _d| _|  ¡  |  ¡  |  ¡  g | _t ¡ | _t|d�| _t ¡ | _t | j¡ dS )zœ Provides the core functionality to configure the used software 
        repositories, the corresponding authentication keys and 
        update automation z/etc/popularity-contest.confr   ÚDirNz/usr/share/software-properties/F)Úrootdir)Ú
popconfiler   Úapt_pkgÚconfigÚsetÚoptionsÚdatadirr   ÚsourceslistÚ
aptsourcesÚdistroÚ
get_distroÚseen_serverÚmodified_sourceslistÚreload_sourceslistÚbackup_sourceslistÚbackup_apt_confÚcustom_mirrorsÚqueueÚQueueÚmyqueuer   Úapt_keyr   ÚCancellableÚcancellableÚatexitÚregisterÚwait_for_threads)Úselfr   r   r   © r3   úG/usr/lib/python3/dist-packages/softwareproperties/SoftwareProperties.pyÚ__init__X   s(   


zSoftwareProperties.__init__c                 C   s*   t  ¡ D ]}|jt  ¡ jkr| ¡  qdS )z9 wait for all running threads (PPA key fetchers) to exit N)Ú	threadingÚ	enumerateÚidentÚcurrent_threadÚjoin)r2   Útr3   r3   r4   r1   �   s
   €þz#SoftwareProperties.wait_for_threadsc                 C   s6   i | _ tj ¡ D ]}tj tj| ¡}|| j |< qdS )z$Backup all apt configuration optionsN)Úapt_conf_backupÚsoftwarepropertiesÚCONF_MAPÚkeysr   r   Úfind_i)r2   ÚoptionÚvaluer3   r3   r4   r'   ‡   s
   þz"SoftwareProperties.backup_apt_confc                 C   s:   | j  ¡ D ]}tj tj| t| j | ƒ¡ q|  ¡  dS )z$Restore the stored apt configurationN)	r<   r?   r   r   r   r=   r>   ÚstrÚwrite_config)r2   rA   r3   r3   r4   Úrestore_apt_confŽ   s
   ÿz#SoftwareProperties.restore_apt_confc                 C   sÜ   t j tjd ¡dkrSt j tjd ¡dkrtj d¡rtjS t j tjd ¡dkr8t j tjd ¡dkr8tj	S t j tjd ¡dkrQt j tjd ¡dkrQtj
S dS t j tjd ¡dkrlt j tjd ¡dkrltjS dS )zÊ Parse the apt cron configuration. Try to fit a predefined use case 
        and return it. Special case: if the user made a custom 
        configurtation, that we cannot represent it will return None Ú
autoupdater   Ú
unattendedr
   z/usr/bin/unattended-upgradeÚautodownloadN)r   r   r@   r=   r>   ÚosÚpathÚexistsÚUPDATE_INST_SECÚUPDATE_DOWNLOADÚUPDATE_NOTIFYÚUPDATE_MANUAL©r2   r3   r3   r4   Úget_update_automation_level•   s    
ÿz.SoftwareProperties.get_update_automation_levelc                 C   s  |t jkrtj t jd tdƒ¡ tj t jd tdƒ¡ n`|t jkr<tj t jd tdƒ¡ tj t jd tdƒ¡ nB|t jkrZtj t jd tdƒ¡ tj t jd tdƒ¡ n$tj t jd tdƒ¡ tj t jd tdƒ¡ tj t jd tdƒ¡ |  	¡  dS )zó Set the apt periodic configurtation to the selected 
        update automation level. To synchronize the cache update and the 
        actual upgrading function, the upgrade function, e.g. unattended, 
        will run every day, if enabled. rG   r
   rH   r   rF   N)
r=   rL   r   r   r   r>   rC   rM   rN   Úset_modified_config)r2   Ústater3   r3   r4   Úset_update_automation_level¬   s   


z.SoftwareProperties.set_update_automation_levelc                 C   s>   |t j tjd ¡kst j tjd t|ƒ¡ |  ¡  dS dS )z8Set the interval in which we check for available updatesrF   N)r   r   r@   r=   r>   r   rC   rR   )r2   Údaysr3   r3   r4   Úset_update_intervalÀ   s   þz&SoftwareProperties.set_update_intervalc                 C   s   t j tjd ¡S )z3 Returns the interval of the apt periodic cron job rF   )r   r   r@   r=   r>   rP   r3   r3   r4   Úget_update_intervalÇ   s   z&SoftwareProperties.get_update_intervalc                 C   sl   t j | j¡s
| jS tƒ }| | j¡ | dd¡r3| dd¡ 	¡ }| j
 ¡ D ]\}}||kr2|  S q&| jS )z�
    return the release upgrade policy:
     RELEASE_UPGRADES_NORMAL,
     RELEASE_UPGRADES_LTS,
     RELEASE_UPGRADES_NEVER
    ÚDEFAULTÚPrompt)rI   rJ   rK   ÚRELEASE_UPGRADES_CONFÚRELEASE_UPGRADES_NORMALr   ÚreadÚ
has_optionÚgetÚlowerÚrelease_upgrades_policy_mapÚitems)r2   ÚparserÚtypeÚkÚvr3   r3   r4   Úget_release_upgrades_policyË   s   ÿz.SoftwareProperties.get_release_upgrades_policyc              	   C   s,  t j | j¡s*t| jdƒ�}| d| j|  ¡ W d  ƒ dS 1 s#w   Y  dS t| jdƒ�\}tdd��>}|D ]}| ¡ }| 	¡  
d¡rQ| d	| j|  ¡ q9| |d
 ¡ q9| ¡  t | j|j¡ t |j| j¡ W d  ƒ n1 sww   Y  W d  ƒ dS W d  ƒ dS 1 s�w   Y  dS )z
    set the release upgrade policy:
     RELEASE_UPGRADES_NORMAL,
     RELEASE_UPGRADES_LTS,
     RELEASE_UPGRADES_NEVER
     Úwz[DEFAULT]
Prompt=%s
NTÚrzw+)ÚmodeÚpromptz
Prompt=%s
Ú
)rI   rJ   rK   rZ   ÚopenÚwriter`   r   Ústripr_   Ú
startswithÚflushÚshutilÚcopymodeÚnameÚcopy)r2   ÚiÚfÚoutÚliner3   r3   r4   Úset_release_upgrades_policyÞ   s,   
ÿþ(÷
ö
ö
z.SoftwareProperties.set_release_upgrades_policyc              	   C   sš   t j | j¡rKt| jƒ�}| ¡  d¡}W d  ƒ n1 sw   Y  |D ]%}z| d¡\}}|dkr@| d¡ ¡ dkr@W  dS W q% t	yJ   Y q%w dS )	z½ Will return True if the user wants to participate in the popularity 
        contest. Otherwise it will return False. Special case: if no 
        popcon is installed it will return False rk   Nú=ÚPARTICIPATEú"ÚyesTF)
rI   rJ   rK   r   rl   r\   Úsplitrn   r_   Ú
ValueError)r2   rv   Úlinesrx   ÚkeyrB   r3   r3   r4   Úget_popcon_participationö   s   ÿ€ÿz+SoftwareProperties.get_popcon_participationc                    s  |dkrd‰ nd‰ t j | j¡r0t| jdƒ�}‡ fdd„|D ƒ}W d  ƒ n1 s*w   Y  n;tƒ }tdd	ƒ�}| | d
¡¡ W d  ƒ n1 sKw   Y  | ¡ }g }| 	d| ¡ | 	dt
ˆ ƒ ¡ | 	d¡ t| jdƒ�}| |¡ W d  ƒ dS 1 s‚w   Y  dS )z? Enable or disable the participation in the popularity contest Tr}   Únorh   c                    s   g | ]}t  d dˆ  |¡‘qS )z^(PARTICIPATE=)(".+?")z\1"%s")ÚreÚsub)Ú.0rx   ©rB   r3   r4   Ú
<listcomp>  s    ÿz@SoftwareProperties.set_popcon_pariticipation.<locals>.<listcomp>Nz/dev/urandomÚrbi   zMY_HOSTID="%s"
zPARTICIPATE="%s"
zUSE_HTTP="yes"
rg   )rI   rJ   rK   r   rl   r   Úupdater\   Ú	hexdigestÚappendrC   Ú
writelines)r2   Ú
is_helpfulr   r€   Úmrv   Úidr3   r‡   r4   Úset_popcon_pariticipation  s,   
ÿÿ€ÿ
"ÿz,SoftwareProperties.set_popcon_pariticipationc                 C   s  t | jjƒdk rd| j_dS d| j_i }g }| | jj¡ | | jj¡ |D ]}|j|v r<|jD ]
}||j  	|¡ q0q&t
|jƒ||j< q&t | jjƒdkrU| jj|| jj< | jjD ]0}|j|vs‚|j|v r‰t t
||j ƒt
|jƒA ƒdks‰t t
|jƒ| jjA ƒdks‰d| j_ dS qYdS )z²Return True if all distro componets are also available as 
       source code. Otherwise return Flase. Special case: If the
       configuration cannot be represented return Noner
   FTr   N)Úlenr!   Úsource_code_sourcesÚget_source_codeÚextendÚmain_sourcesÚchild_sourcesÚtemplateÚcompsÚaddr   Úcdrom_sourcesÚcdrom_compsÚsource_templateÚenabled_comps)r2   Ú	templatesÚsourcesÚsourceÚcompr3   r3   r4   Úget_source_code_state  s2   

ÿ

 €z(SoftwareProperties.get_source_code_statec                 C   s†   d|j fd|jfd|j fd|j ffD ]\}}td||f ƒ q|jr=d|jjfd|jjffD ]\}}td||f ƒ q0tdƒ d	S )
z4Print the data of a source entry to the command linezURI:zComps:zEnabled:zValid:z %s %sz	MatchURI:zBaseURI:rk   N)Úurir™   ÚdisabledÚinvalidÚprintr˜   Ú	match_uriÚbase_uri)r2   r¡   ÚlabelrB   r3   r3   r4   Úprint_source_entryF  s   

ý

ÿz%SoftwareProperties.print_source_entryc                 C   s,   t dƒ | jD ]}t | ¡ ƒ qt dƒ dS )zPrint the complete sources.listzSTART SOURCES.LIST:zEND SOURCES.LIST
N)r§   r   rC   ©r2   r¡   r3   r3   r4   Úmassive_debug_outputS  s   
z'SoftwareProperties.massive_debug_outputc                 C   s    || j _| j  |¡}|  ¡  |S )z! change the main download server )r!   Údefault_serverÚchange_serverÚset_modified_sourceslist)r2   ÚserverÚresr3   r3   r4   Úchange_main_download_serverZ  s   z.SoftwareProperties.change_main_download_serverc                 C   ó   | j  |¡ |  ¡  dS )z Enable a component of the distroN)r!   Úenable_componentr°   ©r2   r¢   r3   r3   r4   rµ   a  ó   z#SoftwareProperties.enable_componentc                 C   r´   )z!Disable a component of the distroN)r!   Údisable_componentr°   r¶   r3   r3   r4   r¸   f  r·   z$SoftwareProperties.disable_componentc                 C   s&   | j jjD ]}|j|kr|  S qd S ©N)r!   r�   Úchildrenrs   )r2   rs   r˜   r3   r3   r4   Ú_find_template_from_stringk  s
   
ÿÿz-SoftwareProperties._find_template_from_stringc                 C   sh   t |tƒr
|  |¡}| jjD ]}|j|kr| j |¡ q| jjD ]}|j|kr-| j |¡ q |  	¡  dS )ú7Enable a child repo of the distribution main repositoryN)
Ú
isinstancerC   r»   r!   r—   r˜   r   Úremover“   r°   )r2   r˜   r¡   r3   r3   r4   Údisable_child_sourcep  s   


€
€z'SoftwareProperties.disable_child_sourcec                 C   sL   t |tƒr
|  |¡}|jdkr| jj}n|j}| jj||jd� |  ¡  dS )r¼   N)r¤   Údist)	r½   rC   r»   r©   r!   r®   Ú
add_sourcers   r°   )r2   r˜   Ú	child_urir3   r3   r4   Úenable_child_source}  s   



z&SoftwareProperties.enable_child_sourcec                 C   sF   g }|  | jj¡ |  | jj¡ | jjD ]}| j |¡ q|  ¡  dS )z%Remove all distro source code sourcesN)r•   r!   r–   r—   r“   r   r¾   r°   ©r2   r    r¡   r3   r3   r4   Údisable_source_code_sources‹  s   z.SoftwareProperties.disable_source_code_sourcesc                 C   sÂ   g }|  | jj¡ |  | jj¡ | jjD ]}| j |¡ q|D ]}| j d|j|j	|j
d| jj |¡d |j¡ q| jjD ]}| j d| jjj| jjj|j
d| jj |¡d |j¡ q=|  ¡  dS )z0Enable source code source for all distro sourcesúdeb-srczAdded by software-propertiesr
   N)r•   r!   r–   r—   r“   r   r¾   rš   r¤   rÀ   r™   ÚlistÚindexÚfiler›   r�   r©   rs   r°   rÄ   r3   r3   r4   Úenable_source_code_sources•  s0   úúz-SoftwareProperties.enable_source_code_sourcesc                 C   s4   g | _ | jjD ]}t|j|jd�}| j  |¡ qdS )z+Store a backup of the source.list in memory©rx   rÉ   N)Úsourceslist_backupr   rÇ   r   rx   rÉ   rŒ   )r2   r¡   Ú
source_bkpr3   r3   r4   r&   ±  s
   þz%SoftwareProperties.backup_sourceslistc                 C   s.   |   ¡  | jjD ]}t|ƒ|kr|  S qd S r¹   )r%   r   rÇ   rC   )r2   rx   r¡   r3   r3   r4   Ú_find_source_from_string¸  s   ÿz+SoftwareProperties._find_source_from_stringc                 C   s*   t |tƒr
|  |¡}|j |_|  ¡  dS )z&Enable or disable the selected channelN)r½   rC   rÎ   r¥   r°   r¬   r3   r3   r4   Útoggle_source_useÂ  s   


z$SoftwareProperties.toggle_source_usec                 C   sT   t | jjƒD ]!\}}t|ƒ|kr'| jj| j}t||ƒ| jj|< |  ¡   dS qdS )NTF)r7   r   rÇ   rC   rÉ   r   r°   )r2   Ú	old_entryÚ	new_entryrÈ   ÚentryrÉ   r3   r3   r4   Úreplace_source_entryÊ  s   üz'SoftwareProperties.replace_source_entryc                 C   s   |   ¡  |  ¡  dS )zNRevert all settings to the state when software-properties 
       was launchedN)rE   Úrevert_sourceslistrP   r3   r3   r4   ÚrevertÔ  s   zSoftwareProperties.revertc                 C   sF   g | j _| jD ]}t|j|jd�}| j j |¡ q|  ¡  |  ¡  dS )z6Restore the source list from the startup of the dialogrË   N)	r   rÇ   rÌ   r   rx   rÉ   rŒ   Úsave_sourceslistr%   )r2   r¡   Úsource_resetr3   r3   r4   rÔ   Û  s   
z%SoftwareProperties.revert_sourceslistc                 C   s0   d| _ | jr| jjr|  ¡  |  ¡  |  ¡  dS )zCThe sources list was changed and now needs to be saved and reloadedTN)r$   r   Úmassive_debugr­   rÖ   r%   rP   r3   r3   r4   r°   ä  s
   z+SoftwareProperties.set_modified_sourceslistc                 C   s   |   ¡  dS )z+Write the changed apt configuration to fileN)rD   rP   r3   r3   r4   rR   ì  s   z&SoftwareProperties.set_modified_configc                 C   sV  |j dkr`|jr=dt|jƒ ¡ |jf }t|jƒdkr'|jD ]}|d| 7 }q|jdv r4|dtdƒ 7 }|d|j	 7 }|S d|j	|jf }|jD ]}|d| 7 }qH|jdv r^|dtdƒ 7 }|S d	|j j
 }|jdv rs|d
tdƒ 7 }|jr}|d|j 7 }|j jdkr©|jD ]"}|j  |¡r¢|j jD ]}|j|kr |d|j
 7 }q’q†|d| 7 }q†|S )z5Render a nice output to show the source in a treeviewNz<b>%s</b> %sr
   z %s)rÆ   zrpm-srcz(Source Code)z
%sz<b>%s %s</b>z	<b>%s</b>z (%s)zSource CodeF)r˜   Úcommentr   rn   rÀ   r’   r™   rc   Ú_r¤   ÚdescriptionÚchildÚhas_componentÚ
componentsrs   )r2   r¡   ÚcontentsÚcr¢   r3   r3   r4   Úrender_sourceð  sB   
ÿ

û




€þz SoftwareProperties.render_sourcec                 C   s‚   d}d}d}d}t ddƒ}|jr d}|jj}|jj| jkrd}nd|j|j|jf }|jr/d}|j	 
d¡r7d}||||| |¡fS )z&extract attributes to sort the sourcesr
   Ú
0123456789Ú
9876543210r   z%s %s %sÚsrc)r   r˜   rÛ   Údistributionr!   r¤   rÀ   r™   rÙ   rc   ÚfindÚ	translate)r2   r¡   Úcur_sysÚhas_templateÚhas_commentÚ	is_sourceÚrevert_numbersÚdescr3   r3   r4   Úget_comparable  s&   
€ÿz!SoftwareProperties.get_comparablec                 C   sb   g }| j jD ](}|js.|| jjvr.|| jjvr.|| jjvr.|| jjvr.|| jjvr.| 	|¡ q|S )z>Return a list of sources that are not part of the distribution)
r   rÇ   r¦   r!   r–   r›   r—   Údisabled_sourcesr“   rŒ   )r2   Úisv_sourcesr¡   r3   r3   r4   Úget_isv_sources,  s   
€z"SoftwareProperties.get_isv_sourcesc                 C   s   | j jS )z-Return the list of CDROM based distro sources)r!   r›   rP   r3   r3   r4   Úget_cdrom_sources9  s   z$SoftwareProperties.get_cdrom_sourcesc                 C   s   |j | jjv dfS )z®Return a tuple: the first value describes if a component is enabled
       in the Internet repositories. The second value describes if the
       first value is inconsistent.F)rs   r!   Údownload_compsr¶   r3   r3   r4   Úget_comp_download_state=  s   z*SoftwareProperties.get_comp_download_statec                 C   s|   g }| j jD ]}|j|kr| |j¡ qt|ƒdkr(t| j jt|ƒA ƒdkr(dS t|ƒdkr<t| j jt|ƒA ƒdkr<dS dS )zÌReturn a tuple: the first value describes if a component is enabled
       in one of the child source that matcth the given template. 
       The second value describes if the first value is inconsistent.r   )TF)FT)FF)r!   r—   r˜   r•   r™   r’   rž   r   )r2   r˜   r™   rÜ   r3   r3   r4   Úget_comp_child_stateD  s   
€z'SoftwareProperties.get_comp_child_statec                 C   s"   | j  ¡  g | _| j | j ¡ d S r¹   )r   ÚrefreshÚsourceslist_visibler!   Úget_sourcesrP   r3   r3   r4   r%   Y  s   
z%SoftwareProperties.reload_sourceslistc              
   C   sœ  | j d | j d | j d g}|D ]
}tj |¡r n!qtdƒ t|d dƒ�}| d¡ W d  ƒ n1 s6w   Y  d	}tj |¡r\t |¡tj	 }|tj
@ s\td
| ƒ t |d¡ |D ]m}g }tj |¡rËt|dƒ�}| ¡ }W d  ƒ n1 s|w   Y  tj d¡}t|dƒ�7}|D ]}| ¡ D ]}	| d|	 ¡dkr¢ nq•| |¡ q�| ¡ D ]}
| d|
| |
¡f ¡ q­W d  ƒ n1 sÆw   Y  q^dS )z+Write the current apt configuration to filez/etc/apt/apt.conf.d/10periodicz#/etc/apt/apt.conf.d/20auto-upgradesz+/etc/apt/apt.conf.d/15adept-periodic-updatezNo config found, creating oner   rg   Ú Nz/etc/cron.daily/aptz file '%s' not executable, fixingií  rh   zAPT::PeriodiczAPT::Periodic::%szAPT::Periodic::%s "%s";
)r   rI   rJ   Úisfiler§   rl   rm   rK   ÚstatÚST_MODEÚS_IXUSRÚchmodÚ	readlinesr   r   ÚsubtreerÇ   ræ   r@   )r2   Ú	conffilesrv   ÚacÚpermÚperiodicÚcontentÚcnfrx   r�   ru   r3   r3   r4   rD   ^  sN   þÿÿ

ÿÿ
€ÿ÷€÷zSoftwareProperties.write_configc                 C   s   | j  d¡ | j  ¡  dS )zRBackup the existing sources.list files and write the current 
       configurationz.saveN)r   ÚbackupÚsaverP   r3   r3   r4   rÖ   Œ  s   z#SoftwareProperties.save_sourceslistc              	   C   s¬   t |ƒ}tj | j¡rTt d| j ¡D ]@}t|ƒ�2}|D ]'}| ¡  d¡r&q|t |ƒkrCtj 	tj 
|¡¡d   W  d  ƒ   S qW d  ƒ n1 sNw   Y  qdS )zs
    helper that checks if a given line is in the source list
    return the channel name or None if not found
    z	%s/*.listú#r   N)r   rI   rJ   rK   ÚCHANNEL_PATHÚglobrl   rn   ro   ÚsplitextÚbasename)r2   ÚsrclineÚsrcentryÚfilenamerv   rx   r3   r3   r4   Ú_is_line_in_whitelisted_channel’  s   
ûýÿ€z2SoftwareProperties._is_line_in_whitelisted_channelc                 C   s8   |  ¡ j}|  |¡}|rd| j|f }|  |¡ dS dS )z•
    helper that adds the gpg key of the channel to the apt
    keyring *if* the channel is in the whitelist
    /usr/share/app-install/channels
    z	%s/%s.keyTF)r   rx   r  r
  Úadd_key)r2   Úshortcutr  ÚchannelÚkeypr3   r3   r4   Ú*check_and_add_key_for_whitelisted_shortcut¢  s   


z=SoftwareProperties.check_and_add_key_for_whitelisted_shortcutc                 C   s   dS )z) abstract interface to keep the UI alive Nr3   rP   r3   r3   r4   Úupdate_interface°  s    z#SoftwareProperties.update_interfacec                 C   s\   |  d¡s|S | ¡ d }z
| dd¡d }W n ty"   d}Y nw d|| jj|f }|S )zÍ
    short cut - this:
      apt-add-repository http://packages.medibuntu.org free non-free
    same as
      apt-add-repository 'deb http://packages.medibuntu.org/ '$(lsb_release -cs)' free non-free'
    Úhttpr   ú r
   Úmainzdeb %s %s %s)ro   r~   Ú
IndexErrorr!   Úcodename)r2   rx   ÚrepoÚareasr3   r3   r4   Úexpand_http_line³  s   
ÿz#SoftwareProperties.expand_http_lineFc                 C   s2   zt | ¡ ƒ}W n
 ty   Y dS w |  ||¡S )z*
    Add a source for the given line.
    F)r   rn   r   Úadd_source_from_shortcut)r2   rx   Úenable_source_coder  r3   r3   r4   Úadd_source_from_lineÄ  s   ÿz'SoftwareProperties.add_source_from_linec              	   C   sò   |  ¡ j}|j}|  |¡}|rdnd}||dd…  }t ||ƒ}t ||ƒ}|js+|jr-dS |  |¡s6| ¡  | jj|j	|j
|j|j|j|j|jd� | jj||j
|j|j|j|j|jd� |  ¡  | jrw| jjrwddl}	|	 ¡ }
|
j|jd� d	S )
z‹
    Add a source with the given shortcut and add the signing key if the
    site is in whitelist or the shortcut implementer adds it.
    rÆ   z	# deb-srcr   NF)rÙ   rÉ   Úarchitecturesr   )Úsources_listT)r   rx   Úsourceparts_filer  r¦   r  r  r   rš   rc   r¤   rÀ   r™   rÙ   rÉ   r#  r°   r   rŠ   ÚaptÚCache)r2   r  r!  Údeb_linerÉ   Údebsrc_entry_typeÚdebsrc_lineÚnew_deb_entryÚnew_debsrc_entryr&  Úcacher3   r3   r4   r   Ï  sB   





úúz+SoftwareProperties.add_source_from_shortcutTc                 C   sî   |r:t |tƒr|  |¡}|  |d¡ dS |dur8|  |d¡ t |¡}d|_d|_|  |d¡ d|_|  |d¡ dS t |tƒrD|  |¡}|du rJdS |jtj	 
d¡kr_| jj td|jd�¡ z| j |¡ W n	 typ   Y nw |  ¡  dS )zRemove the given sourceTNFrÆ   zDir::Etc::sourcelistrù   )rÉ   )r½   rC   rÎ   Úremove_sourcert   rc   r¥   rÉ   r   r   Ú	find_filer   rÇ   rŒ   r   r¾   r   r°   )r2   r¡   Úremove_source_coder3   r3   r4   r.  õ  s4   

ö


ûz SoftwareProperties.remove_sourcec                 C   s0   t j |¡sdS z	| j |¡}|W S    Y dS )z7Add a gnupg key to the list of trusted software vendorsF)rI   rJ   rK   r,   rš   )r2   rJ   r²   r3   r3   r4   r    s   zSoftwareProperties.add_keyc                 C   s,   t  ¡ }| | d¡¡ | ¡  |  |j¡S )z<Add a gnupg key from a utf-8 data string (e.g. copy-n-paste)zutf-8)Útempfiler   rm   Úencoderp   r  rs   )r2   ÚkeydataÚtmpr3   r3   r4   Úadd_key_from_data'  s   z$SoftwareProperties.add_key_from_datac                 C   s    z	| j  |¡ W dS    Y dS )z<Remove a gnupg key from the list of trusted software vendorsTF)r,   Úrm)r2   Úkeyidr3   r3   r4   Ú
remove_key.  s
   zSoftwareProperties.remove_keyc                 C   s   z| j  ¡  W dS    Y dS )z Run apt-key update TF)r,   rŠ   rP   r3   r3   r4   Úupdate_keys6  s
   
zSoftwareProperties.update_keysc                 C   sX   t |tjƒsJ ‚|j}|r|j}|j}nt |¡}| |¡}|j}|j}d|j||f S )z" Return the PackageKit package id z	%s;%s;%s;)	r½   r   ÚPackageÚcurrent_verÚver_strÚarchÚDepCacheÚget_candidate_verrs   )r2   Ú	apt_cacheÚpkgÚcur_verÚverr=  ÚdepcacheÚ	candidater3   r3   r4   Úget_package_id>  s   

z!SoftwareProperties.get_package_idc           
   	   C   sx   t  | ¡}| |¡}g }z"|j d¡D ]}|D ]\}}}	| |¡dkr*| | | ¡ qqW |S  ttfy;   g  Y S w )zF Get the package dependencies, which can be filtered out by a pattern ÚDependséÿÿÿÿ)	r   r>  r?  Údepends_list_strr^   ræ   rŒ   ÚKeyErrorÚ	TypeError)
r@  ÚpackageÚpatternrD  rE  ÚdependenciesÚdep_listÚdep_nameÚdep_verÚdep_opr3   r3   r4   Úget_dependenciesL  s   

€þÿýÿz#SoftwareProperties.get_dependencies)NNr   )F)Tr¹   )AÚ__name__Ú
__module__Ú__qualname__r
  rZ   rÇ   Úranger[   ÚRELEASE_UPGRADES_LTSÚRELEASE_UPGRADES_NEVERr`   r5   r1   r'   rE   rQ   rT   rV   rW   rf   ry   r‚   r‘   r£   r«   r­   r³   rµ   r¸   r»   r¿   rÃ   rÅ   rÊ   r&   rÎ   rÏ   rÓ   rÕ   rÔ   r°   rR   rá   rî   rñ   rò   rô   rõ   r%   rD   rÖ   r  r  r  r  r"  r   r.  r  r5  r8  r9  rF  ÚstaticmethodrS  r3   r3   r3   r4   r   E   s„    
üý
)'


	'.


&(
r   Ú__main__)0Ú
__future__r   r   r   rt   Úhashlibr   r„   rI   r  rq   r6   r/   r1  Ústringr   ÚImportErrorrC   rû   r)   r*   r   Úxml.sax.saxutilsr   Úconfigparserr   r	   rÚ   r    Úaptsources.distror=   r   Úaptsources.sourceslistr   r   Ú"softwareproperties.shortcuthandlerr   Úsoftwareproperties.shortcutsr   Úgi.repositoryr   Úobjectr   rT  Úspr§   rf   ry   r3   r3   r3   r4   Ú<module>   sd   
ÿÿÿ      ý