
     f                        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
d dlZ	 d dlm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Zd dlZd dlZdd	lmZ d d
lmZm Z m!Z! d dl"m#Z# d dl$m%Z% d dl&m'Z'  G d de(      Z)e*dk(  r/ e)       Z+ e,e+j[                                e+j]                  d        yy# e$ r ej                  ZY w xY w# e$ r d dlZY w xY w# e$ r	 d dlmZ Y w xY w)    N)md5)	maketrans)NamedTemporaryFile)escape)ConfigParser)gettext   )AptAuth)SourcesListSourceEntryDeb822SourceEntry)InvalidShortcutException)shortcut_handler)Gioc                      e Zd ZdZdZ e ed            \  ZZZ	edede	diZ
d=dZd	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z d Z!d Z"d  Z#d! Z$d" Z%d# Z&d$ Z'd% Z(d& Z)d' Z*d( Z+d) Z,d* Z-d+ Z.d, Z/d- Z0d. Z1d/ Z2d0 Z3d1 Z4d2 Z5d3 Z6d>d4Z7d>d5Z8d?d6Z9d7 Z:d8 Z;d9 Z<d: Z=d; Z>e?d@d<       Z@y)ASoftwarePropertiesz /usr/share/app-install/channels/z$/etc/update-manager/release-upgrades   normalltsneverNc                 P   |dz   | _         || _        |dk7  r t        j                  j	                  d|       |dk(  rd}|| _        || _        t        |      | _        t        j                  j                         | _
        g | _        d| _        | j                          | j                          | j!                          g | _        t%        j&                         | _        t+        |      | _        t/        j0                         | _        t5        j6                  | j8                         y)	z Provides the core functionality to configure the used software
        repositories, the corresponding authentication keys and
        update automation z/etc/popularity-contest.conf/DirNz/usr/share/software-properties/)deb822F)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   r   s        G/usr/lib/python3/dist-packages/softwareproperties/SoftwareProperties.py__init__zSoftwareProperties.__init__W   s     <<DODL#~nn( $1gDLDL"&1D##..0DKD %D D ;;=DL 7+DL(D
OOD))*    c                     t        j                         D ]>  }|j                  t        j                         j                  k7  s/|j	                          @ y)z9 wait for all running threads (PPA key fetchers) to exit N)	threading	enumerateidentcurrent_threadjoin)r5   ts     r6   r4   z#SoftwareProperties.wait_for_threads   s>      " 	
I,,.44	4	r8   c                     i | _         t        j                  j                         D ]A  }t        j
                  j                  t        j                  |         }|| j                   |<   C y)z$Backup all apt configuration optionsN)apt_conf_backupsoftwarepropertiesCONF_MAPkeysr   r   find_i)r5   optionvalues      r6   r*   z"SoftwareProperties.backup_apt_conf   sX    D$--224 -%%&8&A&A&&IJ',V$-r8   c                     | j                   j                         D ]I  }t        j                  j	                  t
        j                  |   t        | j                   |                K | j                          y)z$Restore the stored apt configurationN)	rA   rD   r   r   r   rB   rC   strwrite_config)r5   rF   s     r6   restore_apt_confz#SoftwareProperties.restore_apt_conf   sa    &&++- >-66v>t33F;<	>> 	r8   c                    t         j                  j                  t        j                  d         dkD  rOt         j                  j                  t        j                  d         dk(  r/t
        j                  j                  d      rt        j                  S t         j                  j                  t        j                  d         dk(  rCt         j                  j                  t        j                  d         dk(  rt        j                  S t         j                  j                  t        j                  d         dk(  rCt         j                  j                  t        j                  d         dk(  rt        j                  S yt         j                  j                  t        j                  d         dk(  rCt         j                  j                  t        j                  d         dk(  rt        j                  S y)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   rE   rB   rC   ospathexistsUPDATE_INST_SECUPDATE_DOWNLOADUPDATE_NOTIFYUPDATE_MANUALr5   s    r6   get_update_automation_levelz.SoftwareProperties.get_update_automation_level   sg    ~~/88FG!K>>  !3!<!<\!JKqPww~~;<%555^^""#5#>#>~#NOSTT^^""#5#>#>|#LMQRR%555^^""#5#>#>|#LMQRR^^""#5#>#>~#NOSTT%333			1::<H	IQ	N			1::>J	Kq	P!///r8   c                    |t         j                  k(  rvt        j                  j	                  t         j
                  d   t        d             t        j                  j	                  t         j
                  d   t        d             n|t         j                  k(  rvt        j                  j	                  t         j
                  d   t        d             t        j                  j	                  t         j
                  d   t        d             n6|t         j                  k(  rut        j                  j	                  t         j
                  d   t        d             t        j                  j	                  t         j
                  d   t        d             nt        j                  j	                  t         j
                  d   t        d             t        j                  j	                  t         j
                  d   t        d             t        j                  j	                  t         j
                  d   t        d             | j                          y)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. rN   r	   rO   r   rM   N)
rB   rS   r   r   r   rC   rI   rT   rU   set_modified_config)r5   states     r6   set_update_automation_levelz.SoftwareProperties.set_update_automation_level   s   
 "222-66|Dc!fM-66~FAO	$44	4-66~FAO-66|Dc!fM	$22	2-66~FAO-66|Dc!fM-66|Dc!fM-66|Dc!fM-66~FAOr8   c                     |t         j                  j                  t        j                  d         k(  sKt         j                  j                  t        j                  d   t        |             | j                          yy)z8Set the interval in which we check for available updatesrM   N)r   r   rE   rB   rC   r   rI   rZ   )r5   dayss     r6   set_update_intervalz&SoftwareProperties.set_update_interval   s]     W^^**+=+F+F|+TUU
..

/88FD	
R

"
"
$ Vr8   c                 b    t         j                  j                  t        j                  d         S )z3 Returns the interval of the apt periodic cron job rM   )r   r   rE   rB   rC   rW   s    r6   get_update_intervalz&SoftwareProperties.get_update_interval   s#    >>  !3!<!<\!JKKr8   c                    t         j                  j                  | j                        s| j                  S t               }|j                  | j                         |j                  dd      rK|j                  dd      j                         }| j                  j                         D ]  \  }}||k(  s|c S  | j                  S )z
    return the release upgrade policy:
     RELEASE_UPGRADES_NORMAL,
     RELEASE_UPGRADES_LTS,
     RELEASE_UPGRADES_NEVER
    DEFAULTPrompt)rP   rQ   rR   RELEASE_UPGRADES_CONFRELEASE_UPGRADES_NORMALr   read
has_optiongetlowerrelease_upgrades_policy_mapitems)r5   parsertypekvs        r6   get_release_upgrades_policyz.SoftwareProperties.get_release_upgrades_policy   s     77>>$445)))^F
KK**+8,ZZ	(+113d2288: $!Q9( '''r8   c                 (   t         j                  j                  | j                        sAt	        | j                  d      5 }|j                  d| j                  |   z         ddd       yt	        | j                  d      5 }t        d      5 }|D ]g  }|j                         }|j                         j                  d      r"|j                  d	| j                  |   z         T|j                  |d
z          i |j                          t        j                  | j                  |j                         t        j                  |j                  | j                         ddd       ddd       y# 1 sw Y   yxY w# 1 sw Y   xY w# 1 sw Y   yxY w)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

)rP   rQ   rR   re   openwriterk   r   striprj   
startswithflushshutilcopymodenamecopy)r5   ifoutlines        r6   set_release_upgrades_policyz.SoftwareProperties.set_release_upgrades_policy   sC    77>>$445**3/ O1	($*J*J1*MMNO	d((	- 	84FD4Q 	8UX $zz|::<""8,
))MD$D$DQ$GG
H
))DI
 
iikood00#((;kk#((D667	8 	8 O	8 	8 	8 s1    "E0FCE<F0E9<F	FFc                    t         j                  j                  | j                        rt	        | j                        5 }|j                         j                  d      }ddd       D ]@  }	 |j                  d      \  }}|dk(  r$|j                  d      j                         dk(  r yB y# 1 sw Y   OxY w# t        $ r Y [w xY w)	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 rw   N=PARTICIPATE"yesTF)
rP   rQ   rR   r   rx   rg   splitrz   rj   
ValueError)r5   r   linesr   keyrG   s         r6   get_popcon_participationz+SoftwareProperties.get_popcon_participation   s     
ww~~doo&$//" 	'a&&(..&%	' 	D"jjoU-'EKK,<,B,B,D,M		 	' 	'  s    B--;B9-B69	CCc           	         |dk(  rd}nd}t         j                  j                  | j                        rGt	        | j                  d      5 }|D cg c]  }t        j                  dd|z  |       }}ddd       nt               }t	        dd	      5 }|j                  |j                  d
             ddd       |j                         }g }|j                  d|z         |j                  dt        |      z         |j                  d       t	        | j                  d      5 }|j                         ddd       yc c}w # 1 sw Y   ?xY w# 1 sw Y   xY w# 1 sw Y   yxY w)z? Enable or disable the participation in the popularity contest Tr   nort   z^(PARTICIPATE=)(".+?")z\1"%s"Nz/dev/urandomrbi   zMY_HOSTID="%s"
zPARTICIPATE="%s"
zUSE_HTTP="yes"
rs   )rP   rQ   rR   r   rx   resubr   updaterg   	hexdigestappendrI   
writelines)	r5   
is_helpfulrG   r   r   r   mr   ids	            r6   set_popcon_pariticipationz,SoftwareProperties.set_popcon_pariticipation  sB   T	ww~~doo& $//3' 	-:!+- VV5y57H$O -E -	- 	-
 E.$' 	#1HHQVVD\"	#[[])B./+c%j89)*	doos	# q	U -	- 	-	# 	# s6   
E!D?0E!E$E?EEEE%c                    t        | j                  j                        dk  rd| j                  _        yd| j                  _        i }g }|j	                  | j                  j
                         |j	                  | j                  j                         |D ]b  }|j                  |v r0|j                  D ]   }||j                     j                  |       " At        |j                        ||j                  <   d t        | j                  j                        dkD  r-| j                  j                  || j                  j                  <   | j                  j                  D ]  }|j                  |vs|j                  |v s t        t        ||j                           t        |j                        z        dk(  r[t        t        |j                        | j                  j                  z        dk(  rd| j                  _         y y)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)r5   	templatessourcessourcecomps        r6   get_source_code_statez(SoftwareProperties.get_source_code_state  s   
 4;;**+a/ ',# '+# 	t{{//0t{{001 	?F)+"LL 9Dfoo.22489 .1->	&//*	? t{{(()A-59[[5L5LIdkk112kk55 	Fi/9,S6??34s6<<7HHIQNV\\*T[[-F-FFG1L.3+	 r8   c                 f   d|j                   fd|j                  fd|j                   fd|j                   ffD ]  \  }}t	        d|d|        |j
                  rId|j
                  j                  fd|j
                  j                  ffD ]  \  }}t	        d|d|        t	        d       y	)
z4Print the data of a source entry to the command linezURI:zComps:zEnabled:zValid: z	MatchURI:zBaseURI:rw   N)urir   disabledinvalidprintr   	match_uribase_uri)r5   r   labelrG   s       r6   print_source_entryz%SoftwareProperties.print_source_entryE  s    "FJJ/$fll3&FOO(;<$&..&89; ) 	%'(	)
  +V__-F-FG *FOO,D,DEG 	-NUEeU+,	- 
$Kr8   c                     t        d       | j                  D ]  }t        |j                                 t        d       y)zPrint the complete sources.listzSTART SOURCES.LIST:zEND SOURCES.LIST
N)r   r"   rI   r5   r   s     r6   massive_debug_outputz'SoftwareProperties.massive_debug_outputR  s7    	
 "" fjjl	
r8   c                 ~    || j                   _        | j                   j                  |      }| j                          |S )z! change the main download server )r$   default_serverchange_serverset_modified_sourceslist)r5   serverress      r6   change_main_download_serverz.SoftwareProperties.change_main_download_serverY  s3    !'DKK
++
#
#F
+C!!#Jr8   c                 Z    | j                   j                  |       | j                          y)z Enable a component of the distroN)r$   enable_componentr   r5   r   s     r6   r   z#SoftwareProperties.enable_component`  s     KK  &!!#r8   c                 Z    | j                   j                  |       | j                          y)z!Disable a component of the distroN)r$   disable_componentr   r   s     r6   r   z$SoftwareProperties.disable_componente  s     KK!!$'!!#r8   c                 t    | j                   j                  j                  D ]  }|j                  |k(  s|c S  y N)r$   r   childrenr   )r5   r   r   s      r6   _find_template_from_stringz-SoftwareProperties._find_template_from_stringj  s2    KK//88 	$	r8   c                 ~   t        |t              r| j                  |      }| j                  j                  D ]-  }|j
                  |k(  s| j                  j                  |       / | j                  j                  D ]-  }|j
                  |k(  s| j                  j                  |       / | j                          y)7Enable a child repo of the distribution main repositoryN)

isinstancerI   r   r$   r   r   r"   remover   r   )r5   r   r   s      r6   disable_child_sourcez'SoftwareProperties.disable_child_sourceo  s    (C 00:h++++ ,??h&##F+, ++11 ,??h&##F+, 	!!#r8   c                    t        |t              r| j                  |      }|j                  dk(  r| j                  j
                  }n|j                  }| j                  j                  ||j                         | j                          y)r   N)r   dist)	r   rI   r   r   r$   r   
add_sourcer   r   )r5   r   	child_uris      r6   enable_child_sourcez&SoftwareProperties.enable_child_source|  sm    (C 00:h D KK..	%%	KKyx}}=!!#r8   c                 (   g }|j                  | j                  j                         |j                  | j                  j                         | j                  j                  D ]  }| j
                  j                  |        | j                          y)z%Remove all distro source code sourcesN)r   r$   r   r   r   r"   r   r   r5   r   r   s      r6   disable_source_code_sourcesz.SoftwareProperties.disable_source_code_sources  sl    GNN4;;++,NN4;;,,-++11 ('(!!#r8   c                 X   g }|j                  | j                  j                         |j                  | j                  j                         | j                  j                  D ]  }| j
                  j                  |        |D ]i  }| j
                  j                  d|j                  |j                  |j                  d| j
                  j                  |      dz   |j                  |       k | j                  j                  D ]  }| j
                  j                  d| j                  j                  j                  | j                  j                  j                   |j                  d| j
                  j                  |      dz   |j                  |        | j#                          y)z0Enable source code source for all distro sourcesdeb-srczAdded by software-propertiesr	   )parentN)r   r$   r   r   r   r"   r   r   r   r   r   indexfiler   r   r   r   r   r   s      r6   enable_source_code_sourcesz-SoftwareProperties.enable_source_code_sources  s`   GNN4;;++,NN4;;,,- ++11 ('(  ,Y#ZZ#[[#\\;!--33F;A=#[[$* 	 	,, ++++ ,Y![[88AA![[88==#\\;!--33F;A=#[[$* 	 	,, 	!!#r8   c                 b   g | _         | j                  j                  D ]  }t        |t              r"t	        |j
                  |j                        }n@t        |t              r"t        |j
                  |j                        }nt        d|       | j                   j                  |        y)z+Store a backup of the source.list in memoryr   r   sectionr   Unknown source: N)
sourceslist_backupr"   listr   r   r   r   r   	TypeErrorr   )r5   r   
source_bkps      r6   r)   z%SoftwareProperties.backup_sourceslist  s     D""'' 3fk*$&++6;;GJ 12*6;;V[[QJ.vh788&&z23r8   c                 ~    | j                          | j                  j                  D ]  }t        |      |k(  s|c S  y r   )r(   r"   r   rI   )r5   r   r   s      r6   _find_source_from_stringz+SoftwareProperties._find_source_from_string  sA     	""'' 	V	 r8   c                     t        |t              r| j                  |      }|j                   |_        | j	                          y)z&Enable or disable the selected channelN)r   rI   r   r   r   r   s     r6   toggle_source_usez$SoftwareProperties.toggle_source_use  s8     &#,,V4f //)FO!!#r8   c                    t        | j                  j                        D ]  \  }}t        |      |k(  s| j                  j                  |   j                  }t        |t              r$t        ||      | j                  j                  |<   n#t        ||      | j                  j                  |<   | j                           y y)NTF)	r;   r"   r   rI   r   r   r   r   r   )r5   	old_entry	new_entryr   entryr   s         r6   replace_source_entryz'SoftwareProperties.replace_source_entry  s    #D$4$4$9$9: 	Uy	 $$U+00e./+<Y+MD!!%(+6y$+GD!!%(%%' r8   c                 D    | j                          | j                          y)zMRevert all settings to the state when software-properties
       was launchedN)rK   revert_sourceslistrW   s    r6   revertzSoftwareProperties.revert  s     	r8   c                    g | j                   _        | j                  D ]  }t        |t              r"t	        |j
                  |j                        }n@t        |t              r"t        |j
                  |j                        }nt        d|       | j                   j                  j                  |        | j                          | j                          y)z6Restore the source list from the startup of the dialogr   r   r   N)r"   r   r   r   r   r   r   r   r   r   save_sourceslistr(   )r5   r   source_resets      r6   r   z%SoftwareProperties.revert_sourceslist  s    D)) 3fk*&FKKV[[IL 12,V[[v{{SL.vh788$$\23 	r8   c                     d| _         | j                  r&| j                  j                  r| j                          | j	                          | j                          y)zCThe sources list was changed and now needs to be saved and reloadedTN)r'   r    massive_debugr   r   r(   rW   s    r6   r   z+SoftwareProperties.set_modified_sourceslist  sA     $D||22!!#r8   c                 $    | j                          y)z+Write the changed apt configuration to fileN)rJ   rW   s    r6   rZ   z&SoftwareProperties.set_modified_config  s    r8   c                 @   |j                   dk(  r,|j                  rt        |j                        dkD  r0|j                  j                  dd      j	                         dd dz   nd}dt        |      d|j                  }t        |j                        d	kD  r|j                  D ]
  }|d
|z  z  } |j                  dv r|d
t        d      z  z  }|d|j                  z  z  }|S d|j                  d|j                  d}|j                  D ]
  }|d
|z  z  } |j                  dv r|d
t        d      z  z  }|S d|j                   j                  z  }|j                  dv r|dt        d      z  z  }|j                  r|d
|j                  z  z  }|j                   j                  dk(  rr|j                  D ]c  }|j                   j                  |      r>|j                   j                  D ]$  }|j                  |k(  s|d|j                  z  z  }& \|d|z  z  }e |S )z5Render a nice output to show the source in a treeviewNP   rw   r   u   […] z<b>z</b> r	   z %s)r   zrpm-srcz(Source Code)z
%sz</b>z	<b>%s</b>z (%s)zSource CodeF)r   commentr   replacerz   r   r   r   rn   _r   descriptionchildhas_component
componentsr   )r5   r   r   contentscr   s         r6   render_sourcez SoftwareProperties.render_source  s   $>> !025 &&tS1779#2>;=  *0&++FH6<< 1$ *A	)H*{{44EAo$666++H '  *0V[[AH\\ &EAI%&{{44EAo$666 !<!<<;;00!M"222H>>uv~~--H??  E) .??006 $__77 ?66T>$(>>H? -H. r8   c                    d}d}d}d}t        dd      }|j                  r>d}|j                  j                  }|j                  j                  | j                  k(  r9d}n6|j
                  d|j                  d|j                  }|j                  rd}|j                  j                  d      rd}|||||j                  |      fS )z&extract attributes to sort the sourcesr	   
0123456789
9876543210r   r   src)r   r   r  distributionr$   r   r   r   r   rn   find	translate)r5   r   cur_syshas_templatehas_comment	is_sourcerevert_numbersdescs           r6   get_comparablez!SoftwareProperties.get_comparable(  s    glki |<n	**??''4;;6G%zz6;;E$^^k			%	 )|[)nn^,. .r8   c                 v   g }| j                   j                  D ]  }|j                  r|| j                  j                  vs)|| j                  j
                  vsB|| j                  j                  vs[|| j                  j                  vst|| j                  j                  vs|j                  |        |S )z>Return a list of sources that are not part of the distribution)
r"   r   r   r$   r   r   r   disabled_sourcesr   r   )r5   isv_sourcesr   s      r6   get_isv_sourcesz"SoftwareProperties.get_isv_sources=  s    K""'' '~~$++222$++333$++333$++666888v&' r8   c                 .    | j                   j                  S )z-Return the list of CDROM based distro sources)r$   r   rW   s    r6   get_cdrom_sourcesz$SoftwareProperties.get_cdrom_sourcesJ  s    ;;$$$r8   c                 J    |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)r   r$   download_compsr   s     r6   get_comp_download_statez*SoftwareProperties.get_comp_download_stateN  s"    
 II333U;;r8   c                    g }| j                   j                  D ]-  }|j                  |k(  s|j                  |j                         / t        |      dkD  r/t        | j                   j                  t        |      z        dk(  ryt        |      dkD  r/t        | j                   j                  t        |      z        dk7  ryy)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   )r5   r   r   r  s       r6   get_comp_child_statez'SoftwareProperties.get_comp_child_stateU  s     E** &>>X%LL%& 5zA~DKK%%E
23q8	UaDKK%%E
23q8  r8   c                     | j                   j                          g | _        | j                  j	                  | j                          y r   )r"   refreshsourceslist_visibler$   get_sourcesrW   s    r6   r(   z%SoftwareProperties.reload_sourceslistj  s4    DKKD,,-r8   c                 0   | j                   dz   | j                   dz   | j                   dz   g}|D ]#  }t        j                  j                  |      s# n t	        d       t        j                  j                        }t        j                  j                  |      s%t        j                  |      j                  d       t        |d   d      5 }|j                  d	       d
d
d
       d}t        j                  j                  |      r]t        j                  |      t        j                     }|t        j                  z  s$t	        d|z         t        j                  |d       |D ]  }g }t        j                  j                  |      s&t        |d      5 }|j!                         }d
d
d
       t"        j$                  j'                  d      }t        |d      5 }|D ]A  }	|j)                         D ]  }
|	j+                  d|
z        dk\  s 0 |j                  |	       C |j)                         D ])  }|j                  d|d|j-                  |      d       + 	 d
d
d
        y
# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   )xY w)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 oneT)parentsr   rs   r   Nz/etc/cron.daily/aptz file '%s' not executable, fixingi  rt   zAPT::PeriodiczAPT::Periodic::%szAPT::Periodic::z "z";
)r   rP   rQ   isfiler   dirnamerR   pathlibPathmkdirrx   ry   statST_MODES_IXUSRchmod	readlinesr   r   subtreer   r  rE   )r5   	conffilesr   r(  acpermperiodiccontentcnfr   r   r   s               r6   rJ   zSoftwareProperties.write_configo  s5    >>CCKKMI
  				 +,"gWW^^G$W##D#1	!c" a	 
B	ww~~bWWR[&dT\\!0256
U  Hg		!(C  	"AKKM'	"nn$$_5 (C  
	HA dxxz .#671< ggdm 88: HaGGQ

1FGH
	H 
	HH  	" 	"

	H 
	Hs+   I2I?"0JAJ2I<?J	J	c                 n    | j                   j                  d       | j                   j                          y)zQBackup the existing sources.list files and write the current
       configurationz.saveN)r"   backupsaverW   s    r6   r   z#SoftwareProperties.save_sourceslist  s*     	G$r8   c                    t        |      }t        j                  j                  | j                        rt        j
                  d| j                  z        D ]  }t        |      5 }|D ]|  }|j                         j                  d      r#|t        |      k(  s2t        j                  j                  t        j                  j                  |            d   c cddd       c S  	 ddd        y# 1 sw Y   xY w)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   rP   rQ   rR   CHANNEL_PATHglobrx   rz   r{   splitextbasename)r5   srclinesrcentryfilenamer   r   s         r6   _get_channel_from_linez)SoftwareProperties._get_channel_from_line  s    
 7#H	ww~~d''(iid.?.? ?@ E((^ 	Eq Edzz|&&s+;t,,WW%%bgg&6&6x&@A!DD	E 	EE	E 	EE 	E 	Es   %4C3A C3&C33C<	c                     |j                         j                  }| j                  |      }|r$| j                  d|d}| j	                  |       yy)z
    helper that adds the gpg key of the channel to the apt
    keyring *if* the channel is in /usr/share/app-install/channels
    r   z.keyTF)r   r   rD  r=  add_key)r5   shortcutrA  channelkeyps        r6   &check_and_add_key_for_channel_shortcutz9SoftwareProperties.check_and_add_key_for_channel_shortcut  sM    
 ""$))G))'2G --w7d
ll4r8   c                      y)z) abstract interface to keep the UI alive N rW   s    r6   update_interfacez#SoftwareProperties.update_interface  s    r8   c                     |j                  d      s|S |j                         d   }	 |j                  dd      d   }d|d| j                  j                  d|}|S # t        $ r d}Y .w xY w)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   r	   mainzdeb )r{   r   
IndexErrorr$   codename)r5   r   repoareass       r6   expand_http_linez#SoftwareProperties.expand_http_line  sr     ??6"k::<?D

3q!!$ #DKK$8$8%ADK  s   A A,+A,c                 x    	 t        |j                               }| j                  ||      S # t        $ r Y yw xY w)z*
    Add a source for the given line.
    F)r   rz   r   add_source_from_shortcut)r5   r   enable_source_coderG  s       r6   add_source_from_linez'SoftwareProperties.add_source_from_line  sA    #DJJL1 ((3EFF $ s   - 	99c           	         |j                         j                  }|j                  }| j                  |      }|rdnd}||dd z   }t        ||      }t        ||      }|j                  s|j                  ry| j                  |      s|j                          | j                  j                  |j                  |j                  |j                  |j                  |j                  |j                  |j                         | j                  j                  ||j                  |j                  |j                  |j                  |j                  |j                         | j!                          | j"                  rF| j"                  j$                  r0ddl}	|	j)                         }
|
j%                  |j                         y	)
z
    Add a source with the given shortcut and add the signing key if the
    site is a known channel or the shortcut implementer adds it.
    r   z	# deb-srcr   NF)r   r   architecturesr   )sources_listT)r   r   sourceparts_filerU  r   rJ  rF  r"   r   rn   r   r   r   r   r   r[  r   r    r   aptCache)r5   rG  rX  deb_liner   debsrc_entry_typedebsrc_linenew_deb_entrynew_debsrc_entryr^  caches              r6   rW  z+SoftwareProperties.add_source_from_shortcut  s    ##%**H$$D$$X.H%7	[#hqrl2K$/M";5 0 8 866x@++&**&++&,,!.!6!6+00'4'B'B  D 	*)--)..)//!1!9!9.33'7'E'E  G 	!!#||++		"2"7"78r8   c                 l   |rt        |t              r$| j                  |      }| j                  |d       y|`| j                  |d       t	        j                  |      }d|_        d|_        | j                  |d       d|_        | j                  |d       yt        |t              r| j                  |      }|y|j                  t        j                  j                  d      k7  r|j                  j                  d      r;| j                  j                  j                  t        d|j                               n:| j                  j                  j                  t!        d|j                               	 | j                  j#                  |       | j'                          y# t$        $ r Y w xY w)	zRemove the given sourceTNFr   zDir::Etc::sourcelistz.sourcesr   )r   )r   rI   r   remove_sourcer   rn   r   r   r   r   	find_fileendswithr"   r   r   r   r   r   r   r   )r5   r   remove_source_codes      r6   rg  z SoftwareProperties.remove_source  sp   	FC	 ..v664(  65) 6"65)65) &#,,V4f~
 {{gnn../EFF			j	)$$%6r%LM$$[&++%FG
f% 	!!#  
 s   ;F' '	F32F3c                     t         j                  j                  |      sy	 | j                  j	                  |      }|S #  Y yxY w)z7Add a gnupg key to the list of trusted software vendorsF)rP   rQ   rR   r/   r   )r5   rQ   r   s      r6   rF  zSoftwareProperties.add_key3  s=    77>>$llt$
s	   ? Ac                     t        j                         }|j                  |j                  d             |j	                          | j                  |j                        S )z<Add a gnupg key from a utf-8 data string (e.g. copy-n-paste)zutf-8)tempfiler   ry   encoder|   rF  r   )r5   keydatatmps      r6   add_key_from_dataz$SoftwareProperties.add_key_from_data=  sA    

%
%
'CIIgnnW%&IIK<<!!r8   c                 J    	 | j                   j                  |       y#  Y yxY w)z<Remove a gnupg key from the list of trusted software vendorsTF)r/   rm)r5   keyids     r6   
remove_keyzSoftwareProperties.remove_keyD  s$    s    "c                 H    	 | j                   j                          y#  Y yxY w)z Run apt-key update TF)r/   r   rW   s    r6   update_keyszSoftwareProperties.update_keysL  s$    
lls    !c                 .   t        |t        j                        sJ |j                  }|r|j                  }|j
                  }n>t        j                  |      }|j                  |      }|j                  }|j
                  }|j                  d|d|dS )z" Return the PackageKit package id ;)	r   r   Packagecurrent_verver_strarchDepCacheget_candidate_verr   )r5   	apt_cachepkgcur_ververr}  depcache	candidates           r6   get_package_idz!SoftwareProperties.get_package_idT  sy    c7??+++ooGoo||##I...s3	~~((C..r8   c                 0   t        j                  |       }|j                  |      }g }	 |j                  j	                  d      D ]6  }|D ]/  \  }}}	|j                  |      dk7  s|j                  | |          1 8 	 |S # t        t        f$ r g cY S w xY w)zF Get the package dependencies, which can be filtered out by a pattern Depends)	r   r~  r  depends_list_strri   r  r   KeyErrorr   )
r  packagepatternr  r  dependenciesdep_listdep_namedep_verdep_ops
             r6   get_dependenciesz#SoftwareProperties.get_dependenciesb  s     	*H**73IL!2266yA 	=H-5 =)'6==)R/ ''	((;<=	=  i  	s   ;B &B BB)NNr   F)F)Tr   )A__name__
__module____qualname__r=  re   r   rangerf   RELEASE_UPGRADES_LTSRELEASE_UPGRADES_NEVERrk   r7   r4   r*   rK   rX   r\   r_   ra   rq   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r)   r   r   r   r   r   r   rZ   r  r  r  r  r  r   r(   rJ   r   rD  rJ  rM  rU  rY  rW  rg  rF  rq  ru  rw  r  staticmethodr  rL  r8   r6   r   r   D   sg    2, A 58n	 heg!'+R-.(%L(&0 2%N $
$

$$$$<
3$(T.*%<*.
/Hb 0"	G$L)$V"/  r8   r   __main__)/r   r   hashlibr   r   rP   r>  r}   r:   r2   rm  r)  stringr   ImportErrorrI   r,  r,   r-   r   xml.sax.saxutilsr   configparserr   r   r   r#   aptsources.distrorB   r
   aptsources.sourceslistr   r   r   "softwareproperties.shortcuthandlerr   softwareproperties.shortcutsr   gi.repositoryr   objectr   r  spr   rq   r   rL  r8   r6   <module>r     s   4    	 	        ( #(' !     P P G 9 m m` z"&&()  # [  mm)    ('(s4   C C C, CC	C)(C),C:9C: