
    kd                       d Z dZdZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddlZddlZddlZ	 ddlmZm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mZ ddl m!Z!m"Z" d	d
l#m$Z$ ddl% ddl& ddl#m'Z' ddl(m)Z) ddl*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2  ejf                  d      Z4d Z5dZ6dZ7 e8       fdZ9d Z:d Z; G d de$      Z<y# e$ r ddlmZmZ Y w xY w# e$ r	 ddlmZ Y w xY w)z0Provides AptWorker which processes transactions.z%Sebastian Heinlein <devel@glatzor.de>	AptWorker    N)urlsplit
urlunsplit)ConfigParser)SourcesList)GObjectGLib   )
BaseWorker   )*)lock)set_euid_egid)DaemonOpenProgressDaemonInstallProgressDaemonAcquireProgressDaemonAcquireRepoProgressDaemonDpkgInstallProgressDaemonDpkgReconfigureProgressDaemonDpkgRecoverProgressDaemonForkProgresszAptDaemon.Workerc                     | S N )ss    </usr/lib/python3/dist-packages/aptdaemon/worker/aptworker.py<lambda>r   L   s    a     z/etc/popularity-contest.confaN  # Config file for Debian's popularity-contest package.
#
# To change this file, use:
#        dpkg-reconfigure popularity-contest
#
# You can also edit it by hand, if you so choose.
#
# See /usr/share/popularity-contest/default.conf for more info
# on the options.
MY_HOSTID="%(host_id)s"
PARTICIPATE="%(participate)s"
USE_HTTP="yes"
c                    | j                   sy| j                  ry| j                  t        t        fvryt
        t        t        t        t        fD ]  }| j                  |   s y | j                  t           sy| j                  t           D cg c]  }|j                  d      d    }}t        |      t        | j                        k(  S c c}w )zzReturn True if this transaction only touches packages in the
    aptdaemon repoisotry high trust repository whitelist
    F=r   )	simulatedunauthenticatedroleROLE_INSTALL_PACKAGESROLE_COMMIT_PACKAGESPKGS_REINSTALLPKGS_REMOVE
PKGS_PURGEPKGS_DOWNGRADEPKGS_UPGRADEpackagesPKGS_INSTALLsplitsethigh_trust_packages)trans	whitelistenumpkgpkgss        r   .trans_only_installs_pkgs_from_high_trust_reposr5   ^   s     ??zz/1EFFj. >>$
 >>,').)EF#CIIcN1FDFt9E55666 Gs   Cc                     t               }t        j                  t        j                  j	                  | d            D ]  }|t        |      z  } |S )zoReturn a set of (origin, component, pkgname-regexp) from a
    high-trust-repository-whitelist.d directory
    z*.cfg)r.   globospathjoin*_read_high_trust_repository_whitelist_file)whitelist_cfg_dr1   r9   s      r   read_high_trust_repository_dirr=   z   sJ     I		"'',,@A F?EE	Fr   c                 x   t               }t               }	 |j                  |        |j                         D ]L  }|j                  |d      }|j                  |d      }|j                  |d      }|j                  |||f       N |S # t        $ r(}t        j                  d| d|d       |cY d}~S d}~ww xY w)zRead a individual high-trust-repository whitelist file and return
       a set of tuples (origin, component, pkgname-regexp)
    z%Failed to read repository whitelist 'z' ()Norigin	componentpkgnames)	r   r.   read	Exceptionlogerrorsectionsgetadd)r9   parserr1   esectionr@   rA   rB   s           r   r;   r;      s     ^FID ??$ 5GX.JJw4	::gz2vy(34	5
   		T1MNs   B 	B9B4.B94B9c                      e Zd ZdZ ej
                         d   ZdZd3dZd4dZ	d Z
	 d5dZd	 Zd
 Zd Z	 d5dZd Zd Zd4dZd Zd Zd Zd5dZd5dZd4dZd4dZd Zd Zed        Zd Zd6dZd Z d Z!d5dZ"d7dZ#d  Z$	 	 d8d!Z%e&jN                  d"        Z(d# Z)d$ Z*d% Z+d& Z,d' Z-d( Z.d) Z/d9d*Z0d:d+Z1d, Z2d4d-Z3d. Z4d/ Z5d0 Z6d1 Z7d2 Z8y);r   z3Worker which processes transactions from the queue.r   z/opt/Nc                    t        j                  | ||       d| _        | j                  rt        j
                  j                  |d      }t        j
                  j                  |      r$t        j                  t        j                  |       t        j
                  j                  |d      }t        j
                  j                  |      r$t        j                  t        j                  |       |t        j                  d<   t        j
                  j                  |d      t        j                  d<   t        j                  j                  d       t        j                  j                  d       d	|z  t        j                  d
<   d|z  t        j                  d
<   t        j                  j                  d      }t        j
                  j                  t        j
                  j                  |      d      t         j"                  _        t        j
                  j                  t        j
                  j                  |      d      t         j$                  _        t        j                  j'                  d      }t        j
                  j                  |d      t         j(                  _        t        j                  j'                  d      }t        j
                  j                  |d      t         j*                  _        t        j,                          t/        t        j
                  j                  t        j                  j'                  d      d            | _        t2        j5                  d| j0                  z         t        j                  j                  d      | _        d| _        |r| j;                  g d       yy)z$Initialize a new AptWorker instance.Nzetc/apt/apt.confzetc/apt/apt.conf.dDirzvar/lib/dpkg/statuszDir::State::StatuszDPkg::Post-InvokezDPkg::Optionsz	--root=%szDPkg::Options::z--log=%s/var/log/dpkg.logDir::State::statuszlock-frontendr   zDir::Cache::ArchiveszDir::State::listsz/etc/aptdaemon/high-trust-repository-whitelist.dz using high-trust whitelist: '%s')modify_cache_aftermodify_cache_beforeget_license_key)r   __init___cachechrootr8   r9   r:   existsapt_pkgread_config_fileconfigisdirread_config_dirclear	find_filedirnamer   frontend_lockstatus_lockfind_dirarchive_lock
lists_lockinit_systemr=   _high_trust_repositoriesrE   debug_status_orig_status_frozen_load_plugins)selfrV   load_pluginsapt_conf_fileapt_conf_dirstatus_filearchives_dir	lists_dirs           r   rT   zAptWorker.__init__   s   D&,7 ;;GGLL1CDMww~~m,((G77<<0DELww}}\*''E$*GNN5!3577<<-4/GNN/0NN  !45NN  10;f0DGNN,-1L1728GNN,-!..223GHK&(ggll277??;3O1@'BD#$&GGLL1M17%9D!">>223IJL%'WW\\,%GD"//0CDI#%77<<	6#BDOO ! )GGGLL007JL)M% 			.1N1NN	P $NN445IJ"  3 4 r   c                 F   |st         j                  j                  | j                        }| j                  j	                         D ]B  }|j                  |       |j                  |       |j                  s2|j                  |       D || j                  vrt        j                  d|z         y| j                  |   D ]-  }t        j                  d||       	  ||| j                         / y# t        $ r%}t        j                  d|d|       Y d}~Zd}~ww xY w)z!Call all plugins of a given type.z$There isn't any registered %s pluginFzCalling %s plugin: %szFailed to call z	 plugin:
NT)aptcacheProblemResolverrU   get_changesr]   protectmarked_deleteremovepluginsrE   rg   rD   critical)rk   nameresolverr3   pluginrF   s         r   _call_pluginszAptWorker._call_plugins   s     yy00=H{{..0 )s#  %$$OOC(	)
 t||#II<tCDll4( 	PFII-tV<Px-	P   PNOOPs   C22	D ;DD c                    	 t        j                  |       |j                  t        k(  s| j	                         s| j                  |       |j                  t        k(  r | j                  |fi |j                   n|j                  t        k(  r | j                  |fi |j                   n|j                  t        k(  r | j                  |fi |j                   nS|j                  t        k(  r | j                  |fi |j                   n!|j                  t        k(  r | j                   |fi |j                   n|j                  t"        k(  r0 | j$                  ||j&                  t(           fi |j                   n|j                  t*        k(  r| j-                  |       n|j                  t.        t0        t2        t4        t6        t8        fv rK| j:                  |j<                  k(  r2| j?                  |       t@        |_!        	 t        jD                          y| jG                  |       |j                  t8        k(  r| jI                  |       n|j                  tJ        k(  r | jL                  |fi |j                   nr|j                  tN        k(  r | jP                  |fi |j                   nA| jR                  r5| jR                  jT                  rtW        tX        | j[                  |            |j                  t\        k(  r| j_                  |       n|j                  t`        k(  r | jb                  |fi |j                   np|j                  t.        t0        t2        t4        fv r | jd                  |g|j&                    n0|j                  t6        k(  r | jf                  |fi |j                   t        jD                          y# t        jD                          w xY w)zRun the workerFN)4r   wait_for_lockr#   ROLE_FIX_INCOMPLETE_INSTALLis_dpkg_journal_cleanfix_incomplete_installROLE_ADD_VENDOR_KEY_FILEadd_vendor_key_from_filekwargs"ROLE_ADD_VENDOR_KEY_FROM_KEYSERVERadd_vendor_key_from_keyserverROLE_REMOVE_VENDOR_KEYremove_vendor_keyROLE_ADD_REPOSITORYadd_repositoryROLE_ENABLE_DISTRO_COMPenable_distro_compROLE_RECONFIGUREreconfigurer+   r&   
ROLE_CLEANcleanROLE_REMOVE_PACKAGESr$   ROLE_UPGRADE_PACKAGESr%   ROLE_UPGRADE_SYSTEMROLE_FIX_BROKEN_DEPENDS
marked_tidtid_apply_changesEXIT_SUCCESSexitrelease_open_cachefix_broken_dependsROLE_UPDATE_CACHEupdate_cacheROLE_ADD_LICENSE_KEYadd_license_keyrU   broken_countTransactionFailedERROR_CACHE_BROKEN_get_broken_detailsROLE_PK_QUERYqueryROLE_INSTALL_FILEinstall_filecommit_packagesupgrade_systemrk   r0   s     r   _run_transactionzAptWorker._run_transaction   s   7	u%

99224++E2zz55---eDu||DAA2225IELLI55&&&u==22###E:U\\:66'''>>//   ~(F 1#(<<1z)

5! **!57L!68L!4!8!: : //UYY.##E*)
0 LLN-   'zz44''.00!!!%85<<833$$$U;ell;!9!9'(:(,(@(@(GI Izz]*

5!00!!!%85<<8 46K 57K M M$$$U<U^^<22###E:U\\:LLNDLLNs   HO #FO O-c	           	      :   t         j                  d||||||       | j                  j                         5  t        j
                  j                  | j                        }	| j                  ||	       | j                  ||	d       | j                  ||	       | j                  ||	d       | j                  ||	       | j                  ||	       | j                  ||	       ddd       | j                  |	       |s| j                  |       yy# 1 sw Y   0xY w)a  Perform a complex package operation.

        Keyword arguments:
        trans - the transaction
        install - list of package names to install
        reinstall - list of package names to reinstall
        remove - list of package names to remove
        purge - list of package names to purge including configuration files
        upgrade - list of package names to upgrade
        downgrade - list of package names to upgrade
        simulate -- if True the changes won't be applied
        z+Committing packages: %s, %s, %s, %s, %s, %sT)	reinstall)purgeN)rE   inforU   actiongrouprs   rt   ru   _mark_packages_for_installation_mark_packages_for_removal_mark_packages_for_upgrade_mark_packages_for_downgrade_resolve_depends_check_obsoleted_dependenciesr   )
rk   r0   installr   ry   r   upgrade	downgradesimulater}   s
             r   r   zAptWorker.commit_packages!  s    	>)VUGY	H[[$$& 		3yy00=H00(C00H;? 1 A++FH=++E84+H++GX>--iB!!%2		3 	**5(;& 		3 		3s   B,DDc                 N   | j                  d|       	 |j                          | j                  d|      r	 |j                          yy# t        $ r" t        t        | j                  |d            w xY w# t        $ r$ | j                  |d      }t        t        |      w xY w)z9Resolve the dependencies using the given ProblemResolver.rR   F)nowrQ   N)r   resolveSystemErrorr   ERROR_DEP_RESOLUTION_FAILEDr   )rk   r0   r}   detailss       r   r   zAptWorker._resolve_depends?  s    0(;	P 2H=N  " >  	P#$?$($<$<U$<$NP P	P  N225e2D'(CWMMNs   A	 A7 	+A47-B$c                      fd}g } j                   j                         D ]Z  }|j                  s|j                  j                  D ]2  } ||j
                  |      s|j                  |j
                          Z \ |S )z< Return a list of packages that come from a high-trust repo c                     j                   D ]>  \  }}}||j                  k(  s||j                  k(  s't        j                  ||       s> y y)NTF)rf   r@   rA   rematch)pkgname	pkgoriginr@   rA   regexprk   s        r   _in_high_trust_repositoryzEAptWorker._get_high_trust_packages.<locals>._in_high_trust_repositoryP  sQ    -1-J-J  )	6i...!Y%8%881	 
 r   )rU   rv   marked_install	candidateoriginsr|   append)rk   r   from_high_trust_repor3   r@   s   `    r   _get_high_trust_packagesz"AptWorker._get_high_trust_packagesN  sy    	  ";;**, 	C!!!mm33 F06B,33CHH=	 $#r   c                 >   g }| j                   j                         D ]}  }|j                  s%|j                  s|j                  s|j
                  s4d}|j                  j                  D ]  }||j                  z  } |rc|j                  |j                          |S )z/Return a list of unauthenticated package names F)rU   rv   r   marked_downgrademarked_upgrademarked_reinstallr   r   trustedr   r|   )rk   r"   r3   r   r@   s        r   _get_unauthenticatedzAptWorker._get_unauthenticateda  s    ;;**, 		5C""((&&((!mm33 .Fv~~-G.#**3884		5 r   c                    |D cg c]  }| j                  |       c}D ]m  \  }}}|j                  d      \  }}}	|	dk7  }
	 | j                  |   }|rf|j                  s$t	        t        t        d      |j                        |r|j                  j                  |k7  rt	        t        t        d      ||      |j                  rg|re||j                  j                  k(  rL|r0||j                  j                  D cg c]  }|j                   c}v rt	        t        t        d      |      |r	 |j                  |   |_        n|r| j#                  ||       |j%                  dd|
       |j'                  |       |j)                  |       p y	c c}w # t        $ r t	        t
        t        d      |      w xY wc c}w # t        $ r t	        t
        t        d      ||      w xY w)
Mark packages for installation.#autoPackage %s isn't availablePackage %s isn't installedz$The version %s of %s isn't installedzPackage %s is already installed%The version %s of %s isn't available.FN)_split_package_id	partitionrU   KeyErrorr   ERROR_NO_PACKAGE_is_installedERROR_PACKAGE_NOT_INSTALLEDr|   	installedversionr   archiveERROR_PACKAGE_ALREADY_INSTALLEDversionsr   _set_candidate_releasemark_installr]   rw   )rk   r+   r}   r   r3   pkg_namepkg_verpkg_relsepauto_marker	from_userr@   s               r   r   z)AptWorker._mark_packages_for_installationp  s    7?+@/2 ,0+A+A#+F +@ .	"&Hgw)1););C)@&Hc;$.I2kk(+
 ''+,G,-.J,K,/HH6 6 s}}44?+,G,- /: -;,3X? ? $$3==+@+@ @ +.==+@+@!B!' "( !B B/;?@(L L P$'LL$9CM
 ++C9UE95NN3S!].	" +@  2'(8()*F(G(02 22,!B   P+,<,- /; -<=DhP PPs#   FF<F>0G$F;%G(c                 f   d|_         t        |_        t        j                  d      }	 t               }t        j                  j                         }|j                  |       |j                  |       |j                          t        j                  |       y# t        j                  |       w xY w)zEnable given component in the sources list.

        Keyword arguments:
        trans -- the corresponding transaction
        component -- a component, e.g. main or universe
        e      N)progressSTATUS_COMMITTINGstatusr8   umaskr   
aptsourcesdistro
get_distroget_sourcesenable_componentsave)rk   r0   rA   	old_umasksourceslistr   s         r   r   zAptWorker.enable_distro_comp  s     (HHUO		 %-K&&113F{+##I.HHYBHHYs   AB B0c                    d|_         t        |_        |rs|j                  d      s|dz  }t        j
                  j                  d      }t        j                  j                  |t        j                  j                  |            }nd}t        j                  d|      r@| j                  |      }t        j
                  j                  d      }	|sd|	z  }n|d|	z  z  }	 t               }
|
j!                  ||||||	      }|j"                  r
t%               	 |
j'                          y#  t(        j+                  d
        xY w)a  Add given repository to the sources list.

        Keyword arguments:
        trans -- the corresponding transaction
        src_type -- the type of the entry (deb, deb-src)
        uri -- the main repository uri (e.g. http://archive.ubuntu.com/ubuntu)
        dist -- the distribution to use (e.g. karmic, "/")
        comps -- a (possible empty) list of components (main, restricted)
        comment -- an (optional) comment
        sourcesfile -- an (optinal) filename in sources.list.d
        r   z.listDir::Etc::sourcepartsNz (http|https|ftp)://\S+?:\S+?@\S+Dir::etc::netrczcredentials stored in %sz; credentials stored in %s)filezadding repository)r   r   r   endswithrX   rZ   rb   r8   r9   r:   basenamer   r   "_store_and_strip_password_from_urir^   r   rI   invalidRepositoryInvalidErrorr   rE   	exception)rk   r0   src_typeuridistcompscommentsourcesfiledirauth_conf_pathsourcesentrys               r   r   zAptWorker.add_repository  s    (''0w&..))*ABC'',,sBGG,<,<[,IJKK886<99#>C$^^556GHN4~E7.HH	!mGKK#tUG%0   2E}},..  LLN		MM-.s   !7D* *Ec           	      
   	 t        |      }|j                  j                  |j                  d|j                  dd      }||j                  z   }|t        j                  j                  d      }i }d}t        j                  j                  |      rSt        j                  |      j                  }t!        |d      5 }	|	j#                         j%                  d      }ddd       d	|d
|j                  d|j                  d}
||v rdt'        j(                  |      dt'        j(                  ||   d         dt'        j(                  ||   d         }d|d
|j                  d|j                  }t'        j*                  ||      st        j                  d|z         |
|z   }nt'        j,                  |||      }n||
z  }d}	 t        j.                  |      t.        j0                     }	 t9        j:                  t        j                  j=                  |      t        j                  j?                  |      d      }|jA                  |jC                  d             |jE                          t        jF                  |jH                  |       t        jJ                  ||       tM        |jN                  ||j                  |jP                  |jR                  f      S # t        $ r"}t        j                  d|       |cY d}~S d}~ww xY w# 1 sw Y   6xY w# t2        $ r)}|j4                  t4        j6                  k7  r Y d}~Yd}~ww xY w# t2        $ r"}t        j                  d|z         Y d}~d}~ww xY w)zExtract the credentials from an URI. Store the password in
        auth.conf and return the URI without any password information.
        zFailed to urlsplit '%s'N:@ r  rbzUTF-8z	
machine z login z
 password 
z
machine\s+z\s+login\s+r   z\s+password\s+r   zmachine zCcan not replace existing netrc entry for '%s' prepending it insteadi  F)r  prefixdeletezFailed to write auth.conf: '%s')*r   
ValueErrorrE   warningnetlocreplaceusernamepasswordr9   rX   rZ   r^   r8   rW   netrchostsopenrC   decoder   escapesearchsubstatST_MODEOSErrorerrnoENOENTtempfileNamedTemporaryFiler_   r  writeencodecloserenamer|   chmodr   schemer   fragment)rk   r  r  resrF   netloc_publicmachinenetrc_hostsnetrc_hosts_as_textfnew_netrc_entry
sub_regexpreplacementmoderK   auth_conf_tmps                   r   r	  z,AptWorker._store_and_strip_password_from_uri  s   	3-C 

**s||7:||,E+-/  #((*!$^^556GHN  77>>.)#kk.9??.$/ C1*+&&(//'*B'C
 S\\3<<1 k!		'"		+g.q12		+g.q124J
 s||5K 99Z)<= 46=> ?&58K&K#&(ff-@'B#  ?2 	77>*4<<8D
	C$77GGOON3ww''7M  3 : :7 CD!IIm((.9HH^T*
 3::}chh		<<) * 	*A  	KK159J	 C C<  	ww%,,& '	  	CKK9EABB	CsZ   K'  L&L" 7B8M '	L0LLLL"	M+MM	N M==Nc                 t   t         j                  d||       	 t        |      }|j                  dvrt	        t
        t        d      |      	 t        |d       t        |_
        d|_        t        |      5 }|j                  t        j                  j                   ||       ddd       j"                  d	k7  r&t	        t
        t        d
      |||j$                        y# t        $ r t	        t
        t        d      |      w xY w# t        $ r t	        t
        t        d      |      w xY w# 1 sw Y   xY w)a  Add the signing key from the given (keyid, keyserver) to the
        trusted vendors.

        Keyword argument:
        trans -- the corresponding transaction
        keyid - the keyid of the key (e.g. 0x0EB12F05)
        keyserver - the keyserver (e.g. keyserver.ubuntu.com)
        z'Adding vendor key from keyserver: %s %sz The keyserver URL is invalid: %s)hkpldapldapshttphttpsz"Invalid protocol of the server: %s   Invalid key id: %sr   Nr   z5Failed to download and install the key %s from %s:
%s)rE   r   r   r  r   ERROR_KEY_NOT_INSTALLEDr   r8  intSTATUS_DOWNLOADINGr   r   r   runrs   authadd_key_from_keyserver_child_exitoutput)rk   r0   keyid	keyserverr:  r   s         r   r   z'AptWorker.add_vendor_key_from_keyserver+  sJ    	:E9M	/9%C ::FF#$; %&&J$K$-	/ /
	DrN *& 	L(LL88%K	L1$#$;$% '8 %9$)9hooG G %/  	/#$; %&&H$I$-	/ /	/  	D#$; %&&:$;UD D	D	L 	Ls#   C  D 6,D. $D$D+.D7c                 P   t         j                  d|       d|_        t        |_        t        |      5 }|j                  t        j                  j                  |       ddd       j                  dk7  r%t        t        t        d      ||j                        y# 1 sw Y   >xY w)zAdd the signing key from the given file to the trusted vendors.

        Keyword argument:
        path -- absolute path to the key file
        zAdding vendor key from file: %sr   Nr   z%Key file %s couldn't be installed: %s)rE   r   r   r   r   r   rP  rs   rQ  add_key_from_filerS  r   rM  r   rT  )rk   r0   r9   r   s       r   r   z"AptWorker.add_vendor_key_from_fileV  s     	2D9(& 	;(LL33T:	;1$#$;$%&M$N$((//; ; %	; 	;s   +BB%c                    t         j                  d|       d|_        t        |_        	 t        |d       t        |      5 }|j                  t        j                  j                  |       ddd       j                  dk7  r%t        t        t        d      ||j                         y# t        $ r t        t        t        d      |      w xY w# 1 sw Y   exY w)zRemove repository key.

        Keyword argument:
        trans -- the corresponding transaction
        fingerprint -- fingerprint of the key to remove
        zRemoving vendor key: %sr   rK  rL  Nr   z/Key with fingerprint %s couldn't be removed: %s)rE   r   r   r   r   rN  r  r   ERROR_KEY_NOT_REMOVEDr   r   rP  rs   rQ  
remove_keyrS  rT  )rk   r0   fingerprintr   s       r   r   zAptWorker.remove_vendor_keyf  s     	*K8(	JR   & 	;(LL,,k:	;1$#$9$% '4 %5$/B B %  	J#$9 %&&:$;[J J	J
	; 	;s   B) +C)$CCc                    t         j                  d|       t        |_        | j	                  |||      }|j
                  \  }}}| j                  d       |r|S | j                         5  t        |      dkD  st        |      dkD  r| j                  |dd       t        |dd	      }	|j                  |	      }
|xj                  |	j                  z  c_        |
rt        t        |j                        	 d
d
d
       y
# 1 sw Y   y
xY w)aV  Install local package file.

        Keyword argument:
        trans -- the corresponding transaction
        path -- absolute path to the package file
        force -- if installing an invalid package is allowed
        simulate -- if True the changes won't be committed but the debfile
                    instance will be returned
        z!Installing local package file: %srQ   r   )   !   )"   ?   )fetch_rangeinstall_range@   _   beginendN)rE   r   STATUS_RESOLVING_DEPr   _check_deb_filerequired_changesr   _frozen_statuslenr   r   r   rT  r   ERROR_PACKAGE_MANAGER_FAILED)rk   r0   r9   forcer   debr   ry   unauthdeb_progressr:  s              r   r   zAptWorker.install_file  s     	4d;+""5$6$'$8$8!&&/0J  " 
	67|a3v;?##Ex2: $ < 5U""ML++l+CLLL///L'(D(-6 6 
	6 
	6 
	6s   *BC??Dc                 b   |D cg c]  }| j                  |       c}D ]  \  }}}	 | j                  |   }|j                  s&|j                  st        t        t        d      |      |j                  du rt        t        t        d      |      |r*|j                  |k7  rt        t        t        d      ||      |j                  d|       |j                  |       |j                  |       |j                  |        yc c}w # t        $ r t        t        t        d      |      w xY w)r   r   r   TzPackage %s cannot be removed.z%The version %s of %s is not installedFN)r   rU   r   r   r   r   r   installed_filesr   	essential"ERROR_NOT_REMOVE_ESSENTIAL_PACKAGEr   mark_deleter]   rw   ry   )rk   r+   r}   r   r3   r   r   r   s           r   r   z$AptWorker._mark_packages_for_removal  sC    7?+@/2 ,0+A+A#+F +@ 	!&Hgw2kk(+
 ##C,?,?'(C()*F(G(02 2 }}$'(J()*I(J(02 2 3==G3'(C() +6 )78?K K OOE5)NN3S!OOC /	! +@  2'(8()*F(G(02 22s   DD

$D.c                 :   |j                   sy|s)t        j                  j                  | j                        }t               }| j                  j                         5  | j                  j                         D ]+  }|j                  s| j                  |j                  |      }- |D ]K  }|| j                  v s| j                  |   }|j                  s.|j                  s;|j                  d       M | j                  ||       ddd       y# 1 sw Y   yxY w)zSMark obsoleted dependencies of to be removed packages
        for removal.
        NF)remove_obsoleted_dependsrs   rt   ru   rU   r.   r   rv   rx   _installed_dependenciesr|   r   is_auto_removablerw  r   )rk   r0   r}   installed_depsr3   dep_names         r   r   z'AptWorker._check_obsoleted_dependencies  s     --yy00=H[[$$& 	3{{..0 2$$%)%A%A.&2N2 + /t{{*++h/C''C,A,A.	/ !!%2	3 	3 	3s$   *D1D:DD#%DDc                 j   |s
t               }|| j                  vr|S | j                  |   j                  j                  }|s|S dD ][  }	 |j                  |   D ]F  }|d   j
                  j                  }||vs!|j                  |       || j                  ||      z  }H ] |S # t        $ r Y kw xY w)zARecursively return all installed dependencies of a given package.)
PreDependsDepends
Recommendsr   )
r.   rU   _pkgcurrent_verdepends_list
target_pkgr|   rI   rz  r   )rk   r   all_depscursecdepr}  s          r   rz  z!AptWorker._installed_dependencies  s    
 uH4;;&Okk(#((44O: 		C++C0 KC"1v0055Hx/ X. D$@$@AI%K K	K		   s   
/B&:(B&&	B21B2c                    |D cg c]  }| j                  |       c}D ]}  \  }}}	 | j                  |   }|j                  st        t        t        d      |      |j                  }|r|j                  rR|j                  j                  |k  r9t        t        t        d      |j                  j                  |j                        |j                  rR|j                  j                  |k(  r9t        t        t        d      |j                  j                  |j                        	 |j                  |   |_        nt        t        t        d      |      |j                  ddd       |j!                  |       |j#                  |       |j%                  |        y	c c}w # t        $ r t        t        t        d      |      w xY w# t        $ r t        t        t        d      ||      w xY w)
zMark packages for downgrade.r   r   z0The former version %s of %s is already installed)The version %s of %s is already installedz$The version %s of %s isn't availablez0You need to specify a version to downgrade %s toFTN)r   rU   r   r   r   r   r   r   is_auto_installedr   r   r|   r   r   r   r   	mark_autor]   rw   rk   r+   r}   r3   r   r   r   r   s           r   r   z&AptWorker._mark_packages_for_downgrade  s    7?+@/2 ,0+A+A#+F +@ )	"&Hgw2kk(+
 ##'(C()*F(G(02 2 ((D==S]]%:%:W%D+,<,- /E -F,/MM,A,A388M M ]]s}}'<'<'G+,K,- /E -F,/MM,A,A388M MO$'LL$9CM ((8() +< )=(02 2
 UE40MM$NN3S!S)	" +@  2'(8()*F(G(02 220   O+,<,- /: -;<CXO OOs   F"F')G'$G%G3c                 0   |D cg c]  }| j                  |       c}D ]  \  }}}	 | j                  |   }|j                  st        t        t        d      |      |j                  }|r|j                  rft        j                  |j                  j                  |      dk(  r9t        t        t        d      |j                  j                  |j                        |j                  rft        j                  |j                  j                  |      dk(  r9t        t        t        d      |j                  j                  |j                        	 |j                  |   |_        n|r| j#                  ||       |j%                  ddd	       |j'                  |       |j)                  |       |j+                  |        y
c c}w # t        $ r t        t        t        d      |      w xY w# t        $ r t        t        t        d      ||      w xY w)zMark packages for upgrade.r   r   r
   z/The later version %s of %s is already installedr   r  r   FTN)r   rU   r   r   r   r   r   r   r  r   rX   version_comparer   ERROR_PACKAGE_UPTODATEr|   r   r   r   r   r  r]   rw   r  s           r   r   z$AptWorker._mark_packages_for_upgrade  s    7?+@/2 ,0+A+A#+F +@ *	"&Hgw2kk(+
 ##'(C()*F(G(02 2 ((DMM++CMM,A,A,3589:+,B,- /E -F,/MM,A,A388M M mm--cmm.C.C.57:;<+,B,- /E -F,/MM,A,A388M MP$'LL$9CM ++C9UE40MM$NN3S!U*	" +@  2'(8()*F(G(02 226   P+,<,- /; -<=DhP PPs   GG	G0	$G-0%Hc                    | j                   D ]-  }|j                  D cg c]  }|j                  |k(  r| c}s- n& t        t        t        d      | j                  |      | j                  j                          | j                  j                  j                  | j                  |j                  |       | j                  j                          yc c}w )zASet the candidate of a package to the one from the given release.z1The package %s isn't available in the %s release.N)r   r   r   r   r   r   r|   _pcachecache_pre_change	_depcacheset_candidate_releaser  _candcache_post_change)r3   r   r   r@   s       r   r   z AptWorker._set_candidate_releaseF  s    
 || 	MG%,__ 26~~0  2	M
 $$4$% '8 %9:=((GM M 	$$&33CHHgmm4;	=%%'2s   Cc                 2   d }t         j                  d       t        |dd      }|rP|j                  d      s?t        j
                  j                  d      }t        j                  j                  ||      }|rt        j
                  j                  d      }t        j
                  j                  d      }d|v rdt        j                  j                  |      }t        j                  j                  ||g      } |||      s; |||      s2t        d	|d
|d      t        j                  j                  ||      }	 | j                  j                  ||       | j7                  |dd       y# t         j"                  j$                  $ r4}	|j&                  r
t)               t+        t,        t/        |	            d}	~	wt         j"                  j0                  $ r t)               t         j"                  j2                  $ r t+        t4              w xY w)zUpdate the cache.

        Keyword arguments:
        trans -- the corresponding transaction
        sources_list -- only update the repositories found in the sources.list
                        snippet by the given file name.
        c                     t         j                  j                  |       t         j                  j                  |      k(  S )z#Small helper to compare two pathes.)r8   r9   normpath)firstseconds     r   compare_pathesz.AptWorker.update_cache.<locals>.compare_pathesa  s+    77##E*bgg.>.>v.FFFr   zUpdating cache
   Z   rf  /r  zDir::Etc::sourcelistz,Only alternative sources.list files inside 'z' are allowed (not 'z'))sources_listN[   re  )rE   r   r   
startswithrX   rZ   rb   r8   r9   r:   r^   abspathcommonprefixAptDaemonErrorrU   updaters   rt   FetchFailedException	cancelledTransactionCancelledr   ERROR_REPO_DOWNLOAD_FAILEDstrFetchCancelledExceptionLockFailedExceptionERROR_NO_LOCKr   )
rk   r0   r  r  r   r  basedirsystem_sourcescommon_prefixrF   s
             r   r   zAptWorker.update_cacheX  s   	G 	!",U""E 7 7 <..))*ABC77<<\:L nn--.EFG$^^556LMNl"!ww|< !# 4 4lG5L M&}g>&|^D(*1<*A B B  "ww||G\B	3KKxlC 	bb1 yy-- 	4 *,,'(B(+E
4 4yy00 	)&((yy,, 	3#M22	3s   E4 4H/G  AHc                 h   t         j                  d|z         t        |_        | j	                  d       	 | j
                  j                  |        | j	                  d       | j                  |       |s| j                  |       yy# t        $ r}t        t        t        |            d}~ww xY w)a*  Upgrade the system.

        Keyword argument:
        trans -- the corresponding transaction
        safe_mode -- if additional software should be installed or removed to
                     satisfy the dependencies the an updates
        simulate -- if the changes should not be applied
        z!Upgrade system with safe mode: %srR   )dist_upgradeNrQ   )rE   r   ri  r   r   rU   r   r   r   r   r  r   r   )rk   r0   	safe_moder   exceps        r   r   zAptWorker.upgrade_system  s     	4y@A+01	MKK]; 	/0**51& 	  	M#$?ULL	Ms   B
 
	B1B,,B1c                 ~   t         j                  d       t        |_        | j	                         5  t        |      5 }|j                          ddd       ddd       |xj                  j                  z  c_        |j                  dk7  rt        t        |j                        y# 1 sw Y   ZxY w# 1 sw Y   ^xY w)zRun dpkg --configure -a to recover from a failed installation.

        Keyword arguments:
        trans -- the corresponding transaction
        zFixing incomplete installsNr   )rE   r   STATUS_CLEANING_UPr   rl  r   rP  rT  rS  r   rn  )rk   r0   r   s      r   r   z AptWorker.fix_incomplete_install  s     	-.)  " 	*51 X	 	'1$#$@$)LL2 2 % 	 	s"   B3B'B3'B0	,B33B<c                 l   t         j                  d       | j                         5  t        |      5 }|j	                  ||       ddd       ddd       |xj
                  j
                  z  c_        |j                  dk7  rt        t        |j
                        y# 1 sw Y   ZxY w# 1 sw Y   ^xY w)a	  Run dpkg-reconfigure to reconfigure installed packages.

        Keyword arguments:
        trans -- the corresponding transaction
        packages -- list of packages to reconfigure
        priority -- the lowest priority of question which should be asked
        zReconfiguring packagesNr   )	rE   r   rl  r   rP  rT  rS  r   rn  )rk   r0   r+   priorityr   s        r   r   zAptWorker.reconfigure  s     	)*  " 	1.u5 1Xx01	1 	'1$#$@$)LL2 2 %1 1	1 	1s"   B*BB*B'	#B**B3c                    t         j                  d       t        |_        	 | j                  j
                  j                          |s| j                  |       yy# t        $ r  t        t        | j                  |            w xY w)zTry to fix broken dependencies.

        Keyword arguments:
        trans -- the corresponding transaction
        simualte -- if the changes should not be applied
        zFixing broken dependsN)rE   r   ri  r   rU   r  
fix_brokenr   r   r   r   r   )rk   r0   r   s      r   r   zAptWorker.fix_broken_depends  s     	()+	EKK!!,,. &   	E#$?$($<$<U$CE E	Es   $A )Bc                    d| _         t        |_        |s| j                  }t        j
                  j                  d|       t	        j                          t        ||||      }	 t        | j                  t        j                  j                        s%t        j                  j                  |      | _
        y| j                  j                  |       y# t        $ r}t!        t"        t%        |            d}~ww xY w)a.  Open the APT cache.

        Keyword arguments:
        trans -- the corresponding transaction
        start -- the begin of the progress range
        end -- the end of the the progress range
        quiet -- if True do no report any progress
        status -- an alternative dpkg status file
        NrP   )rg  rh  quiet)r   STATUS_LOADING_CACHEr   rh   rX   rZ   r.   re   r   
isinstancerU   rs   rt   Cacher'  r   r   ERROR_NO_CACHEr  )rk   r0   rg  rh  r  r   r   r  s           r   r   zAptWorker._open_cache  s     +&&F/8%e5c,13	@dkk399??;!iiooh7  * 	@#NCJ??	@s   %AC 8C 	C;C66C;c                     t         j                  j                  t         j                  j                  | j                        d      }t        j
                  |      D ]  }|j                         s y y)zKReturn False if there are traces of incomplete dpkg status
        updates.zupdates/FT)r8   r9   r:   r_   rh   listdirisdigit)rk   status_updatesdentrys      r   r   zAptWorker.is_dpkg_journal_clean  sX     bggood6G6G&H&02jj0 	F~~	 r   c                    | j                   j                         }|sy|D ]u  }|j                  s|j                  du s5|j                  r|j                  j
                  dk(  s|j                  dk(  sSt        t        t        d      |j                         |j                  s>|j                  r2t        t        dj                  t        |j                                    |j                  r
t!               d|_        t%        ||d   |d	   
      }t'        ||d   |d	   
      }| j)                         5  	 dt*        j,                  j.                  j0                  v r)| j                   j/                  |||j                         n| j                   j/                  ||       |xj2                  |j2                  z  c_        	 ddd       y# t*        j4                  j6                  $ r}t        t8        t;        |            d}~wt*        j4                  j<                  $ r t!               t>        $ r}	t@        |_!        tE        |dd
      5 }
|
jG                          ddd       n# 1 sw Y   nxY w|j2                  
j2                  z   }|xj2                  |z  c_        t        tH        |	d|j2                        d}	~	ww xY w# 1 sw Y   yxY w)ae  Apply previously marked changes to the system.

        Keyword arguments:
        trans -- the corresponding transaction
        fetch_range -- tuple containing the start and end point of the
                       download progress
        install_range -- tuple containing the start and end point of the
                         install progress
        NTrequired	aptdaemonzPackage %s cannot be removed Fr   r
   rf  allow_unauthenticated)r  r  re  : )%rU   rv   rx   ru  r   r  r|   r   rv  r   r  r"   ERROR_PACKAGE_UNAUTHENTICATEDr:   sortedr  r  cancellabler   r   rl  rs   r  commit__doc__rT  rt   r  ERROR_PACKAGE_DOWNLOAD_FAILEDr  r  r   r  r   r   rP  rn  )rk   r0   rb  rc  changesr3   fetch_progressinst_progressrF   r  prorT  s               r   r   zAptWorker._apply_changes  sn    ++))+ 	2C  cmmt&;'*}}'*}}'='='K&)hh+&='(J()*H(I(+2 2	2 **u/D/D#$A$'HHVE4I4I-J$KM M??&((!.uKN3>q>C-e=;K2?2BD  " 	55*cii.>.>.F.FFKK&&~}=B=X=X ' Z KK&&~}E   4 44/	5 	5 9911 4'(E(+E
4 49944 -*,, J1.uBBG 3GGI  &--

:&'(D495<<(HJ JJ	5 	5sV   7K	9A+G$K	K*H2K5KI(	K(I1-AKKK		Kc              #   F  K   t        j                  d      }t        j                  | j                  |       t
        j                  j                  |d      | _        	 d t        j                  |       d| _        y# t        j                  |       d| _        w xY ww)zOFreeze the status file to allow simulate operations during
        a dpkg call.zaptdaemon-frozen-status)r  r   N)
r1  mkdtempshutilcopyrh   r8   r9   r:   ri   rmtree)rk   
frozen_dirs     r   rl  zAptWorker._frozen_status4  sw      %%-FG
D%%z2 ggll:x@	'MM*%"&D MM*%"&Ds   AB!B  #B! BB!c                     t         )z'Process a PackageKit query transaction.)NotImplementedErrorr   s     r   r   zAptWorker.queryA  s    !!r   c                 >   g g g g g g g g}g }g }g }d}g x}x}x}	x}
x}x}}|j                   t        t        t        t        t
        t        t        fvr|ddg g fS | j                  r| j                  }n| j                  }| j                  |d|       |j                   t        k(  r| j                  |d       n| j                  j                  rt        t        | j!                  |            |j                   t        k(  rT| j#                         D ]   }|j$                  s|j'                  |       "  | j(                  |fddi|j*                   np|j                   t        k(  r | j,                  |fddi|j*                  }|j'                  |j.                         	 t1        |d   j3                  dd            d	z  }|t4        j6                  kD  rt9        d
|z        	 	 | j                  |j.                     }|jD                  r7|dk7  r||jF                  jH                  z  }g |j.                  gg g g g g|_%        n|j.                  gg g g g g g|_%        ni|jJ                  D cg c]*  }|D cg c]  }tM        jN                  d|d      d    c}, c}}\  }}}	}
}} | jP                  |g|jJ                  ddi | j                  jS                         }g }|D ]  }|jT                  rY|jD                  rM|jV                  |vr?|jV                  d|jX                  jZ                  }|t\           j'                  |       n`|j^                  rM|jV                  |vr?|jV                  d|jX                  jZ                  }|t`           j'                  |       n|jb                  rL|jV                  |vr>|jV                  d|jX                  jZ                  }|td           j'                  |       n|jf                  rL|jV                  |vr>|jV                  d|jX                  jZ                  }|th           j'                  |       nW|jj                  rK|jV                  |	vr=|jV                  d|jF                  jZ                  }|tl           j'                  |       |j'                  |jV                          | jo                         }| jq                         }|D ]L  }|jr                  s|jV                  d|jX                  jZ                  }|tt           j'                  |       N 	 | j                  jv                  }|| j                  j~                  z   }|||||fS # t:        t<        t>        t8        f$ r4 |j*                  d   s!|jA                  d      }t        tB        |      Y w xY w# t:        $ r |j.                  gg g g g g g|_%        Y !w xY wc c}w c c}}w # tx        $ r}t        tz        t}        |            d }~ww xY w)Nr   T)r  r   )r   r   zInstalled-Size,r     zSize is too large: %s Bytesro  z[The package doesn't provide a valid Installed-Size control field. See Debian Policy 5.6.20.z(=|/)r
   r    )@r#   r$   r   r   r   r%   r   r   ri   rh   r   r   rU   r   r   r   r   _iterate_packagesis_upgradabler   r   r   r   r   rN  r"  sysmaxsizeOverflowErrorr   AttributeErrorr  gettextERROR_INVALID_PACKAGE_FILEr   r   installed_sizer+   r   r-   r   rv   r   r|   r   r   r*   r   r&   r   r)   r   r,   rx   r'   r   r   marked_keep	PKGS_KEEPrequired_downloadr   ERROR_INCOMPLETE_INSTALLr  required_space)rk   r0   dependsr"   r/   	skip_pkgssizeinstalls
reinstallsremovalspurgesupgradesupgradables
downgradesstatus_pathr3   rp  msglstr  r  changes_namespkg_strr  rF   r  s                             r   _simulate_transactionzAptWorker._simulate_transactionE  s   r2r2r2. 		 	: 	 	6 	H 	{ ::35J13G24E57 7 Aq"b(( --K++Kd;?::00##ED#9[[%%#$6$($<$<U$CE EZZ..--/ ,$$&&s+,  DEEEZZ,,#$##EIDIELLICS[[)M 3/088bABTI #++%'(E(LMM &Ikk#++. ## qy < <<&(3;;-RR%HEN'*{{mRRR%HEN 27&A*- 58'9+0 (*xx'B1'E '9 &A#Xz8Vz !D  GG$G++))+ 	+C""s'7'7HHH,%(XXs}}/D/DE%,,W5%%#((**D%(XXs}}/D/DE'..w7%%#((**D%(XXs}}/D/DE'..w7##(@%(XXs}}/D/DE%,,W5""sxxx'?%(XXs}}/D/DE$++G4  *%	+( 335";;= 	3C%(XXs}}/D/DE	"))'2	3	J $ = =  : ::*NO#% 	%K nj-H M||G,-- )K LC ,,FLL	 -M  E#&;;-RR!DE'9 &AT  	J#$<c%jII	JsV   #AU2 'W +	W/4!W*W/<W5 2A	V?>V?!W'&W'*W/5	X>XXc                 X   t        |j                  |j                        5  t        j                  j                  |      st        t        |      	 ddd       	 t        j                  j                  || j                        }	 |j!                         }|st        t"        |j$                        |S # 1 sw Y   cxY w# t        $ r t        t        |      t        $ r}t        t        t        |            d}~ww xY w# t        $ r}t        t"        t        |            d}~ww xY w)zPerform some basic checks for the Debian package.

        :param trans: The transaction instance.

        :returns: An apt.debfile.Debfile instance.
        N)r   uidgidr8   r9   isfiler   ERROR_UNREADABLE_PACKAGE_FILErs   debfile
DebPackagerU   IOErrorrD   r  r  checkr   _failure_string)rk   r0   r9   ro  rp  rF   rets          r   rj  zAptWorker._check_deb_file  s    599eii0 	M77>>$''(EtLL (	M	L++((t{{;C
	M))+C #$?$'$7$79 9
#	M 	M  	I#$A4HH 	L#$>E
KK	L  	M#$?ULL	Ms;   0B4*C  D 4B= !C?!C::C?	D)D$$D)c                    t         |_        t        j                  j	                  d      }|t
        j                  j                  |d      fD ]  }t        j                  |      D ]s  }|dk(  r	t
        j                  j                  ||      }t
        j                  j                  |      sIt        j                  d|       t        j                  |       u  y)zsClean the download directories.

        Keyword arguments:
        trans -- the corresponding transaction
        zDir::Cache::archivespartialr   zRemoving file %sN)r  r   rX   rZ   rb   r8   r9   r:   r  r  rE   rg   ry   )rk   r0   archive_pathr  filenamer9   s         r   r   zAptWorker.clean  s     *~~../EF "'',,|Y"GH 	$CJJsO $v%ww||C277>>$'II0$7IIdO$	$r   c                 J   t         |_        	  | j                  d   d   |j                  |||      \  }}|r|st        t        t        d            | j                  |||       y# t        $ r3}t        j                  d       t        t        t        |            d}~ww xY w)a4  Add a license key data to the given package.

        Keyword arguemnts:
        trans -- the coresponding transaction
        pkg_name -- the name of the corresponding package
        json_token -- the oauth token as json
        server_name -- the server to use (ubuntu-production, ubuntu-staging)
        rS   r   zget_license_key plugin failedNzThe license key is empty)rO  r   rz   r  rD   loggingr  r   !ERROR_LICENSE_KEY_DOWNLOAD_FAILEDr  r   _add_license_key_to_system)rk   r0   r   
json_tokenserver_namelicense_keylicense_key_pathrF   s           r   r   zAptWorker.add_license_key  s     *	02./2599h3={L *K)  {#$E$%&@$AC C 	''+(8	:  	0=>#$E$'J0 0	0s   (A& &	B"/.BB"c                    t         j                  j                  t        j                  j                  d      |j                  d            }|j                         j                  d      s|j                  d      rt        t        t        d            t         j                  j                  |      }t         j                  j                  t        j                  d   | j                  j                  d      |      }|j                  |      st        t        t        d      |      t         j                  j                  |      rt        t        t        d      |      t         j                  j                  |      |k7  r8t        t        t        d      |t         j                  j                  |            t         j                  j!                  t         j                  j#                  |            st        t        t        d	      |      t$        j'                  d
|z         t        j(                  d      }	 t+        |d      5 }|j-                  |       d d d        t        j(                  |       y # 1 sw Y   xY w# t.        $ r t        t        t        d      |      w xY w# t        j(                  |       w xY w)NrO   r  z#!zELFz:The license key is not allowed to contain executable code.z"The license key path %s is invalidz"The license key already exists: %szdThe location of the license key is unsecure since it contains symbolic links. The path %s maps to %sz?The directory where to install the key to doesn't exist yet: %szWriting license key to '%s'r   wzFailed to write key file to: %s)r8   r9   r:   rX   rZ   rb   lstripstripr  r    ERROR_LICENSE_KEY_INSTALL_FAILEDr   r  LICENSE_KEY_ROOTDIRlexistsrealpathr[   r_   rE   r   r   r'  r3  r	  )rk   r   r  r  license_key_path_rootdirr  license_files          r   r  z$AptWorker._add_license_key_to_system  s\    77<<(?(?(F(8(?(?(DF **40&&y1#$D$% 'A %BC C 77++,<=#%77<<NN5!4#;#;#B#B3#G$   **+CD#$D$%&J$K$46 6 77??+,#$D$%&J$K$46 6 77,-1AA#$D$% 'F %G %5$&GG$4$45E$FH H ww}}RWW__-=>?#$D$% 'A %B$46 6
 	.1AABHHRL		 &, 0"";/0 HHY0 0 	6#$D$%&G$H$46 6	6
 HHYs0   J
 I> J
 >JJ
 
$J..J1 1Kc                     t        j                         j                         rFt        j                         j                          t        j                         j                         rEyy)z)Process pending actions on the main loop.N)r	   main_context_defaultpending	iterationrk   s    r   _iterate_mainloopzAptWorker._iterate_mainloop8  sC    '')113%%'113 '')113r   c              #   x   K   t        | j                        D ]  \  }}||z  s| j                          |   yw)zItarte von the packages of the cache and iterate on the
        GObject main loop time for more responsiveness.

        Keyword arguments:
        interval - the number of packages after which we iterate on the
            mainloop
        N)	enumeraterU   r(  )rk   intervalr2   r3   s       r   r  zAptWorker._iterate_packages=  s<      #4;;/ 	ID#(?&&(I	s   8:c           	         |j                  d      }|dz  }| j                  D ]  }|r|j                  s|s|j                  s!|d|j                  z  z  }|r|j
                  }n|j                  }dt        |j                        dz   z  }d}|j                  D ]
  }d}	|j                  D ]  }
|	r
|	dz  }	|	|z  }	|
j                  dv s_t        j                  j                  d	d
      r|
j                  dk(  s0t        j                  j                  dd
      r|
j                  dk(  s}	 | j                  |
j                     }|r|j
                  }n|j                  }	 |r7t        j                  |
j                   |
j"                  |j                         s |
j                  d|
j                  d}	|
j                   r |	d|
j"                  d|
j                   dz  }	| j                  j%                  |
j                        r|	|j                  d      z  }	o|s.|r|	|j                  d      z  }	|	|j                  d      z  }	|r#|	|j                  d      |j                   z  z  }	|	|j                  d      |j                   z  z  }	 |r||z  }||	z  }|dz  } ||z  } |S # t        $ r d}Y aw xY w)ak  Return a message which provides debugging information about
        broken packages.

        This method is basically a Python implementation of apt-get.cc's
        ShowBroken.

        Keyword arguments:
        trans -- the corresponding transaction
        now -- if we check currently broken dependecies or the installation
               candidate
        z/The following packages have unmet dependencies:z

z%s: r  r   r  zor
)r  r  	Obsoletes
DpkgBreaks	ConflictszAPT::Install-RecommendsFr  zAPT::Install-SuggestsSuggestsNr  (z) zbut it is a virtual packagezbut it is not installedz#but it is not going to be installedzbut %s is installedzbut %s is to be installedr  )r  rU   is_now_brokenis_inst_brokenr|   r   r   rm  dependenciesor_dependenciesrawtyperX   rZ   find_br   	check_depr   relationis_virtual_package)rk   r0   r   r  r3   r   indentdep_msgr  or_msgbase_deppkg_depdep_versions                r   r   zAptWorker._get_broken_detailsJ  s    mmMNv;; I	CS..!3!36CHH$$C----CMA-.FG++ =$ # 3 3 ;$H&(&( %,, 1> > %^^223L38:$,,<$^^223J38:%--; <"&++hmm"< *1*;*;K*1*;*;K #7+<+<X=M=M=E=N=N=D__,N *2*:*:HMMJF''0A0A080@0@#B B{{55hmmD%--0M"NN("emm4M&NNF"emm 5C 'D DF5==1F#G#.#6#6$7 8 5==1L#M#.#6#6$7 8g;$p 6)v%GtOG{=$| 7NCSI	T 
S $ +&*+s   *J22K	 K	c                     t         j                  j                  t         j                  j                  t        j
                  j                  d      d            S )z7If a reboot is required to get all changes into effect.rO   zvar/run/reboot-required)r8   r9   rW   r:   rX   rZ   rb   r'  s    r   is_reboot_requiredzAptWorker.is_reboot_required  s=    ww~~bggll7>>+B+B5+I+DF G 	Gr   c                 b    |dv r| j                  |||       y|dk(  r| j                  |       yy)z$Write a configuration value to file.)AutoUpdateIntervalAutoDownloadAutoCleanIntervalUnattendedUpgradePopConParticipationN)_set_apt_config_set_popcon_pariticipation)rk   optionvaluer  s       r   
set_configzAptWorker.set_config  s?     @ @  9,,++E2 -r   c                 n    t               }t        j                  |||       t        j                          y r   )ConfigWritercw	set_valuerX   init_config)rk   rK  rL  r  config_writers        r   rI  zAptWorker._set_apt_config  s%    $
VUH-r   c                    |dv rd}nd}t         j                  j                  t              rCt	        t              5 }|j                         }d d d        t        j                  dd|z        }nNt               }|j                  t	        dd      j                  d             t        |j                         |d	z  }t	        t        d
      5 }|j                  |       d d d        y # 1 sw Y   xY w# 1 sw Y   y xY w)N)Tr
   yesrU  noz(PARTICIPATE=*)(".+?")z\1"%s"z/dev/urandomrr  )host_idparticipater  )r8   r9   rW   _POPCON_PATHr'  rC   r   r+  md5r  _POPCON_DEFAULT	hexdigestr3  )rk   rY  rL  	conf_file
old_configrZ   ms          r   _set_popcon_participationz#AptWorker._set_popcon_participation  s    **EE77>>,' l# .y&^^-
.VV5%-&(F
 AHHT.#.33D9:$1;;=7<(> >F ,$ 	$	OOF#	$ 	$. .	$ 	$s   C)C5)C25C>c                 f   |dk(  r"d}t         j                  j                  |d      S |dk(  r"d}t         j                  j                  |d      S |dk(  r"d}t         j                  j                  |d      S |d	k(  r"d
}t         j                  j                  |d      S |dk(  r| j	                         S y)zReturn a configuration value.rD  z#APT::Periodic::Update-Package-Listsr   rE  z,APT::Periodic::Download-Upgradeable-PackagesFrF  z APT::Periodic::AutocleanIntervalrG  z!APT::Periodic::Unattended-UpgradeGetPopconParticipationN)rX   rZ   find_ir7  _get_popcon_pariticipation)rk   rK  keys      r   
get_configzAptWorker.get_config  s    ))7C>>((a00~%@C>>((e44**4C>>((a00**5C>>((e44//2244 0r   c                     t         j                  j                  t              rIt	        t              5 }|j                         }d d d        t        j                  d      }|r	|d   dk(  ryy# 1 sw Y   +xY w)Nz
PARTICIPATE="(yes|no)"r   rU  TF)r8   r9   rW   rZ  r'  rC   r   r   )rk   r^  rZ   r   s       r   _get_popcon_participationz#AptWorker._get_popcon_participation  sd     77>>,'l# *y")*HH96BEqU** *s   A..A7c                 x    t         j                  j                         D cg c]  }|j                   c}S c c}w )z"Return a list of trusted GPG keys.)rs   rQ  	list_keysrU  )rk   rf  s     r   get_trusted_vendor_keysz!AptWorker.get_trusted_vendor_keys  s'    %(XX%7%7%9:c		:::s   7)NTr   )F)TF)r
      FN))r^  2   )rn  r  )i  )T)9__name__
__module____qualname__r  rX   get_architecturesNATIVE_ARCHr  rT   r   r   r   r   r   r   r   r   r   r	  r   r   r   r   r   r   rz  r   r   staticmethodr   r   r   r   r   r   r   r   r   
contextlibcontextmanagerrl  r   r  rj  r   r   r  r(  r  r   rB  rM  rI  ra  rg  ri  rl  r   r   r   r   r      sS   =+'++-a0K ".4`,9x 6;'<N$&  381"f &*XG*R)GV; B26>!6340+"Z,"\ ( ("22h',2 2"'"@4 19%-:5x 
' 
'"r%h:$$:82 h4
XtG
3
$05";r   )=r  
__author____all__ru  r/  r7   r  r%  r8   r   r  r,  r  r1  time	tracebackurllib.parser   r   ImportErrorurlparseconfigparserr   rs   apt.auth	apt.cacheapt.debfilerX   r   aptsources.distroaptsources.sourceslistr   gi.repositoryr   r	   r  r   enumserrorsr   utilsr   r   r   r   r   r   r   r   r   r   	getLoggerrE   r   rZ  r\  r.   r5   r=   r;   r   r   r   r   <module>r     s    7" 5
      	 	   
   .1*)        . '     !   g*+ -  >AU78&X;
 X;a  .--.
  *)*s#   C C- C*)C*-C;:C;