
    kd                        d Z dZd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  G d d	e      Z G d
 de      Zd Z ej$                          ej&                  j)                  ej*                  j-                  d            Z eej&                  j1                  ed            Z eej&                  j1                  ed            Zej*                  j7                  d      Z eej&                  j1                  ed            Z eej&                  j1                  ej*                  j7                  d      d            Zd Zd Z ddZ!y)zHandles the apt system lockz%Sebastian Heinlein <devel@glatzor.de>)LockFailedErrorsystem    N)GLib)enums)TransactionCancelledc                       e Zd ZdZddZy)r   zThe locking of file failed.Nc                 j    d|z  }|r|d|z  z  }t         j                  | |       || _        || _        y)zReturn a new LockFailedError instance.

        Keyword arguments:
        flock -- the path of the file lock
        process -- the process which holds the lock or None
        zCould not acquire lock on %s.z The lock is held by %s.N)	Exception__init__flockprocess)selfr   r   msgs       0/usr/lib/python3/dist-packages/aptdaemon/lock.pyr   zLockFailedError.__init__'   s?     .5-77C4%
    N)__name__
__module____qualname____doc__r    r   r   r   r   #   s
    %r   r   c                   2    e Zd ZdZd Zed        Zd Zd Zy)FileLockzRepresents a file lock.c                      || _         d | _        y r   )pathfd)r   r   s     r   r   zFileLock.__init__:   s    	r   c                     | j                   d uS r   )r   r   s    r   lockedzFileLock.locked>   s    wwd""r   c                     | j                   r| j                   S t        j                  | j                        }|dk  r+t	        | j                        }t        | j                  |      || _         |S )zuReturn the file descriptor of the lock file or raise
        LockFailedError if the lock cannot be obtained.
        r   )r   apt_pkgget_lockr   get_locking_process_namer   )r   fd_lockr   s      r   acquirezFileLock.acquireB   sZ     7777N""499-Q;.tyy9G!$))W55DGNr   c                 j    | j                   r't        j                  | j                          d| _         yy)zRelase the lock.N)r   oscloser   s    r   releasezFileLock.releaseP   s%    77HHTWWDG r   N)	r   r   r   r   r   propertyr   r%   r)   r   r   r   r   r   6   s(    ! # #r   r   c                    	 t        | d      }t        j                  dt        j                  t
        j                  ddd      }t        j                  |t        j                  |      }t        j                  d|      d   }	 t        d|z  d      5 }	 d |j                         D        D ]5  \  }}|dk(  s|j                         c cddd       |j                          S  	 ddd       |j                          y# t        $ r Y ddd       |j                          yw xY w# 1 sw Y   AxY w# t        $ r Y |j                          yw xY w# |j                          w xY w# t        $ r Y yw xY w)	zjReturn the name of a process which holds a lock. It will be None if
    the name cannot be retrivied.
    rhhQQir      z/proc/%s/statusc              3   >   K   | ]  }|j                  d         yw):N)split).0lines     r   	<genexpr>z+get_locking_process_name.<locals>.<genexpr>h   s      '>4tzz# '>s   NameN)openstructpackfcntlF_WRLCKr'   SEEK_SETF_GETLKunpack	readlinesstripr(   r
   IOError)	lock_pathfd_lock_readflkflk_retpid	fd_statuskeyvalues           r   r#   r#   W   s[   !Is+
 kk'5=="++q!QG++lEMM3?mmGW-a0	!'#-s3  y '>'0':':'<'> 1
U&=#(;;=0     1    !           	 	  )  s   E$ D0 D$"C=7C=	D0 "C=$D0 =	D!D$D0  D!!D$$D-)D0 0	E9E EE E!$	E0/E0zDir::State::statuslockzlock-frontendzDir::Cache::ArchiveszDir::State::listsc                      	 t         t        t        t        fD ]  } | j                  r| j                          ! 	 dt        j                  d<   y#  t                 xY w)z<Acquire an exclusive lock for the package management system.1DPKG_FRONTEND_LOCKEDN)	frontend_lockstatus_lockarchive_lock
lists_lockr   r%   r)   r'   environrI   s    r   r%   r%      sO    !;jH 	D;;	 *-BJJ%&		s   %A A Ac                      t         t        t        t        fD ]  } | j	                           	 t
        j                  d= y# t        $ r Y yw xY w)z<Release an exclusive lock for the package management system.rL   N)rP   rO   rN   rM   r)   r'   rQ   KeyErrorrR   s    r   r)   r)      sG    L+}D JJ-. s   ? 	A
Ac                 >     fd}	 rj                          yt                y# t        $ r}d _        t        j                   _        |j                  r% j                  d      }||j                  z   _        t        j                  d|      } j                  rG j                  s;t        j                         j                           j                  r j                  s;t        j                  |        j                  r
t               Y d}~yd}~ww xY w)zAcquire the system lock or the optionally given one. If the lock
    cannot be obtained pause the transaction in the meantime.

    :param trans: the transaction
    :param lock: optional alternative lock
    c                  n    	  r j                          n
t                d_        y# t        $ r Y yw xY w)zHelper to unpause the transaction if the lock can be obtained.

        Keyword arguments:
        trans -- the corresponding transaction
        alt_lock -- alternative lock to the system lock
        TF)r%   r   paused)alt_locktranss   r   
watch_lockz!wait_for_lock.<locals>.watch_lock   s<    	  "	   		s   ( 	44TzWaiting for %s to exit   N)r%   r   rW   r   STATUS_WAITING_LOCKstatusr   gettextstatus_detailsr   timeout_add_seconds	cancelledmain_context_default	iterationsource_remover   )rY   rX   rZ   errorr   
lock_watchs   ``    r   wait_for_lockrg      s    ")I )00==-- 89C#&#6E --a<
ll5??%%'113 ll5??:&??&(( )s   ( 
( 	DB5D'+DDr   )"r   
__author____all__r9   r'   r7   r!   gi.repositoryr   	aptdaemonr   aptdaemon.errorsr   r
   r   objectr   r#   initr   dirnameconfig	find_file_status_dirjoinrN   rM   find_dir_archives_dirrO   rP   r%   r)   rg   r   r   r   <module>rv      s   "" 5

'  	     1i &v B:  ggoognn667KLMrww||K89k?CD ''(>?]F;< bggllNN/0&: ;

-))r   