o
    ak                     @   s  d Z ddlZddlZddlZz&ddlmZmZmZmZm	Z	m
Z
mZmZmZmZmZmZmZmZmZmZ W n	 ey=   Y nw ddlmZ zddlZe  dZW n ey[   dZY nw z	ddlZejZW n eyr   dd ZY nw z	ddlZejZ W n ey   d	d
 Z Y nw G dd de!Z"G dd de#Z$G dd de$Z%G dd de$Z&erG dd de%Z'nG dd de&Z'dd Z(G dd dZ)G dd dZ*G dd de*Z+dd Z,ee,Z-e, Z.d6d d!Z/ee/Z0[-[,d"d# Z1d$d% Z2ee2Z3d&Z4e5e4Z6e5e47d'Z8	d6d(d)Z9ee9Z:d*d+ Z;ee;Z<d7d,d-Z=ee=Z>d.d/ Z?ee?Z@d0d1 ZAeeAZBd8d2d3ZCeeCZDd4d5 ZEeeEZFdS )9z2 Facilities to deal with Debian-specific metadata     N)AnyAnyStrBinaryIODictIterableIterator	GeneratorListMatchNoReturnOptionalPatternTextTextIOTupleUnion)function_deprecated_byTFc                  G      t dNzBuilt-in sha1 implementation not found; cannot use hashlib implementation because it depends on OpenSSL, which may not be linked with this library due to license incompatibilitiesNotImplementedErrorargs r   7/usr/lib/python3/dist-packages/debian/debian_support.pynew_sha1?      r   c                  G   r   r   r   r   r   r   r   
new_sha256N   r   r   c                       s@   e Zd ZdZ fddZdd Zdd Zdd	 ZeeZ	  Z
S )

ParseErrorzAn exception which is used to signal a parse failure.

    Attributes:

    filename - name of the file
    lineno - line number in the file
    msg - error message

    c                    s4   t |tsJ || _|| _|| _tt| |  d S N)
isinstanceintfilenamelinenomsgsuperr   __init__)selfr"   r#   r$   	__class__r   r   r&   b   s
   zParseError.__init__c                 C      | j S r   )r$   r'   r   r   r   __str__n      zParseError.__str__c                 C   s   d| j | j| jf S )NzParseError(%r, %d, %r))r"   r#   r$   r+   r   r   r   __repr__r   s   zParseError.__repr__c                 C   s&   | d| j| j| jf  |  dS )z0Writes a machine-parsable error message to file.z
%s:%d: %s
N)writer"   r#   r$   flush)r'   filer   r   r   	print_outx   s   zParseError.print_out)__name__
__module____qualname____doc__r&   r,   r.   r2   r   printOut__classcell__r   r   r(   r   r   W   s    
r   c                       s   e Zd ZdZedZdZdd Zdd Z	 fdd	Z
 fd
dZdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zd d! Z  ZS )"BaseVersiona  Base class for classes representing Debian versions

    It doesn't implement any comparison, but it does check for valid versions
    according to Section 5.6.12 in the Debian Policy Manual.  Since splitting
    the version into epoch, upstream_version, and debian_revision components is
    pretty much free with the validation, it sets those fields as properties of
    the object, and sets the raw version to the full_version property.  A
    missing epoch or debian_revision results in the respective property set to
    None.  Setting any of the properties results in the full_version being
    recomputed and the rest of the properties set from that.

    It also implements __str__, just returning the raw version given to the
    initializer.
    zd^((?P<epoch>\d+):)?(?P<upstream_version>[A-Za-z0-9.+:~-]+?)(-(?P<debian_revision>[A-Za-z0-9+.~]+))?$)full_versionepochupstream_versiondebian_revisiondebian_versionc                 C   s   t |tr	t|}|| _d S r   )r    r9   strr:   r'   versionr   r   r   r&      s   

zBaseVersion.__init__c                 C   sr   | j |}|std| |dd u r"d|dv r"td| || _|d| _|d| _|d| _d S )NzInvalid version string %rr;   :r<   r=   )re_valid_versionmatch
ValueErrorgroup_BaseVersion__full_version_BaseVersion__epoch_BaseVersion__upstream_version_BaseVersion__debian_revision)r'   rA   mr   r   r   _set_full_version   s   zBaseVersion._set_full_versionc                    s   || j vrtt| || d S |dkrd}|dkr#| t| d S |d ur+t|}d| }t| |}t| || z|   W d S  t	yZ   t| || |   t	d||f w )Nr>   r=   r:   _BaseVersion__%sz+Setting %s to %r results in invalid version)
magic_attrsr%   r9   __setattr__rL   r?   getattrsetattr_update_full_versionrE   )r'   attrvalueprivate	old_valuer(   r   r   rO      s*   

zBaseVersion.__setattr__c                    s8   || j vrtt| |S |dkrd}d| }t| |S )Nr>   r=   rM   )rN   r%   r9   __getattribute__rP   )r'   rS   rU   r(   r   r   __getattr__   s   

zBaseVersion.__getattr__c                 C   sD   d}| j d ur|| j d 7 }|| j7 }| jr|d| j 7 }|| _d S )N rB   -)rH   rI   rJ   r:   r@   r   r   r   rR      s   


z BaseVersion._update_full_versionc                 C   s   | j d ur| j S dS )NrY   )r:   r+   r   r   r   r,         zBaseVersion.__str__c                 C   s   d| j j| f S )Nz%s('%s'))r)   r3   r+   r   r   r   r.      s   zBaseVersion.__repr__c                 C   s   t r   r   r'   otherr   r   r   _compare   s   zBaseVersion._comparec                 C   s   |  |dk S Nr   r^   r\   r   r   r   __lt__      zBaseVersion.__lt__c                 C   s   |  |dkS r_   r`   r\   r   r   r   __le__   rb   zBaseVersion.__le__c                 C   s   |  |dkS r_   r`   r\   r   r   r   __eq__   rb   zBaseVersion.__eq__c                 C   s   |  |dkS r_   r`   r\   r   r   r   __ne__   rb   zBaseVersion.__ne__c                 C   s   |  |dkS r_   r`   r\   r   r   r   __ge__   rb   zBaseVersion.__ge__c                 C   s   |  |dkS r_   r`   r\   r   r   r   __gt__  rb   zBaseVersion.__gt__c                 C   s   t t| S r   )hashr?   r+   r   r   r   __hash__     zBaseVersion.__hash__)r3   r4   r5   r6   recompilerC   rN   r&   rL   rO   rX   rR   r,   r.   r^   ra   rc   rd   re   rf   rg   ri   r8   r   r   r(   r   r9      s*    
r9   c                       s(   e Zd ZdZ fddZdd Z  ZS )AptPkgVersionzARepresents a Debian package version, using apt_pkg.VersionComparec                    s    t stdtt| | d S )Nz5apt_pkg not available; install the python-apt package)_have_apt_pkgr   r%   rm   r&   r@   r(   r   r   r&     s   zAptPkgVersion.__init__c                 C   s   t t| t|S r   )apt_pkgversion_comparer?   r\   r   r   r   r^     r[   zAptPkgVersion._compare)r3   r4   r5   r6   r&   r^   r8   r   r   r(   r   rm     s    rm   c                   @   sd   e Zd ZdZedZedZedZedZ	dd Z
edd	 Zed
d Zedd ZdS )NativeVersionzBRepresents a Debian package version, with native Python comparisonz\d+|\D+z\d+z\dz[A-Za-z]c              
   C   s   |d u rdS t |ts(ztt|}W n ty' } ztd||f d }~ww t| jp-d}t|jp4d}||k r<dS ||krBdS | | jpHd|jpLd}|dkrT|S | | jpZd|jp^dS )N   z&Couldn't convert %r to BaseVersion: %s0r   )	r    r9   r?   rE   r!   r;   _version_cmp_partr<   r=   )r'   r]   elepochrepochresr   r   r   r^   %  s2   
zNativeVersion._comparec                 C   sD   |dkrdS | j |rt|d S | j|rt|S t|d S )z'Return an integer value for character x~rt   rr      )re_digitrD   r!   re_alphaord)clsxr   r   r   _orderD  s   zNativeVersion._orderc                    st    fdd|D } fdd|D }|s|r8d}d}|r!| d}|r(| d}||k r.dS ||kr4dS |s|sdS )Nc                       g | ]}  |qS r   r   .0r   r   r   r   
<listcomp>S      z5NativeVersion._version_cmp_string.<locals>.<listcomp>c                    r   r   r   r   r   r   r   r   T  r   r   rt   rr   )pop)r   vavblalbabr   r   r   _version_cmp_stringP  s   

z!NativeVersion._version_cmp_stringc           
      C   s   | j |}| j |}|s|rSd}d}|r|d}|r"|d}| j|rC| j|rCt|}t|}||k r<dS ||krBdS n| ||}	|	dkrO|	S |s|sdS )Nrs   r   rt   rr   )re_all_digits_or_notfindallr   	re_digitsrD   r!   r   )
r   r   r   r   r   r   r   avalbvalry   r   r   r   ru   b  s,   

zNativeVersion._version_cmp_partN)r3   r4   r5   r6   rk   rl   r   r   r|   r}   r^   classmethodr   r   ru   r   r   r   r   rq     s    





rq   c                   @      e Zd ZdS VersionNr3   r4   r5   r   r   r   r   r   }      r   c                   @   r   r   r   r   r   r   r   r     r   c                 C   s,   t | }t |}||k rdS ||krdS dS )Nrt   rr   r   )r   )r   r   r   r   r   r   r   rp     s   rp   c                   @   sT   e Zd ZdZedZedZ		dddZdd	 Z	d
d Z
dddZeeZdS )PackageFilezmA Debian package file.

    Objects of this class can be used to read Debian's Source and
    Packages files.z+^([A-Za-z][A-Za-z0-9-_]+):(?:\s*(.*?))?\s*$z^\s+(?:\.|(\S.*?)\s*)$Nutf-8c                 C   s.   |du r	t |d}|| _|| _d| _|| _dS )zCreates a new package file object.

        name - the name of the file the data comes from
        file_obj - an alternate data source; the default is to open the
                  file with the indicated name.
        Nrbr   )opennamer1   r#   encoding)r'   r   file_objr   r   r   r   r&     s   

zPackageFile.__init__c                 c   s    |   }|  jd7  _g }|rw|ddkr/|s| d |V  g }|   }|  jd7  _q| j|}|s<| d | \}}|pEd}	 |   }|  jd7  _| j|}|rl| \}|d u red}d||f }nnqG|||f |s|r~|V  d S d S )	Nrr   z 	
zexpected package recordzexpected package fieldrY   Tz%s
%s)	_aux_read_liner#   stripraise_syntax_errorre_fieldrD   groupsre_continuationappend)r'   linepkgrD   r   contents	ncontentsr   r   r   __iter__  sD   



zPackageFile.__iter__c                 C   s$   | j  }t|tr|| jS |S r   )r1   readliner    bytesdecoder   )r'   r   r   r   r   r     s   

zPackageFile._aux_read_linec                 C   s   |d u r| j }t| j||r   )r#   r   r   )r'   r$   r#   r   r   r   r     s   zPackageFile.raise_syntax_error)Nr   r   )r3   r4   r5   r6   rk   rl   r   r   r&   r   r   r   r   raiseSyntaxErrorr   r   r   r   r     s    


$
	r   c                   @   s`   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd Zdd ZdS )
PseudoEnumz8A base class for types which resemble enumeration types.c                 C   s   || _ || _d S r   )_namer   )r'   r   orderr   r   r   r&     s   
zPseudoEnum.__init__c                 C   s   d| j j| jf S )Nz%s(%r))r)   r3   r   r+   r   r   r   r.     s   zPseudoEnum.__repr__c                 C   r*   r   )r   r+   r   r   r   r,     r-   zPseudoEnum.__str__c                 C   s   | j |j k S r   r   r\   r   r   r   ra     rj   zPseudoEnum.__lt__c                 C   s   | j |j kS r   r   r\   r   r   r   rc     rj   zPseudoEnum.__le__c                 C   s   | j |j kS r   r   r\   r   r   r   rd     rj   zPseudoEnum.__eq__c                 C   s   | j |j kS r   r   r\   r   r   r   re     rj   zPseudoEnum.__ne__c                 C   s   | j |j kS r   r   r\   r   r   r   rf     rj   zPseudoEnum.__ge__c                 C   s   | j |j kS r   r   r\   r   r   r   rg   	  rj   zPseudoEnum.__gt__c                 C   s
   t | jS r   )rh   r   r+   r   r   r   ri     s   
zPseudoEnum.__hash__N)r3   r4   r5   r6   r&   r.   r,   ra   rc   rd   re   rf   rg   ri   r   r   r   r   r     s    r   c                       s(   e Zd ZdZi Z	d fdd	Z  ZS )Releasez
    Debian release defined with respect to its name, order of release
    and version. The latter can be empty in case of 'sid'.

    See https://www.debian.org/releases/
    rY   c                    s   t t| || || _d S r   )r%   r   r&   rA   )r'   r   r   rA   r(   r   r   r&     s   
zRelease.__init__)rY   )r3   r4   r5   r6   releasesr&   r8   r   r   r(   r   r     s
    r   c                  C   s<   i } d}t |D ]\}}|\}}t|||| |< q| t_| S )z*
     Returns dict of Debian releases
    ))buzzz1.1)rexz1.2)boz1.3)hammz2.0)slinkz2.1)potatoz2.2)woodyz3.0)sargez3.1)etchz4.0)lennyz5.0)squeezez6.0)wheezy7)jessie8)stretch9)buster10)bullseye11)bookworm12)trixie13)sidrY   )	enumerater   r   )r   relsidxrelr   rA   r   r   r   list_releases$  s   r   c                 C   s   |d u rt }|| S r   )_release_listget)r   r   r   r   r   intern_releaseI  s   
r   c                 C   >   t  }| D ]}t|tr|| q||d q| S NUTF-8)r   r    r   updateencode	hexdigestlinesrK   lr   r   r   read_lines_sha256W     
r   c                 C   r   r   )r   r    r   r   r   r   r   r   r   r   read_lines_sha1b  r   r   z^(\d+)(?:,(\d+))?([acd])$r   c                 c   s.   t | }|}|D ]}|st|trtnt}||}|du r%td| | \}}}t|}	|du r6dnt|}
t	|dkrS|	d }	|
du rL|	d }
|	|
g fV  q	t	|dkrf|
durctd| |	}
n|	d }	|
du rr|	d }
g }|D ]}|dv rtd| |d	v r n|
| qv|	|
|fV  q	dS )
a1  Converts source to a stream of patches.

    Patches are triples of line indexes:

    - number of the first line to be replaced
    - one plus the number of the last line to be replaced
    - list of line replacements

    This is enough to model arbitrary additions, deletions and
    replacements.
    Nzinvalid patch command: %rd   rr   a   zinvalid patch argument: %r)rY       zend of stream in command: %r)z.
.s   .
   .)iterr    r   _patch_re_b	_patch_rerD   rE   r   r!   r~   r   )sourcere_cmdipatch_rer   rD   first_last_cmdfirstlastr   cr   r   r   patches_from_ed_scriptu  sD   
r   c                 C   s    |D ]\}}}|| ||< qdS )z2Applies patches to lines.  Updates lines in place.Nr   )r   patchesr   r   r   r   r   r   patch_lines  s   r   c              	   C   s   |d }z7t |d|d}| D ]}|| qW d    n1 s!w   Y  t|| W tj|r:t| d S d S tj|rHt| w w )Nz.newzw+r   )r   r/   osrenamepathexistsunlink)r   localr   	local_newnew_filer   r   r   r   replace_file  s   r  c           
   	   C   s   ddl }ddl}ddlm} | \}}z6t| || |\}}||d}| }	W d   n1 s6w   Y  W t	| |	S W t	| |	S t	| w )z[Downloads a file from a remote location and gunzips it.

    Returns the lines in the file.r   N)urlretrievert)
gziptempfileurllib.requestr  mkstempr   closer   	readlinesr  )
remoter  r	  r  handlefnamer"   _gfiler   r   r   r   download_gunzip_lines  s    




r  c                 C   s   t | d }t|| |S )zCopies a gzipped remote file to the local system.

    remote - URL, without the .gz suffix
    local - name of the local file
    .gz)r  r  )r  r  r   r   r   r   download_file  s   
r  c                 C   s  zt |ddd}| }W d   n1 sw   Y  W n ty1   |r*td t| | Y S w g }i }ddlm} | d }td	}	z||}
t	t
||
}W d   n1 s\w   Y  W n' tyv   |rotd
 t| | Y S  ty   |rtd t| | Y S w ddd |D v r|rtd d}t|}t}n|rtd d}t|}t}|D ]y}|D ]t\}}||d kr|	|\}}||kr|rtd |    S q||d kr| D ]}|dkrq|	|\}}}|s||kr|| qq||d kr!| D ]}|dkrq	|	|\}}}|||< q	q|r*td|  qq|s<|r7td| t| |S |D ]0}|rItd|  t| d | d }|||| krbtd| t	|}t|t| q>||}||krtd||f t|| |S )zjUpdates the local file by downloading a remote patch.

    Returns a list of lines in the local file.
    rr   r   Nz1update_file: no local copy, downloading full filer   )urlopenz.diff/Indexz\s+z1update_file: could not interpret patch index filez0update_file: could not download patch index filezSHA256-Currentc                 S   s   g | ]}|D ]\}}|qqS r   r   )r   fieldskvr   r   r   r   $  s    zupdate_file.<locals>.<listcomp>zupdate_file: using sha256SHA256zupdate_file: using sha1SHA1z-Currentz%update_file: local file is up-to-datez-HistoryrY   z-Patcheszupdate_file: field %r ignoredz*update_file: could not find historic entryz!update_file: downloading patch %rz.diff/r  zpatch %r was garbledz"patch failed, got %s instead of %s)r   r  IOErrorprintr  r
  r  rk   rl   listr   r   r   r   split
splitlinesr   r  rE   r   r   r  )r  r  verbose
local_filer   patches_to_applypatch_hashesr  
index_namere_whitespace	index_urlindex_fieldsprefix
local_hash
read_linesr  fieldrT   remote_hashr  entry	hist_hash
patch_name
patch_hashpatch_contentspatch_contents_unicodenew_hashr   r   r   update_file  s   




!



r6  c                  G   s(   i }| D ]}|D ]}d||< qqt |S )zdCreate an order set (represented as a list) of the objects in
    the sequences passed as arguments.T)sorted)r   sr   yr   r   r   merge_as_setsn  s   
r:  r   )r   )F)Gr6   r   os.pathrk   typingr   r   r   r   r   r   r   r	   r
   r   r   r   r   r   r   r   ImportErrordebian.deprecationr   ro   initrn   _sha1sha1r   _sha256sha256r   	Exceptionr   objectr9   rm   rq   r   rp   r   r   r   r   listReleasesr   r   internReleaser   r   readLinesSHA1_patch_re_rawrl   r   r   r   r   patchesFromEdScriptr   
patchLinesr  replaceFiler  downloadGunzipLinesr  downloadFiler6  
updateFiler:  mergeAsSetsr   r   r   r   <module>   s    L


* _R1 


;


n
