§
    cÌci†  ã                   ó  — d Z ddlZddlZddlZddlmZmZmZmZm	Z	m
Z
mZ ddlmZ ddlmZmZmZmZmZmZmZmZmZmZ ddlmZ ddl	mZmZmZ dd	lmZmZ dd
l m!Z!m"Z" dZ#dZ$ G d„ dej%        ¦  «        Z& G d„ d¦  «        Z'd„ Z(dS )a‚  On-disk mutex protecting a resource

bzr on-disk objects are locked by the existence of a directory with a
particular name within the control directory.  We use this rather than OS
internal locks (such as flock etc) because they can be seen across all
transports, including http.

Objects can be read if there is only physical read access; therefore
readers can never be required to create a lock, though they will
check whether a writer is using the lock.  Writers can't detect
whether anyone else is reading from the resource as they write.
This works because of ordering constraints that make sure readers
see a consistent view of existing data.

Waiting for a lock must be done by polling; this can be aborted after
a timeout.

Locks must always be explicitly released, typically from a try/finally
block -- they are not released from a finalizer or when Python
exits.

Locks may fail to be released if the process is abruptly terminated
(machine stop, SIGKILL) or if a remote transport becomes permanently
disconnected.  There is therefore a method to break an existing lock.
This should rarely be used, and generally only with user approval.
Locks contain some information on when the lock was taken and by who
which may guide in deciding whether it can safely be broken.  (This is
similar to the messages displayed by emacs and vim.) Note that if the
lock holder is still alive they will get no notification that the lock
has been broken and will continue their work -- so it is important to be
sure they are actually dead.

A lock is represented on disk by a directory of a particular name,
containing an information file.  Taking a lock is done by renaming a
temporary directory into place.  We use temporary directories because
for all known transports and filesystems we believe that exactly one
attempt to claim the lock will succeed and the others will fail.  (Files
won't do because some filesystems or transports only have
rename-and-overwrite, making it hard to tell who won.)

The desired characteristics are:

* Locks are not reentrant.  (That is, a client that tries to take a
  lock it already holds may deadlock or fail.)
* Stale locks can be guessed at by a heuristic
* Lost locks can be broken by any client
* Failed lock operations leave little or no mess
* Deadlocks are avoided by having a timeout always in use, clients
  desiring indefinite waits can retry or set a silly big timeout.

Storage formats use the locks, and also need to consider concurrency
issues underneath the lock.  A format may choose not to use a lock
at all for some operations.

LockDirs always operate over a Transport.  The transport may be readonly, in
which case the lock can be queried but not acquired.

Locks are identified by a path name, relative to a base transport.

Calling code will typically want to make sure there is exactly one LockDir
object per actual lock on disk.  This module does nothing to prevent aliasing
and deadlocks will likely occur if the locks are aliased.

In the future we may add a "freshen" method which can be called
by a lock holder to check that their lock has not been broken, and to
update the timestamp within it.

Example usage:

>>> from breezy.transport.memory import MemoryTransport
>>> # typically will be obtained from a BzrDir, Branch, etc
>>> t = MemoryTransport()
>>> l = LockDir(t, 'sample-lock')
>>> l.create()
>>> token = l.wait_lock()
>>> # do something here
>>> l.unlock()

Some classes of stale locks can be predicted by checking: the host name is the
same as the local host name; the user name is the same as the local user; the
process id no longer exists.  The check on user name is not strictly necessary
but helps protect against colliding host names.
é    Né   )ÚconfigÚdebugÚerrorsÚlockÚosutilsÚuiÚurlutils)Úonly_raises)
ÚDirectoryNotEmptyÚLockBreakMismatchÚ
LockBrokenÚLockContentionÚLockCorruptÚ
LockFailedÚLockNotHeldÚ	PathErrorÚResourceBusyÚTransportError)Úgettext)Úformat_deltaÚ
rand_charsÚget_host_name)ÚmutterÚnote)Ú
FileExistsÚ
NoSuchFileé   g      ð?c                   óú   — e Zd ZdZdZ	 	 d!d„Zd„ Z ed„ ¦  «        Zd"d	„Z	d
„ Z
d„ Zd„ Zd„ Z eee¦  «        d„ ¦   «         Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ Zd#d„Zd„ Zd„ Zd"d„Zd„ Z d„ Z!d„ Z"d „ Z#dS )$ÚLockDirz(Write-lock guarding access to data.
    z/infoé¤  éí  Nc                 óØ   — || _         || _        d| _        d| _        d| _        |dz   | _        | j        | j        z   | _        || _        || _	        t          | _        || _        d| _        dS )aº  Create a new LockDir object.

        The LockDir is initially unlocked - this just creates the object.

        :param transport: Transport which will contain the lock

        :param path: Path to the lock within the base directory of the
            transport.

        :param extra_holder_info: If passed, {str:str} dict of extra or
            updated information to insert into the info file when the lock is
            taken.
        Fz/heldN)Ú	transportÚpathÚ
_lock_heldÚ_locked_via_tokenÚ_fake_read_lockÚ	_held_dirÚ_LockDir__INFO_NAMEÚ_held_info_pathÚ_file_modebitsÚ_dir_modebitsr   Ú_report_functionÚextra_holder_infoÚ_warned_about_lock_holder)Úselfr$   r%   Úfile_modebitsÚdir_modebitsr/   s         ú0/usr/lib/python3/dist-packages/breezy/lockdir.pyÚ__init__zLockDir.__init__ª   st   € ð #ˆŒØˆŒ	ØˆŒØ!&ˆÔØ$ˆÔØ ™ˆŒØ#œ~°Ô0@Ñ@ˆÔØ+ˆÔØ)ˆÔÝ $ˆÔØ!2ˆÔØ)-ˆÔ&Ð&Ð&ó    c                 ób   — d                      | j        j        | j        j        | j        ¦  «        S )Nz{}({}{}))ÚformatÚ	__class__Ú__name__r$   Úbaser%   ©r1   s    r4   Ú__repr__zLockDir.__repr__Æ   s.   € Ø× Ò  ¤Ô!8Ø!œ^Ô0Ø!œYñ(ô (ð 	(r6   c                 ó   — | j         S ©N)r&   r<   s    r4   ú<lambda>zLockDir.<lambda>Ë   s   €  D¤O€ r6   c                 óÆ   — |                       d¦  «         	 | j                             | j        |¬¦  «         dS # t          t
          f$ r}t          | |¦  «        ‚d}~ww xY w)z¾Create the on-disk lock.

        This is typically only called when the object/directory containing the
        directory is first created.  The lock is not held when it's created.
        zcreate lock directory©ÚmodeN)Ú_tracer$   Úmkdirr%   r   r   r   )r1   rC   Úes      r4   ÚcreatezLockDir.createÍ   ss   € ð 	�ŠÐ+Ñ,Ô,Ð,ð	&ØŒN× Ò  ¤°Ð Ñ6Ô6Ð6Ð6Ð6øÝ¥	Ð*ð 	&ð 	&ð 	&Ý˜T 1Ñ%Ô%Ð%øøøøð	&øøøs   —!: ºA ÁAÁA c                 ó|  — |                       d¦  «         t          j        ¦   «         }	 |                      ¦   «         }nD# t          j        t
          f$ r+}|                       d|¦  «         t          | |¦  «        ‚d}~ww xY w	 	 | j                             || j	        ¦  «         ní# t          j        t
          t          t          t          f$ r‡}|                       d|¦  «         |                      ¦   «         }|                       d|z  ¦  «         	 |                      |¦  «         n$# t          $ r |                      |¦  «         ‚ w xY wY d}~nAd}~wt"          $ r1}|                       d|¦  «         |                      |¦  «         ‚ d}~ww xY w�Œ|                      ¦   «         }|                       d|¦  «         |€t          | d	¦  «        ‚|j        | j        k    r$|                       d
¦  «         t'          | ¦  «        ‚d| _        |                       dt          j        ¦   «         |z
  dz  ¦  «         | j        S )a6  Make the pending directory and attempt to rename into place.

        If the rename succeeds, we read back the info file to check that we
        really got the lock.

        If we fail to acquire the lock, this method is responsible for
        cleaning up the pending directory if possible.  (But it doesn't do
        that yet.)

        :returns: The nonce of the lock, if it was successfully acquired.

        :raises LockContention: If the lock is held by someone else.  The
            exception contains the info of the current holder of the lock.
        zlock_write...z$... failed to create pending dir, %sNTz... contention, %szother holder is %rz... lock failed, %szafter locking, info=%rz0lock was renamed into place, but now is missing!z8rename succeeded, but lock is still held by someone elsez... lock succeeded after %dmséè  )rD   ÚtimeÚ_create_pending_dirr   r   r   r   r$   Úrenamer)   r   r   r   ÚpeekÚ_handle_lock_contentionÚBaseExceptionÚ_remove_pending_dirÚ	ExceptionÚnoncer   r&   )r1   Ú
start_timeÚtmpnamerF   Úother_holderÚinfos         r4   Ú_attempt_lockzLockDir._attempt_lockÙ   sx  € ð 	�Š�OÑ$Ô$Ð$Ý”Y‘[”[ˆ
ð	&Ø×.Ò.Ñ0Ô0ˆGˆGøÝÔ%¥yÐ1ð 	&ð 	&ð 	&Ø�KŠKÐ>ÀÑBÔBÐBÝ˜T 1Ñ%Ô%Ð%øøøøð	&øøøð	ðØ”×%Ò% g¨t¬~Ñ>Ô>Ð>ØøÝÔ)­9Õ6GÝ¥ð.ð 	ð 	ð 	à—’Ð0°!Ñ4Ô4Ð4Ø#Ÿyšy™{œ{�Ø—’Ð0°<Ñ?Ñ@Ô@Ð@ðØ×0Ò0°Ñ>Ô>Ð>Ð>øÝ$ð ð ð Ø×,Ò,¨WÑ5Ô5Ð5Øðøøøð ?Ð>Ð>Ð>Ð>øøøøõ ð ð ð Ø—’Ð1°1Ñ5Ô5Ð5Ø×(Ò(¨Ñ1Ô1Ð1Øøøøøðøøøñ	ð: �yŠy‰{Œ{ˆØ�ŠÐ,¨dÑ3Ô3Ð3Øˆ<Ý˜Tð $/ñ 0ô 0ð 0àŒ:˜œÒ#Ð#Ø�KŠKð Añ Bô Bð Bå  Ñ&Ô&Ð&ØˆŒØ�ŠÐ3Ý”Y‘[”[ :Ñ-°Ñ5ñ	7ô 	7ð 	7àŒzÐsR   ª? ¿B Á&A;Á;B Â B& Â&(FÃAEÄD'Ä&EÄ'!EÅEÅFÅ,F	Æ	Fc                 ó”  — |�¸|                      ¦   «         r¤|                      ¦   «                              d¦  «        r}t          j                             dt          j        | j        j	        | j
        ¦  «        t          |¦  «        ¬¦  «         |                      |¦  «         |                      d¦  «         dS t          | ¦  «        ‚)aó  A lock we want to take is held by someone else.

        This function can: tell the user about it; possibly detect that it's
        safe or appropriate to steal the lock, or just raise an exception.

        If this function returns (without raising an exception) the lock will
        be attempted again.

        :param other_holder: A LockHeldInfo for the current holder; note that
            it might be None if the lock can be seen to be held but the info
            can't be read.
        Nzlocks.steal_deadÚlocks_steal_dead)Úlock_urlÚother_holder_infozstole lock from dead holder)Úis_lock_holder_known_deadÚ
get_configÚgetr	   Ú
ui_factoryÚshow_user_warningr
   Újoinr$   r;   r%   ÚstrÚforce_breakrD   r   )r1   rU   s     r4   rN   zLockDir._handle_lock_contention  s¾   € ð Ð$Ø×5Ò5Ñ7Ô7ð Ø—?’?Ñ$Ô$×(Ò(Ð);Ñ<Ô<ð Ý”M×3Ò3Ø*Ý!)¤¨t¬~Ô/BÀDÄIÑ!NÔ!NÝ*-¨lÑ*;Ô*;ð 4ñ =ô =ð =ð ×$Ò$ \Ñ2Ô2Ð2Ø—K’KÐ =Ñ>Ô>Ð>Ø�FÝ˜TÑ"Ô"Ð"r6   c                 ó  — |                       d|¦  «         	 | j                             || j        z   ¦  «         | j                             |¦  «         dS # t
          $ r(}t          t          d¦  «        |¦  «         Y d}~dS d}~ww xY w)zŸRemove the pending directory

        This is called if we failed to rename into place, so that the pending
        dirs don't clutter up the lockdir.
        z	remove %szerror removing pending lock: %sN)rD   r$   Údeleter*   Úrmdirr   r   r   )r1   rT   rF   s      r4   rP   zLockDir._remove_pending_dir3  s¦   € ð 	�Š�K Ñ)Ô)Ð)ð	@ØŒN×!Ò! '¨DÔ,<Ñ"<Ñ=Ô=Ð=ØŒN× Ò  Ñ)Ô)Ð)Ð)Ð)øÝð 	@ð 	@ð 	@Ý•Ð:Ñ;Ô;¸QÑ?Ô?Ð?Ð?Ð?Ð?Ð?Ð?Ð?øøøøð	@øøøs   ˜<A Á
BÁ BÂBc                 ó  — d                      | j        t          d¦  «        ¦  «        }	 | j                             |¦  «         nZ# t
          $ rM |                      d¦  «         |                      | j        ¬¦  «         | j                             |¦  «         Y nw xY wt           
                    | j        ¦  «        }|j        | _        | j                             || j        z   |                     ¦   «         ¦  «         |S )Nz	{}/{}.tmpé
   z*lock directory does not exist, creating itrB   )r8   r%   r   r$   rE   r   rD   rG   r-   ÚLockHeldInfoÚfor_this_processr/   rR   Úput_bytes_non_atomicr*   Úto_bytes)r1   rT   rV   s      r4   rK   zLockDir._create_pending_dir@  s÷   € Ø×$Ò$ T¤Yµ
¸2±´Ñ?Ô?ˆð		*ØŒN× Ò  Ñ)Ô)Ð)Ð)øÝð 	*ð 	*ð 	*ð �KŠKÐDÑEÔEÐEØ�KŠK˜TÔ/ˆKÑ0Ô0Ð0àŒN× Ò  Ñ)Ô)Ð)Ð)Ð)ð	*øøøõ ×,Ò,¨TÔ-CÑDÔDˆØ”ZˆŒ
ð
 	Œ×+Ò+¨G°dÔ6FÑ,FØ,0¯MªM©O¬Oñ	=ô 	=ð 	=àˆs   ªA ÁABÂBc                 óž  — | j         r	d| _         dS | j        st          j        | ¦  «        S | j        rd| _        d| _        dS | j        }t          j        ¦   «         }|                      d¦  «         d                     | j	        t          d¦  «        ¦  «        }|                      ¦   «          | j                             | j        |¦  «         d| _        | j                             || j        z   ¦  «         	 | j                             |¦  «         n@# t$          $ r3 |                      d|¦  «         | j                             |¦  «         Y nw xY w|                      dt          j        ¦   «         |z
  dz  ¦  «         t          j        | j                             | j	        ¦  «        |¦  «        }| j        d	         D ]} ||¦  «         ŒdS )
zRelease a held lock
        FNÚ	unlockingz{}/releasing.{}.tmpé   z2doing recursive deletion of non-empty directory %sz... unlock succeeded after %dmsrI   Úlock_released)r(   r&   r   Úcant_unlock_not_heldr'   rR   rJ   rD   r8   r%   r   Úconfirmr$   rL   r)   re   r*   rf   r   Údelete_treeÚ
LockResultÚabspathÚhooks)r1   Ú	old_noncerS   rT   ÚresultÚhooks         r4   ÚunlockzLockDir.unlockV  sÞ  € ð Ôð 	Ø#(ˆDÔ ØˆFØŒð 	3ÝÔ,¨TÑ2Ô2Ð2ØÔ!ð 	Ø%*ˆDÔ"Ø#ˆDŒOˆOˆOàœ
ˆIõ œ™œˆJØ�KŠK˜Ñ$Ô$Ð$Ø+×2Ò2°4´9½jÈ¹n¼nÑMÔMˆGà�LŠL‰NŒNˆNØŒN×!Ò! $¤.°'Ñ:Ô:Ð:Ø#ˆDŒOØŒN×!Ò! '¨DÔ,<Ñ"<Ñ=Ô=Ð=ð	4Ø”×$Ò$ WÑ-Ô-Ð-Ð-øÝ$ð 4ð 4ð 4ð
 —’ð !Ø")ñ+ô +ð +à”×*Ò*¨7Ñ3Ô3Ð3Ð3Ð3ð4øøøð �KŠKÐ9Ýœ™œ zÑ1°TÑ9ñ;ô ;ð ;å”_ T¤^×%;Ò%;¸D¼IÑ%FÔ%FØ%.ñ0ô 0ˆFàœ
 ?Ô3ð ð �Ø��V‘”��ðð s   Ã8D Ä:EÅEc           	      ó  — |                       ¦   «          	 |                      ¦   «         }nd# t          $ rW}t          j                             d                     | ¦  «        ¦  «        r|                      |j        ¦  «         Y d}~dS d}~ww xY w|�zt          j         	                    ddt          t          |¦  «        ¬¦  «        ¦  «        r@|                      |¦  «        }t          j                             d|j        z  ¦  «         dS dS dS )a\  Break a lock not held by this instance of LockDir.

        This is a UI centric function: it uses the ui.ui_factory to
        prompt for input if a lock is detected and there is any doubt about
        it possibly being still active.  force_break is the non-interactive
        version.

        :returns: LockResult for the broken lock.
        zBreak (corrupt {!r})NzBreak %(lock_info)szbreezy.lockdir.break)Ú	lock_infozBroke lock %s)Ú_check_not_lockedrM   r   r	   r_   Úget_booleanr8   Úforce_break_corruptÚ	file_dataÚconfirm_actionÚdictrb   rc   Úshow_messagerZ   )r1   Úholder_inforF   rx   s       r4   Ú
break_lockzLockDir.break_lock  s&  € ð 	×ÒÑ Ô Ð ð	ØŸ)š)™+œ+ˆKˆKøÝð 	ð 	ð 	åŒ}×(Ò(Ð)?×)FÒ)FÀtÑ)LÔ)LÑMÔMð 6Ø×(Ò(¨¬Ñ5Ô5Ð5ØˆFˆFˆFˆFˆFøøøøð		øøøð
 Ð"ÝŒ}×+Ò+Ø%Ø&Ý¥3 {Ñ#3Ô#3Ð4Ñ4Ô4ñ6ô 6ð 7ð ×)Ò)¨+Ñ6Ô6�Ý”×*Ò*Ø# f¤oÑ5ñ7ô 7ð 7ð 7ð 7ð #Ð"ð7ð 7s   –+ «
BµABÂBc                 óâ  — t          |t          ¦  «        st          d|z  ¦  «        ‚|                      ¦   «          |                      ¦   «         }|€dS ||k    rt          | ||¦  «        ‚d                     | j        t          d¦  «        ¦  «        }| j	         
                    | j        |¦  «         || j        z   }|                      |¦  «        }||k    rt          | ||¦  «        ‚| j	                             |¦  «         | j	                             |¦  «         t!          j        | j	                             | j        ¦  «        |j        ¦  «        }| j        d         D ]} ||¦  «         Œ|S )a2  Release a lock held by another process.

        WARNING: This should only be used when the other process is dead; if
        it still thinks it has the lock there will be two concurrent writers.
        In general the user's approval should be sought for lock breaks.

        After the lock is broken it will not be held by any process.
        It is possible that another process may sneak in and take the
        lock before the breaking process acquires it.

        :param dead_holder_info:
            Must be the result of a previous LockDir.peek() call; this is used
            to check that it's still held by the same process that the user
            decided was dead.  If this is not the current holder,
            LockBreakMismatch is raised.

        :returns: LockResult for the broken lock.
        zdead_holder_info: %rNú{}/broken.{}.tmpro   Úlock_broken)Ú
isinstanceri   Ú
ValueErrorr}   rM   r   r8   r%   r   r$   rL   r)   r*   Ú_read_info_filere   rf   r   rt   ru   rR   rv   )r1   Údead_holder_infoÚcurrent_inforT   Úbroken_info_pathÚbroken_inforx   ry   s           r4   rc   zLockDir.force_breakš  sr  € õ& Ð*­LÑ9Ô9ð 	HÝÐ3Ð6FÑFÑGÔGÐGØ×ÒÑ Ô Ð Ø—y’y‘{”{ˆØÐàˆFØÐ+Ò+Ð+Ý# D¨,Ð8HÑIÔIÐIØ$×+Ò+¨D¬IµzÀ"±~´~ÑFÔFˆØŒ×Ò˜dœn¨gÑ6Ô6Ð6ð # TÔ%5Ñ5ÐØ×*Ò*Ð+;Ñ<Ô<ˆØÐ*Ò*Ð*Ý# D¨+Ð7GÑHÔHÐHØŒ×ÒÐ.Ñ/Ô/Ð/ØŒ×Ò˜WÑ%Ô%Ð%Ý” ¤×!7Ò!7¸¼	Ñ!BÔ!BØ!-Ô!3ñ5ô 5ˆà”J˜}Ô-ð 	ð 	ˆDØˆD�‰LŒLˆLˆLØˆr6   c                 ó\  — |                       ¦   «          d                     | j        t          d¦  «        ¦  «        }| j                             | j        |¦  «         || j        z   }| j                             |¦  «        }t          j
        |¦  «        }||k    rt          | ||¦  «        ‚| j                             |¦  «         | j                             |¦  «         t          j        | j                             | j        ¦  «        ¦  «        }| j        d         D ]} ||¦  «         ŒdS )as  Release a lock that has been corrupted.

        This is very similar to force_break, it except it doesn't assume that
        self.peek() can work.

        :param corrupt_info_lines: the lines of the corrupted info file, used
            to check that the lock hasn't changed between reading the (corrupt)
            info file and calling force_break_corrupt.
        r‡   ro   rˆ   N)r}   r8   r%   r   r$   rL   r)   r*   Ú	get_bytesr   Úsplit_linesr   re   rf   r   rt   ru   rv   )r1   Úcorrupt_info_linesrT   rŽ   Úbroken_contentÚbroken_linesrx   ry   s           r4   r   zLockDir.force_break_corruptÇ  s  € ð 	×ÒÑ Ô Ð Ø$×+Ò+¨D¬IµzÀ"±~´~ÑFÔFˆØŒ×Ò˜dœn¨gÑ6Ô6Ð6ð # TÔ%5Ñ5ÐØœ×1Ò1Ð2BÑCÔCˆÝÔ*¨>Ñ:Ô:ˆØÐ-Ò-Ð-Ý# D¨,Ð8JÑKÔKÐKØŒ×ÒÐ.Ñ/Ô/Ð/ØŒ×Ò˜WÑ%Ô%Ð%Ý” ¤×!7Ò!7¸¼	Ñ!BÔ!BÑCÔCˆØ”J˜}Ô-ð 	ð 	ˆDØˆD�‰LŒLˆLˆLð	ð 	r6   c                 ó8   — | j         rt          d| z  ¦  «        ‚dS )z5If the lock is held by this instance, raise an error.zcan't break own lock: %rN)r&   ÚAssertionErrorr<   s    r4   r}   zLockDir._check_not_lockedä  s-   € àŒ?ð 	DÝ Ð!;¸dÑ!BÑCÔCÐCð	Dð 	Dr6   c                 óº   — | j         st          | ¦  «        ‚|                      ¦   «         }|€t          | ¦  «        ‚|j        | j        k    rt          | ¦  «        ‚dS )a\  Make sure that the lock is still held by this locker.

        This should only fail if the lock was broken by user intervention,
        or if the lock has been affected by a bug.

        If the lock is not thought to be held, raises LockNotHeld.  If
        the lock is thought to be held but has been broken, raises
        LockBroken.
        N)r&   r   rM   r   rR   ©r1   rV   s     r4   rr   zLockDir.confirmé  sa   € ð Œð 	$Ý˜dÑ#Ô#Ð#Ø�yŠy‰{Œ{ˆØˆ<å˜TÑ"Ô"Ð"ØŒ:˜œÒ#Ð#å˜TÑ"Ô"Ð"ð $Ð#r6   c                 óf   — t                                | j                             |¦  «        ¦  «        S )zbRead one given info file.

        peek() reads the info file of the lock holder, if any.
        )ri   Úfrom_info_file_bytesr$   r‘   )r1   r%   s     r4   r‹   zLockDir._read_info_fileý  s/   € õ
 ×0Ò0ØŒN×$Ò$ TÑ*Ô*ñ,ô ,ð 	,r6   c                 ó²   — 	 |                       | j        ¦  «        }|                      d¦  «         |S # t          $ r |                      d¦  «         Y dS w xY w)zÛCheck if the lock is held by anyone.

        If it is held, this returns the lock info structure as a dict
        which contains some information about the current lock holder.
        Otherwise returns None.
        zpeek -> heldzpeek -> not heldN)r‹   r+   rD   r   r™   s     r4   rM   zLockDir.peek  sk   € ð	,Ø×'Ò'¨Ô(<Ñ=Ô=ˆDØ�KŠK˜Ñ'Ô'Ð'ØˆKøÝð 	,ð 	,ð 	,Ø�KŠKÐ*Ñ+Ô+Ð+Ð+Ð+Ð+ð	,øøøs   ‚03 ³AÁAc                 ó   — dS )zDWrite information about a pending lock to a temporary file.
        N© r<   s    r4   Ú_prepare_infozLockDir._prepare_info  s   € € € r6   c                 óþ   — | j         rt          | ¦  «        ‚|                      ¦   «         }t          j        | j                             | j        ¦  «        | j        ¦  «        }| j	        d         D ]} ||¦  «         Œ|S )zùTake the lock; fail if it's already held.

        If you wish to block until the lock can be obtained, call wait_lock()
        instead.

        :return: The lock token.
        :raises LockContention: if the lock is held by someone else.
        Úlock_acquired)
r(   r   rW   r   rt   r$   ru   r%   rR   rv   )r1   rx   Úhook_resultry   s       r4   Úattempt_lockzLockDir.attempt_lock  s†   € ð Ôð 	'Ý  Ñ&Ô&Ð&Ø×#Ò#Ñ%Ô%ˆÝ”o d¤n×&<Ò&<¸T¼YÑ&GÔ&GØ&*¤jñ2ô 2ˆà”J˜Ô/ð 	ð 	ˆDØˆD�ÑÔÐÐØˆr6   c                 óª   — | j                              | j        ¦  «        }|                     d¦  «        r|                     d¦  «        d         }nd}|S )z?Give a nicely-printable representation of the URL of this lock.zfile://z.bzr/r   Ú )r$   ru   r%   Ú
startswithÚsplit)r1   rZ   s     r4   Úlock_url_for_displayzLockDir.lock_url_for_display)  sS   € ð ”>×)Ò)¨$¬)Ñ4Ô4ˆØ×Ò˜yÑ)Ô)ð 	Ø—~’~ gÑ.Ô.¨qÔ1ˆHˆHàˆHØˆr6   c                 ó¶  — |€t           }|€t          }t          j        ¦   «         |z   }d}d}d}|                      ¦   «         }	 |dz  }	 |                      ¦   «         S # t
          $ r Y nw xY w|                      ¦   «         }	|	�¾|	|k    r¸|€t          d¦  «        }
nt          d¦  «        }
|	}t          d¦  «                             |
||	¦  «        }|€'t          j	        dt          j
        |¦  «        ¦  «        }|dk    r|d	t          d
¦  «        |z  z   z  }|d	t          d¦  «        z   z  }|                      |¦  «         |�*||k    r$|                      d¦  «         t          | ¦  «        ‚t          j        ¦   «         |z   |k     r+|                      d|¦  «         t          j        |¦  «         n&|                      d|¦  «         t          d|¦  «        ‚�Œ—)a}  Wait a certain period for a lock.

        If the lock can be acquired within the bounded time, it
        is taken and this returns.  Otherwise, LockContention
        is raised.  Either way, this function should return within
        approximately `timeout` seconds.  (It may be a bit more if
        a transport operation takes a long time to complete.)

        :param timeout: Approximate maximum amount of time to wait for the
        lock, in seconds.

        :param poll: Delay in seconds between retrying the lock.

        :param max_attempts: Maximum number of times to try to lock.

        :return: The lock token.
        Nr   Tr   zUnable to obtainzLock owner changed forz{0} lock {1} {2}.z%H:%M:%Sú
z7Will continue to try until %s, unless you press Ctrl-C.z#See "brz help break-lock" for more.zexceeded %d attemptszwaiting %ssztimeout after waiting %ssz(local))Ú_DEFAULT_TIMEOUT_SECONDSÚ_DEFAULT_POLL_SECONDSrJ   r¨   r£   r   rM   r   r8   ÚstrftimeÚ	localtimer.   rD   Úsleep)r1   ÚtimeoutÚpollÚmax_attemptsÚdeadlineÚdeadline_strÚ	last_infoÚattempt_countrZ   Únew_infoÚstartÚmsgs               r4   Ú	wait_lockzLockDir.wait_lock4  s#  € ð$ ˆ?Ý.ˆGØˆ<Ý(ˆDõ ”9‘;”; Ñ(ˆØˆØˆ	ØˆØ×,Ò,Ñ.Ô.ˆð*	:Ø˜QÑˆMðØ×(Ò(Ñ*Ô*Ð*øÝ!ð ð ð à�ðøøøð —y’y‘{”{ˆHØÐ#¨°IÒ(=Ð(=ØÐ$Ý#Ð$6Ñ7Ô7�E�Eå#Ð$<Ñ=Ô=�EØ$�	ÝÐ1Ñ2Ô2×9Ò9¸%ÀØ:BñDô D�àÐ'Ý#'¤=°Ý15´ÀÑ1IÔ1Iñ$Kô $K�Là˜Q’;�;Ø˜4¥'ð,ñ#-ô #-à/;ñ#<ñ <ñ <�Cð �t�gÐ&KÑLÔLÑLÑL�Ø×%Ò% cÑ*Ô*Ð*ØÐ(¨}ÀÒ/LÐ/LØ—’Ð2Ñ3Ô3Ð3Ý$ TÑ*Ô*Ð*ÝŒy‰{Œ{˜TÑ! HÒ,Ð,Ø—’˜M¨4Ñ0Ô0Ð0Ý”
˜4Ñ Ô Ð Ð ð
 —’Ð7¸ÑAÔAÐAÝ$ Y°Ñ9Ô9Ð9ñU*	:s   Á
A Á
A+Á*A+c                 ó   — d| _         d S )NT©r'   r<   s    r4   Úleave_in_placezLockDir.leave_in_place~  s   € Ø!%ˆÔÐÐr6   c                 ó   — d| _         d S )NFr¼   r<   s    r4   Údont_leave_in_placezLockDir.dont_leave_in_place�  s   € Ø!&ˆÔÐÐr6   c                 ó†   — |�,|                       |¦  «         || _        d| _        d| _        |S |                      ¦   «         S )aÙ  Wait for and acquire the lock.

        :param token: if this is already locked, then lock_write will fail
            unless the token matches the existing lock.
        :returns: a token if this instance supports tokens, otherwise None.
        :raises TokenLockingNotSupported: when a token is given but this
            instance doesn't support using token locks.
        :raises MismatchedToken: if the specified token doesn't match the token
            of the existing lock.

        A token should be passed in if you know that you have locked the object
        some other way, and need to synchronise this object's state with that
        fact.

        XXX: docstring duplicated from LockableFiles.lock_write.
        NT)Úvalidate_tokenrR   r&   r'   rº   )r1   Útokens     r4   Ú
lock_writezLockDir.lock_write„  sI   € ð" ÐØ×Ò Ñ&Ô&Ð&ØˆDŒJØ"ˆDŒOØ%)ˆDÔ"ØˆLà—>’>Ñ#Ô#Ð#r6   c                 óN   — | j         s| j        rt          | ¦  «        ‚d| _        dS )z¯Compatibility-mode shared lock.

        LockDir doesn't support shared read-only locks, so this
        just pretends that the lock is taken but really does nothing.
        TN)r&   r(   r   r<   s    r4   Ú	lock_readzLockDir.lock_readž  s4   € ð Œ?ð 	'˜dÔ2ð 	'Ý  Ñ&Ô&Ð&Ø#ˆÔÐÐr6   c                 ó°   — |�S|                       ¦   «         }|€d }n|j        }||k    rt          j        ||¦  «        ‚|                      d|¦  «         d S d S )Nzrevalidated by token %r)rM   rR   r   ÚTokenMismatchrD   )r1   rÂ   rV   Ú
lock_tokens       r4   rÁ   zLockDir.validate_token­  si   € ØÐØ—9’9‘;”;ˆDØˆ|à!�
�
à!œZ�
Ø˜
Ò"Ð"ÝÔ*¨5°*Ñ=Ô=Ð=à—’Ð5°uÑ=Ô=Ð=Ð=Ð=ð Ðr6   c                 óp   — dt           j        vrd S t          t          | ¦  «        dz   ||z  z   ¦  «         d S )Nr   z: )r   Údebug_flagsr   rb   )r1   r8   Úargss      r4   rD   zLockDir._traceº  s=   € Ø�Ô*Ð*Ð*ØˆFÝ�s�4‰yŒy˜4Ñ 6¨D¡=Ñ1Ñ2Ô2Ð2Ð2Ð2r6   c                 ó(   — t          j        ¦   «         S )z0Get the configuration that governs this lockdir.)r   ÚGlobalStackr<   s    r4   r]   zLockDir.get_config¿  s   € õ
 Ô!Ñ#Ô#Ð#r6   )r!   r"   Nr?   )NNN)$r:   Ú
__module__Ú__qualname__Ú__doc__r*   r5   r=   ÚpropertyÚis_heldrG   rW   rN   rP   rK   r   r   r   rz   r…   rc   r   r}   rr   r‹   rM   rŸ   r£   r¨   rº   r½   r¿   rÃ   rÅ   rÁ   rD   r]   rž   r6   r4   r    r    ¤   s  € € € € € ðð ð €Kà6;Ø7;ð.ð .ð .ð .ð8(ð (ð (ð
 ˆhÐ3Ð3Ñ4Ô4€Gð
&ð 
&ð 
&ð 
&ð?ð ?ð ?ðB#ð #ð #ð2@ð @ð @ðð ð ð, €[�˜jÑ)Ô)ð&ð &ñ *Ô)ð&ðP7ð 7ð 7ð6+ð +ð +ðZð ð ð:Dð Dð Dð
#ð #ð #ð(,ð ,ð ,ð,ð ,ð ,ðð ð ðð ð ð$	ð 	ð 	ðH:ð H:ð H:ð H:ðT&ð &ð &ð'ð 'ð 'ð$ð $ð $ð $ð4$ð $ð $ð>ð >ð >ð3ð 3ð 3ð
$ð $ð $ð $ð $r6   r    c                   ó–   — e Zd ZdZd„ Zd„ Zd„ Zd„ Zed„ ¦   «         Z	d„ Z
ed„ ¦   «         Zd	„ Zed
„ ¦   «         Zd„ Zd„ Zd„ Zd„ Zd„ ZdS )ri   a?  The information recorded about a held lock.

    This information is recorded into the lock when it's taken, and it can be
    read back by any process with access to the lockdir.  It can be used, for
    example, to tell the user who holds the lock, or to try to detect whether
    the lock holder is still alive.
    c                 ó   — || _         d S r?   )Ú	info_dict)r1   rÕ   s     r4   r5   zLockHeldInfo.__init__Ð  s   € Ø"ˆŒˆˆr6   c                 óL   — d                      | j        j        | j        ¦  «        S )z1Return a debugging representation of this object.z{}({!r}))r8   r9   r:   rÕ   r<   s    r4   r=   zLockHeldInfo.__repr__Ó  s   € à× Ò  ¤Ô!8¸$¼.ÑIÔIÐIr6   c                 óN   — |                       ¦   «         }t          d¦  «        |z  S )z2Return a user-oriented description of this object.zJheld by %(user)s on %(hostname)s (process #%(pid)s), acquired %(time_ago)s)Úto_readable_dictr   )r1   Úds     r4   Ú__str__zLockHeldInfo.__str__×  s2   € à×!Ò!Ñ#Ô#ˆÝð$ñ%ô %à'(ñ)ð 	*r6   c                 ód  — | j                              d¦  «        }|€d}n.t          t          j        ¦   «         | j         d         z
  ¦  «        }| j                              dd¦  «        }| j                              dd¦  «        }| j                              dd¦  «        }t	          ||||¬¦  «        S )	a#  Turn the holder info into a dict of human-readable attributes.

        For example, the start time is presented relative to the current time,
        rather than as seconds since the epoch.

        Returns a list of [user, hostname, pid, time_ago] all as readable
        strings.
        rS   Nz	(unknown)Úuserz	<unknown>ÚhostnameÚpid)rÜ   rÝ   rÞ   Útime_ago)rÕ   r^   r   rJ   r‚   )r1   rS   rß   rÜ   rÝ   rÞ   s         r4   rØ   zLockHeldInfo.to_readable_dictÞ  s²   € ð ”^×'Ò'¨Ñ5Ô5ˆ
ØÐØ"ˆHˆHå#Ý”	‘”˜dœn¨\Ô:Ñ:ñ<ô <ˆHàŒ~×!Ò! &¨+Ñ6Ô6ˆØ”>×%Ò% j°+Ñ>Ô>ˆØŒn× Ò  ¨Ñ4Ô4ˆÝØØØØð	ñ ô ð 	r6   c                 ó^   — |                       d¦  «        }|r|                     d¦  «        nd S )NrR   Úascii)r^   Úencode)r1   rR   s     r4   rR   zLockHeldInfo.nonceö  s/   € à—’˜Ñ!Ô!ˆØ(-Ð7ˆu�|Š|˜GÑ$Ô$Ð$°4Ð7r6   c                 ó6   — | j                              |¦  «        S )z;Return the contents of a field from the lock info, or None.)rÕ   r^   )r1   Ú
field_names     r4   r^   zLockHeldInfo.getû  s   € àŒ~×!Ò! *Ñ-Ô-Ð-r6   c           	      ó  — t          t          ¦   «         t          j        ¦   «         t	          d¦  «        t          t          j        ¦   «         ¦  «        t          ¦   «         ¬¦  «        }|�|                     |¦  «          | |¦  «        S )zDReturn a new LockHeldInfo for a lock taken by this process.
        ro   )rÝ   rÞ   rR   rS   rÜ   )	r‚   r   ÚosÚgetpidr   ÚintrJ   Úget_username_for_lock_infoÚupdate)Úclsr/   rV   s      r4   rj   zLockHeldInfo.for_this_processÿ  st   € õ Ý"‘_”_Ý”	‘”Ý˜R‘.”.Ý�4œ9™;œ;Ñ'Ô'Ý+Ñ-Ô-ðñ ô ˆð Ð(Ø�KŠKÐ)Ñ*Ô*Ð*Øˆs�4‰yŒyÐr6   c                 óZ   — t          j        | j        ¦  «                             d¦  «        S )Nzutf-8)ÚyamlÚdumprÕ   râ   r<   s    r4   rl   zLockHeldInfo.to_bytes  s"   € ÝŒy˜œÑ(Ô(×/Ò/°Ñ8Ô8Ð8r6   c                 ó  — 	 t          j        |¦  «        }n`# t           j        j        $ rI}t	          j        |¦  «        }t          d|¦  «         t          dt          |¦  «        z   |¦  «        ‚d}~ww xY w|€ | i ¦  «        S  | |¦  «        S )z-Construct from the contents of the held file.zCorrupt lock info file: %rz could not parse lock info file: N)	rí   Ú	safe_loadÚreaderÚReaderErrorr   r’   r   r   rb   )rë   Úinfo_file_bytesÚretrF   Úliness        r4   r›   z!LockHeldInfo.from_info_file_bytes  s£   € ð	%Ý”. Ñ1Ô1ˆCˆCøÝŒ{Ô&ð 	%ð 	%ð 	%ÝÔ'¨Ñ8Ô8ˆEÝÐ/°Ñ7Ô7Ð7ÝÐ@Å3ÀqÁ6Ä6ÑIØ#ñ%ô %ð %øøøøð	%øøøð
 ˆ;ð �3�r‘7”7ˆNà�3�s‘8”8ˆOs   ‚ —A4«AA/Á/A4c                 ó    — t          | ¦  «        S r?   )Úidr<   s    r4   Ú__hash__zLockHeldInfo.__hash__#  s   € Ý�$‰xŒxˆr6   c                 óf   — t          | ¦  «        t          |¦  «        k    rdS | j        |j        k    S )z Equality check for lock holders.F)ÚtyperÕ   ©r1   Úothers     r4   Ú__eq__zLockHeldInfo.__eq__&  s-   € å�‰:Œ:�˜e™œÒ$Ð$Ø�5ØŒ~ ¤Ò0Ð0r6   c                 ó   — | |k     S r?   rž   rû   s     r4   Ú__ne__zLockHeldInfo.__ne__,  s   € Ø˜5’=Ð Ð r6   c                 óê   — |                       d¦  «        t          ¦   «         k    oN|                       d¦  «        t          j        ¦   «         k    o$|                       d¦  «        t	          ¦   «         k    S )z9True if this process seems to be the current lock holder.rÝ   rÞ   rÜ   )r^   r   ræ   rç   ré   r<   s    r4   Úis_locked_by_this_processz&LockHeldInfo.is_locked_by_this_process/  sa   € ð �HŠH�ZÑ Ô ¥M¡O¤OÒ3ð AØ—’˜‘”¥2¤9¡;¤;Ò.ðAà—’˜Ñ Ô Õ$>Ñ$@Ô$@Ò@ð	Br6   c                 óì  — |                       d¦  «        t          ¦   «         k    rdS |                       d¦  «        dk    rdS |                       d¦  «        t          ¦   «         k    rdS | j                              dd¦  «        }|s$t	          d                     | ¦  «        ¦  «         dS 	 t          |¦  «        }n&# t          $ r t	          d|›d	| ›�¦  «         Y dS w xY wt          j	        |¦  «        S )
a  True if the lock holder process is known to be dead.

        False if it's either known to be still alive, or if we just can't tell.

        We can be fairly sure the lock holder is dead if it declared the same
        hostname and there is no process with the given pid alive.  If people
        have multiple machines with the same hostname this may cause trouble.

        This doesn't check whether the lock holder is in fact the same process
        calling this method.  (In that case it will return true.)
        rÝ   FÚ	localhostrÜ   rÞ   Nzno pid recorded in {!r}zcan't parse pid z from )
r^   r   ré   rÕ   r   r8   rè   rŠ   r   Úis_local_pid_dead)r1   Úpid_strrÞ   s      r4   r\   z&LockHeldInfo.is_lock_holder_known_dead6  s  € ð �8Š8�JÑÔ¥=¡?¤?Ò2Ð2Ø�5Ø�8Š8�JÑÔ ;Ò.Ð.à�5Ø�8Š8�FÑÔÕ9Ñ;Ô;Ò;Ð;ð �5Ø”.×$Ò$ U¨DÑ1Ô1ˆØð 	ÝÐ,×3Ò3°DÑ9Ô9Ñ:Ô:Ð:Ø�5ð	Ý�g‘,”,ˆCˆCøÝð 	ð 	ð 	ÝˆFØ�g�g˜t˜tð%ñ &ô &ð &à�5�5ð	øøøõ Ô(¨Ñ-Ô-Ð-s   Â,B< Â<CÃCN)r:   rÎ   rÏ   rÐ   r5   r=   rÚ   rØ   rÑ   rR   r^   Úclassmethodrj   rl   r›   rø   rý   rÿ   r  r\   rž   r6   r4   ri   ri   Ç  s  € € € € € ðð ð#ð #ð #ðJð Jð Jð*ð *ð *ðð ð ð0 ð8ð 8ñ „Xð8ð.ð .ð .ð ðð ñ „[ðð9ð 9ð 9ð ðð ñ „[ðð"ð ð ð1ð 1ð 1ð!ð !ð !ðBð Bð Bð.ð .ð .ð .ð .r6   ri   c                  ó    — 	 t          j        ¦   «                              d¦  «        S # t          j        $ r t          j        ¦   «         cY S w xY w)zªGet a username suitable for putting into a lock.

    It's ok if what's written here is not a proper email address as long
    as it gives some clue who the user is.
    Úemail)r   rÍ   r^   r   ÚNoWhoamir   Úgetuser_unicoderž   r6   r4   ré   ré   X  sV   € ð)ÝÔ!Ñ#Ô#×'Ò'¨Ñ0Ô0Ð0øÝŒ?ð )ð )ð )ÝÔ&Ñ(Ô(Ð(Ð(Ð(ð)øøøs   ‚%( ¨"AÁA))rÐ   ræ   rJ   rí   r¥   r   r   r   r   r   r	   r
   Ú
decoratorsr   r   r   r   r   r   r   r   r   r   r   Úi18nr   r   r   r   Útracer   r   r$   r   r   r«   r¬   ÚLockr    ri   ré   rž   r6   r4   ú<module>r     s  ðð"Rð Rðz 
€	€	€	Ø €€€Ø €€€ðð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð $Ð #Ð #Ð #Ð #Ð #ðð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð Ð Ð Ð Ð Ð Ø <Ð <Ð <Ð <Ð <Ð <Ð <Ð <Ð <Ð <Ø Ð Ð Ð Ð Ð Ð Ð ðð ð ð ð ð ð ð ð* Ð ØÐ ð`$ð `$ð `$ð `$ð `$ˆdŒiñ `$ô `$ð `$ðFN.ð N.ð N.ð N.ð N.ñ N.ô N.ð N.ðb	)ð 	)ð 	)ð 	)ð 	)r6   