
    9i              	          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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$\RJ                  RM                  \#\$5      r'Sr(SSSSS./r)Sr*\RJ                  RM                  \RV                  " 5       SS5      r,/ r-\S5S\.S\/4S jj5       r0\S\.4S j5       r1\*4S\.S\\.   4S jjr2S6S\\.   4S jjr3S6S\\.   S\\.   4S jjr4S6S\\.   S\\.   4S jjr5S6S\\.   4S jjr6  S7S \.S!\\\.      S"\\\.      SS4S# jjr7S$ r8S% r9S& r:S' r;S\\.   4S( jr<S)\\.   4S* jr=S\\.   SS4S+ jr>S5S8S, jjr?S-r@S. rA " S/ S0\B5      rC " S1 S2\B5      rD\ES3:X  a  \=" S/5        \A" S45      u  rFrGrHgg)9    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plugins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      X/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/modelscope/utils/plugins.pypushdr&   2   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   resolvestrsysr'   insertremove)r'   s    r%   push_python_pathr.   E   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)openstrip)r/   fmodule_names      r%   discover_file_pluginsr6   U   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   r'   isfileLOCAL_PLUGINS_FILENAMEr.   r6   addGLOBAL_PLUGINS_FILENAME)requirement_pathr   plugins      r%   discover_pluginsr@   a   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_pluginsrY      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.*rP   Plugin %s availablePlugin  could not be loaded: )lenr   r    r+   r'   appendrS   r!   r"   ModuleNotFoundErrorerror)r   rX   cwdr5   es        r%   rU   rU      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

rb   rc   rd   N)r   r    r+   r'   rf   r@   	importlibimport_moduler!   r"   rg   rh   )r>   rX   ri   r5   rj   s        r%   rW   rW      s     #% ))+C
#(('(89	K##K0KK-{;##K0	 :  # 	KLL7;-/EaSIJJ	Ks   =B
C'CCpackage_namerQ   rR   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.
rn   pattern_setr0   c                 :    U H  n[        X5      (       d  M    g   gNTFr   )rn   rp   patterns      r%   fn_in+import_module_and_submodules.<locals>.fn_in   s    "G|-- # rZ   Nr8   __path__r   r   _testrP   z not imported: z, but should continue   zPackage not installed)r9   r*   r   boolrl   invalidate_cachesr.   rm   getattrpkgutiliter_modulesr'   
startswithrS   
SystemExitr!   warning	Exceptionre   splitrg   )rn   rQ   rR   rt   moduler'   path_stringmodule_finderr   rw   
subpackagerj   s               r%   rS   rS      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 )	r2   read
splitlinesPluginsManagercheck_plugin_installedrf   re   pip_commandImportError)	r>   install_listr4   requirementsreq	installedrw   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

)rl   utilspec_from_file_locationmodule_from_specloaderexec_module)r5   	file_pathspecr   s       r%   import_module_from_filer   8  sC     >>11+ID^^,,T2FKKF#MrZ   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)r2   )r   rw   s     r%   create_empty_file3create_module_from_files.<locals>.create_empty_fileU  s    )S!Q "!!s   
&T)exist_okz__init__.pyN)r   r'   joinBASE_MODULE_DIRdirnamemakedirsexistsbasenamefilecmpcmpshutilcopyfilerl   r{   )	file_listfile_prefixr5   r   dest_dirr   file_dir
target_dir	init_filetarget_filesrc_files              r%   create_module_from_filesr   H  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  !rZ   c                 >   SSK Jn  [        5       nUR                  U SS9  UR                  nUR
                  n[        U5        [        [        R                  ;  a#  [        R                  R                  [        5        U" U 5      R                  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[#        XU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

r   r   T)include_initr   r8   z.pyN)pathlibr   r   traversal_files	file_dirsrequirement_dirsinstall_requirements_by_filesr   r+   r'   rf   stemr   r   replacerstripsepr   rl   rm   )
	model_dirr   file_scannerr   r   r5   MODULE_PATHfilerelative_file_dirs	submodules
             r%   import_module_from_model_dirr   k  s>    #%L   >&&I00L ",/chh&(y/&&K '',,<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   rf   install_pluginsEnvironmentErrorr   )r   plugins_manageruninstalled_pluginsr?   plugin_installedr   statusrw   s           r%   install_requirements_by_namesr     s     %&O$3$J$J%!&&v.	 
  //0CDIF{$SXX.A%B$CCVW@J]A^@__qr
 	
 rZ   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 $rZ   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_packagerT   rU   )r   modulesr?   r5   module_versionrw   s         r%   register_plugins_repor     sI    %g.F-Ef-M*KNN;'  	w rZ   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)get_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_openerr2   r"   writer   )r   r/   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   )	blocksizer4   s   r%   <lambda>Dget_modules_from_package.<locals>.compute_checksum.<locals>.<lambda>	  s    affY&7rZ   rZ   zcomputed checksum)result)r|   r!   debugr2   iterupdate	hexdigest)	r  r  r	  hashtypehash_chunkr  r4   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__)rl   r  s     r%   _get_pip_version2get_modules_from_package.<locals>._get_pip_version  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rZ   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$ )Nz-mr  wheelz-vvvz	--no-depsz--no-cache-dirz--disable-pip-version-checkz--index-urlz--trusted-hostz--extra-index-urlpip_versionpython_executabler   r8      
   z--progress-bar=off)      )   ry   z --use-deprecated=legacy-resolver)r+   
executableDEFAULT_INDEXr  dictr   intrf   )
r!  envr"  r   r  r)  	pip_major	pip_minorr  r   s
           r%   _get_wheel_args1get_modules_from_package.<locals>._get_wheel_args+  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:;rZ   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   )stderrri   r   outputrZ   zwheel command completed ok)	dist_namezDownloading from URL r.  zDownloading ()   ry   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)rE  linksz*.whlr   #z/%2Bf/z/+f/zmd5=zsha256=md5/r  =)r'   r   checksum)r!   r  r   r'   abspathr   decodeChildProcessErrorr|   r   r   	enumerater3   r   r   rf   re   endswithschemelistr2  fromkeysr   
ValueError	partitionr   r   lowerrsplit)(r>  r!  r4  r"  tmpdirignore_errorsr   r;  rE  errrF  local_linkslinesilinepartslinklast	next_linexfirstsecondwhlswhlr   _seprK  r  r  r   r$  r  r   r%  r7  r   r  r   r   r   s(                                   r%   get%get_modules_from_package.<locals>.getP  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   rw   z^Version: (?P<version>.+)$z^Name: (?P<name>.+)$r   r   )rer!   r  namelistrP  r   rM  r3   r   r   compile	MULTILINEsearchgroup)whl_filerl  zipfilerm  rc  top_level_fnamemetadata_fname	all_namesr  npublic_namesversion_patternname_patternversion_match
name_matchr   r5   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)rX  r'   NN)sha256i    )NNNNF)rs  r   tempfiler   
subprocessr   r   r   r  urllib.parser   urllibr   pip._internal.utils.packagingr   rP  r   r'   r:   r   r   r   r   rmtree)packager   ri  r}  rX  	file_namer   rr  rw   r   r   import_namesimport_versionrn   r   r   r%  r  r7  r   r  r   r  r   r   r   r   s                 @@@@@@@@@@@@@r%   r   r     s#      /%)=! !6		 "#L  h hT94 YFOww~~g&&((1IV7I(T4HKHaH	 g&((7*<1F2.L,
MM&55rZ   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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   getenvr'   r   
_file_path)self	cache_dirplugins_files      r%   __init__PluginsManager.__init__  s:     II0)<	yy!:LI'',,y?rZ   c                     U R                   $ r  r  r  s    r%   r   PluginsManager.file_path  s    rZ   c                     Xl         g r  r  )r  values     r%   r   r    s    rZ   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

r8   r?  rg  FT)r   r   r   _check_plugin_installed)r  rw   r   rn   local_installedr   s         r%   r   %PluginsManager.check_plugin_installed  sz     ==b!U*.Fw.O+A|'5'M'M($O>#<g~%$g~%''!99'BBrZ   Nc                 v   SSK JnJn  U" U 5      n [        R                  " [
        5        [
        R                  R                  UR                     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$ Xv4$ ! [         a    SnSn N&f = f)Nr   )r   
specifiersTFr   )r  r   r  rl   reloadpkg_resourcesworking_setby_keyr   r   	specifiercontainsr   )
r  verified_versionr   r  r   package_meta_infor   r   r   installed_valid_versions
             r%   r  &PluginsManager._check_plugin_installed  s    Mg&	]+ - 9 9 @ @ J'//G I *.--*@'.. %I	 & )5:J:U***%%  	GI	s   A4B' 
B' B' 'B87B8command_argsc                 &   SSK Jn  [        R                  " [        5        U S:X  a"  UR                  S5        UR                  S5        U" U 5      n U R                  U5      u  p4U R                  U5      n[        R                  " [        5        XSU4$ )ak  

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 option from system args by pip command
    args: the unknown args that could be parsed by pip command

r   )create_commandr   -fzAhttps://modelscope.oss-cn-beijing.aliyuncs.com/releases/repo.html)pip._internal.commandsr  rl   r  r  rf   
parse_argsmain)commandr  r  optionsr   r   s         r%   r   PluginsManager.pip_command6  s    $ 	:'i%S !)**<8ll<0 	'T))rZ   install_argsr!  r0   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   z-iFr  r   The plugins r   z is installed)re   r   r   r!   r"   r   parse_args_infoupdate_plugins_file)r  r  r!  force_updater   r  r   installed_packages           r%   r   PluginsManager.install_pluginsY  s     |!b5L T9--Lu$TF"L%3%?%?&
"d
 !KK,sxx~&6mDE
 !% 4 4T C$$%67((rZ   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   r8   r?  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)r   r   r!  r   r   r   r!   r   )rn   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'rZ   )re   src_dirrequirmentsr   r'   r   rf   )
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  ! rZ   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
 ))$/**rZ   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   r'   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 "! "$!!rZ   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   )parse_versionr   r   z0.0.0r   )popr  r  r   )r  new_plugins_listr  overrideitemrn   original_itemr  item_versionorigin_versionr   r   r   s                r%   _update_pluginsPluginsManager._update_plugins  s     %D88F+L 1( 2 @7,'+I"'<DO' K!.$Y/25 "),;B"D F| (B.42:(0D+<t-#V I"$,0,G,G -") ")Y/3|,1 %4 "!rZ   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_info  sZ    (//	0=? 	@b	&,,.DA+221	lAfINO /rZ   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  rf   r  )r  show_allr  local_official_pluginsnot_installed_listr  s         r%   list_pluginsPluginsManager.list_plugins  s     "88: "&/?!@!112H2DF t$$%78%%  &D!'	2b8"))$/ ' 'D""4( ' 	  !34!!rZ   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  rT   r  r  r  r  r  dumpsr  r   encoder   )r  plugins_listr  r  local_plugins_info_jsons        r%   r  "PluginsManager.update_plugins_file0  s      "88: #.> ?@!11,2DP #'**-?"@-446G&&rZ   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*   rR  r  r  r  r  r   r  r   )r  r  r  r  r  s        r%   r  'PluginsManager.remove_plugins_from_fileN  s|     "88:#% /M!D)"&&t, " #'**-?"@-446G&&rZ   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  rR  r  __static_attributes__ rZ   r%   r   r     s+   
 /.@       C C2 & &8  *3i *  *H 48%*")&*3i")#+C=") 03")H9!DI 9!z "'+*/T	*:+<
"  	 "DP #"P '<'S$Y''rZ   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)EnvsManagerii  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  r'   r   r  r   env_dirr   modelscope.utils.hubr  ri  r   r   venv
EnvBuilderenv_builder)r  model_idmodel_revisionr  r   r  cfgr  s           r%   r  EnvsManager.__init__l  s     II0)<	ww||I{/?/?J%hH	4)$wwy"-GGNE:??!%	 + rZ   c                     U R                   $ r  )r  r  s    r%   get_env_dirEnvsManager.get_env_dir  s    ||rZ   c                 X    [         R                  R                  U R                  SS5      $ )Nbinactivate)r   r'   r   r  r  s    r%   get_activate_dirEnvsManager.get_activate_dir  s    ww||DLL%<<rZ   c                 \    [        U R                  5      (       d  U R                  (       a  ggrr   )re   r   r   r  s    r%   check_if_need_envEnvsManager.check_if_need_env  s    t|| 1 1rZ   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   r'   r   r  r   r  creater   	clean_envr   )r  rj   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   r'   r   r  r  clear_directoryr  s    r%   r,  EnvsManager.clean_env  s6    77>>$,,'',,T\\: (rZ   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   )cmdr  r   r  s       r%   run_processEnvsManager.run_process  s`    #33C8=DD 	Q;>EE!" " rZ   )r   r  r  r   N)r   r  r  r  r   r   r  r  r   r%  r(  r-  r,  r	  r5  r
  r  rZ   r%   r  r  i  sA    D !7.2=	; 	 	rZ   r  __main__z
pai-easycvr  r  r  )r0   N)Ir  r   rl   r   r}   r   r+   
contextlibr   r   r   r   typingr   r	   r
   r   r   r   r  r  rB   r   modelscope.fileio.filer   modelscope.utils.ast_utilsr   modelscope.utils.constantr   modelscope.utils.file_utilsr   modelscope.utils.loggerr   r!   r  r  MODELSCOPE_DYNAMIC_MODULEr'   r   r   r  r  r;   homer=   rV   r*   rz   r&   r.   r6   r@   rY   rU   rW   rS   r   r   r   r   r   r   r   r   r1  r   objectr   r  r   import_namer  rn   r  rZ   r%   <module>rC     sg  
    	   
 %   < <   ( / 7 < @ .	
..0 0 '',,24MN(  d  / '',,tyy{M9M  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%+P
49 
0
6S	 
6 49     +T6n{'V {'|A& AH z!8*-0H1-K rZ   