
    .Ph"                         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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 d	l(Z(d
Z) G d deee          Z*d	S )    )suppress)
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                        e Zd ZdZeZ edd          Zej	        dk    r edd          Z fdZ
d Zd Zd	 Zd
 Z fdZd Zd Zd Zd Zd Zd Z xZS )r    z/A database which operates on loose object files444   nt644c                     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__s     N/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/gitdb/db/loose.pyr)   zLooseObjectDB.__init__M   sC    ####vv &b+q99    c                 B    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/   hexshas     r2   object_pathzLooseObjectDB.object_pathV   s$     F2A2Jqrr
+++r3   c                    t          t                    5  | j        |         cddd           S # 1 swxY w Y   |                     |                     |                    }t          |          r|| j        |<   |S t          |          )z
        :return: readable object path to the object identified by hexsha
        :raise BadObject: If the object file does not existN)r   KeyErrorr+   db_pathr7   r   r   )r/   r6   paths      r2   readable_db_object_pathz%LooseObjectDB.readable_db_object_path\   s     h 	0 	0'/	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0
 ||D,,V4455$<< 	+/D (Ks   /33c                     d}|                                  D ]D}t          |                              t          |                    r|t	          |          |}E|t          |          |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/   partial_hexsha	candidatebinshas       r2   partial_to_complete_sha_hexz)LooseObjectDB.partial_to_complete_sha_hexl   sz    
 	mmoo 	# 	#F&!!,,[-H-HII #(-n==="	N+++r3   c                 l   |                      |                     t          |                              }	 t          || j                  S # t
          $ r\}|j        t          k    r7	 t          |          cY d}~S # t
          $ r}t          |          |d}~ww xY wt          |          |d}~ww xY w)z~
        :return: memory map of that file to allow random read access
        :raise BadObject: if object could not be located)flagsN)	r:   r7   r   r   r.   OSErrorerrnor   r   )r/   shar:   enew_es        r2   _map_loose_objectzLooseObjectDB._map_loose_object   s     ,,t//
3@@AA	,,WD<OPPPP 
	, 
	, 
	,w&  44W======== 4 4 4#C..e34
  nn!+
	,s;   A 
B3B.(A<6B3<
BBBB..B3c                     |1t          |t                    st          dt          j        z            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/   streamr1   s     r2   rP   zLooseObjectDB.set_ostream   sG    j&D&DJZM``aaaww""6***r3   c                    |                      |          }	 t          |          \  }}t          |||          t          |d          r|                                 S S # t          |d          r|                                 w w xY w)Nclose)rK   r   r   hasattrrS   )r/   rH   mtypsizes        r2   infozLooseObjectDB.info   s    ""3''	033ICc4((q'"" 				wq'"" 				s   "A 'Bc                     |                      |          }t          j        |d          \  }}}t          ||||          S )NT)close_on_deletion)rK   r	   newr   )r/   rH   rU   typerW   rQ   s         r2   rQ   zLooseObjectDB.stream   sE    ""3''37TRRRdFsD$///r3   c                 n    	 |                      t          |                     dS # t          $ r Y dS w xY w)NTF)r<   r   r   )r/   rH   s     r2   
has_objectzLooseObjectDB.has_object   sH    	((C9994 	 	 	55	s   "& 
44c                    d}|                                  }|Dt          j        d| j                  \  }}|j        t          |          }nt          |          }	 	 |j        ,t          |j        |j	        t          j        | j                   n-t          |j        |j        |j        |j	        | j                   |r|                                 n# |r|                                 w w xY wn#  |rt#          |            xY wd}|j        r|j        }n|                    d          }|r|                     |                     |                    }t-          |          }t/          j        |d           t3          |          rt#          |           nt5          ||           dD ]\}t7          t8                    5  t;          || j                   	 ddd            nE# 1 swxY w Y   t?          j         |d	z             ]t9          d
!                    |                    tE          |          |_        |S )z.note: The sha we produce will be hex by natureNobj)prefixdir)r   T)as_hex)exist_ok)      	         $   1   @   Q   d   y            g     @@z&Impossible to apply `chmod` to file {})#ostreamtempfilemkstemp
_root_pathrB   r
   r   r   readwritesysmaxsizestream_chunk_sizer   r\   rW   rS   r   r6   rH   r:   r7   r   r-   makedirsr   r   r   PermissionErrorr   new_objects_modetimesleepformatr   )	r/   istreamtmp_pathwriterfdr6   obj_pathobj_direxp_backoff_mss	            r2   storezLooseObjectDB.store   s   >#+5doNNNLB~%/33!"	#>-  flCKI_```` !w|W\6<,0,BD D D D  #LLNNN  #LLNNNN#	 !x    > 	-^FFZZtZ,,F  	||D$4$4V$<$<==Hh''GK$//// h +x    x*** #] 
 
o..   (D$9:::	               
 
>F23333%<CCHMM   $F++s1   A C ?C3 C//C3 3DG++G/	2G/	c              #     K   t          j        |                                           D ]V\  }}}t          |          }t	          |          dk    r)|D ]*}t	          |          dk    rt          ||z             V  +Wd S )Nr5   &   )r-   walkr0   r   lenr   )r/   rootdirsfiles	root_basefs         r2   r>   zLooseObjectDB.sha_iter   s      !#)9)9!:!: 	0 	0D$ I9~~"" 0 0q66R<< Q//////0	0 	0r3   c                 ^    t          t          |                                                     S )N)r   tupler>   )r/   s    r2   rW   zLooseObjectDB.size  s     5))***r3   )rO   
__module____qualname____doc__r   r{   intr~   r-   namer)   r7   r<   rC   rK   rP   rX   rQ   r^   r   r>   rW   __classcell__)r1   s   @r2   r    r    ?   s&       99 # s5!}}	w$3ua==: : : : :, , ,        &, , ,*+ + + + +  0 0 0
  J J JX
0 
0 
0+ + + + + + +r3   r    )+
contextlibr   gitdb.db.baser   r   r   	gitdb.excr   r   gitdb.streamr	   r
   r   r   
gitdb.baser   r   
gitdb.utilr   r   r   r   r   r   r   r   r   r   r   r   	gitdb.funr   r   r   r   gitdb.utils.encodingr   rt   r-   ry   r   __all__r     r3   r2   <module>r      s   
                       
                  
                                       - , , , , ,  				 



  M+ M+ M+ M+ M+J	9 M+ M+ M+ M+ M+r3   