o
    Uh                     @   sn   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 dZdd
dZdddZdS )z!Generates tarballs for Git trees.    N)closing)BytesIO)SEEK_ENDc                   @   s$   e Zd ZdZdddZd	ddZdS )
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)
    returnNc                 C   s   || _ d| _d S )N)r   r   )contentspos)selfr    r
   c/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/dulwich/archive.py__init__-   s   
zChunkedBytesIO.__init__c                 C   s   |dk rt d}g }| j\}}|t| jk rb|t| j| | k r9|| j| |||   ||7 }||f| _n)|| j| |d   |t| j| | 8 }|d7 }d}||f| _|t| jk sd|S )Nr   inf       )floatr   lenr   appendjoin)r	   maxbytesbufchunkcursorr
   r
   r   read1   s"   



zChunkedBytesIO.read)r   N)N)__name__
__module____qualname____doc__r   r   r
   r
   r
   r   r   "   s    

r   r    c              
   c   s8   t  }ttdd| |}}|dkr9|d |ddks%J d|d |td	| |dt	 t
| ||D ]F\}}z| |j }	W n	 tyS   Y q?w t|	j}
t }|d
d|_|	 |_|j|_||_|||
 | V  |d |d q?W d   n1 sw   Y  | V  dS )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:gzr      s   zInvalid gzip header   z<Lzutf-8surrogateescape)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treer2   prefixformatr   tarentry_abspathentryblobdatainfor
   r
   r   
tar_streamG   s8   





!r@   c                 c   sR    |  D ]!}t||j}t|jr!t| | |j |E dH  q||fV  qdS )zfRecursively walk a dulwich Tree, yielding tuples of
    (absolute path, TreeEntry) along the way.
    N)		iteritems	posixpathr   pathstatS_ISDIRr1   r(   r)   )r6   r7   rootr<   r;   r
   r
   r   r(   |   s   r(   )r   r   )r   )r   rB   rD   r&   r"   
contextlibr   ior   osr   r   r@   r(   r
   r
   r
   r   <module>   s   
%5