
    	^ca                         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 d dl	m
Z
mZ  G d de          Z G d d	e          Z G d
 d          Z G d d          ZddZ G d de          ZdS )    N)hex)open)pycompat)hashutilprocutilc                       e Zd ZdS )BundleWriteExceptionN__name__
__module____qualname__     :/usr/lib/python3/dist-packages/hgext/infinitepush/store.pyr	   r	              Dr   r	   c                       e Zd ZdS )BundleReadExceptionNr
   r   r   r   r   r      r   r   r   c                   `    e Zd ZdZej        Zej        d             Zej        d             Z	dS )abstractbundlestorea6  Defines the interface for bundle stores.

    A bundle store is an entity that stores raw bundle data. It is a simple
    key-value store. However, the keys are chosen by the store. The keys can
    be any Python object understood by the corresponding bundle index (see
    ``abstractbundleindex`` below).
    c                     dS )zWrite bundle data to the store.

        This function receives the raw data to be written as a str.
        Throws BundleWriteException
        The key of the written data MUST be returned.
        Nr   )selfdatas     r   writezabstractbundlestore.write'         r   c                     dS )zObtain bundle data for a key.

        Returns None if the bundle isn't known.
        Throws BundleReadException
        The returned object should be a file object supporting read()
        and close().
        Nr   )r   keys     r   readzabstractbundlestore.read0   r   r   N)
r   r   r   __doc__abcABCMeta__metaclass__abstractmethodr   r   r   r   r   r   r      sc          KM   	    r   r   c                   0    e Zd ZdZd Zd Zd Zd Zd ZdS )filebundlestorezkbundle store in filesystem

    meant for storing bundles somewhere on disk and on network filesystems
    c                 .   || _         || _        |                    dd          | _        | j        s%| j        j                            dd          | _        t          j                            | j                  st          j	        | j                   d S d S )Ns   scratchbranchs	   storepaths   scratchbranchess   filebundlestore)
uirepo
configpath	storepathvfsjoinospathexistsmakedirs)r   r&   r'   s      r   __init__zfilebundlestore.__init__A   s    	'7FF~ 	!Y]//"$6 DN w~~dn-- 	(K'''''	( 	(r   c                 n    t           j                            | j        |dd         |dd                   S )zFirst two bytes of the hash are the name of the upper
        level directory, next two bytes are the name of the
        next level directoryr         )r,   r-   r+   r)   )r   	hashvalues     r   _dirpathzfilebundlestore._dirpathL   s.     w||DNIacNIacNKKKr   c                 h    t           j                            |                     |          |          S )N)r,   r-   r+   r5   )r   filenames     r   	_filepathzfilebundlestore._filepathR   s$    w||DMM(33X>>>r   c                    t          t          j        |                                                    }|                     |          }t
          j                            |          st          j        |           t          | 
                    |          d          5 }|                    |           d d d            n# 1 swxY w Y   |S )Ns   wb)r   r   sha1digestr5   r,   r-   r.   r/   r   r8   r   )r   r   r7   dirpathfs        r   r   zfilebundlestore.writeU   s    x}T**113344--))w~~g&& 	!K   $..**E22 	aGGDMMM	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 s   CCCc                     	 t          |                     |          d          5 }|                                cd d d            S # 1 swxY w Y   d S # t          $ r Y d S w xY w)Ns   rb)r   r8   r   IOError)r   r   r=   s      r   r   zfilebundlestore.reada   s    	dnnS))511  Qvvxx                                    	 	 	44	s3   #A AA A

A A
A 
A! A!N)	r   r   r   r   r0   r5   r8   r   r   r   r   r   r$   r$   ;   sl         
	( 	( 	(L L L? ? ?
 
 
    r   r$   c                     g }| D ]S}|r|dk    r|                     |            |r|dk    r|                     |           >|                     |           T|S )zFormats `args` with Infinitepush replacements.

    Hack to get `str.format()`-ed strings working in a BC way with
    bytes.
    s
   {filename}s   {handle})append)argsr7   handleformatted_argsargs        r   format_placeholders_argsrF   i   s     N ' ' 	'},,!!(++++ 	'{**!!&))))!!#&&&&r   c                   &    e Zd Zd Zd Zd Zd ZdS )externalbundlestorec                 >    || _         || _        || _        || _        dS )a6  
        `put_binary` - path to binary file which uploads bundle to external
            storage and prints key to stdout
        `put_args` - format string with additional args to `put_binary`
                     {filename} replacement field can be used.
        `get_binary` - path to binary file which accepts filename and key
            (in that order), downloads bundle from store and saves it to file
        `get_args` - format string with additional args to `get_binary`.
                     {filename} and {handle} replacement field can be used.
        N)put_argsget_args
put_binary
get_binary)r   rL   rJ   rM   rK   s        r   r0   zexternalbundlestore.__init__{   s$     ! $$r   c                     t          j        t          j        t          j        |          t           j        t           j        d          }|                                \  }}|j        }|||fS )NT)stdoutstderr	close_fds)	
subprocessPopenr   rapplyr   tonativestrPIPEcommunicate
returncode)r   rB   prO   rP   rX   s         r   _call_binaryz externalbundlestore._call_binary   s_    OH0$77??	
 
 
 \
66))r   c                    t          j                    5 }|                    |           |                                 |                    d           t          | j        |j                  }|                     | j	        g|z             \  }}}|dk    rt          d|z            |                                }t          |          dk    r|d         cd d d            S t          d| j	        |fz            # 1 swxY w Y   d S )Nr   )r7   s&   Failed to upload to external store: %s   s   Bad output from %s: %s)r   namedtempfiler   flushseekrF   rJ   namerZ   rL   r	   
splitlineslen)r   r   temprD   rX   rO   rP   stdout_liness           r   r   zexternalbundlestore.write   s_   
 #%% 	JJtJJLLLIIaLLL5	  N *.):):!N2* *&J Q*=F   ",,..L<  A%%#A#	 	 	 	 	 	 	 	& +-&0II  '	 	 	 	 	 	 	 	 	 	s   CC<#C<<D D c                 0   t          j                    5 }t          | j        |j        |          }|                     | j        g|z             \  }}}|dk    rt          d|z            |                                cd d d            S # 1 swxY w Y   d S )N)r7   rC   r   s*   Failed to download from external store: %s)	r   r]   rF   rK   r`   rZ   rM   r   r   )r   rC   rc   rD   rX   rO   rP   s          r   r   zexternalbundlestore.read   s     #%% 	5	&  N *.):):!N2* *&J Q)AFJ   99;;	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   A*BBBN)r   r   r   r0   rZ   r   r   r   r   r   rH   rH   z   sP        % % %"	* 	* 	*  8    r   rH   )NN)r   r,   rR   mercurial.noder   mercurial.pycompatr   	mercurialr   mercurial.utilsr   r   	Exceptionr	   r   r   r$   rF   rH   r   r   r   <module>rk      s{   


 				           # # # # # #             	 	 	 	 	9 	 	 		 	 	 	 	) 	 	 	       >+ + + + + + + +\   "H H H H H- H H H H Hr   