o
    va(                     @   s   d 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	m
Z
 ddlmZ ddlmZmZmZmZ ddlmZ dd	lmZ dd
lmZ dZG dd deZdS )zManage Glances update.    )datetime	timedelta)VersionN)CertificateError)__version__)	nativestrurlopen	HTTPErrorURLError)user_cache_dir)safe_makedirs)loggerz)https://pypi.python.org/pypi/Glances/jsonc                   @   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 )Outdatedz
    This class aims at providing methods to warn the user when a new Glances
    version is available on the PyPI repository (https://pypi.python.org/pypi/Glances/).
    c                 C   sd   || _ || _t | _tj| jd| _tdt	
 d| _| | td| j j  |   dS )zInit the Outdated classzglances-version.dbz0.0)installed_versionlatest_versionrefresh_datez$Check Glances version up-to-date: {}N)argsconfigr   	cache_dirospathjoin
cache_filer   r   nowdataload_configr   debugformatdisable_check_updateget_pypi_version)selfr   r    r!   2/usr/lib/python3/dist-packages/glances/outdated.py__init__.   s   
zOutdated.__init__c                 C   sH   d}t |dr||r||d dk| j_dS td| dS )zHLoad outdated parameter in the global section of the configuration file.globalhas_sectionZcheck_updateZfalsez0Cannot find section {} in the configuration fileFT)	hasattrr%   Z	get_valuelowerr   r   r   r   r   )r    r   Zglobal_sectionr!   r!   r"   r   B   s   
zOutdated.load_configc                 C   
   | j d S )Nr   r   r    r!   r!   r"   r   O      
zOutdated.installed_versionc                 C   r(   )Nr   r)   r*   r!   r!   r"   r   R   r+   zOutdated.latest_versionc                 C   r(   )Nr   r)   r*   r!   r!   r"   r   U   r+   zOutdated.refresh_datec                 C   sR   | j jrdS |  }|i krtj| jd}|  dS |d | jd< t	d dS )zWrapper to get the latest PyPI version (async)
        The data are stored in a cached file
        Only update online once a week
        N)targetr   z#Get Glances version from cache file)
r   r   _load_cache	threadingZThread_update_pypi_versionstartr   r   r   )r    cached_datathreadr!   r!   r"   r   X   s   zOutdated.get_pypi_versionc                 C   s>   | j jrdS td|  |   t|  t|  kS )z)Return True if a new version is availableFz2Check Glances version (installed: {} / latest: {}))r   r   r   r   r   r   r   r   r*   r!   r!   r"   is_outdatedm   s   zOutdated.is_outdatedc              
   C   s   t dd}i }zt| jd}t|}W d   n1 sw   Y  W n tyA } ztd| j| W Y d}~|S d}~ww td |d | 	 ksYt
 |d  |kr[i }|S )	z&Load cache file and return cached data   )ZdaysrbNz,Cannot read version from cache file: {} ({})zRead version from cache filer   r   )r   openr   pickleload	Exceptionr   r   r   r   r   r   )r    Zmax_refresh_dater1   fer!   r!   r"   r-   v   s$   
	
zOutdated._load_cachec              
   C   s   t | j z"t| jd}t| j| W d   W dS 1 s w   Y  W dS  tyD } zt	d
| j| W Y d}~dS d}~ww )zSave data to the cache file.wbNz*Cannot write version to cache file {} ({}))r   r   r6   r   r7   dumpr   r9   r   errorr   )r    r:   r;   r!   r!   r"   _save_cache   s   
&"zOutdated._save_cachec              
   C   s   t dt t | jd< z
ttdd }W n t	t
tfy6 } zt d| W Y d}~nd}~ww tt|d d | jd	< t d
 |   | jS )zBGet the latest PyPI version (as a string) via the RESTful JSON APIz9Get latest Glances version from the PyPI RESTful API ({})r      )Ztimeoutz9Cannot get Glances version from the PyPI RESTful API ({})Ninfoversionr   z&Save Glances version to the cache file)r   r   r   PYPI_API_URLr   r   r   r   readr	   r
   r   jsonloadsr   r?   )r    resr;   r!   r!   r"   r/      s   
zOutdated._update_pypi_versionN)__name__
__module____qualname____doc__r#   r   r   r   r   r   r3   r-   r?   r/   r!   r!   r!   r"   r   '   s    	r   )rK   r   r   Zpackaging.versionr   r.   rE   r7   r   Zsslr   Zglancesr   Zglances.compatr   r   r	   r
   Zglances.configr   Zglances.globalsr   Zglances.loggerr   rC   objectr   r!   r!   r!   r"   <module>   s   