U
    +Gd+                     @   s   d Z ddlZddlZddlZdd Zdd ZG dd deZG d	d
 d
eZ	dd Z
G dd deZG dd deZG dd deZdS )z>provides information about Ubuntu's and Debian's distributions    Nc                 C   s   | s
d}ndd |  dD }t|dkrD|\}}}t|||}nRt|dkr|\}}|dkrpt||d}qt||d	 d	td	 }ntd
|S )z9Convert a date string in ISO 8601 into a datetime object.Nc                 S   s   g | ]}t |qS  )int.0xr   r   -/usr/lib/python3/dist-packages/distro_info.py
<listcomp>   s     z convert_date.<locals>.<listcomp>-               zDate not in ISO 8601 format.)splitlendatetimedateZ	timedelta
ValueError)stringr   partsZyearZmonthZdayr   r   r   convert_date   s    
r   c                   C   s   dS )z4Get the data directory based on the module location.z/usr/share/distro-infor   r   r   r   r   _get_data_dir*   s    r   c                       s    e Zd ZdZ fddZ  ZS )DistroDataOutdatedzDistribution data outdated.c                    s   t t| d d S )NzDistribution data outdated. Please check for an update for distro-info-data. See /usr/share/doc/distro-info-data/README.Debian for details.)superr   __init__self	__class__r   r   r   2   s    
zDistroDataOutdated.__init__)__name__
__module____qualname____doc__r   __classcell__r   r   r   r   r   /   s   r   c                   @   s"   e Zd ZdZdddZdd ZdS )DistroReleasez"Represents a distributions releaseNc	           	      C   s4   || _ || _|| _|| _|| _|| _|| _|| _d S N)versioncodenameseriescreatedreleaseeol
eol_servereol_esm)	r   r&   r'   r(   r)   r*   r+   r,   r-   r   r   r   r   >   s    zDistroRelease.__init__c                 C   s2   || j ko0| jdkp0|| jkp0| jdk	o0|| jkS )z:Check whether this release is supported on the given date.N)r)   r+   r,   r   r   r   r   r   is_supportedJ   s    zDistroRelease.is_supported)NNNNN)r   r    r!   r"   r   r/   r   r   r   r   r$   9   s       
r$   c                 C   s   || krt | | S d S r%   )r   )rowcolumnr   r   r   	_get_dateP   s    r2   c                   @   s   e Zd ZdZdd Zedd ZdddZd	d
 ZdddZ	dddZ
dddZdd Zd ddZd!ddZdd Zd"ddZdS )#
DistroInfozzBase class for distribution information.
    Use DebianDistroInfo or UbuntuDistroInfo instead of using this directly.
    c                 C   s   || _ tjt | d }t|}t|}g | _	|D ]P}t
|d |d |d t|dt|dt|dt|dt|d	}| j	| q:|  tj | _d S )
Nz.csvr&   r'   r(   r)   r*   r+   z
eol-serverzeol-esm)_distroospathjoinr   loweropencsvZ
DictReader	_releasesr$   r2   appendcloser   r   Ztoday_date)r   ZdistrofilenameZcsvfileZ
csv_readerr0   r*   r   r   r   r   Y   s     
  zDistroInfo.__init__c                 C   s   dd | j D S )z*List codenames of all known distributions.c                 S   s   g | ]
}|j qS r   r(   r   r   r   r   r   k   s     z"DistroInfo.all.<locals>.<listcomp>r;   r   r   r   r   allh   s    zDistroInfo.allr'   c                    s    fddj D S )zList all known distributions.c                    s   g | ]}  |qS r   )_formatr   resultr   r   r   r   o   s     z&DistroInfo.get_all.<locals>.<listcomp>rA   )r   rE   r   rD   r   get_allm   s    zDistroInfo.get_allc                    s    fdd| j D S )z?Return all distributions that were available on the given date.c                    s   g | ]} |j kr|qS r   )r)   r   r   r   r   r   s   s     
 z%DistroInfo._avail.<locals>.<listcomp>rA   r.   r   rG   r   _availq   s    zDistroInfo._availNc                 C   s   |S )z3Map codename aliases to the codename they describe.r   )r   r*   r   defaultr   r   r   r'   u   s    zDistroInfo.codenamec                 C   s*   | j D ]}||j|jfkr|j  S q|S )z!Map codename or series to version)r;   r'   r(   r&   )r   namerI   r*   r   r   r   r&   z   s    
zDistroInfo.versionc                    s@    dkr| j   fdd|  D }|s0t | ||d S )<Get latest development distribution based on the given date.Nc                    s8   g | ]0}|j d ks0 |j k r|jd ks0 |jkr|qS r%   r*   r+   r   rG   r   r   r      s     

 
 
z$DistroInfo.devel.<locals>.<listcomp>r>   rH   r   rC   r   r   rE   distrosr   rG   r   devel   s    zDistroInfo.develc                 C   sb   |dkr|S |dkr|j S |dkr@| jd |j d |j d S |dkrN|jS td| d	 d
S )z"Format a given distribution entry.objectr'   fullname z ""r*   zQOnly codename, fullname, object, and release are allowed result values, but not 'z'.N)r(   r4   r&   r'   r   )r   format_stringr*   r   r   r   rC      s    zDistroInfo._formatc                    s@    dkr| j   fdd|  D }|s0t | ||d S )z7Get latest stable distribution based on the given date.Nc                    s8   g | ]0}|j d k	r |j kr|jd ks0 |jkr|qS r%   rL   r   rG   r   r   r      s     

 
 
z%DistroInfo.stable.<locals>.<listcomp>rM   rN   rO   r   rG   r   stable   s    zDistroInfo.stablec                 C   s
   t  dS )z@Get list of all supported distributions based on the given date.N)NotImplementedError)r   r   rE   r   r   r   	supported   s    zDistroInfo.supportedc                 C   s
   || j kS )%Check if the given codename is known.)rB   r   r'   r   r   r   valid   s    zDistroInfo.validc                    s8   |dkrj }| fdd|D }|S )zBGet list of all unsupported distributions based on the given date.Nc                    s"   g | ]}|j kr |qS r   )r(   rC   r   rE   r   rY   r   r   r      s    
z*DistroInfo.unsupported.<locals>.<listcomp>)r>   rY   rH   rO   r   r]   r   unsupported   s
    
zDistroInfo.unsupported)r'   )NN)N)Nr'   )Nr'   )NN)Nr'   )r   r    r!   r"   r   propertyrB   rF   rH   r'   r&   rQ   rC   rW   rY   r\   r^   r   r   r   r   r3   T   s   








r3   c                       sZ   e Zd ZdZ fddZdddZddd	Zdd
dZdddZdddZ	dd Z
  ZS )DebianDistroInfoz1provides information about Debian's distributionsc                    s   t t| d d S )NZDebian)r   r`   r   r   r   r   r   r      s    zDebianDistroInfo.__init__Nc                 C   sX   |dkr|  |}n@|dkr(| |}n,|dkr<| |}n|dkrP| |}n|}|S )z3Map 'unstable', 'testing', etc. to their codenames.unstabletestingrW   	oldstable)rQ   rb   rW   old)r   r*   r   rI   r'   r   r   r   r'      s    zDebianDistroInfo.codenamer'   c                    sH    dkr| j   fdd|  D }t|dk r8t | ||d S )rK   Nc                    s8   g | ]0}|j d ks0 |j k r|jd ks0 |jkr|qS r%   rL   r   rG   r   r   r      s     

 
 
z*DebianDistroInfo.devel.<locals>.<listcomp>r   r>   rH   r   r   rC   rO   r   rG   r   rQ      s    zDebianDistroInfo.develc                    sH    dkr| j   fdd|  D }t|dk r8t | ||d S )z=Get old (stable) Debian distribution based on the given date.Nc                    s$   g | ]}|j d k	r |j kr|qS r%   )r*   r   rG   r   r   r      s    
 
z(DebianDistroInfo.old.<locals>.<listcomp>r   re   rf   rO   r   rG   r   rd      s    zDebianDistroInfo.oldc                    s.    dkrj   fdd D }|S )zRGet list of all supported Debian distributions based on the given
           date.Nc                    s,   g | ]$}|j d ks |j kr|qS r%   )r+   rC   r   r   rE   r   r   r   r      s    
 
z.DebianDistroInfo.supported.<locals>.<listcomp>r>   rH   rO   r   rg   r   rY      s    zDebianDistroInfo.supportedc                    s@    dkr| j   fdd|  D }|s0t | ||d S )z?Get latest testing Debian distribution based on the given date.Nc                    sH   g | ]@}|j d kr|js@|j d k	r |j k r|jd ks@ |jkr|qS r%   )r*   r&   r+   r   rG   r   r   r      s     
 
 

 
z,DebianDistroInfo.testing.<locals>.<listcomp>rM   rN   rO   r   rG   r   rb      s    zDebianDistroInfo.testingc                 C   s   t | |p|dkS )rZ   )ra   rb   rW   rc   )r3   r\   r[   r   r   r   r\      s    zDebianDistroInfo.valid)NN)Nr'   )Nr'   )Nr'   )Nr'   )r   r    r!   r"   r   r'   rQ   rd   rY   rb   r\   r#   r   r   r   r   r`      s   





	
r`   c                       sF   e Zd ZdZ fddZdddZdd	 Zdd
dZdddZ  Z	S )UbuntuDistroInfoz1provides information about Ubuntu's distributionsc                    s   t t| d d S )NZUbuntu)r   ri   r   r   r   r   r   r      s    zUbuntuDistroInfo.__init__Nr'   c                    s<    dkr| j   fdd| jD }|s,t | ||d S )zZGet latest long term support (LTS) Ubuntu distribution based on the
           given date.Nc                    s<   g | ]4}|j d dkr|j   kr0|jkrn q|qS )LTSr   )r&   findr*   r+   r   rG   r   r   r     s      
 z(UbuntuDistroInfo.lts.<locals>.<listcomp>rM   )r>   r;   r   rC   rO   r   rG   r   lts   s    zUbuntuDistroInfo.ltsc                    s*    fdd| j D }|sdS d|d jkS )zIs codename an LTS release?c                    s   g | ]}|j  kr|qS r   r@   r   r'   r   r   r   
  s     
 z+UbuntuDistroInfo.is_lts.<locals>.<listcomp>Frj   r   )r;   r&   )r   r'   rP   r   rm   r   is_lts  s    zUbuntuDistroInfo.is_ltsc                    s.    dkrj   fdd D }|S )zRGet list of all supported Ubuntu distributions based on the given
           date.Nc                    s6   g | ].} |j ks&|jd k	r |jkr|qS r%   )r+   r,   rC   r   rg   r   r   r     s
    

 
z.UbuntuDistroInfo.supported.<locals>.<listcomp>rh   rO   r   rg   r   rY     s    zUbuntuDistroInfo.supportedc                    s.    dkrj   fdd D }|S )zVGet list of all ESM supported Ubuntu distributions based on the
           given date.Nc                    s,   g | ]$}|j d k	r |j kr|qS r%   )r-   rC   r   rg   r   r   r     s    
 
z2UbuntuDistroInfo.supported_esm.<locals>.<listcomp>rh   rO   r   rg   r   supported_esm  s    zUbuntuDistroInfo.supported_esm)Nr'   )Nr'   )Nr'   )
r   r    r!   r"   r   rl   rn   rY   ro   r#   r   r   r   r   ri      s   


ri   )r"   r:   r   r5   r   r   	Exceptionr   rR   r$   r2   r3   r`   ri   r   r   r   r   <module>   s   
aB