a
    K56a^%                     @   s   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 d dl	m
Z
 d dlmZ d dlmZ dZd	Zd
Zdd Zdd ZdddZdddZdddZdddZdS )    N   )utils)open_url)	NoNetwork)Deb822)debian_supportz4https://qa.debian.org/madison.php?package=%s&text=onzhttp://incoming.debian.org/z$http://ftp-master.debian.org/new.822c                 C   s   | r|sdS t || S )aI  Return 1 if upstream is newer than current, -1 if current is newer
    than upstream, and 0 if the same.

    .. warning::

        Beware, this is the opposite of usual convention!

        The usual convention is: f(a, b) returns <0, =0, >0 when a <,
        =, > b.

        See e.g. the comparison function in qsort(3).

    r   )r   Zversion_compare)ZcurrentZupstream r   9/usr/lib/python3/dist-packages/reportbug/checkversions.pycompare_versions=   s    r
   c                 C   s   t | |dkr|S | S )Nr   )r
   )abr   r   r	   later_versionP   s    r   i386c              
   C   s  |sd}t  }t|  }|dd| 7 }|d| 7 }zt|||}W nP ty\   i  Y S  tjjy } z t	d|t
jd i W  Y d}~S d}~0 0 |si S |dd	 }i }	|d
D ]>}
|
d}t|dkrqt j|d |d }|d |	|< q|	S )a  :param package:

        Name of the package, e.g. ``"reportbug"``.

    :param timeout:

        Socket timeout, in seconds.

    :param dists:

        The distributions where the search should be done, as an
        iterable, e.g. ``('oldstable', 'stable', 'testing',
        'unstable', 'experimental')`` (which is the default).

    :param http_proxy:

        The proxy to use for the http protocol.  By default, use the
        :func:`urllib.request.getproxies()` settings.

    :returns:

        A map of each dist to a version of the package, e.g.::

            dict(oldstable="46.1",
                 stable="1:26.1+1-3.2+deb10u1",
                 testing="1:26.1+1-4",
                 unstable="1:26.3+1-1")

        .. todo::

            What if there are several versions in the same dist?
            E.g.::

                emacs | 1:26.1+1-4           | sid      | source, all
                emacs | 1:26.3+1-1           | sid      | source, all

    )Z	oldstableZstableZtestingZunstableZexperimentalz&s=,z&a=source,all,Warning:fileN  
|      r   )r   Zget_archRMADISON_URLjoinr   r   urlliberror	HTTPErrorprintsysstderrreplacestripsplitlenZCODENAME2SUITEget)packagetimeoutdists
http_proxyarchZurlpagexZcontentversionslineldistr   r   r	   get_versions_availableV   s0    &
r1   c           
   
   C   s   |d u rd}zt t||}W nP ty2   i  Y S  tjjyl } z td|tjd i W  Y d }~S d }~0 0 |svi S i }t	
|D ]<}|d | kr|d d |d  d }	t|d	  ||	< q|S )
N)zunstable (new queue)r   r   ZSourceZDistributionz (ZQueue)ZVersion)r   NEWQUEUE_URLr   r   r   r   r   r   r    r   Ziter_paragraphsmaxr#   )
r&   r'   r(   r)   r*   r+   r,   r-   Zparakr   r   r	   get_newqueue_available   s"    r6   c              
   C   sl   zt t||}W nL ty$   Y dS  tjjy\ } ztd|tjd W Y d}~dS d}~0 0 |sfdS ~dS )aG  
    :param timeout:

        Socket timeout, in seconds.

    :param http_proxy:

        The proxy to use for the http protocol.  By default, use the
        :func:`urllib.request.getproxies()` settings.

    :returns:

        None.

        .. todo::

            Looks like this function does not work at the moment.

    Nr   r   )	r   INCOMING_URLr   r   r   r   r   r   r    )r&   r'   r)   r*   r+   r,   r   r   r	   get_incoming_version   s    r8   Tc                 C   s  i }|r"t | |||}	|	r"|	|d< t| ||||}
||
 |rpt| }|du rV| }t|||||}
||
 i }d}|D ]n}|dkrd|v r|ddd }n|}t||| }nt||| }|dkr|| ||< q||dk r||d7 }q||o|t|k}||fS )a  :param package:

        Name of the package, e.g. ``"emacs"``.

    :param version:

        The version as a string, e.g. ``"1:26.3+1-1"``.

    :param timeout:

        Socket timeout, in seconds.

    :param check_incoming:

        If truthy, also check versions in
        ``http://incoming.debian.org/``.

    :param http_proxy:

        The proxy to use for the http protocol.  By default, use the
        :func:`urllib.request.getproxies()` settings.

    :returns:

        A tuple ``(avail, toonew)``::

        avail
            The versions that are more recent that current, as a
            mapping of each distribution to the version, e.g.::

                dict(unstable="42.1", testing="42.0")

        toonew
            Whether the current version is "too new", as a boolean.
            The version is "too new" if it is strictly newer than all
            distributions.

    ZincomingNr   :r   )	r8   r1   updater   Zget_source_namer6   r#   r
   r$   )r&   versionr'   r(   Zcheck_incomingZcheck_newqueuer)   r*   ZavailZivZstuffZ
srcpackagenewZnewerr0   ZverZ
comparisonZtoo_newr   r   r	   check_available   s8    )



r=   )NNr   )NNr   )Nr   )NTTNr   )r   Zurllib.requestr   Zurllib.errorZurllib.parser   r   Zurlutilsr   Zreportbug.exceptionsr   Zdebian.deb822r   Zdebianr   r   r7   r3   r
   r   r1   r6   r8   r=   r   r   r   r	   <module>   s$   
V

2   