
    9iG                        S SK r S SKrS SKrS SKrS SKrS SKrS SKJr  S SKJ	r	J
r
  S SKJr  S SKJr  S SKJr  S SKJrJrJrJrJrJrJrJrJrJrJr  S SKJr  S S	KJr  S S
KJ r   \" S5      r!/ SQr"\S   r#/ SQr$/ SQr% " S S5      r&\	 " S S5      5       r'\	 " S S5      5       r(\	 " S S5      5       r)\	 " S S5      5       r*S\+S\,4S jr-\	 " S S5      5       r.\	 " S S 5      5       r/S!\,S\,4S" jr0\\/4   r1g)#    N)contextmanager)	dataclassfield)datetimefnmatch)Path)AnyBinaryIOCallable	GeneratorIterableIteratorListLiteralOptionalTypeVarUnion) DEFAULT_MODELSCOPE_DATA_ENDPOINT)convert_timestamp)get_file_hashT)z.gitz.git/*z*/.gitz
**/.git/**z.cachez.cache/*z*/.cachez**/.cache/**)lfsnormal)+.7zz.aac.arrowz.audioz.bmp.bin.bz2z.flac.ftzz.gif.gz.h5z.jackz.jpegz.jpgz.pngz.jsonl.joblibz.lz4.msgpack.npy.npz.ot.parquet.pb.picklez.pcm.pklz.raw.rarz.sam.tar.tgz.wasmz.wavz.webmz.webp.zip.zstz.tiffz.mp3z.mp4z.ogg)r   r   r   r   z.ckptr   r    r!   r"   z.mlmodelz.modelr#   r$   r%   z.onnxr&   r'   r(   r)   r*   z.ptz.pthr+   z.safetensorsr,   z.tfliter-   r.   z.xzr/   r0   c                       \ rS rSr\SSSS.S\\   S\\\	\
   \
4      S\\\	\
   \
4      S\\\/\
4      S\\SS4   4
S	 jj5       r\S
\
S\
4S j5       rSrg)	RepoUtilsu   N)allow_patternsignore_patternskeyitemsr4   r5   r6   returnc             #      ^#    U(       a  UOSnU(       a  UOSn[        U[        5      (       a  U/n[        U[        5      (       a  U/nUb%  U Vs/ s H  n[        R                  U5      PM     nnUb%  U Vs/ s H  n[        R                  U5      PM     nnUc  S[        S[        4S jnUnU  HM  nU" U5      mUb  [        U4S jU 5       5      (       d  M*  Ub  [        U4S jU 5       5      (       a  MI  Uv   MO     gs  snf s  snf 7f)a  Filter repo objects based on an allowlist and a denylist.

Input must be a list of paths (`str` or `Path`) or a list of arbitrary objects.
In the later case, `key` must be provided and specifies a function of one argument
that is used to extract a path from each element in iterable.

Patterns are Unix shell-style wildcards which are NOT regular expressions. See
https://docs.python.org/3/library/fnmatch.html for more details.

Args:
    items (`Iterable`):
        List of items to filter.
    allow_patterns (`str` or `List[str]`, *optional*):
        Patterns constituting the allowlist. If provided, item paths must match at
        least one pattern from the allowlist.
    ignore_patterns (`str` or `List[str]`, *optional*):
        Patterns constituting the denylist. If provided, item paths must not match
        any patterns from the denylist.
    key (`Callable[[T], str]`, *optional*):
        Single-argument function to extract a path from each item. If not provided,
        the `items` must already be `str` or `Path`.

Returns:
    Filtered list of objects, as a generator.

Raises:
    :class:`ValueError`:
        If `key` is not provided and items are not `str` or `Path`.

Example usage with paths:
```python
>>> # Filter only PDFs that are not hidden.
>>> list(RepoUtils.filter_repo_objects(
...     ["aaa.PDF", "bbb.jpg", ".ccc.pdf", ".ddd.png"],
...     allow_patterns=["*.pdf"],
...     ignore_patterns=[".*"],
... ))
["aaa.pdf"]
```
Nitemr8   c                     [        U [        5      (       a  U $ [        U [        5      (       a  [        U 5      $ [        SU  S35      e)Nz9Please provide `key` argument in `filter_repo_objects`: `z` is not a string.)
isinstancestrr	   
ValueError)r:   s    [/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/modelscope/utils/repo_utils.py	_identity0RepoUtils.filter_repo_objects.<locals>._identity   sF    dC((KdD))t9$ OPTvUgh     c              3   <   >#    U  H  n[        TU5      v   M     g 7fNr   .0rpaths     r?   	<genexpr>0RepoUtils.filter_repo_objects.<locals>.<genexpr>   s      6>.<GD!$$n   c              3   <   >#    U  H  n[        TU5      v   M     g 7frD   r   rE   s     r?   rI   rJ      s      3?.=GD!$$orK   )r<   r=   r2   _add_wildcard_to_directoriesr   any)r7   r4   r5   r6   pr@   r:   rH   s          @r?   filter_repo_objectsRepoUtils.filter_repo_objectsw   s8    b ,:t-</$nc**,-Nos++./O% ('A 66q9'   & )(A 66q9(  
 ; c  CDt9D )# 6>.<6> 3> 3> *s 3?.=3? 0? 0?J /
s   ADD0	D9D	A6Dpatternc                 "    U S   S:X  a  U S-   $ U $ )N/* )rR   s    r?   rM   &RepoUtils._add_wildcard_to_directories   s    2;#S= rB   rW   )__name__
__module____qualname____firstlineno__staticmethodr   r   r   r   r   r=   r   r   rP   rM   __static_attributes__rW   rB   r?   r2   r2   u   s     ;?;?,0^{^ !tCy#~!67^ "%S	3"78	^
 hsCx()^ 
1dD=	!^ ^@ c c  rB   r2   c                   H    \ rS rSr% Sr\\S'   \\S'   \\S'   \\S'   S rSrg	)

CommitInfo   a  Data structure containing information about a newly created commit.

Returned by any method that creates a commit on the Hub: [`create_commit`], [`upload_file`], [`upload_folder`],
[`delete_file`], [`delete_folder`]. It inherits from `str` for backward compatibility but using methods specific
to `str` is deprecated.

Attributes:
    commit_url (`str`):
        Url where to find the commit.

    commit_message (`str`):
        The summary (first line) of the commit that has been created.

    commit_description (`str`):
        Description of the commit that has been created. Can be empty.

    oid (`str`):
        Commit hash id. Example: `"91c54ad1727ee830252e457677f467be0bfd8a57"`.


commit_urlcommit_messagecommit_descriptionoidc                 `    U R                   U R                  U R                  U R                  S.$ )Nrb   rc   rd   re   rg   )clss    r?   to_dictCommitInfo.to_dict   s,    ..!00"%"8"877	
 	
rB   rW   N)	rY   rZ   r[   r\   __doc__r=   __annotations__ri   r^   rW   rB   r?   r`   r`      s$    * O	H
rB   r`   c                       \ rS rSr% Sr\\   \S'   \\   \S'   \\   \S'   \\   \S'   \\   \S'   \\   \S'   \\   \S	'   \\   \S
'   \\   \S'   \\   \S'   \\   \S'   \	S\
SS 4S j5       rS\
4S jrSrg)DetailedCommitInfoi  z8Detailed commit information from repository history API.idshort_idtitlemessageauthor_nameauthored_dateauthor_emailcommitted_datecommitter_namecommitter_email
created_atdatar8   c                    U " UR                  SS5      UR                  SS5      UR                  SS5      UR                  SS5      UR                  SS5      [        UR                  SS5      5      UR                  S	S5      [        UR                  S
S5      5      UR                  SS5      UR                  SS5      [        UR                  SS5      5      S9$ )z1Create DetailedCommitInfo from API response data.Id ShortIdTitleMessage
AuthorNameAuthoredDateNAuthorEmailCommittedDateCommitterNameCommitterEmail	CreatedAtro   rp   rq   rr   rs   rt   ru   rv   rw   rx   ry   )getr   )rh   rz   s     r?   from_api_response$DetailedCommitInfo.from_api_response  s     xxb!XXi,((7B'HHY+r2+DHH^T,JK-4,TXXot-LM88OR8 HH%5r:(+t)DE
 	
rB   c                     U R                   U R                  U R                  U R                  U R                  U R
                  U R                  U R                  U R                  U R                  U R                  S.$ )zConvert to dictionary.r   r   selfs    r?   ri   DetailedCommitInfo.to_dict$  sg     ''ZZ||++!// --"11"11#33//
 	
rB   rW   N)rY   rZ   r[   r\   rk   r   r=   rl   r   classmethoddictr   ri   r^   rW   rB   r?   rn   rn     s    BsmC=c]#H%%3-X&&SM!c]"""
T 
.B 
 
 
 
rB   rn   c                   \    \ rS rSr% Sr\\\      \S'   \\	   \S'   \
S\SS 4S j5       rSrg	)
CommitHistoryResponsei5  z!Response from commit history API.commitstotal_countrz   r8   c                     UR                  S0 5      R                  S/ 5      nU(       d  U " / SS9$ U Vs/ s H  n[        R                  U5      PM     nnU " UUR                  SS5      S9$ s  snf )z4Create CommitHistoryResponse from API response data.DataCommitr   )r   r   
TotalCount)r   rn   r   )rh   rz   commits_datacommitr   s        r?   r   'CommitHistoryResponse.from_api_response;  s     xx+//"=  '
& 008& 	 

 q1
 	

s   A-rW   N)rY   rZ   r[   r\   rk   r   r   rn   rl   intr   r   r   r^   rW   rB   r?   r   r   5  sA    +d-.//#
T 
.E 
 
rB   r   c                       \ rS rSr% Sr\\   \S'   Sr\\   \S'   Sr	\\   \S'   Sr
\\   \S'   Sr\\   \S'   \r\\   \S'   S	\4S
 jrSrg)RepoUrliQ  Nurl	namespace	repo_namerepo_id	repo_typeendpointr8   c           	      \    SU  SU R                    SU R                   SU R                   S3	$ )Nz	RepoUrl('z', endpoint='z', repo_type='z', repo_id='z'))r   r   r   r   s    r?   __repr__RepoUrl.__repr__[  s6    4&dmm_N4>>JZZfgkgsgsfttvwwrB   rW   )rY   rZ   r[   r\   r   r   r=   rl   r   r   r   r   r   r   r   r^   rW   rB   r?   r   r   Q  sf     C##Ix}##Ix}#!GXc]!#Ix}#>Hhsm>x# xrB   r   rz   r8   c                 v   [         R                  S:  a  SS0O0 n[        R                  " [        R
                  40 UD6nU" 5       nUR                  S5        UR                  [        [        U 5      5      R                  5       5        UR                  S5        UR                  U 5        UR                  5       $ )aw  
Computes the git-sha1 hash of the given bytes, using the same algorithm as git.

This is equivalent to running `git hash-object`. See https://git-scm.com/docs/git-hash-object
for more details.

Note: this method is valid for regular files. For LFS files, the proper git hash is supposed to be computed on the
      pointer file content, not the actual file content. However, for simplicity, we directly compare the sha256 of
      the LFS file content when we want to compare LFS files.

Args:
    data (`bytes`):
        The data to compute the git-hash for.

Returns:
    `str`: the git-hash of `data` as an hexadecimal string.
)   	   usedforsecurityFs   blob     )sysversion_info	functoolspartialhashlibsha1updater=   lenencode	hexdigest)rz   _kwargsr   shas       r?   git_hashr   _  s    $ -0,<,<,F %(BGW\\5W5D
&CJJxJJs3t9~$$&'JJuJJt==?rB   c                       \ rS rSr% Sr\\S'   \\S'   \\S'   \	SS\S\
4S	 jj5       r\	SS
\S\
4S jj5       r\	SS\S\
4S jj5       rSrg)
UploadInfoi{  a:  
Dataclass holding required information to determine whether a blob
should be uploaded to the hub using the LFS protocol or the regular protocol

Args:
    sha256 (`str`):
        SHA256 hash of the blob
    size (`int`):
        Size in bytes of the blob
    sample (`bytes`):
        First 512 bytes of the blob
sha256sizesampleNrH   file_hash_infoc                     U=(       d    [        U5      nUS   nUS   n[        US5       nUR                  S5      nS S S 5        U " XCWS9$ ! , (       d  f       N= f)N	file_size	file_hashrb   r   r   r   )r   openread)rh   rH   r   r   r   fr   s          r?   	from_pathUploadInfo.from_path  s[    '>=+>k*[)$VVC[F  #88 s   A
Arz   c                 \    U=(       d    [        U5      nUS   nU " [        U5      US S US9$ )Nr   r   )r   r   r   )r   r   )rh   rz   r   r   s       r?   
from_bytesUploadInfo.from_bytes  s4    '>=+>[)D	$t*SAArB   fileobjc                     U=(       d    [        U5      nUR                  S[        R                  5        UR	                  S5      nUR                  S[        R                  5        U " US   US   US9$ )Nr   r   r   r   r   )r   seekosSEEK_SETr   )rh   r   r   r   s       r?   from_fileobjUploadInfo.from_fileobj  sc    -Gw1GQ$c"Q$!+., 	rB   rW   rD   )rY   rZ   r[   r\   rk   r=   rl   r   bytesr   r   r   r   r   r   r^   rW   rB   r?   r   r   {  s     K
IM9S 9$ 9 9 Be BT B B
 8 T  rB   r   c                   R   \ rS rSr% Sr\\S'   \\\\	\
4   \S'   \" SSS9r\\S'   \" \S9r\\S	'   \" SSS
S9r\\   \S'   \" SSS
S9r\\   \S'   \" SSS
S9r\\   \S'   \" SSSS9r\\S'   \" SSSS9r\\S'   SS jr\S\\
   4S j5       rS\	4S jr\S\\   4S j5       rSrg
)CommitOperationAddi  zKData structure containing information about a file to be added to a commit.path_in_repopath_or_fileobjF)initreprupload_info)default_factoryr   N)r   r   default_upload_mode_should_ignore_remote_oid_is_uploaded_is_committedr8   c                    [        U R                  5      U l        [        U R                  [        5      (       a  [        U R                  5      U l        [        U R                  [
        5      (       az  [        R                  R                  [        R                  R                  U R                  5      5      n[        R                  R                  U5      (       d  [        SU S35      eO:[        U R                  [        R                  [        45      (       d  [        S5      e[        U R                  [        R                  5      (       aE   U R                  R                  5         U R                  R!                  S[        R"                  5        [        U R                  [
        5      (       a0  [(        R+                  U R                  U R,                  5      U l        g[        U R                  [        5      (       a0  [(        R1                  U R                  U R,                  5      U l        g[(        R3                  U R                  U R,                  5      U l        g! [$        [&        4 a  n[        S5      UeSnAff = f)z6Validates `path_or_fileobj` and compute `upload_info`.zProvided path: 'z(' is not a file on the local file systemzpath_or_fileobj must be either an instance of str, bytes or io.BufferedIOBase. If you passed a file-like object, make sure it is in binary mode.r   zNpath_or_fileobj is a file-like object but does not implement seek() and tell()N)_validate_path_in_repor   r<   r   r	   r=   r   rH   normpath
expanduserisfiler>   ioBufferedIOBaser   tellr   SEEK_CUROSErrorAttributeErrorr   r   r   r   r   r   )r   r   excs      r?   __post_init__ CommitOperationAdd.__post_init__  s    343D3DE d**D11#&t';';#<D d**C00 gg..""4#7#78:O77>>/22 &&77_`  3 D0023D3De2LMM#$ $ d**B,=,=>>$$))+$$))!R[[9 d**C00)33D4H4H484G4G ID,,e44)44T5I5I595H5H JD  *66t7K7K7;7J7J LD ^,  ds   AI# #J3I??Jc              #   R  #    [        U R                  [        5      (       d  [        U R                  [        5      (       a$  [	        U R                  S5       nUv   SSS5        g[        U R                  [
        5      (       a#  [        R                  " U R                  5      v   g[        U R                  [        R                  5      (       aE  U R                  R                  5       nU R                  v   U R                  R                  US5        gg! , (       d  f       g= f7f)zq
A context manager that yields a file-like object allowing to read the underlying
data behind `path_or_fileobj`.
r   Nr   )r<   r   r=   r	   r   r   r   BytesIOr   r   r   )r   fileprev_poss      r?   as_fileCommitOperationAdd.as_file  s      d**C00J$$d5, 5,d**D1T
 21,,e44**T1122,,b.?.?@@++002H&&&  %%h2 A	 21s   AD'DB:D'
D$ D'c                     U R                  5        n[        R                  " UR                  5       5      sSSS5        $ ! , (       d  f       g= f)zC
The base64-encoded content of `path_or_fileobj`

Returns: `bytes`
N)r   base64	b64encoder   r   r   s     r?   
b64contentCommitOperationAdd.b64content  s-     \\^t##DIIK0 ^^s	   $?
Ac                     U R                   c  gU R                   S:X  a  U R                  R                  $ U R                  5        n[	        UR                  5       5      sSSS5        $ ! , (       d  f       g= f)a  Return the OID of the local file.

This OID is then compared to `self._remote_oid` to check if the file has changed compared to the remote one.
If the file did not change, we won't upload it again to prevent empty commits.

For LFS files, the OID corresponds to the SHA256 of the file content (used a LFS ref).
For regular files, the OID corresponds to the SHA1 of the file content.
Note: this is slightly different to git OID computation since the oid of an LFS file is usually the git-SHA1
    of the pointer file content (not the actual file content). However, using the SHA256 is enough to detect
    changes and more convenient client-side.
Nr   )r   r   r   r   r   r   r   s     r?   
_local_oidCommitOperationAdd._local_oid
  sX     $%'##*** 4		,  s   A((
A6)r   r   r   )r8   N) rY   rZ   r[   r\   rk   r=   rl   r   r	   r   r   r   r   r   r   r   r   r   
UploadModer   boolr   r   r   r   r   r   r   r   propertyr   r^   rW   rB   r?   r   r     s   U3eX566#U;K; 6ND6
 */*.L(:& .
 &+&.NHTN .
 "'Et!LK#L EuEL$E  UFM4F&LP 3(+ 3 3 1E 1 -HSM - -rB   r   r   c                     U R                  S5      (       a  U SS  n U S:X  d  U S:X  d  U R                  S5      (       a  [        SU  S35      eU R                  S5      (       a  U S	S  n U $ )
NrU      .z..z../z,Invalid `path_in_repo` in CommitOperation: ''z./   )
startswithr>   )r   s    r?   r   r   "  s    s###AB'sld2l6M6M7 7:<.JL 	Lt$$#AB'rB   )2r   r   r   r   r   r   
contextlibr   dataclassesr   r   r   r   pathlibr	   typingr
   r   r   r   r   r   r   r   r   r   r   modelscope.hub.constantsr   modelscope.hub.utils.utilsr   modelscope.utils.file_utilsr   r   DEFAULT_IGNORE_PATTERNSr  DATASET_LFS_SUFFIXMODEL_LFS_SUFFIXr2   r`   rn   r   r   r   r=   r   r   r   r   CommitOperationrW   rB   r?   <module>r     sZ      	 	 
 % (   = = = = F 8 5CL	  _%
, \  Fg gT !
 !
 !
H -
 -
 -
` 
 
 
6 
x 
x 
x5 S 8 + + +\ t- t- t-n   *+-rB   