U
    ÞËd9  ã                   @   sØ  d dl mZmZ d dl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k
r–   ejZY nX d dlZzd dlZW n ek
rÈ   d dlZY nX d dlmZ d dlmZ zd dlmZ W n" ek
r   d dlmZ Y nX 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' d dl(m)Z) e&j*e'j*e%j*gZ+G dd„ de,ƒZ-dd„ Z*e.dkrÔe-ƒ Z/e0e/ 1¡ ƒ e/ 2d ¡ dS )é    )Úabsolute_importÚprint_functionN)Úmd5)Ú	maketrans)ÚNamedTemporaryFile)Úescape)ÚConfigParser)Úgettexté   )ÚAptAuth)ÚSourcesListÚSourceEntry)Ú	shortcuts)Úppa)Úcloudarchive)ÚGioc                   @   sô  e Zd ZdZdZeedƒƒ\ZZZ	edede	diZ
dwd	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`„ Z6dadb„ Z7dxddde„Z8dydfdg„Z9dzdidj„Z:dkdl„ Z;dmdn„ Z<dodp„ Z=dqdr„ Z>dsdt„ Z?e@d{dudv„ƒZAdS )|ÚSoftwarePropertiesz /usr/share/app-install/channels/z$/etc/update-manager/release-upgradesé   ZnormalZltsÚneverNú/c                 C   s¬   |d | _ || _|dkr&tj d|¡ |dkr2d}|| _|| _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   ZDirNz/usr/share/software-properties/F)Úrootdir)Ú
popconfiler   Úapt_pkgÚconfigÚsetÚoptionsÚdatadirr   ÚsourceslistÚ
aptsourcesÚdistroZ
get_distroZseen_serverÚmodified_sourceslistÚreload_sourceslistÚbackup_sourceslistÚbackup_apt_confZcustom_mirrorsÚqueueÚQueueÚmyqueuer   Úapt_keyr   ZCancellableZcancellableÚatexitÚregisterÚwait_for_threads)Úselfr   r   r   © r,   úG/usr/lib/python3/dist-packages/softwareproperties/SoftwareProperties.pyÚ__init__`   s(    


zSoftwareProperties.__init__c                 C   s0   t  ¡ D ]"}|jt  ¡ jkr|js| ¡  qdS )z9 wait for all running threads (PPA key fetchers) to exit N)Ú	threadingÚ	enumerateZidentZcurrent_threadZdaemonÚjoin)r+   Útr,   r,   r-   r*   ‰   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)r+   ÚoptionÚvaluer,   r,   r-   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)	r3   r6   r   r   r   r4   r5   ÚstrÚwrite_config)r+   r8   r,   r,   r-   Úrestore_apt_conf—   s
    ÿz#SoftwareProperties.restore_apt_confc                 C   sâ   t j tjd ¡dkr¨t j tjd ¡dkr>tj d¡r>tjS t j tjd ¡dkrpt j tjd ¡dkrptj	S t j tjd ¡dkr¢t j tjd ¡dkr¢tj
S dS n6t j tjd ¡dkrÚt j tjd ¡dkrÚtjS d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   r7   r4   r5   ÚosÚpathÚexistsÚUPDATE_INST_SECÚUPDATE_DOWNLOADÚUPDATE_NOTIFYZUPDATE_MANUAL©r+   r,   r,   r-   Ú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rxt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ƒ¡ nHt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. r>   r
   r?   r   r=   N)
r4   rC   r   r   r   r5   r:   rD   rE   Úset_modified_config)r+   Ústater,   r,   r-   Úset_update_automation_levelµ   s    


z.SoftwareProperties.set_update_automation_levelc                 C   s:   |t j tjd ¡ks6t j tjd t|ƒ¡ |  ¡  dS )z8Set the interval in which we check for available updatesr=   N)r   r   r7   r4   r5   r   r:   rH   )r+   Zdaysr,   r,   r-   Ú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 r=   )r   r   r7   r4   r5   rF   r,   r,   r-   Úget_update_intervalÐ   s    z&SoftwareProperties.get_update_intervalc                 C   sl   t j | j¡s| jS tƒ }| | j¡ | dd¡rf| dd¡ 	¡ }| j
 ¡ D ]\}}||krL|  S qL| jS )z
    return the release upgrade policy:
     RELEASE_UPGRADES_NORMAL,
     RELEASE_UPGRADES_LTS,
     RELEASE_UPGRADES_NEVER
    ZDEFAULTZPrompt)r@   rA   rB   ÚRELEASE_UPGRADES_CONFÚRELEASE_UPGRADES_NORMALr   ÚreadZ
has_optionÚgetÚlowerÚrelease_upgrades_policy_mapÚitems)r+   ÚparserÚtypeÚkÚvr,   r,   r-   Úget_release_upgrades_policyÔ   s    
z.SoftwareProperties.get_release_upgrades_policyc                 C   s¸   t j | j¡s2t| jdƒ}| d| j|  ¡ dS t| jdƒ}tdd}|D ]>}| ¡ }| 	¡  
d¡r|| d| j|  ¡ qL| |d	 ¡ qL| ¡  t | j|j¡ t |j| j¡ dS )
z
    set the release upgrade policy:
     RELEASE_UPGRADES_NORMAL,
     RELEASE_UPGRADES_LTS,
     RELEASE_UPGRADES_NEVER
     Úwz[DEFAULT]
Prompt=%s
TÚrzw+)ÚmodeÚpromptz
Prompt=%s
Ú
)r@   rA   rB   rM   ÚopenÚwriterR   r   ÚstriprQ   Ú
startswithÚflushÚshutilZcopymodeÚnameÚcopy)r+   ÚiÚfÚoutÚliner,   r,   r-   Úset_release_upgrades_policyç   s    
z.SoftwareProperties.set_release_upgrades_policyc              	   C   s†   t j | j¡r~t| jƒ ¡  d¡}d}|D ]N}z0| d¡\}}|dkr\| d¡ ¡ dkr\d}W q* t	k
rv   Y q*Y q*X q*|S dS 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 r]   Fú=ZPARTICIPATEú"ÚyesTN)
r@   rA   rB   r   r^   rO   Úsplitr`   rQ   Ú
ValueError)r+   ÚlinesZactiveri   Úkeyr9   r,   r,   r-   Úget_popcon_participation   s    z+SoftwareProperties.get_popcon_participationc              	      s´   |dkrd‰ nd‰ t j | j¡rLt| jdƒ}‡ fdd„|D ƒ}W 5 Q R X nRtƒ }| tddƒ d	¡¡ | ¡ }g }| 	d
| ¡ | 	dt
ˆ ƒ ¡ | 	d¡ t| jdƒ |¡ dS )z? Enable or disable the participation in the popularity contest Trm   ZnorZ   c                    s   g | ]}t  d dˆ  |¡‘qS )z^(PARTICIPATE=)(".+?")z\1"%s")ÚreÚsub)Ú.0ri   ©r9   r,   r-   Ú
<listcomp>  s   ÿz@SoftwareProperties.set_popcon_pariticipation.<locals>.<listcomp>z/dev/urandomÚrbi   zMY_HOSTID="%s"
zPARTICIPATE="%s"
zUSE_HTTP="yes"
rY   N)r@   rA   rB   r   r^   r   ÚupdaterO   Z	hexdigestÚappendr:   Ú
writelines)r+   Z
is_helpfulr   rp   ÚmÚidr,   rv   r-   Ú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|krx|jD ]}||j  	|¡ q`qLt
|jƒ||j< qLt | jjƒdkrª| jj|| jj< | jjD ]b}|j|ks|j|kr²t t
||j ƒt
|jƒA ƒdks²t t
|jƒ| jjA ƒdks²d| j_ dS q²d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_sourcesZget_source_codeÚextendÚmain_sourcesÚchild_sourcesÚtemplateÚcompsÚaddr   Úcdrom_sourcesZcdrom_compsÚsource_templateÚenabled_comps)r+   Z	templatesÚsourcesÚsourceÚcompr,   r,   r-   Úget_source_code_state)  s6    

ÿþý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rzd|jjfd|jjffD ]\}}td||f ƒ q`t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:r]   N)Úurir…   ÚdisabledÚinvalidÚprintr„   Z	match_uriÚbase_uri)r+   r‹   Zlabelr9   r,   r,   r-   Úprint_source_entryP  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   r:   ©r+   r‹   r,   r,   r-   Úmassive_debug_output]  s    
z'SoftwareProperties.massive_debug_outputc                 C   s    || j _| j  |¡}|  ¡  |S )z! change the main download server )r   Údefault_serverZchange_serverÚset_modified_sourceslist)r+   ZserverÚresr,   r,   r-   Úchange_main_download_serverd  s    z.SoftwareProperties.change_main_download_serverc                 C   s   | j  |¡ |  ¡  dS )z Enable a component of the distroN)r   Úenable_componentr—   ©r+   rŒ   r,   r,   r-   rš   k  s    z#SoftwareProperties.enable_componentc                 C   s   | j  |¡ |  ¡  dS )z!Disable a component of the distroN)r   Údisable_componentr—   r›   r,   r,   r-   rœ   p  s    z$SoftwareProperties.disable_componentc                 C   s&   | j jjD ]}|j|kr
|  S q
d S ©N)r   rˆ   Zchildrenrd   )r+   rd   r„   r,   r,   r-   Ú_find_template_from_stringu  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)
Ú
isinstancer:   rž   r   rƒ   r„   r   Úremover€   r—   )r+   r„   r‹   r,   r,   r-   Údisable_child_sourcez  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    r:   rž   r’   r   r–   Z
add_sourcerd   r—   )r+   r„   Z	child_urir,   r,   r-   Ú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—   ©r+   rŠ   r‹   r,   r,   r-   Údisable_source_code_sources•  s    z.SoftwareProperties.disable_source_code_sourcesc                 C   sÂ   g }|  | jj¡ |  | jj¡ | jjD ]}| j |¡ q(|D ]2}| 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¡ qz|  ¡  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’   rd   r—   r¥   r,   r,   r-   Ú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©ri   rª   N)Úsourceslist_backupr   r¨   r   ri   rª   rz   )r+   r‹   Z
source_bkpr,   r,   r-   r"   »  s    z%SoftwareProperties.backup_sourceslistc                 C   s.   |   ¡  | jjD ]}t|ƒ|kr|  S qd S r   )r!   r   r¨   r:   )r+   ri   r‹   r,   r,   r-   Ú_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    r:   r®   r   r—   r”   r,   r,   r-   Útoggle_source_useÌ  s    


z$SoftwareProperties.toggle_source_usec                 C   sT   t | jjƒD ]B\}}t|ƒ|kr| jj| j}t||ƒ| jj|< |  ¡   dS qdS )NTF)r0   r   r¨   r:   rª   r   r—   )r+   Z	old_entryZ	new_entryr©   Úentryrª   r,   r,   r-   Úreplace_source_entryÔ  s    z'SoftwareProperties.replace_source_entryc                 C   s   |   ¡  |  ¡  dS )zNRevert all settings to the state when software-properties 
       was launchedN)r<   Úrevert_sourceslistrF   r,   r,   r-   Ú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   ri   rª   rz   Úsave_sourceslistr!   )r+   r‹   Zsource_resetr,   r,   r-   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   Zmassive_debugr•   r´   r!   rF   r,   r,   r-   r—   î  s
    z+SoftwareProperties.set_modified_sourceslistc                 C   s   |   ¡  dS )z+Write the changed apt configuration to fileN)r;   rF   r,   r,   r-   rH   ö  s    z&SoftwareProperties.set_modified_configc                 C   sb  |j dkr¾|jrxdt|jƒ ¡ |jf }t|jƒdkrN|jD ]}|d| 7 }q<|jdkrh|dtdƒ 7 }|d|j	 7 }nBd|j	|jf }|jD ]}|d| 7 }qŽ|jdkrº|dtdƒ 7 }|S d	|j j
 }|jdkrä|d
tdƒ 7 }|jrø|d|j 7 }|j jdkrZ|jD ]L}|j  |¡rJ|j jD ] }|j|kr&|d|j
 7 }q&n|d| 7 }q|S d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   r`   r£   r   r…   rU   Ú_rŽ   ÚdescriptionÚchildZhas_componentZ
componentsrd   )r+   r‹   ÚcontentsÚcrŒ   r,   r,   r-   Úrender_sourceú  s<    
ÿ





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¡rnd}||||| |¡fS )z&extract attributes to sort the sourcesr
   Ú
0123456789Z
9876543210r   z%s %s %sÚsrc)r   r„   r·   Zdistributionr   rŽ   r£   r…   rµ   rU   ÚfindÚ	translate)r+   r‹   Zcur_sysZhas_templateZhas_commentZ	is_sourceZrevert_numbersZdescr,   r,   r-   Úget_comparable!  s$    
ÿz!SoftwareProperties.get_comparablec                 C   sb   g }| j jD ]P}|js|| jjkr|| jjkr|| jjkr|| jjkr|| jjkr| 	|¡ q|S )z>Return a list of sources that are not part of the distribution)
r   r¨   r   r   r‚   r‡   rƒ   Zdisabled_sourcesr€   rz   )r+   Zisv_sourcesr‹   r,   r,   r-   Úget_isv_sources6  s    
ÿ
þ
ý
ü
ûz"SoftwareProperties.get_isv_sourcesc                 C   s   | j jS )z-Return the list of CDROM based distro sources)r   r‡   rF   r,   r,   r-   Úget_cdrom_sourcesC  s    z$SoftwareProperties.get_cdrom_sourcesc                 C   s   |j | jjk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)rd   r   Zdownload_compsr›   r,   r,   r-   Úget_comp_download_stateG  s    z*SoftwareProperties.get_comp_download_statec                 C   s€   g }| j jD ]}|j|kr| |j¡ qt|ƒdkrPt| j jt|ƒA ƒdkrPdS t|ƒdkrxt| j jt|ƒA ƒdkrxdS 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)FFN)r   rƒ   r„   r   r…   r   r‰   r   )r+   r„   r…   r¸   r,   r,   r-   Úget_comp_child_stateN  s    
ÿÿz'SoftwareProperties.get_comp_child_statec                 C   s"   | j  ¡  g | _| j | j ¡ d S r   )r   ZrefreshZsourceslist_visibler   Zget_sourcesrF   r,   r,   r-   r!   c  s    
z%SoftwareProperties.reload_sourceslistc              	   C   s8  | j d | j d | j d g}|D ]}tj |¡r  qLq tdƒ t|d dƒ d}tj |¡rŽt |¡tj }|tj	@ sŽtd| ƒ t 
|d	¡ |D ] }g }tj |¡r’t|d
ƒ ¡ }tj d¡}t|dƒ}|D ]2}| ¡ D ]}	| d|	 ¡dkrÚ qÎqÚ| |¡ qÎ| ¡ D ]}
| d|
| |
¡f ¡ q
| ¡  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   rY   z/etc/cron.daily/aptz file '%s' not executable, fixingií  rZ   zAPT::PeriodiczAPT::Periodic::%szAPT::Periodic::%s "%s";
N)r   r@   rA   Úisfiler‘   r^   rB   ÚstatÚST_MODEÚS_IXUSRÚchmodÚ	readlinesr   r   Zsubtreer¨   r¾   r_   r7   Úclose)r+   Z	conffilesrg   ZacZpermZperiodicZcontentZcnfri   rq   rf   r,   r,   r-   r;   h  s:    þ

zSoftwareProperties.write_configc                 C   s   | j  d¡ | j  ¡  dS )zRBackup the existing sources.list files and write the current 
       configurationz.saveN)r   ZbackupZsaverF   r,   r,   r-   r´   •  s    z#SoftwareProperties.save_sourceslistc                 C   sz   t |ƒ}tj | j¡rvt d| j ¡D ]N}t|ƒD ]@}| ¡  d¡rFq2|t |ƒkr2tj 	tj 
|¡¡d     S q2q&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   r@   rA   rB   ÚCHANNEL_PATHÚglobr^   r`   ra   ÚsplitextÚbasename)r+   ÚsrclineZsrcentryrg   ri   r,   r,   r-   Ú_is_line_in_whitelisted_channel›  s    $z2SoftwareProperties._is_line_in_whitelisted_channelc                 C   s   |   t|ƒ¡S r   )Ú*check_and_add_key_for_whitelisted_shortcutÚshortcut_handler)r+   rÑ   r,   r,   r-   Ú*check_and_add_key_for_whitelisted_channelsª  s    z=SoftwareProperties.check_and_add_key_for_whitelisted_channelsc                    sz   |j ˆjjˆjj ¡ d\}}ˆ |¡}|rDdˆj|f }ˆ |¡ |jdˆjif‰ ‡ ‡fdd„}t	j
|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 or it is a public Launchpad PPA.
    ©Úcodenamer   z	%s/%s.keyZ	keyserverc               
      sf   ˆ \} }d}z| f |Ž}|s d}W n. t k
rP } zd}t|ƒ}W 5 d }~X Y nX ˆj ||g¡ d S )Nz
Added key.zFailed to add key.F)Ú	Exceptionr:   r&   Zput)ÚfuncÚkwargsÚmsgÚretÚe©Zcdatar+   r,   r-   Úaddkey_func¼  s    
zRSoftwareProperties.check_and_add_key_for_whitelisted_shortcut.<locals>.addkey_func)Útarget)Úexpandr   r×   r}   rQ   rÒ   rÍ   Úadd_keyr   r/   ZThreadÚstart)r+   ÚshortcutrÑ   Z_fnameZchannelZkeyprß   Úworkerr,   rÞ   r-   rÓ   ®  s     
ÿ


z=SoftwareProperties.check_and_add_key_for_whitelisted_shortcutc                 C   s   dS )z) abstract interface to keep the UI alive Nr,   rF   r,   r,   r-   Úupdate_interfaceÌ  s    z#SoftwareProperties.update_interfacec                 C   s^   |  d¡s|S | ¡ d }z| dd¡d }W n tk
rF   d}Y nX 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'
    Zhttpr   ú r
   Úmainzdeb %s %s %s)ra   rn   Ú
IndexErrorr   r×   )r+   ri   ZrepoZareasr,   r,   r-   Úexpand_http_lineÏ  s    

z#SoftwareProperties.expand_http_lineFc                 C   s   | j t| ¡ ƒ|dS )z*
    Add a source for the given line.
    )rä   Úenable_source_code)Úadd_source_from_shortcutrÔ   r`   )r+   ri   rë   r,   r,   r-   Úadd_source_from_lineà  s    
þz'SoftwareProperties.add_source_from_linec              	   C   s<  |j | jj| jj ¡ d\}}|  |¡}|r0dnd}||dd…  }t||ƒ}t||ƒ}|jsd|jrhdS |  |¡}	| j	j
|j|j|j|j|j|j|jd | j	j
||j|j|j|j|j|jd |  ¡  |	r|	 d¡ |	 ¡ rêt d	¡‚| j ¡ \}
}|
st |¡‚| jr8| jjr8d
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Ö   r§   z	# deb-srcr   NF)rµ   rª   Úarchitecturesé   z$Error: retrieving gpg key timed out.r   )Zsources_listT)rá   r   r×   r}   rQ   rê   r   r   rÓ   r   r†   rU   rŽ   r£   r…   rµ   rª   rî   r—   r1   ZisAliver   ÚShortcutExceptionr&   rP   r   ry   ÚaptZCache)r+   rä   rë   Zdeb_linerª   Zdebsrc_entry_typeZdebsrc_lineZnew_deb_entryZnew_debsrc_entryrå   ÚresultrÛ   rñ   Úcacher,   r,   r-   rì   è  sR     
ÿ





úú


z+SoftwareProperties.add_source_from_shortcutTc                 C   sî   |rrt |tƒr&|  |¡}|  |d¡ nH|dk	rn|  |d¡ t |¡}d|_d|_|  |d¡ d|_|  |d¡ dS t |tƒr†|  |¡}|dkr’dS |jtj	 
d¡kr¼| jj td|jd¡ z| j |¡ W n tk
rà   Y nX |  ¡  dS )zRemove the given sourceTNFr§   zDir::Etc::sourcelistÚ )rª   )r    r:   r®   Úremove_sourcere   rU   r   rª   r   r   Z	find_filer   r¨   rz   r   r¡   ro   r—   )r+   r‹   Zremove_source_coder,   r,   r-   rõ     s0    




z SoftwareProperties.remove_sourcec                 C   s6   t j |¡sdS z| j |¡}|W S    Y dS X dS )z7Add a gnupg key to the list of trusted software vendorsFN)r@   rA   rB   r'   r†   )r+   rA   r˜   r,   r,   r-   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   r_   Úencoderb   râ   rd   )r+   ZkeydataZtmpr,   r,   r-   Úadd_key_from_dataI  s    z$SoftwareProperties.add_key_from_datac                 C   s&   z| j  |¡ W dS    Y dS X dS )z<Remove a gnupg key from the list of trusted software vendorsTFN)r'   Zrm)r+   Zkeyidr,   r,   r-   Ú
remove_keyP  s
    zSoftwareProperties.remove_keyc                 C   s$   z| j  ¡  W dS    Y dS X dS )z Run apt-key update TFN)r'   ry   rF   r,   r,   r-   Úupdate_keysX  s
    
zSoftwareProperties.update_keysc                 C   s,   t |tjjƒst‚d|jj|j|j ¡ f S )z" Return the PackageKit package id z	%s;%s;%s;)r    rñ   ÚpackageZVersionÚAssertionErrorZ	shortnameÚversionZarchitecture)r+   Zverr,   r,   r-   Úget_package_id`  s    ÿz!SoftwareProperties.get_package_idc                    sP   g }| | j jD ]$}|D ]}|jdkr| |j¡ qqˆ rL‡ fdd„|D ƒ}|S )zF Get the package dependencies, which can be filtered out by a pattern )ZDependsZ
PreDependsc                    s   g | ]}|  ˆ ¡d kr|‘qS )éÿÿÿÿ)r¾   )ru   Úx©Úpatternr,   r-   rw   p  s      z7SoftwareProperties.get_dependencies.<locals>.<listcomp>)Ú	candidateÚdependenciesZrawtyperz   rd   )Z	apt_cacheZpackage_namer  r  Zor_groupZdepr,   r  r-   Úget_dependenciesg  s    
z#SoftwareProperties.get_dependencies)NNr   )F)F)T)N)BÚ__name__Ú
__module__Ú__qualname__rÍ   rM   r¨   ÚrangerN   ZRELEASE_UPGRADES_LTSZRELEASE_UPGRADES_NEVERrR   r.   r*   r#   r<   rG   rJ   rK   rL   rX   rj   rr   r~   r   r“   r•   r™   rš   rœ   rž   r¢   r¤   r¦   r«   r"   r®   r¯   r±   r³   r²   r—   rH   r»   rÀ   rÁ   rÂ   rÃ   rÄ   r!   r;   r´   rÒ   rÕ   rÓ   ræ   rê   rí   rì   rõ   râ   rø   rù   rú   rþ   Ústaticmethodr  r,   r,   r,   r-   r   M   sŠ   
ü   ý
)'


	'-

/
(
r   c                 C   s4   t D ]}|| ƒ}|d k	r|  S qt d|  ¡‚d S )NzUnable to handle input '%s')Ú_SHORTCUT_FACTORIESr   rð   )rä   ÚfactoryrÜ   r,   r,   r-   rÔ   s  s
    
rÔ   Ú__main__)3Z
__future__r   r   r   rñ   re   Zhashlibr   rs   r@   rÎ   rc   r/   r(   rö   Ústringr   ÚImportErrorr:   rÆ   r$   r%   r   Zxml.sax.saxutilsr   Úconfigparserr   r	   r¶   r   Zaptsources.distror4   r   Zaptsources.sourceslistr   r   rô   r   r   r   Zgi.repositoryr   rÔ   r  Úobjectr   r  Zspr‘   rX   rj   r,   r,   r,   r-   Ú<module>   sh   ý      ,	
