o
    >Ic                     @   sB   d dl Z d dlZd dlZd dlZd dlZdd ZG dd dZdS )    Nc                   C   s   t jdu r	t  t _t jS )z'Return a singleton _Telemetry instance.N)
_Telemetry
_telemetry r   r   7/usr/lib/python3/dist-packages/DistUpgrade/telemetry.pyget   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S )r   Nc                 C   s   i | _ i | _|  | _tjg dtjdd d  | j d< | 	d d| _
z td}| | j d	< W d    W d S 1 s@w   Y  W d S  tyQ   Y d S w )
N)lsb_releasez-rz-sT)stdoutuniversal_newlinesr   Fromstartz/var/log/upgrade/telemetryz/var/log/installer/media-infoInstallMedia)_metrics_stages_hist_get_current_uptime_start_time
subprocessPopenPIPEcommunicatestrip	add_stage
_dest_pathopenreadlineFileNotFoundError)selffr   r   r   __init__)   s(   



&z_Telemetry.__init__c              
   C   s   d}z#t d}t|  d }W d   W |S 1 sw   Y  W |S  tttfyD } ztdt	|  W Y d}~|S d}~ww )z6Get current uptime info. None if we couldn't fetch it.Nz/proc/uptimer   z)Exception while fetching current uptime: )
r   floatreadsplitr   OSError
ValueErrorloggingwarningstr)r   uptimer   er   r   r   r   8   s   
z_Telemetry._get_current_uptimec                 C   s6   |   }| jdu s|du rdS || jt|| j < dS )z(Record installer stage with current timeN)r   r   r   int)r   
stage_namenowr   r   r   r   C   s   z_Telemetry.add_stagec                 C      || j d< dS )zRecord updater typeTypeNr   )r   updater_typer   r   r   set_updater_typeJ      z_Telemetry.set_updater_typec                 C   r+   )z*Record if the user had third party sourcesThirdPartySourcesNr-   )r   usingr   r   r   set_using_third_party_sourcesN   r0   z(_Telemetry.set_using_third_party_sourcesc              
   C   s   | j | jd< tj| j}z<tj|st| t| jd}t	
| j| W d   n1 s1w   Y  t| jtjtjB tjB tjB  W dS  tye } ztdt|  W Y d}~dS d}~ww )z<Close telemetry collection

        Save to destination fileStageswNz(Exception while storing telemetry data: )r   r   ospathdirnamer   existsmakedirsr   jsondumpchmodstatS_IRUSRS_IWUSRS_IRGRPS_IROTHr!   r#   r$   r%   )r   
target_dirr   r'   r   r   r   doneR   s*   


z_Telemetry.done)
__name__
__module____qualname__r   r   r   r   r/   r3   rD   r   r   r   r   r   %   s    r   )r#   r;   r6   r>   r   r   r   r   r   r   r   <module>   s   