o
    (dW1                     @   s   d 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ZddlZddl	Z	ddl
mZmZ ddlZg dZG dd deZG dd	 d	eZG d
d deZG dd deZdS )zBase classes for progress reporting.

Custom progress classes should inherit from these classes. They can also be
used as dummy progress classes which simply do nothing.
    )print_functionN)OptionalUnion)AcquireProgressCdromProgressInstallProgress
OpProgressc                   @   sp   e Zd ZdZd Z Z Z ZZd Z	 Z
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 )r   zMonitor object for downloads controlled by the Acquire class.

    This is an mostly abstract class. You should subclass it and implement the
    methods to get something useful.
            r   c                 C      dS )z<Invoked when an item is successfully and completely fetched.N selfitemr   r   3/usr/lib/python3/dist-packages/apt/progress/base.pydone5       zAcquireProgress.donec                 C   r
   )z*Invoked when an item could not be fetched.Nr   r   r   r   r   fail9   r   zAcquireProgress.failc                 C   r
   )z0Invoked when some of the item's data is fetched.Nr   r   r   r   r   fetch=   r   zAcquireProgress.fetchc                 C   r
   )zInvoked when an item is confirmed to be up-to-date.

        Invoked when an item is confirmed to be up-to-date. For instance,
        when an HTTP download is informed that the file on the server was
        not modified.
        Nr   r   r   r   r   ims_hitA   r   zAcquireProgress.ims_hitc                 C   r
   )a  Prompt the user to change the inserted removable media.

        The parameter 'media' decribes the name of the media type that
        should be changed, whereas the parameter 'drive' should be the
        identifying name of the drive whose media should be changed.

        This method should not return until the user has confirmed to the user
        interface that the media change is complete. It must return True if
        the user confirms the media change, or False to cancel it.
        Fr   )r   mediadriver   r   r   media_changeJ   s   zAcquireProgress.media_changec                 C   r
   )a  Periodically invoked while the Acquire process is underway.

        This method gets invoked while the Acquire progress given by the
        parameter 'owner' is underway. It should display information about
        the current state.

        This function returns a boolean value indicating whether the
        acquisition should be continued (True) or cancelled (False).
        Tr   )r   ownerr   r   r   pulseX   s   zAcquireProgress.pulsec                 C   s4   d| _ d| _d| _d| _d| _d| _d| _d| _dS )z0Invoked when the Acquire process starts running.r	   r   N)current_bytescurrent_cpscurrent_itemselapsed_timefetched_bytes
last_bytestotal_bytestotal_itemsr   r   r   r   starte   s   
zAcquireProgress.startc                 C   r
   )z/Invoked when the Acquire process stops running.Nr   r"   r   r   r   stopr   r   zAcquireProgress.stopN)__name__
__module____qualname____doc__r   r   r   r   r    r   r   r!   r   r   r   r   r   r   r#   r$   r   r   r   r   r   *   s    	r   c                   @   s,   e Zd ZdZdZdd Zdd Zdd Zd	S )
r   a
  Base class for reporting the progress of adding a cdrom.

    Can be used with apt_pkg.Cdrom to produce an utility like apt-cdrom. The
    attribute 'total_steps' defines the total number of steps and can be used
    in update() to display the current progress.
    r   c                 C   r
   )zAsk for the name of the cdrom.

        If a name has been provided, return it. Otherwise, return None to
        cancel the operation.
        Nr   r"   r   r   r   ask_cdrom_name   r   zCdromProgress.ask_cdrom_namec                 C   r
   )zAsk for the CD-ROM to be changed.

        Return True once the cdrom has been changed or False to cancel the
        operation.
        Nr   r"   r   r   r   change_cdrom   r   zCdromProgress.change_cdromc                 C   r
   )zPeriodically invoked to update the interface.

        The string 'text' defines the text which should be displayed. The
        integer 'current' defines the number of completed steps.
        Nr   )r   textcurrentr   r   r   update   r   zCdromProgress.updateN)r%   r&   r'   r(   total_stepsr)   r*   r-   r   r   r   r   r   w   s    r   c                   @   s   e Zd ZdZd\ZZZ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dd Zdd Zdd ZdS ) r   z4Class to report the progress of installing packages.)r   r	   g? c                 C   sH   t  \| _| _t | jd| _t | jd| _t| jtjt j	 d S )Nwr)
ospipestatusfdwritefdfdopenwrite_streamstatus_streamfcntlF_SETFL
O_NONBLOCKr"   r   r   r   __init__   s   zInstallProgress.__init__c                 C   r
   )z(Abstract) Start update.Nr   r"   r   r   r   start_update   r   zInstallProgress.start_updatec                 C   r
   )z+(Abstract) Called when update has finished.Nr   r"   r   r   r   finish_update   r   zInstallProgress.finish_updatec                 C   s   | S Nr   r"   r   r   r   	__enter__   s   zInstallProgress.__enter__c                 C   s   | j   | j  d S r?   )r7   closer8   )r   typevalue	tracebackr   r   r   __exit__   s   
zInstallProgress.__exit__c                 C   r
   )z>(Abstract) Called when a error is detected during the install.Nr   )r   pkgerrormsgr   r   r   error   r   zInstallProgress.errorc                 C   r
   )zA(Abstract) Called when a conffile question from dpkg is detected.Nr   )r   r,   newr   r   r   conffile   r   zInstallProgress.conffilec                 C   r
   )z.(Abstract) Called when the APT status changed.Nr   )r   rF   percentstatusr   r   r   status_change   r   zInstallProgress.status_changec                 C   r
   )z/(Abstract) Called when the dpkg status changed.Nr   )r   rF   rL   r   r   r   dpkg_status_change   r   z"InstallProgress.dpkg_status_changec                 C   r
   )z(Abstract) Sent just before a processing stage starts.

        The parameter 'stage' is one of "upgrade", "install"
        (both sent before unpacking), "configure", "trigproc", "remove",
        "purge". This method is used for dpkg only.
        Nr   )r   rF   stager   r   r   
processing   r   zInstallProgress.processingc                 C   s   |   }|dkrfz	t| jd W n	 ty   Y nw zt|| j  W n= tyE   tt	tj
dddt| j d| Y n! tye } ztjd|  ttjj W Y d}~nd}~ww || _|  }t|S )a  Install using the object 'obj'.

        This functions runs install actions. The parameter 'obj' may either
        be a PackageManager object in which case its do_install() method is
        called or the path to a deb file.

        If the object is a PackageManager, the functions returns the result
        of calling its do_install() method. Otherwise, the function returns
        the exit status of dpkg. In both cases, 0 means that there were no
        problems.
        r   Tdpkgz--status-fdz-iz%s
N)forkr2   set_inheritabler5   AttributeError_exit
do_installr7   filenospawnlpP_WAITstr	Exceptionsysstderrwriteapt_pkgPackageManagerRESULT_FAILED	child_pid
wait_childWEXITSTATUS)r   objpideresr   r   r   run   s,   

zInstallProgress.runc                 C   s   t  S )zFork.)r2   rR   r"   r   r   r   rR      s   zInstallProgress.forkc           	   
   C   s  z| j  }W n$ ty+ } z|jtjkr |jtjkr t|j W Y d}~dS d}~ww d } } } }}|drRz|	dd\}}}}W nF t
yQ   Y dS w |drvz|	dd\}}}}W n* t
yu   |	dd\}}}Y nw |dr|	dd\}}}| | |  | }| }| }|d	ks|d
kr| || dS |dks|dkrtd|}|r| |d|d dS dS |dkrt|| jks|| jkr| |t||  t|| _| | _dS dS |dkr| || dS dS )zUpdate the interface.Nr/   pm:   rL      rP   pmerrorrH   zconffile-prompt
pmconffilez\s*'(.*)'\s*'(.*)'.*   pmstatus)r8   readlineIOErrorerrnoEAGAINEWOULDBLOCKprintstrerror
startswithsplit
ValueErrorrP   striprH   rematchrJ   groupfloatrK   rL   rM   rN   )	r   lineerrpkgnamerL   
status_strrK   baser~   r   r   r   update_interface   sV   




z InstallProgress.update_interfacec              
   C   s   d\}}	 zt  | jgg g | j W n t jy0 } z|j\}}|tjkr& W Y d}~nd}~ww |   zt	| j
tj\}}|| j
krHW |S W n& tyo } z|jtjkr^W Y d}~|S |jtjkre W Y d}~nd}~ww q)a  Wait for child progress to exit.

        This method is responsible for calling update_interface() from time to
        time. It exits once the child has exited. The return values is the
        full status returned from os.waitpid() (not only the return code).
        )r   r   TN)selectr8   select_timeoutrH   argsrt   EINTRr   r2   waitpidrb   WNOHANGOSErrorECHILD)r   rf   rh   rH   errno__errstrr   r   r   r   rc   /  s<   



zInstallProgress.wait_childN)r%   r&   r'   r(   rb   rK   r   rL   r<   r=   r>   r@   rE   rH   rJ   rM   rN   rP   ri   rR   r   rc   r   r   r   r   r      s"    	)0r   c                   @   s.   e Zd ZdZd\ZZZZdddZdd Z	dS )	r   zbMonitor objects for operations.

    Display the progress of operations such as opening the cache.)Fr/   r	   r/   Nc                 C   s   |dur	|| _ dS dS )zCalled periodically to update the user interface.

        You may use the optional argument 'percent' to set the attribute
        'percent' in this call.
        N)rK   )r   rK   r   r   r   r-   V  s   
zOpProgress.updatec                 C   r
   )z,Called once an operation has been completed.Nr   r"   r   r   r   r   `  r   zOpProgress.doner?   )
r%   r&   r'   r(   major_changeoprK   subopr-   r   r   r   r   r   r   O  s
    

r   )r(   
__future__r   rt   r9   ior2   r}   r   r\   typingr   r   r_   __all__objectr   r   r   r   r   r   r   r   <module>   s"   M# 6