o
    U7e}                     @   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re* 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 sw   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 sw   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 rt t
||j t
|jA dkst t
|j| jjA dksd| j_ dS qYdS )zReturn 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 }qq|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 )zReturn 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 )zReturn 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|rt|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  qW d   n1 sw   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   
      