o
    4fe                     @   s   d 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	m
Z
mZmZmZmZmZmZmZ ddlmZ ddlmZ G dd deZG d	d
 d
eZG dd deZdd Zedkree  dS dS )z;Classes for working with locally available Debian packages.    )print_functionN)DictIterableListOptionalSetTupleUnioncast)gettext)BytesIOc                   @   s   e Zd ZdZdS )NoDebArchiveExceptionz9Exception which is raised if a file is no Debian archive.N)__name__
__module____qualname____doc__ r   r   -/usr/lib/python3/dist-packages/apt/debfile.pyr   #   s    r   c                   @   sR  e Zd ZdZed\ZZZZdZ	dEddZ
dd Zd	d
 Zdd Zedd Zedd Z	dFddZdd Zdd Zdd Zdd Zedd Zedd Zed d! Zed"d# Zd$d% Zd&d' Zd(d) ZdGd+d,ZdFd-d.Zd/d0 Zd1d2 Z ed3d4 Z!ed5d6 Z"e#d7d8 Z$e#d9d: Z%dHd;d<Z&d=d> Z'd?d@ Z(dAdB Z)dIdCdDZ*dS )J
DebPackagezA Debian Package (.deb file).   r   Nc                 C   sd   |d u rt  }|| _ttjd | _d| _d | _i | _	g | _
d| _d| _d | _|r0| | d S d S )N F)aptCache_cacher
   apt_instDebFile_debfilepkgnamefilename	_sections
_need_pkgs_check_was_run_failure_string
_multiarchopenselfr   cacher   r   r   __init__3   s   zDebPackage.__init__c                 C   sh   |  dd|  g | _t | _d| _|| _t| j| _| jj	
d}t|| _| jd | _d| _dS )z open given debfile    z	open '%s'r   controlPackageFN)_dbgr    set_installed_conflictsr"   r   r   r   r   r*   extractdataapt_pkg
TagSectionr   r   r!   )r&   r   r*   r   r   r   r$   C   s   
zDebPackage.openc                 C   s
   | j | S Nr   r&   keyr   r   r   __getitem__Q      
zDebPackage.__getitem__c                 C   s
   || j v S r2   r3   r4   r   r   r   __contains__U   r7   zDebPackage.__contains__c                    sD   g  z| j j fdd W  S  ty!   td| j g Y S w )z$return the list of files in the deb.c                         | jS r2   appendnameitemdatafilesr   r   <lambda>_       z%DebPackage.filelist.<locals>.<lambda>z(List of files for '%s' could not be read)r   r?   goSystemError_r   r&   r   r@   r   filelistY   s   
zDebPackage.filelistc                    sH   g  z| j j fdd W t S  ty#   td| j g Y S w )z, return the list of files in control.tar.gz c                    r9   r2   r:   r=   r*   r   r   rB   l   rC   z-DebPackage.control_filelist.<locals>.<lambda>z0List of control files for '%s' could not be read)r   r*   rD   rE   rF   r   sortedrG   r   rI   r   control_fileliste   s   

zDebPackage.control_filelistFc                 C   s   d|v r|S | j s|S | j|r|S || jv r0| j| jd ur0ttjj| j| jjdkr0|S d|| j f }|| jvr>|S | j| }|jd u rJ|S |jj	}|j
|j@ rV|S |r`|j
|j@ s`|S |S )N:all%s:%s)r#   r   is_virtual_package	candidater
   r   packageVersionarchitecture_cand
multi_archMULTI_ARCH_FOREIGNMULTI_ARCH_SAME)r&   r   in_conflict_checkingmultiarch_pkgnamemultiarch_pkgcandr   r   r   _maybe_append_multiarch_suffixs   s8   





z)DebPackage._maybe_append_multiarch_suffixc           	      C   s   |  dd|  |D ]k}|d }|d }|d }| |}|| jvrA| j|r@|  dd|  | j|D ]	}|jr?  dS q6q
| j| j}|durVt|j	||rV dS |su| jj|dd	D ]}|jrt|  dd
|j
|f    dS q`q
dS )zReturn True if at least one dependency of the or-group is satisfied.

        This method gets an 'or_group' and analyzes if at least one dependency
        of this group is already satisfied.
           z_checkOrGroup(): %s r      r)   z+_is_or_group_satisfied(): %s is virtual depTN)include_nonvirtualz'found installed '%s' that provides '%s'F)r,   r\   r   rO   get_providing_packagesis_installed	installedr0   	check_depversionr<   )	r&   or_groupdepdepnameveroperpkginstppkgr   r   r   _is_or_group_satisfied   sF   


z!DebPackage._is_or_group_satisfiedc           
      C   s  |D ]S}|\}}}|  |}|| jvr,| j|sq| j|}t|dkr'q|d j}| j| }| jj|j}|s<qt	
|j||sEq| dd|  | j|  dS d}	|D ]&}|	|d 7 }	|rr|rr|	d|d |d f 7 }	||t|d  kr|	d7 }	qZ|  jtd	|	 7  _d
S )zTry to satisfy the or_group.r^   r   r]   zNeed to get: %sTr   z (%s %s)|z"Dependency is not satisfiable: %s
F)r\   r   rO   r`   lenr<   	_depcacheget_candidate_ver_pkgr0   rc   ver_strr,   r    r;   r"   rF   )
r&   re   rf   rg   rh   ri   	providersrj   r[   or_strr   r   r   _satisfy_or_group   sB   




zDebPackage._satisfy_or_groupc                 C   s   |  dd|||f  | j| }|jr|jdusJ |jj}n|jr.|jdus)J |jj}ndS t|||rU| 	|||sU|  j
td|j 7  _
|  dd|j  dS dS )z@Return True if a pkg conflicts with a real installed/marked pkg.r)   z8_check_single_pkg_conflict() pkg='%s' ver='%s' oper='%s'NFz)Conflicts with the installed package '%s'z!conflicts with installed pkg '%s'T)r,   r   ra   rb   rd   marked_installrP   r0   rc   replaces_real_pkgr"   rF   r<   )r&   r   rh   ri   rj   pkgverr   r   r   _check_single_pkg_conflict   s,   


z%DebPackage._check_single_pkg_conflictc                 C   s   |  dd|  |D ]\}|d }|d }|d }| j|dd}|| jvrY| j|rX| j|D ]'}|  dd|j  | j|jkrH|  dd	 q0| |j||rW| j	|j q0q
| |||rf| j	| q
t
| jS )
z5Check the or-group for conflicts with installed pkgs.r]   z _check_conflicts_or_group(): %s r   r^   T)rX   r)   zconflicts virtual check: %szconflict on self, ignoring)r,   r\   r   rO   r`   r<   r   rz   r.   addbool)r&   re   rf   rg   rh   ri   rj   r   r   r   _check_conflicts_or_group  s2   

z$DebPackage._check_conflicts_or_groupc                 C   0   d}z
t | j| dW S  ty   g  Y S w )z/List of packages conflicting with this package.	ConflictsFr0   parse_dependsr   KeyErrorr4   r   r   r   	conflicts3     zDebPackage.conflictsc              	   C   s@   g }dD ]}z| t| j| d W q ty   Y qw |S )z2List of packages on which this package depends on.)DependszPre-DependsF)extendr0   r   r   r   )r&   dependsr5   r   r   r   r   =  s   zDebPackage.dependsc                 C   r~   )z<List of virtual packages which are provided by this package.ProvidesFr   r4   r   r   r   providesK  r   zDebPackage.providesc                 C   r~   )z4List of packages which are replaced by this package.ReplacesFr   r4   r   r   r   replacesU  r   zDebPackage.replacesc                 C   s   |  dd|||f  | j| }d}|jr!|jdusJ |jj}n|jr0|jdus+J |jj}nd}| jD ]%}|D ] \}}}||krY|du sMt	|||rY|  dd|    dS q9q5dS )zReturn True if a given non-virtual package is replaced.

        Return True if the deb packages replaces a real (not virtual)
        packages named (pkgname, oper, ver).
        r)   zreplaces_real_pkg() %s %s %sNz?we have a replaces in our package for the conflict against '%s'TF)
r,   r   ra   rb   rd   rw   rP   r   r0   rc   )r&   r   ri   rh   rj   ry   re   r<   r   r   r   rx   _  s,   



zDebPackage.replaces_real_pkgc                 C   s"   d}| j D ]	}| |rd}q|S )zCheck if there are conflicts with existing or selected packages.

        Check if the package conflicts with a existing or to be installed
        package. Return True if the pkg is OK.
        TF)r   r}   )r&   resre   r   r   r   check_conflictsz  s   

zDebPackage.check_conflictsc                 C   sH  t t| j}tt|d d}| jd }| jd }dd | jD }t| jD ]\}}|| dkr>| jj	t || d  |j
sBq'|jd	usIJ |jj}|jjD ]?}	|	jD ]9}
|
j| jkrt||
j|
js| d
d|j  |  jtd|j|
j|
j|
jd 7  _| jj     dS qVqQd|jv r|jd D ]~}|D ]y}|jj| jkr|jj|krt||j|jr| d
d|j  |  jtd|j|jj|j|jd 7  _| jj     dS |jj|v r| j|jkr| d
d|  |  jtdd|| j |jj|jd 7  _| jj     dS qqq'| jj  dS )z
        check if installing the package would break exsisting
        package on the system, e.g. system has:
        smc depends on smc-data (= 1.4)
        and user tries to installs smc-data 1.6
        2   r^   rR   Architecturec                 S   s   g | ]}|d  d  qS )r   r   ).0xr   r   r   
<listcomp>  s    z=DebPackage.check_breaks_existing_packages.<locals>.<listcomp>r   g      Y@Nr]   zwould break (depends) %sz]Breaks existing package '%(pkgname)s' dependency %(depname)s (%(deprelation)s %(depversion)s))r   rg   deprelation
depversionFr   zwould break (conflicts) %szZBreaks existing package '%(pkgname)s' conflict: %(targetpkg)s (%(comptype)s %(targetver)s))r   	targetpkgcomptype	targetverz{Breaks existing package '%(pkgname)s' that conflict: '%(targetpkg)s'. But the '%(debfile)s' provides it via: '%(provides)s',)r   debfiler   r   T)!floatro   r   maxintr   r   	enumerateop_progressupdatera   rb   rr   current_verdependenciesor_dependenciesr<   r   r0   rc   relationrd   r,   r"   rF   donedepends_list
target_pkgrS   	comp_type
target_verjoinr   )r&   sizestepsdebverdebarchr   irj   rh   dep_orrf   conflicts_ver_listc_orr   r   r   check_breaks_existing_packages  s   	







	
&z)DebPackage.check_breaks_existing_packagesTc                 C   s   |  dd | jd }| jd }d||g}| jd }|  dd|  || jv rp| j| }|r:|jd	ur:|jj}n|sF|jd	urF|jj}n| jS |d	urpt	||}|  dd
|  |dkrb| j
S |dk ri| jS |dkrp| jS | jS )a	  Compare the package to the version available in the cache.

        Checks if the package is already installed or availabe in the cache
        and if so in what version, returns one of (VERSION_NONE,
        VERSION_OUTDATED, VERSION_SAME, VERSION_NEWER).
        r)   compare_to_version_in_cacher+   r   rL   rR   r^   z
debver: %sNz"CompareVersion(debver,instver): %sr   )r,   r   r   r   rb   rd   rP   VERSION_NONEr0   version_compareVERSION_SAMEVERSION_NEWERVERSION_OUTDATED)r&   use_installedr   rS   r   rj   cachevercmpr   r   r   r     s.   






z&DebPackage.compare_to_version_in_cachec                 C   s>  |  dd d| _d| jvr|  dd td| _dS | jd }|d	krV|tjd
krV|t v rG|| _	d| j
| j	f | _
|  dd|  n|  dd td| | _dS |sm|  | jkrm| j| j
 jrmtd| _dS d| _|  svdS |  s|dS | | jsdS |  sdS | jjjdkrtd| _| j  dS dS )z$Check if the package is installable.r)   checkTr   r^   zERROR: no architecture fieldz$No Architecture field in the packageFrM   zAPT::ArchitecturerN   zFound multiarch arch: '%s'zERROR: Wrong architecture dude!zVWrong architecture '%s' -- Run dpkg --add-architecture to add it and update afterwardsz$A later version is already installedr   r   z1Failed to satisfy all dependencies (broken cache))r,   r!   r   rF   r"   r0   configfindget_architecturesr#   r   r   r   r   rb   r   r   _satisfy_dependsr   rp   broken_countclear)r&   allow_downgradearchr   r   r   r     sL   




zDebPackage.checkc                 C   s   |  t|dS )z-Satisfy the dependencies in the given string.F)r   r0   r   )r&   
dependsstrr   r   r   satisfy_depends_strE  s   zDebPackage.satisfy_depends_strc              	   C   s   zt | jj}| W n	 ty   Y nw |D ]}| |s&| |s& dS q| jD ]$}z| j| jdd W q* t	yN   t
d| | _| j  Y  dS w dS )zSatisfy the dependencies.F)	from_userzCannot install '%s'T)r0   ActionGroupr   rp   AttributeErrorrm   rv   r    mark_installrE   rF   r"   r   )r&   r   _actiongroupre   rj   r   r   r   r   J  s(   



zDebPackage._satisfy_dependsc                 C   s&   |  dd| j  | jstd| jS )zReturn missing dependencies.r^   zInstalling: %s-property only available after check() was run)r,   r    r!   r   rG   r   r   r   missing_depsb  s   zDebPackage.missing_depsc                 C   s   g }g }g }| j std| jD ]4}|js|jr;|jdusJ ||j d}|jjD ]}||j	O }q+|s;||j |j
rD||j q|||fS )zGet the changes required to satisfy the dependencies.

        Returns: a tuple with (install, remove, unauthenticated)
        r   NF)r!   r   r   rw   marked_upgraderP   r;   r<   originstrustedmarked_delete)r&   installremoveunauthenticatedrj   authenticatedoriginr   r   r   required_changesl  s(   

zDebPackage.required_changesc                 C   s>   d}t | D ]\}}|d dkr|d7 }|dt| 7 }q|S )Nr   P   r   
z%2.2x )r   ord)in_datahexr   cr   r   r   to_hex  s   zDebPackage.to_hexc                 C   sz   d}t | tr#| D ]}t|dk st|dkr|d7 }q	||7 }q	|S | D ]}|dk s/|dkr4|d7 }q%|t|7 }q%|S )Nr   
       )
isinstancestrr   chr)r   sr   br   r   r   	to_strish  s   



zDebPackage.to_strishc           	      C   s   | dr|dd  }||}|dr.|r.t|}tj|d}tdd}|| 7 }z|	dW S  t
yJ   td}|| |7 }| Y S w )Nz./r]   z.gz)fileobjzAutomatically decompressed:

zutf-8z,Automatically converted to printable ascii:
)
startswithr/   endswithr   gzipGzipFilerF   encodereaddecode	Exceptionr   )	r&   partr<   auto_decompressauto_hexr?   iogznew_datar   r   r   _get_content  s   

zDebPackage._get_contentc                 C   (   z	|  | jj|W S  ty   Y dS w z6 return the content of a specific control.tar.gz file r   )r   r   r*   LookupErrorr&   r<   r   r   r   control_content  
   zDebPackage.control_contentc                 C   r   r   )r   r   r?   r   r   r   r   r   data_content  r   zDebPackage.data_contentc                 C   s    || j krt|tjd dS dS )z%Write debugging output to sys.stderr.)fileN)debugprintsysstderr)r&   levelmsgr   r   r   r,     s   
zDebPackage._dbgc                 C   s   | j du r
td|du rttjddd| j S z|  W n ty,   |  Y nw |	| j }z|
  W |S  tyH   |  Y |S w )zInstall the package.NzNo filename specifieddpkgz-i)r   r0   ErrorosspawnlpP_WAITstart_updater   startUpdaterunfinish_updatefinishUpdate)r&   install_progressr   r   r   r   r     s"   



zDebPackage.installNNF)T)TTr2   )+r   r   r   r   ranger   r   r   r   r   r(   r$   r6   r8   propertyrH   rK   r\   rm   rv   rz   r}   r   r   r   r   rx   r   r   r   r   r   r   r   r   staticmethodr   r   r   r   r   r,   r   r   r   r   r   r   '   sd    



%/.!
	

	
	
Y
$?
	

	

r   c                   @   sP   e Zd ZdZdddZedd Zedd Zed	d
 Zdd Z	dddZ
dS )DscSrcPackagez#A locally available source package.Nc                 C   sX   t | d | || _g | _g | _t | _d| _g | _i | _	| jd ur*| 
| j d S d S )Nr   )r   r(   r   _depends
_conflictsr-   r.   r   binariesr   r$   r%   r   r   r   r(     s   
zDscSrcPackage.__init__c                 C      | j S z&Return the dependencies of the package)r  rG   r   r   r   r        zDscSrcPackage.dependsc                 C   r  r  )r  rG   r   r   r   r     r  zDscSrcPackage.conflictsc                 C   s   | j d  ddd S )z6Return the list of files associated with this dsc fileFilesr]   Nr)   )r   splitrG   r   r   r   rH     s   zDscSrcPackage.filelistc           
      C   s.  ddg}ddg}t |}t|}t |}zb|D ]X}|D ]}||vr%q| jt ||  q|D ]}||vr:q3| jt ||  q3d|v rO|d | _	d|v r`dd |d 
d	D | _| D ]}||v rq|| | j|< qdqW ~|  n~|  w td
| j	d| jf }	|	| jd< d| _dS )zOpen the package.zBuild-DependszBuild-Depends-IndepzBuild-ConflictszBuild-Conflicts-IndepSourceBinaryc                 S   s   g | ]}|  qS r   )strip)r   r   r   r   r   r     s    z&DscSrcPackage.open.<locals>.<listcomp>r   zBInstall Build-Dependencies for source package '%s' that builds %s
r   DescriptionFN)r0   open_maybe_clear_signed_filer  fdopenTagFiler  r   parse_src_dependsr  r   r  r  keysr   closerF   r   r!   )
r&   r   depends_tagsconflicts_tagsfdfobjtagfilesectagr   r   r   r   r$     sL   








zDscSrcPackage.openFc                 C   sN   |   s| jD ]}| j| jjrttd| j|   qd| _| 	| j
S )zCheck if the package is installable.

        The second parameter is ignored and only exists for compatibility
        with parent type.z%An essential package would be removedT)r   r.   r   rr   	essentialr   rF   mark_deleter!   r   r   )r&   r   r   r   r   r   r   *  s   
zDscSrcPackage.checkr  r  )r   r   r   r   r(   r  r   r   rH   r$   r   r   r   r   r   r    s    



$r  c            
      C   s  ddl m}  ddlm} |  }d}td|||f  ||}td|  |D ]	}td|j  q)tt	j
d |}td	|j  | sOtd
 t|j td|j  t|j t|j td || }t| t|d}d}	t|t|	d dS )zTest functionr   )r   )InstallProgresszwww-browserz%s virtual: %szProviders for %s :z %sr^   zDeb: %szcan't be satifiedzmissing deps: %szInstalling ...)r'   z:libc6 (>= 2.3.2), libaio (>= 0.3.96) | libaio1 (>= 0.3.96)FN)	apt.cacher   apt.progress.baser2  r  rO   r`   r<   r   r  argvr   r   r"   r   r   rH   r   r  r   r0   r   )
r   r2  r'   vprt   rj   dretr   dsr   r   r   _test<  s.   




r:  __main__)r   
__future__r   r   r   r0   r   r  r  typingr   r   r   r   r   r   r	   r
   r   rF   r   r   IOErrorr   objectr   r  r:  r   r   r   r   r   <module>   s.   (     AY'
