
    6pj              	          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rS SKrS SKrS SK	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  S SKrS SKJr  S SKJr  S SKJr  S S	KJr  S S
KJ r   S SK!J"r"  \"" 5       r#\" 5       r$\ " 5       r%Sr&\RN                  RQ                  \%\&5      r)Sr*SSSSS./r+Sr,\RN                  RQ                  \RZ                  " 5       SS5      r./ r/S/r0S r1\S8S\2S\34S jj5       r4\S\24S j5       r5\,4S\2S\\2   4S jjr6S9S\\2   4S jjr7S9S\\2   S\\2   4S jjr8S9S\\2   S\\2   4S  jjr9S9S\\2   4S! jjr:  S:S"\2S#\\\2      S$\\\2      SS4S% jjr;S& r<S' r=S( r>S)\2S\24S* jr?S+ r@S\\2   4S, jrAS-\\2   4S. jrBS\\2   SS4S/ jrCS8S;S0 jjrDS1rES2 rF " S3 S4\G5      rH " S5 S6\G5      rI\JS7:X  a  \B" S/5        \F" S5      u  rKrLrMgg)<    N)contextmanagerfnmatchPath)AnyIterableListOptionalSetUnion)snapshot_download)LocalStorage)FilesAstScanning)DEFAULT_MODEL_REVISION)get_modelscope_cache_dir)
get_loggermodelscope_modulesz.modelscope_pluginsadaseqzZProvide hundreds of additions NERs algorithms, check: https://github.com/modelscope/AdaSeq )namedescversionurlz.modelscopepluginsz
pai-easycvc                     U (       d  U $ [        U [        5      (       a  U /n U  Vs/ s HE  n[        R                  " SUR	                  5       5      S   R	                  5       [
        ;  d  MC  UPMG     sn$ s  snf )Nz[><=!~]r   )
isinstancestrresplitstripPLUGIN_WHITE_LIST)r   plugins     T/var/www/html/pdf-tiff/venv/lib/python3.13/site-packages/modelscope/utils/plugins.pyfilter_plugin_in_whitelistr%   5   si    '3)$$6V\\^(L	
)EG,)-W  s   AA5,A5new_dirverbosec              #     #    [         R                  " 5       nU(       a  [        R                  SU  35        [         R                  " U 5         Sv   U(       a  [        R                  SU 35        [         R                  " U5        g! U(       a  [        R                  SU 35        [         R                  " U5        f = f7f)z
Changes the current directory to the given path and prepends it to `sys.path`.
This method is intended to use with `with`, so after its usage, the current
directory will be set to the previous value.
zChanging directory to NzChanging directory back to )osgetcwdloggerinfochdir)r&   r'   previous_dirs      r$   pushdr/   @   s      99;L,WI67HHWKK5l^DE
 KK5l^DE
s   ACB 6C7B??Cpathc              #   $  #    [        U 5      R                  5       n [        U 5      n [        R                  R                  SU 5         Sv   [        R                  R                  U 5        g! [        R                  R                  U 5        f = f7f)z
Prepends the given path to `sys.path`.
This method is intended to use with `with`, so after its usage, its value
will be removed from `sys.path`.
r   N)r   resolver   sysr0   insertremove)r0   s    r$   push_python_pathr6   S   s\      :Dt9DHHOOAts   ABA,  B,!BBfilenamereturnc              #      #    [        U 5       nU H   nUR                  5       nU(       d  M  Uv   M"     SSS5        g! , (       d  f       g= f7f)z
Discover plugins from file
N)openr!   )r7   fmodule_names      r$   discover_file_pluginsr=   c   s<     
 
h1K%++-K{!!  
s   A>>	A
AAc              #     #    [        5       nU c  [        R                  R                  [        5      (       aF  [        S5         [        [        5       H  nX!;   a  M
  Uv   UR                  U5        M!     SSS5        [        R                  R                  [        5      (       a3  [        [        5       H  nX!;   a  M
  Uv   UR                  U5        M!     gg[        R                  R                  U 5      (       a/  [        U 5       H  nX!;   a  M
  Uv   UR                  U5        M!     gg! , (       d  f       N= f7f)zQ
Discover plugins

    Args:
    requirement_path: The file path of requirement

N.)	setr)   r0   isfileLOCAL_PLUGINS_FILENAMEr6   r=   addGLOBAL_PLUGINS_FILENAME)requirement_pathr   r#   s      r$   discover_pluginsrF   o   s      G77>>011!#&34JKF(  LKK'	 L ' 77>>122/0GH$F#	 I 3 77>>*++/0@A$F#	 B , '&s   AD?3D.6B8D?.
D<8D?c                     [        S1 Sk1 SkS9  / nUR                  [        [        5      5        UR                  [        U 5      5        UR                  [	        5       5        U$ )zE
Imports default plugins, input plugins and file discovered plugins.

modelscope>   modelscope.models.buildermodelscope.metrics.buildermodelscope.trainers.buildermodelscope.pipelines.builder modelscope.preprocessors.builder>   modelscope.utilsmodelscope.models.*modelscope.exportersmodelscope.metrics.*modelscope.msdatasetsmodelscope.trainers.*modelscope.pipelines.*modelscope.preprocessors.*includeexclude)import_module_and_submodulesextendimport_pluginsDEFAULT_PLUGINSimport_file_plugins)r   imported_pluginss     r$   import_all_pluginsr_      s_     !
	
( #%N?;<N734/12    c                    / nU b  [        U 5      S:X  a  U$ [        R                  " 5       nU[        R                  ;  a  [        R                  R                  U5        U  H:  n [        U1 SkSS1S9  [        R                  SU5        UR                  U5        M<     U$ ! [         a&  n[        R                  SU S	U 35         SnAMk  SnAff = f)
z.
Imports the plugins listed in the arguments.
Nr   >   easycv.coreeasycv.hookseasycv.modelseasycv.toolkiteasycv.predictorseasycv.toolkit.modelscopezeasycv.toolkit.*zeasycv.*rV   Plugin %s availablePlugin  could not be loaded: )lenr)   r*   r3   r0   appendrY   r+   r,   ModuleNotFoundErrorerror)r   r^   cwdr<   es        r$   r[   r[      s     #%#g,!+ ))+C
#((	K( ' KK-{;##K0% ,  # 	KLL7;-/EaSIJJ	Ks   %6B!!
C+CCc                    / n[         R                  " 5       nU[        R                  ;  a  [        R                  R	                  U5        [        U 5       HA  n [        R                  " U5        [        R                  SU5        UR	                  U5        MC     U$ ! [         a&  n[        R                  SU SU 35         SnAMr  SnAff = f)zq
Imports the plugins found with `discover_plugins()`.

Args:
    requirement_path: The file path of requirement

rh   ri   rj   N)r)   r*   r3   r0   rl   rF   	importlibimport_moduler+   r,   rm   rn   )rE   r^   ro   r<   rp   s        r$   r]   r]      s     #% ))+C
#(('(89	K##K0KK-{;##K0	 :  # 	KLL7;-/EaSIJJ	Ks   =B
C'CCpackage_namerW   rX   c           	         U(       a  UO	[        5       nU(       a  UO	[        5       nS[        S[        [           S[        4S jnU" X5      (       d  U" X5      (       a  g[        R
                  " 5         [        S5          [        R                  " U 5      n[        US/ 5      nU(       d  SOUS	   n[        R                  " U5       H`  u  pxn	U(       a  UR                  U:w  a  M  UR                  S
5      (       a  M7  UR                  S5      (       a  MO  U  SU 3n
[        XUS9  Mb     SSS5        g! [         a.  n[        R!                  U  S[        U5       S35         SnAN<SnAf["         aV  n[        R!                  U  S[        U5       35        [%        U R'                  S5      5      S:X  a  [)        S5      e SnANSnAff = f! , (       d  f       g= f)z7
Import all public submodules under the given package.
rt   pattern_setr8   c                 :    U H  n[        X5      (       d  M    g   gNTFr   )rt   rv   patterns      r$   fn_in+import_module_and_submodules.<locals>.fn_in   s    "G|-- # r`   Nr?   __path__r   r   _testrV   z not imported: z, but should continue   zPackage not installed)r@   r   r   boolrr   invalidate_cachesr6   rs   getattrpkgutiliter_modulesr0   
startswithrY   
SystemExitr+   warning	Exceptionrk   r    rm   )rt   rW   rX   rz   moduler0   path_stringmodule_finderr   r}   
subpackagerp   s               r$   rY   rY      s    !gceG gceGC c#h 4  ''E,,H,H!
 
#		C,,\:F6:r2D$("d1gK +2*>*>t*D&Q =#5#5#D??3''??6** ,~Qtf5
,B +E 
	.  	NN.Ax7LMO 	CNNl^?3q6(CD<%%c*+q0)*ABB 1	C9 
	sD    GB+D66
G $E)$G)G6AGGGG
Gc                    / n[        U SSS9 nUR                  5       R                  5       nU H<  nUS:X  a  M  [        R	                  U5      u  pVU(       a  M+  UR                  U5        M>     SSS5        [        U5      S:  a.  [        R                  SU5      u  pvnUS:w  a  [        SU  35      egg! , (       d  f       NL= f)	z}install module from requirements
Args:
    requirement_path: The path of requirement file

No returns, raise error if failed
rutf-8)encodingr   Nr   installz$Failed to install requirements from )	r:   read
splitlinesPluginsManagercheck_plugin_installedrl   rk   pip_commandImportError)	rE   install_listr;   requirementsreq	installedr}   status_codeargss	            r$    install_module_from_requirementsr   *  s     L	g	6!vvx**,Cby)@@ELI9##C(   
7 <1-99 
 !67G6HIK K   
7	6s   AB5B55
Cc                     [         R                  R                  X5      n[         R                  R                  U5      nUR                  R                  U5        U$ )zinstall module by name with file path

Args:
    module_name: the module name need to be import
    file_path: the related file path that matched with the module name

Returns: return the module class

)rr   utilspec_from_file_locationmodule_from_specloaderexec_module)r<   	file_pathspecr   s       r$   import_module_from_filer   F  sC     >>11+ID^^,,T2FKKF#Mr`   c                 8   S n[         R                  R                  [        U5      nU  GHW  n[         R                  R	                  U5      n[         R                  R                  XF5      n[         R
                  " USS9  [         R                  R                  US5      n[         R                  R                  U5      (       d  U" U5        [         R                  R                  U[         R                  R                  U5      5      n	[         R                  R                  X5      n
[         R                  R                  U	5      (       a  [        R                  " X5      (       a  GMA  [        R                  " X5        GMZ     [        R                  " 5         g)a$  
Create a python module from a list of files by copying them to the destination directory.

Args:
    file_list (List[str]): List of relative file paths to be copied.
    file_prefix (str): Path prefix for each file in file_list.
    module_name (str): Name of the module.

Returns:
    None
c                 R    [        U S5       n S S S 5        g ! , (       d  f       g = f)Nw)r:   )r   r}   s     r$   create_empty_file3create_module_from_files.<locals>.create_empty_filec  s    )S!Q "!!s   
&T)exist_okz__init__.pyN)r)   r0   joinBASE_MODULE_DIRdirnamemakedirsexistsbasenamefilecmpcmpshutilcopyfilerr   r   )	file_listfile_prefixr<   r   dest_dirr   file_dir
target_dir	init_filetarget_filesrc_files              r$   create_module_from_filesr   V  s     ww||O[9H	77??9-WW\\(5

J.GGLL];	ww~~i((i(ggll:rww/?/?	/JK77<<7ww~~k**'++3' 3'OOH2  !r`   	model_dirc                 \   [        U 5      R                  5       nUR                  R                  S:X  a0  UR                  R                  R                   SUR                   3nOUR                  n[        R
                  " SSU5      nU(       a  US   R                  5       (       a  SU 3nU$ )a  Derive a valid Python package name from a local model directory.

modelscope_hub 0.1.x returns paths like
``{cache}/models/{owner}--{name}/snapshots/{revision}``.  Using
``Path(model_dir).stem`` on a revision such as ``v1.0.4`` yields
``v1.0``, and ``importlib.import_module('v1.0.xxx')`` fails with
``ModuleNotFoundError: No module named 'v1'`` because dots are package
separators.  Prefer the ``{owner}--{name}`` segment (plus revision for
isolation) and sanitize to a valid identifier.
	snapshots__z[^0-9A-Za-z_]r}   r   m_)r   r2   parentr   r   subisdigit)r   r0   baser<   s       r$   _module_name_from_model_dirr   y  s     	?""$D{{;&++$$))*"TYYK8 yy&&)35K+a.0022;-(r`   c                 $   [        5       nUR                  U SS9  UR                  nUR                  n[	        U5        [
        [        R                  ;  a#  [        R                  R                  [
        5        [        U 5      n[        R                  R                  [
        U5      nU[        R                  ;  a  [        R                  R                  U5        U Vs/ s HC  nUR                  U R                  [        R                  5      [        R                  -   S5      PME     nn[        XpU5        U HO  nUS-   UR                  SS5      R                  [        R                  S5      -   n[         R"                  " U5        MQ     gs  snf )zimport all the necessary module from a model dir

Args:
    model_dir: model file location

 No returns, raise error if failed

T)include_initr   r?   z.pyN)r   traversal_files	file_dirsrequirement_dirsinstall_requirements_by_filesr   r3   r0   rl   r   r)   r   replacerstripsepr   rr   rs   )	r   file_scannerr   r   r<   MODULE_PATHfilerelative_file_dirs	submodules	            r$   import_module_from_model_dirr     s6    $%L   >&&I00L ",/chh&(-i8K '',,<K#(("$ D 	Y%%bff-6;   /KH"#%UB(?(G(GFFC) 		* #s   A
Fc                    [        5       n/ nU  H0  nUR                  U5      u  pEU(       a  M  UR                  U5        M2     UR                  U5      u  pgUS:w  a2  [	        SSR                  U5       S3SSR                  U5       S35      eg)	zinstall the requirements by names

Args:
    plugins: name of plugins (pai-easyscv, transformers)

 No returns, raise error if failed

r   zThe required packages ,z are not installed.z2Please run the command `modelscope plugin install  z` to install them.N)r   r   rl   install_pluginsEnvironmentErrorr   )r   plugins_manageruninstalled_pluginsr#   plugin_installedr   statusr}   s           r$   install_requirements_by_namesr     s     %&O$3$J$J%!&&v.	 
  //0CDIF{$SXX.A%B$CCVW@J]A^@__qr
 	
 r`   r   c                 ,    U  H  n[        U5        M     g)zinstall the requirements by files

Args:
    requirements: a list of files including requirements info (requirements.txt)

 No returns, raise error if failed

N)r   )r   requirements     r$   r   r     s     $(5 $r`   c                     U bA  [        U 5        / nU  H"  n[        U5      u  p4nUR                  U5        M$     [        U5        gg)z+Try to install and import plugins from repoN)r   get_modules_from_packagerZ   r[   )r   modulesr#   r<   module_versionr}   s         r$   register_plugins_repor     sI    %g.F-Ef-M*KNN;'  	w r`   c                     U(       a&  [         R                  SU  S35         [        U 5        gg! [         a    [         R                  S5         gf = f)z4Try to install and import remote model from modelhubz.Use allow_remote=True. Will invoke codes from z9. Please make sure that you can trust the external codes.z;Multi component keys in the hub are registered in same fileN)r+   r   r   KeyError)r   allow_remotes     r$   register_modelhub_repor     sY    <YK H5 5	6	(3   	NNMO	s   / AAzhttps://pypi.org/simple/c           	        ^^^^^^^^^^^^^ SSK Jm  SSKJm  SSKJmJm  SSKJm  SSKmSSK	J
m  SSKJm  SS	KJn  SUU4S
 jjmSU4S jjmS mUU4S jmUU4S jm     SUUUUUUUU4S jjnU4S jnT" 5       nU R                  S5      (       as   [         R"                  R%                  U 5      (       dK  [         R"                  R'                  U 5      n[         R"                  R)                  XE5      nT" XSS5      u  pxO#U nO  U" U 5      n	U	R*                  n U" XS9n
U
S   nU" U5      u  pn[,        R.                  " U5        XU4$ )ag  to get the modules from an installed package

Args:
    package: The distribution name or package name

Returns:
    import_names: The modules that in the package distribution
    import_version: The version of those modules, should be same and identical
    package_name: The package name, if installed by whl file, the package is unknown, should be passed

r   )ZipFile)mkdtemp)check_outputSTDOUT)globN)urlparse)requestRequirementc                   > Ub\  TR                  5       nUu  pVT" U 5      R                  nUR                  S XuU5        TR                  U5      nTR	                  U5      n	OTR	                  5       n	U	R                  XS9n
U
R                  5       n[        US5       nUR                  U
R                  5       5        S S S 5        X4$ ! , (       d  f       X4$ = f)N)datawb)	HTTPPasswordMgrWithDefaultRealmnetlocadd_passwordHTTPBasicAuthHandlerbuild_openerr:   r,   writer   )r   r7   r   authpassword_mgrusernamepasswordtop_level_urlhandleropenerresheadersfpurllib2r   s                r$   urlretrieve-get_modules_from_package.<locals>.urlretrieve  s    "BBDL "&H$SM00M%%dMXN22<@G ))'2F))+Fkk#k)((*(D!RHHSXXZ  "    "!   s    C
Cc                 @  >^^ [        TU5      nU" 5       n[        R                  SXS9  [        U S5       m[	        UU4S jS5       H  nUR                  U5        M     S S S 5        UR                  5       n[        R                  SUS9  U$ ! , (       d  f       N4= f)Nzcomputing checksumtarget	algorithmrbc                  &   > TR                  T 5      $ N)r   )	blocksizer;   s   r$   <lambda>Dget_modules_from_package.<locals>.compute_checksum.<locals>.<lambda>.  s    affY&7r`   r`   zcomputed checksum)result)r   r+   debugr:   iterupdate	hexdigest)	r  r  r  hashtypehash_chunkr  r;   hashlibs	     `    @r$   compute_checksum2get_modules_from_package.<locals>.compute_checksum)  s    7I.
)&N&$17=U# >   "(8  s   *B
Bc                       SS K n U R                  R                  S5      $ ! [         a     Of = fSS KnUR
                  $ )Nr   pip)importlib.metadatametadatar   r   r%  __version__)rr   r%  s     r$   _get_pip_version2get_modules_from_package.<locals>._get_pip_version4  s@    	%%%--e44 		s   ! 
..c                   > S nU(       ab  T" U5      nUR                   (       aI  UR                  (       a8  UR                  T" U 5      R                  :X  a  UR                   UR                  4nU(       ab  T" U5      nUR                   (       aI  UR                  (       a8  UR                  T" U 5      R                  :X  a  UR                   UR                  4nT	" XUS9u  pgXg4$ )N)r  )r  r  hostname)
r   scratch_file	index_urlextra_index_urlr  parsedr  _headersr   r  s
           r$   _download_dist0get_modules_from_package.<locals>._download_dist?  s    i(F6??v(K!8" 9o.F6??v(K!8" 9&stDr`   c                   > [         R                  SSSSSSS/nU b1  USU /-  nU [        :w  a   T	" U 5      R                  nU(       a  US	U/-  nUb  US
US	T	" U5      R                  /-  nUc  T" 5       nO[	        U5      S   n[	        U5      S   US'   UR                  S5      SS u  pg[        U5      n[        U5      nUS:  a  UR                  S5        SXg4s=::  a  S:  a  O  U$ UR                  S5        U$ )N-mr%  wheelz-vvvz	--no-depsz--no-cache-dirz--disable-pip-version-check--index-urlz--trusted-hostz--extra-index-urlpip_versionpython_executabler   r?      
   z--progress-bar=off)      )   r   z --use-deprecated=legacy-resolver)r3   
executableDEFAULT_INDEXr,  dictr    intrl   )
r.  envr/  r   r,  r8  	pip_major	pip_minorr)  r   s
           r$   _get_wheel_args1get_modules_from_package.<locals>._get_wheel_argsP  s.   NN)	
  ]I..DM)#I.77-x88D&#_6F)22 D ;*,Ks)M2K3i 34DG*005a:		N		N	?KK,-y,6w6  KK:;r`   c                 
  > T"" XU5      U /-   nT&" US9n[         R                  S[        R                  R	                  U5      SR                  U5      S9   T#" UT US9R                  S5      n[         R                  S	U S
9  / n
/ nUR                  5       n[        U5       GH9  u  pUR                  5       nUR                  S5      (       a(  UR                  5       nUS   nU
R                  U5        MT  UR                  S5      (       Ga  UR                  5       nUS   n[!        U5      S:X  a2  UR                  S5      (       a  UR#                  S5      (       a  US   nO[!        U5      S:X  a  US   R                  S5      (       a  UR#                  S5      (       as  US   nT'" U5      R$                  (       dV  XS-      R                  5       nUR                  S5      (       a+  SU;   a%  UR                  S5      S   R                  5       S   nOUnU
R                  U5        GM  UR                  S5      (       a-  SU;   a'  UR                  5       S   nU
R                  U5        GM  UR                  S5      (       d  GM  SU;   d  GM  UR                  5        Vs/ s H  nUR                  S5      (       d  M  UPM     snu  nUR                  U5        GM<     U
(       d  X-  n
['        [(        R+                  U
5      5      n
U
(       d  [         R                  SUS9  [-        S5      e[!        U
5      S:X  a8  U
u  nnUR                  U5      (       a  U
S	 OUR                  U5      (       a  U
S	 [!        U
5      S:  a  [         R                  S XS!9  U
S   nT%" [        R                  R                  [        R                  R	                  U5      S"5      5      n Uu  nUR1                  S$5      u  nnnUR3                  S%S&5      nUR                  S'5      (       d  UR                  S(5      (       d  S)n[        R                  R5                  U5      R7                  5       UR9                  S*S5      S   R7                  5       :X  a  UnOK[        R                  R                  U[        R                  R5                  U5      5      nT!" UUUU5      u  nnT$" UUS+9nS,R                  UU/5      nUUUS-.nU$ ! [         aD  n	[        U	SS5      R                  S5      n[         R                  U5        U(       d  e  S n	A	GNS n	A	ff = fs  snf ! [.         a    U(       a  S#n GNe f = f).N)dirzwheeling and dealingr   )scratch_dirr   )stderrro   r   outputr`   zwheel command completed ok)	dist_namezDownloading from URL r=  zDownloading ()   r   zAdded z to build trackerr   z
Source in zwhich satisfies requirementz from file://zfile://zcould not find download link)outzfailed to collect distr:  zmore than 1 link collected)rT  linksz*.whlr   #z/%2Bf/z/+f/zmd5=zsha256=md5/r  =)r0   r   checksum)r+   r  r)   r0   abspathr   decodeChildProcessErrorr   r   r   	enumerater!   r   r    rl   rk   endswithschemelistrA  fromkeysr   
ValueError	partitionr   r   lowerrsplit)(rM  r.  rC  r/  tmpdirignore_errorsr   rJ  rT  errrU  local_linkslinesilinepartslinklast	next_linexfirstsecondwhlswhlr   _seprZ  r  r  r-  r1  r  r   r2  rF  r   r"  r   r   r   s(                                   r$   get%get_modules_from_package.<locals>.getu  s    y?9+M&)"4$ 	 	!	V66<fWo  	1YG  'GA::<D677

QxT"00

Ryu:?ts';';A A 9DZ1_r)=)=* *!%s!3!3 9D#D>00 %*a%L$6$6$8	$// (* *.AY.N#,?? 3$556$88=$DD  DT" " "&Ct&Kzz|B'T"**$/F%)ZZ\M\Q\\)5L!\M""4(C (D  ET]]5)*NN9sNC455u:?!ME6''!H""5))!Hu:>LL53LL
 QxBGGLL!=wGH	ES #nnS1T8kkf
 ""6**83F3F4 4 Iww$**,

30B20F0L0L0NN!ww||K9I9I#9NO#1#|Y2A$C 'vKHxxH 56Hcx@s ! 	#x-44W=CNN3  !	R N4  			s6   S<  U>U!U <
U
9UU
U*(U*c                 >  > SS K n[        R                  S5        T" U S9nUR                  5       nU Vs/ s H  oDR	                  S5      (       d  M  UPM     snu  nU Vs/ s H  oDR	                  S5      (       d  M  UPM     snu  nUR                  U5      R                  S5      R                  5       R                  5       nUR                  U5      R                  S5      nU V	s/ s H  oR                  S5      (       a  M  U	PM     n
n	UR                  " SUR                  5      nUR                  " S	UR                  5      nUR                  U5      nUR                  U5      nUR                  S
5      nUR                  S5      nXU4$ s  snf s  snf s  sn	f )Nr   zfinding import names)r   ztop_level.txtz.dist-info/METADATAr   r}   z^Version: (?P<version>.+)$z^Name: (?P<name>.+)$r   r   )r   r+   r  namelistr_  r   r\  r!   r   r   compile	MULTILINEsearchgroup)whl_filer   zipfiler{  rr  top_level_fnamemetadata_fname	all_namesr'  npublic_namesversion_patternname_patternversion_match
name_matchr   r<   r   s                    r$   discover_import_names7get_modules_from_package.<locals>.discover_import_names  s[   +,x(##%!AAZZ%@aA	 "GAZZ0E%FaG	LL188UWZZ\ 	<</66w?#,F9aLL4E9F**%B%'\\3zz"92<<H'..x8!((2
&,,Y7 &&v.[88% BG Gs#   FF!F>F)FFz.whl)rg  r0   NN)sha256i    )NNNNF)r  r   tempfiler   
subprocessr   r   r   r!  urllib.parser   urllibr   packaging.requirementsr   r_  r)   r0   rA   r   r   r   r   rmtree)packager   rx  r  rg  	file_namer   r  r}   r   r   import_namesimport_versionrt   r   r   r2  r)  rF  r   r"  r   r!  r   r  r   r  s                 @@@@@@@@@@@@@r$   r   r     s#      /%)2! !6		 "#L  h hT94 YFOww~~g&&((1IV7I(T4HKHaH	 '"((7*<1F2.L,
MM&55r`   c                   ^   \ rS rSrSr\\4S jr\S 5       r	\	R                  S 5       r	\S 5       r\SS j5       r\S	\\   4S
 j5       r\S	\\   4S j5       r  SS\\   S\\   S\4S jjrS\\   4S jr SS\\\4   4S jjrS r SS jrS r SS jr SS jrS\\\4   4S jrSrg)r   i  z
plugins manager class
c                     [         R                  " SU5      n[         R                  " SU5      n[         R                  R                  X5      U l        g )NMODELSCOPE_CACHEMODELSCOPE_PLUGINS_FILE)r)   getenvr0   r   
_file_path)self	cache_dirplugins_files      r$   __init__PluginsManager.__init__  s:     II0)<	yy!:LI'',,y?r`   c                     U R                   $ r  r  r  s    r$   r   PluginsManager.file_path  s    r`   c                     Xl         g r  r  )r  values     r$   r   r     s    r`   c                     U R                  S5      S   S:X  aD  [        U 5      u  pn[        R                  U5      u  pEU(       a	  X%:w  a  SU4$ U(       d  SU4$ SU4$ [        R                  U 5      $ )zCheck if the plugin is installed, and if the version is valid

Args:
    package: the package name need to be installed

Returns:
    if_installed: True if installed
    version: the version of installed or None if not installed

r?   rN  rv  FT)r    r   r   _check_plugin_installed)r  r}   r   rt   local_installedr   s         r$   r   %PluginsManager.check_plugin_installed$  sz     ==b!U*.Fw.O+A|'5'M'M($O>#<g~%$g~%''!99'BBr`   Nc                    SSK Jn  U" U 5      n [        R                  " 5         [        R                  R                  UR                  5      nUR                  nSnUR                   H  nUR                  U5      nU(       a  M  Sn  O    U(       a  Ub	  X:w  a  SU4$ Xe4$ ! [        R                  R                   a    SnSn N:f = f)Nr   r   TFr   )r  r   rr   r   r'  distributionr   r   	specifiercontainsPackageNotFoundError)	r  verified_versionr   r   distr   r   r   installed_valid_versions	            r$   r  &PluginsManager._check_plugin_installed>  s    6'"	'')%%22388<DllG I *.--*@'.. %I	 & )5:J:U***%% !!66 	GI	s   A1B" B" 	B" ""CCcommand_argsc                 N   [         R                  " 5         U S:X  a"  UR                  S5        UR                  S5        [        R	                  U5      u  p#[
        R                  SSU /U-   n[        R                  " U5      nUR                  n[         R                  " 5         XbU4$ )a  
Run a pip command via subprocess.

Args:
    command: install, uninstall command
    command_args: the args to be used with command, should be in list
      such as ['-r', 'requirements']

Returns:
    status_code: The pip command status code, 0 if success, else is failed
    options: parsed options extracted from command_args
    args: positional arguments (package names) extracted from command_args

r   -fzAhttps://modelscope.oss-cn-beijing.aliyuncs.com/releases/repo.htmlr5  r%  )
rr   r   rl   r   _parse_pip_argsr3   r?  r  run
returncode)commandr  optionsr   cmdr  r   s          r$   r   PluginsManager.pip_command\  s    ( 	##%i%S '66|D~~tUG4|C$'' 	##%T))r`   c                    SSK Jn  U" SS/ S9n/ nSSSSSSSS.nSnU[        U 5      :  a  X   nXd;   af  XF   nUS	-   [        U 5      :  aO  UbE  XS	-      n[        X'5      n	[	        U	[
        5      (       a  U	R                  U5        O[        X'U5        US
-  nM}  O.UR                  S5      (       a  US	-  nM  UR                  U5        US	-  nU[        U 5      :  a  M  X#4$ )a  Parse pip command arguments into options and positional args.

Extracts commonly used pip options and separates positional arguments
(package names) from flag arguments, maintaining backward compatibility
with callers that depend on the parsed structure.

Args:
    command_args: list of arguments passed to pip command

Returns:
    options: SimpleNamespace with index_url, src_dir, and requirements
    args: list of positional arguments (package names)

r   )SimpleNamespaceN)r.  src_dirr   r.  r   r  )-ir7  z-rz--requirementz--srcr  z--find-linksr   r:  -)	typesr  rk   r   r   ra  rl   setattrr   )
r  r  r  r   flags_with_valuerl  argattrr  currents
             r$   r  PluginsManager._parse_pip_args  s     	*!

  & + 
 #l##/C&',q53|,,' ,U 3")'"8%gt44#NN51#G59FA - $$QC FA) #l##, }r`   install_argsr.  r8   c                 *   [        U5      S:X  a  S/ 4$ Ub  USU/-  nUSLa  US/-  n[        R                  SU5      u  pEnUS:X  aJ  [        R	                  SSR                  U5       S35        U R                  Xe5      nU R                  U5        XA4$ )	zInstall packages via pip
Args:
install_args (list): List of arguments passed to `pip install`.
index_url (str, optional): The pypi index url.
force_update: If force update on or off
r   r  Fr  r   The plugins r   z is installed)rk   r   r   r+   r,   r   parse_args_infoupdate_plugins_file)r  r  r.  force_updater   r  r   installed_packages           r$   r   PluginsManager.install_plugins  s     |!b5L T9--Lu$TF"L%3%?%?&
"d
 !KK,sxx~&6mDE
 !% 4 4T C$$%67((r`   r   c                 *  ^ ^ / n[        U5      S:X  aV  TR                  nTR                  nU H8  nU[        R                  R                  XF5      SSS.nUR                  U5        M:     UU 4S jnU H  n	U" U	5      nUR                  U5        M     U$ )aM  
parse arguments input info
Args:
    args: the list of args from pip command output
    options: the options that parsed from system args by pip command method

Returns:
    installed_package: generate installed package info in order to store in the file
                        the info includes: name, url and desc of the package
r   r   )r   r   r   r   c                   > SSK Jn  U TR                  SS.nU R                  S5      S   S:X  a  U" U 5      R                  n TR                  U 5      u  p4U(       a
  XBS'   XS	'   U$ [        R                  S
U  S35        SUS'   U$ )Nr   r   r   )r   r   r   r?   rN  gitr   r   zThe package zo is not in the lib, this might be happened when installing the package with git+https method, should be ignored)pathlibr   r.  r    stemr   r+   r   )rt   r   package_infor   r   r  r  s        r$   get_package_info8PluginsManager.parse_args_info.<locals>.get_package_info   s    $$((L !!#&r*e3#L166(,(C(C)%*1Y''3V$   "<. 1\ ] +-Y'r`   )rk   r  r   r)   r0   r   rl   )
r  r   r  r  r  r   r   r  r  r  s
   ` `       r$   r  PluginsManager.parse_args_info  s      t9>ooG"//L+'77<<=!	  "((6  ,	 4 G+G4L$$\2  ! r`   uninstall_argsc                     Ub  US/-  n[         R                  SU5      u  p4nUS:X  a9  [        R                  SSR	                  U5       S35        U R                  U5        X14$ )z
uninstall plugins
Args:
    uninstall_args: args used to uninstall by pip command
    is_yes: force yes without verified

Returns: status code, and uninstall args

z-y	uninstallr   r  r   z is uninstalled)r   r   r+   r,   r   remove_plugins_from_file)r  r  is_yesr   r  r   s         r$   uninstall_plugins PluginsManager.uninstall_plugins   sr     tf$N%3%?%?&
"d
 !KK,sxx~&6oFG
 ))$/**r`   c                    [         R                  SU R                   35        [        R                  R                  U R                  5      (       a7  [        R                  U R                  5      n[        R                  " U5      nU$ 0 nU$ )zget plugins from file

        z!Loading plugins information from )
r+   r,   r   r)   r0   r   storager   jsonloads)r  local_plugins_info_byteslocal_plugins_infos      r$   _get_plugins_from_file%PluginsManager._get_plugins_from_file>  sm     	77GHI77>>$..))'.||DNN'C$!%,D!E "! "$!!r`   c                 R   U H  nUR                  S5      nXR;   ac  U(       d\  X%   nSSKJn  U" US   S:w  a  US   OS5      nU" US   S:w  a  US   OS5      n	US   n
US   S:w  a  U
S:X  a  US   n
X:  a  UOUnXS'   US   S:X  a  U R                  U5      u  pXS'   XBU'   M     U$ )Nr   r   )parser   r   z0.0.0r   )poppackaging.versionr  r   )r  new_plugins_listr  overrideitemrt   original_itemparse_versionitem_versionorigin_versionr   r   r   s                r$   _update_pluginsPluginsManager._update_pluginsJ  s     %D88F+L 1( 2 @D,'+I"'<DO' K!.$Y/25 "),;B"D F| (B.42:(0D+<t-#V I"$,0,G,G -") ")Y/3|,1 %4 "!r`   c           	          [        SR                  SSS5      5        [        S5        UR                  5        H&  u  p#[        SR                  X#S   US   5      5        M(     g )	Nz{:<15} |{:<10}  |{:<100}NAMEVERSIONDESCRIPTIONr   z{:<15} |{:<10} |{:<100}r   r   )printformatitems)r  r  kvs       r$   _print_plugins_info"PluginsManager._print_plugins_infol  sZ    (//	0=? 	@b	&,,.DA+221	lAfINO /r`   c                 V   U R                  5       n[        R                  " [        5      nU R	                  UU5      nUSL a  U R                  U5        U$ / nU H!  nX%   S   S:X  d  M  UR                  U5        M#     U H  nUR                  U5        M     U R                  U5        U$ )z

Args:
    show_all: show installed and official supported if True, else only those installed

Returns:
    local_plugins_info: show the list of plugins info

Tr   r   )r  copydeepcopyOFFICIAL_PLUGINSr  r  rl   r  )r  show_allr  local_official_pluginsnot_installed_listr  s         r$   list_pluginsPluginsManager.list_pluginss  s     "88: "&/?!@!112H2DF t$$%78%%  &D!'	2b8"))$/ ' 'D""4( ' 	  !34!!r`   c                 0   U R                  5       nU0 :X  a)  UR                  [        R                  " [        5      5        U R                  UX25      n[        R                  " U5      n[        R                  UR                  5       U R                  5        U$ )a  update the plugins file in order to maintain the latest plugins information

Args:
    plugins_list: The plugins list contain the information of plugins
        name, version, introduction, install url and the status of delete or update
    override: Override the file by the list if True, else only update.

Returns:
    local_plugins_info_json: the json version of updated plugins info

)r  rZ   r  r  r  r  r  dumpsr  r  encoder   )r  plugins_listr  r  local_plugins_info_jsons        r$   r  "PluginsManager.update_plugins_file  s      "88: #.> ?@!11,2DP #'**-?"@-446G&&r`   package_namesc                 *   U R                  5       n[        U5      [        L a  [        U5      nU H  nX2;   d  M
  UR	                  U5        M     [
        R                  " U5      n[        R                  UR                  5       U R                  5        U$ )zremove the plugins from file
Args:
    package_names:  package name

Returns:
    local_plugins_info_json: the json version of updated plugins info

)r  typer   ra  r  r  r  r  r  r  r   )r  r  r  r  r  s        r$   r  'PluginsManager.remove_plugins_from_file  s|     "88:#% /M!D)"&&t, " #'**-?"@-446G&&r`   r  r  )NFF) __name__
__module____qualname____firstlineno____doc__MODELSCOPE_FILE_DIRPLUGINS_FILENAMEr  propertyr   setterstaticmethodr   r  r
   r   r   r  r   r   r   r  r   r  r  r  r  r  r  ra  r  __static_attributes__ r`   r$   r   r     sK   
 /.@       C C2 & &: %*3i%* %*N :d3i : :| 48%*")&*3i")#+C=") 03")H9!DI 9!z "'+*/T	*:+<
"  	 "DP #"P '<'S$Y''r`   r   c                   T    \ rS rSrSr\\4S jrS rS r	S r
S rS r\S	 5       rS
rg)EnvsManageri  envsc                 Z   [         R                  " SU5      n[         R                  R                  U[        R
                  U5      U l        [        XS9nSSKJ	n  U" U5      nUR                  S/ 5      U l        UR                  SS5      U l        SSKnUR                  S	SS	SS
9U l        g)z

Args:
    model_id:  id of the model, not dir
    model_revision: revision of the model, default as master
    cache_dir: the system modelscope cache dir
r  )revisionr   )read_configr   r   FNT)system_site_packagesclearsymlinkswith_pip)r)   r  r0   r   r&  r   env_dirr   modelscope.utils.hubr*  rx  r   r   venv
EnvBuilderenv_builder)r  model_idmodel_revisionr  r   r*  cfgr1  s           r$   r  EnvsManager.__init__  s     II0)<	ww||I{/?/?J%hH	4)$wwy"-GGNE:??!%	 + r`   c                     U R                   $ r  )r/  r  s    r$   get_env_dirEnvsManager.get_env_dir  s    ||r`   c                 X    [         R                  R                  U R                  SS5      $ )Nbinactivate)r)   r0   r   r/  r  s    r$   get_activate_dirEnvsManager.get_activate_dir  s    ww||DLL%<<r`   c                 \    [        U R                  5      (       d  U R                  (       a  ggrx   )rk   r   r   r  s    r$   check_if_need_envEnvsManager.check_if_need_env  s    t|| 1 1r`   c                 f   [         R                  R                  U R                  5      (       d   [         R                  " U R                  5         U R
                  R                  U R                  5        g ! [         a0  nU R                  5         [        SU R                   SU 35      eS nAff = f)Nz Failed to create virtual env at z with error: )
r)   r0   r   r/  r   r3  creater   	clean_envr   )r  rp   s     r$   
create_envEnvsManager.create_env  s    ww~~dll++KK%	##DLL1 	NN"24<<.aSQ 	s   %A6 6
B0 +B++B0c                     [         R                  R                  U R                  5      (       a&  U R                  R                  U R                  5        g g r  )r)   r0   r   r/  r3  clear_directoryr  s    r$   rE  EnvsManager.clean_env  s6    77>>$,,'',,T\\: (r`   c                     SS K nUR                  " U 5      u  p#[        R                  SR	                  X#5      5        US:w  a  [        SR	                  X5      5      eU$ )Nr   z&The status and the results are: {}, {}z,running the cmd: {} failed, with message: {})r  getstatusoutputr+   r  r  r   )r  r  r   r  s       r$   run_processEnvsManager.run_process  s`    #33C8=DD 	Q;>EE!" " r`   )r   r3  r/  r   N)r  r  r  r  r   r   r  r  r9  r>  rA  rF  rE  r"  rM  r#  r$  r`   r$   r&  r&    sA    D !7.2=	; 	 	r`   r&  __main__r  r  r  )r8   N)Nr  r   rr   r&  r)   r   r   r   r  r3   
contextlibr   r   r  r   typingr   r	   r
   r   r   r   r  rH   r   modelscope.fileio.filer   modelscope.utils.ast_utilsr   modelscope.utils.constantr   modelscope.utils.file_utilsr   modelscope.utils.loggerr   r+   r  r  MODELSCOPE_DYNAMIC_MODULEr0   r   r   r  r  rB   homerD   r\   r"   r%   r   r   r/   r6   r=   rF   r_   r[   r]   rY   r   r   r   r   r   r   r   r   r   r@  r   objectr   r&  r  import_namer  rt   r$  r`   r$   <module>r[     s  
     	  	   
 %   < <  ( / 7 < @ .	
..0 0 '',,24MN(  d  / '',,tyy{M9M !N  3   $ 3    /	"	"3;C=	"$x} $@S	 T#Y B#DI #c #L$s) 6 @D?C7Cs 7C*23s8*<7C*23s8*<7CHL7CtK8  "F3 3 0$+N
49 
0
6S	 
6 49     +T6n~'V ~'BA& AH z!8*-0H1-K r`   