a
    (Dfd                     @   s   d dl mZmZmZ d dlmZmZ d dlmZm	Z	m
Z
mZ d dlmZ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mZmZ d dlmZmZmZm Z  d dl!m"Z" d dl#Z#d dl$Z$d dl%Z%d	Z&G d
d deeeZ'dS )    )
FileDBBase	ObjectDBR	ObjectDBW)	BadObjectAmbiguousObjectName)DecompressMemMapReaderFDCompressedSha1WriterFDStream
Sha1Writer)OStreamOInfo)file_contents_ro_filepathENOENT
hex_to_bin
bin_to_hexexistschmodisfileremoverenamedirnamebasenamejoin)
chunk_sizeloose_object_header_infowrite_objectstream_copy)force_bytesN)LooseObjectDBc                       s   e Zd ZdZeZeddZej	dkr.eddZ fddZ
dd	 Zd
d Zdd Zdd Z fddZdd Zdd Zdd Zdd Zdd Zdd Z  ZS )r   z/A database which operates on loose object filesZ444   ntZ644c                    s&   t  | t | _ttdd| _d S )N	O_NOATIMEr   )super__init__dict_hexsha_to_filegetattros_fd_open_flags)self	root_path	__class__ [/nfs/NAS7/SABIOD/METHODE/ermites/ermites_venv/lib/python3.9/site-packages/gitdb/db/loose.pyr#   J   s    zLooseObjectDB.__init__c                 C   s   t |dd |dd S )z{
        :return: path at which the object with the given hexsha would be stored,
            relative to the database rootN   )r   )r)   hexshar-   r-   r.   object_pathS   s    zLooseObjectDB.object_pathc                 C   sR   z| j | W S  ty   Y n0 | | |}t|rF|| j |< |S t|dS )z
        :return: readable object path to the object identified by hexsha
        :raise BadObject: If the object file does not existN)r%   KeyErrordb_pathr1   r   r   )r)   r0   pathr-   r-   r.   readable_db_object_pathY   s    
z%LooseObjectDB.readable_db_object_pathc                 C   sL   d}|   D ]*}t|t|r|dur2t||}q|du rHt||S )z:return: 20 byte binary sha1 string which matches the given name uniquely
        :param name: hexadecimal partial name (bytes or ascii string)
        :raise AmbiguousObjectName:
        :raise BadObject: N)sha_iterr   
startswithr   r   r   )r)   Zpartial_hexsha	candidatebinshar-   r-   r.   partial_to_complete_sha_hexk   s    z)LooseObjectDB.partial_to_complete_sha_hexc                 C   s   |  | t|}zt|| jdW S  ty } zn|jtkrzt|W W  Y d}~S  ty } zt||W Y d}~n
d}~0 0 d| _n
t||W Y d}~n
d}~0 0 dS )z~
        :return: memory map of that file to allow random read access
        :raise BadObject: if object could not be located)flagsNr   )	r3   r1   r   r   r(   OSErrorerrnor   r   )r)   shar3   eZnew_er-   r-   r.   _map_loose_object~   s    
 zLooseObjectDB._map_loose_objectc                    s,   |dur t |ts tdtj t |S )zI:raise TypeError: if the stream does not support the Sha1Writer interfaceNz,Output stream musst support the %s interface)
isinstancer
   	TypeError__name__r"   set_ostream)r)   streamr+   r-   r.   rD      s    zLooseObjectDB.set_ostreamc              	   C   sR   |  |}z.t|\}}t|||W t|dr6|  S nt|drL|  0 d S )Nclose)r@   r   r   hasattrrF   )r)   r>   mtypsizer-   r-   r.   info   s    

 
zLooseObjectDB.infoc                 C   s,   |  |}tj|dd\}}}t||||S )NT)Zclose_on_deletion)r@   r   newr   )r)   r>   rH   typerJ   rE   r-   r-   r.   rE      s    
zLooseObjectDB.streamc                 C   s.   z|  t| W dS  ty(   Y dS 0 d S )NTF)r5   r   r   )r)   r>   r-   r-   r.   
has_object   s
    zLooseObjectDB.has_objectc                 C   sF  d}|   }|du rDtjd| jd\}}|jdu r<t|}nt|}zbzN|jdurjt|j|j	t
j| j nt|j|j|j|j	| jd W |r|  n|r|  0 W n   |rt|  Y n0 d}|jr|j}n|jdd}|r8| | |}t|}tj|dd t|r"t| n
t|| t|| j t||_|S )z.note: The sha we produce will be hex by natureNobj)prefixdir)r   T)Zas_hex)exist_ok)ZostreamtempfilemkstempZ
_root_pathr9   r   r	   r   readwritesysmaxsizestream_chunk_sizer   rM   rJ   rF   r'   r   r0   r>   r3   r1   r   makedirsr   r   r   new_objects_moder   )r)   ZistreamZtmp_pathwriterfdr0   Zobj_pathZobj_dirr-   r-   r.   store   sH    








zLooseObjectDB.storec                 c   sZ   t |  D ]F\}}}t|}t|dkr.q|D ] }t|dkrDq2t|| V  q2qd S )Nr/   &   )r'   walkr*   r   lenr   )r)   rootdirsfilesZ	root_basefr-   r-   r.   r6      s    zLooseObjectDB.sha_iterc                 C   s   t t|  S )N)ra   tupler6   )r)   r-   r-   r.   rJ      s    zLooseObjectDB.size)rC   
__module____qualname____doc__r   rY   intr[   r'   namer#   r1   r5   r:   r@   rD   rK   rE   rN   r^   r6   rJ   __classcell__r-   r-   r+   r.   r   <   s"   


	
?r   )(Zgitdb.db.baser   r   r   Z	gitdb.excr   r   Zgitdb.streamr   r   r	   r
   Z
gitdb.baser   r   Z
gitdb.utilr   r   r   r   r   r   r   r   r   r   r   r   Z	gitdb.funr   r   r   r   Zgitdb.utils.encodingr   rS   r'   rW   __all__r   r-   r-   r-   r.   <module>   s   8