
                         r    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d
dZdd	Zy)z#Generates tarballs for Git trees.

    N)SEEK_END)BytesIO)closingc                       e Zd ZdZd ZddZy)ChunkedBytesIOa=  Turn a list of bytestrings into a file-like object.

    This is similar to creating a `BytesIO` from a concatenation of the
    bytestring list, but saves memory by NOT creating one giant bytestring
    first::

        BytesIO(b''.join(list_of_bytestrings)) =~= ChunkedBytesIO(
            list_of_bytestrings)
    c                      || _         d| _        y )N)r   r   )contentspos)selfr	   s     "lib/third_party/dulwich/archive.py__init__zChunkedBytesIO.__init__.   s         Nc                    |dk  rt        d      }g }| j                  \  }}|t        | j                        k  r|t        | j                  |         |z
  k  r3|j	                  | j                  |   |||z           ||z  }||f| _        nh|j	                  | j                  |   |d         |t        | j                  |         |z
  z  }|dz  }d}||f| _        |t        | j                        k  rdj                  |      S )Nr   inf   r   )floatr
   lenr	   appendjoin)r   maxbytesbufchunkcursors        r   readzChunkedBytesIO.read2   s    a<U|Hvc$--((#dmmE23f<<

4==/(9JKL("!6?

4==/89Ce 45>>
!6? c$--(( xx}r   )N)__name__
__module____qualname____doc__r   r    r   r   r   r   #   s    r   r   c              #   |  K   t               }t        t        j                  dd|z  |            5 }|dk(  rx|j	                  d       |j                  d      dk(  sJ d       |j	                  d       |j                  t        j                  d	|             |j	                  dt               t        | ||      D ]  \  }}	 | |j                     }	t        |	j                        }
t        j                         }|j!                  d
      |_        |	j%                         |_        |j(                  |_        ||_        |j-                  ||
       |j/                          |j1                  d       |j	                  d        	 ddd       |j/                          y# t        $ r Y w xY w# 1 sw Y   +xY ww)aE  Generate a tar stream for the contents of a Git tree.

    Returns a generator that lazily assembles a .tar.gz archive, yielding it in
    pieces (bytestrings). To obtain the complete .tar.gz binary file, simply
    concatenate these chunks.

    Args:
      store: Object store to retrieve objects from
      tree: Tree object for the tree root
      mtime: UNIX timestamp that is assigned as the modification time for
        all files, and the gzip header modification time if format='gz'
      format: Optional compression format for tarball
    Returns:
      Bytestrings
    Nzw:%sgzr      s   zInvalid gzip header   z<Lascii)r   r   tarfileopenseekr   writestructpackr   
_walk_treeshaKeyErrorr   chunkedTarInfodecodename
raw_lengthsizemodemtimeaddfilegetvaluetruncate)storetreer5   prefixformatr   tarentry_abspathentryblobdatainfos               r   
tar_streamrC   H   sf      )C	dFVOS9	:cT> HHQK88A;+-D/DD-HHQKIIfkk$./HHQ!$.udF$C M5UYY'
 "$,,/D??$D%,,W5DI)DI

DIDJKKd#,,. LLOHHQK' %D 
;B ,,.#   ! 
;	:sB   .F<BF0F!B4F0F<!	F-*F0,F--F00F95F<c              #     K   |j                         D ]k  }t        j                  ||j                        }t	        j
                  |j                        r$t        | | |j                     |      D ]  }|  f||f m yw)zfRecursively walk a dulwich Tree, yielding tuples of
    (absolute path, TreeEntry) along the way.
    N)		iteritems	posixpathr   pathstatS_ISDIRr4   r+   r,   )r9   r:   rootr?   r>   _s         r   r+   r+   }   sl      !!tUZZ8<<

#uUYY'7G H !%(( "s   B B)r    )r   )r   rF   rH   r%   r)   osr   ior   
contextlibr   objectr   rC   r+   r   r   r   <module>rQ      s9   ,       "V "J2j
)r   