
    vj              	          d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	m
Z
 d dlmZ d dlmZ d dlmZmZmZmZmZmZ d dlZd dlmZ d dlmZ d dlmZ d d	lmZ d d
lmZ d dl m!Z!  e!            Z" e            Z# e            Z$dZ%ej&        '                    e$e%          Z(dZ)dddddgZ*dZ+ej&        '                     ej,                    dd          Z-g Z.e
d6de/de0fd            Z1e
de/fd            Z2e+fde/dee/         fdZ3d7dee/         fdZ4d7dee/         dee/         fdZ5d7dee/         dee/         fdZ6d7dee/         fd Z7	 	 d8d!e/d"eee/                  d#eee/                  ddfd$Z8d% Z9d& Z:d' Z;d( Z<dee/         fd)Z=d*ee/         fd+Z>dee/         ddfd,Z?d6d9d-Z@d.ZAd/ ZB G d0 d1eC          ZD G d2 d3eC          ZEeFd4k    r e>dg            eBd5          \  ZGZHZIdS dS ):    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pluginsFnew_dirverbosec              #   v  K   t          j                    }|rt                              d|             t          j        |            	 dV  |rt                              d|            t          j        |           dS # |rt                              d|            t          j        |           w xY w)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      h/lsinfo/ai/hellotax_ai/data_center/backend/venv/lib/python3.11/site-packages/modelscope/utils/plugins.pypushdr&   3   s       9;;L 86W66777HW 	FKKDlDDEEE
  	FKKDlDDEEE
s   
B 5B8pathc              #   :  K   t          |                                           } t          |           } t          j                            d|            	 dV  t          j                            |            dS # t          j                            |            w xY w)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.   F   s       ::Dt99DHOOAts   A9 9!Bfilenamereturnc              #      K   t          |           5 }|D ]}|                                }|r|V  	 ddd           dS # 1 swxY w Y   dS )z$
    Discover plugins from file
    N)openstrip)r/   fmodule_names      r%   discover_file_pluginsr6   V   s      
 
h "1 	" 	"K%++--K "!!!!	"" " " " " " " " " " " " " " " " " "s    A  AAc              #     K   t                      }| t          j                            t                    r]t          d          5  t          t                    D ] }||v r|V  |                    |           !	 ddd           n# 1 swxY w Y   t          j                            t                    r5t          t                    D ]"}||v r|V  |                    |           !dS dS t          j                            |           r0t          |           D ]"}||v r|V  |                    |           !dS dS )za
    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@   b   s      G7>>011 	(!#&& ( (34JKK ( (F((  LLLKK''''	(( ( ( ( ( ( ( ( ( ( ( ( ( ( ( 7>>122 	$/0GHH $ $W$$F####	$ 	$$ $ 7>>*++ 	$/0@AA $ $W$$F####	$ 	$$ $s   6B		BBc                 
   t          dh dh d           g }|                    t          t                               |                    t          |                      |                    t	                                 |S )zM
    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3444/11222    c                    g }| t          |           dk    r|S t          j                    }|t          j        vrt          j                            |           | D ]}	 t          |h dddh           t                              d|           |                    |           J# t          $ r*}t          
                    d| d	|            Y d}~yd}~ww xY w|S )
z6
    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 K	K(   '    KK-{;;;##K0000" 	K 	K 	KLLI;IIaIIJJJJJJJJ	K s   AB&&
C0 CCc                    g }t          j                    }|t          j        vrt          j                            |           t          |           D ]~}	 t          j        |           t          	                    d|           |                    |           H# t          $ r*}t                              d| d|            Y d}~wd}~ww xY w|S )z
    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9 K K	K#K000KK-{;;;##K0000" 	K 	K 	KLLI;IIaIIJJJJJJJJ	K s   AB
C$ C		Cpackage_namerQ   rR   c           	         |r|nt                      }|r|nt                      }dt          dt          t                   dt          fd} || |          s || |          rdS t	          j                     t          d          5  	 t	          j        |           }t          |dg           }|sdn|d	         }t          j
        |          D ]Y\  }}}	|r|j        |k    r|                    d
          r*|                    d          r@|  d| }
t          |
||           Zn# t          $ r7}t                              |  dt          |           d           Y d}~n{d}~wt"          $ rk}t                              |  dt          |                      t%          |                     d                    dk    rt)          d          Y d}~nd}~ww xY wddd           dS # 1 swxY w Y   dS )z?
    Import all public submodules under the given package.
    rn   pattern_setr0   c                 6    |D ]}t          | |          r dS dS NTFr   )rn   rp   patterns      r%   fn_inz+import_module_and_submodules.<locals>.fn_in   s4    " 	 	G|W-- tturZ   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   rv   
subpackagerj   s               r%   rS   rS      s    !+ggceeG +ggceeGC c#h 4     5w'' EE,,H,H !!!
 
#		 C C	C,\::F6:r22D$(5""d1gK +2*>t*D*D B B&tQ  =#5#D#D??3'' ??6**  ,55t55
,B B B B BB  	 	 	NNMMAMMMO O ODDDD 	C 	C 	CNNlCC3q66CCDDD<%%c**++q00)*ABBB 10000	C9C C C C C C C C C C C C C C C C C CsP   G+BD'&G+'
G1-E#G+#G0A!GG+GG++G/2G/c                    g }t          | dd          5 }|                                                                }|D ]=}|dk    r	t                              |          \  }}|s|                    |           >	 ddd           n# 1 swxY w Y   t          |          dk    r7t                              d|          \  }}}|dk    rt          d|            dS dS )	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	installedrv   status_codeargss	            r%    install_module_from_requirementsr     sS    L	g	6	6	6 )!vvxx**,, 	) 	)Cbyy)@@EELIq )##C(((	)) ) ) ) ) ) ) ) ) ) ) ) ) ) ) <1-99 
  
Q !I7GIIK K K 
 s   A'B		BBc                     t           j                            | |          }t           j                            |          }|j                            |           |S )z install 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   9  sK     >11+yIID^,,T22FKF###MrZ   c                    d }t           j                            t          |          }| D ]H}t           j                            |          }t           j                            ||          }t          j        |d           t           j                            |d          }t           j                            |          s ||           t           j                            |t           j                            |                    }	t           j                            ||          }
t           j                            |	          rt          j	        |
|	          st          j        |
|	           Jt          j                     dS )aD  
    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                 Z    t          | d          5 }	 d d d            d S # 1 swxY w Y   d S )Nw)r2   )r   rv   s     r%   create_empty_filez3create_module_from_files.<locals>.create_empty_fileV  sv    )S!! 	Q	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s    $$T)exist_okz__init__.pyN)r   r'   joinBASE_MODULE_DIRdirnamemakedirsexistsbasenamefilecmpcmpshutilcopyfilerl   rz   )	file_listfile_prefixr5   r   dest_dirr   file_dir
target_dir	init_filetarget_filesrc_files              r%   create_module_from_filesr   I  s@      w||O[99H 3 3	7??9--W\\(H55

J....GLL];;	w~~i(( 	)i(((gll:rw/?/?	/J/JKK7<<Y77w~~k** 	3'++3' 3' 	3OHk222!!!!!rZ   c                     ddl m} t                      }|                     d           |j        }|j        }t          |           t          t          j	        vr$t          j	        
                    t                      |           j        }t          j	                            t          |          }|t          j	        vrt          j	        
                    |            fd|D             }t          | |           |D ]P}|dz   |                    dd                              t          j        d          z   }	t#          j        |	           Qd	S )
z import all the necessary module from a model dir

    Args:
        model_dir: model file location

     No returns, raise error if failed

    r   r   T)include_initc                     g | ]B}|                                         t          j                  t          j        z   d           CS )r   )replacerstripr   sep).0file	model_dirs     r%   
<listcomp>z0import_module_from_model_dir.<locals>.<listcomp>  sN        	Y%%bf--6;;  rZ   r8   z.pyr   N)pathlibr   r   traversal_files	file_dirsrequirement_dirsinstall_requirements_by_filesr   r+   r'   rf   stemr   r   r   r   r   rl   rm   )
r   r   file_scannerr   r   r5   MODULE_PATHrelative_file_dirsr   	submodules
   `         r%   import_module_from_model_dirr   l  se    #%%L   >>>&I0L ",///ch&&((($y//&K ',,<<K#(""$$$      /KHHH" + +#%UB(?(?(G(GFC) ) 		****+ +rZ   c                 F   t                      }g }| D ]1}|                    |          \  }}|s|                    |           2|                    |          \  }}|dk    r>t	          dd                    |           ddd                    |           d          dS )	z install 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   statusrv   s           r%   install_requirements_by_namesr     s     %&&O / /$3$J$J% %!' 	/&&v...//0CDDIFA{{WSXX.A%B%BWWWrJ]A^A^rrr
 
 	
 {rZ   r   c                 .    | D ]}t          |           dS )z install 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.     $ 6 6(55556 6rZ   c                     | Ot          |            g }| D ]*}t          |          \  }}}|                    |           +t          |           dS dS )z, Try to install and import plugins from repoN)r   get_modules_from_packagerT   rU   )r   modulesr?   r5   module_versionrv   s         r%   register_plugins_repor     so    %g... 	( 	(F-Ef-M-M*KNN;''''w rZ   c                     |r[t                               d|  d           	 t          |            dS # t          $ r t                               d           Y dS w xY wdS )z5 Try 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     s     	5Y 5 5 5	6 	6 	6	(33333 	 	 	NNMO O ODD		 	s   3 $AAzhttps://pypi.org/simple/c           	         ddl m ddlm ddlmm ddlm ddlddl	m
 ddlm dd	lm} dfd
	dfd	d fdfd	 	 	 	 	 dfd	}fd}             }|                     d          rt	 t           j                            |           sQt           j                            |           }t           j                            ||          } | |dd          \  }}n+| }n(	  ||           }	|	j        }  || |          }
|
d         } ||          \  }}}t-          j        |           |||fS )a   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                    |l                                 }|\  }} |           j        }|                    d |||                               |          }                    |          }	n                                }	|	                    | |          }
|
                                }t          |d          5 }|                    |
                                           d d d            n# 1 swxY w Y   ||fS )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%   urlretrievez-get_modules_from_package.<locals>.urlretrieve  s6   "BBDDL "&Hh$HSMM0M%%dM8XNNN22<@@G ))'22FF))++Fkk#Dk))((**(D!! 	!RHHSXXZZ   	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	!   s   ?(C33C7:C7sha256    c                    t          |          } |            }t                              d| |           t          | d          5 t	          fdd          D ]}|                    |           	 d d d            n# 1 swxY w Y   |                                }t                              d|           |S )Nzcomputing checksumtarget	algorithmrbc                  .                                    S N)r   )	blocksizer4   s   r%   <lambda>zDget_modules_from_package.<locals>.compute_checksum.<locals>.<lambda>
  s    affY&7&7 rZ   rZ   zcomputed checksum)result)r{   r!   debugr2   iterupdate	hexdigest)	r  r  r  hashtypehash_chunkr  r4   hashlibs	     `    @r%   compute_checksumz2get_modules_from_package.<locals>.compute_checksum  s   7I..

)&INNN&$ 	$177777== $ $U####$	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ ""(888s   -BB	B	c                  v    	 dd l } | j                            d          S # t          $ r Y nw xY wdd l}|j        S )Nr   pip)importlib.metadatametadatar   r   r  __version__)rl   r  s     r%   _get_pip_versionz2get_modules_from_package.<locals>._get_pip_version  s[    	%%%%%--e444 	 	 	D	


s     
--c                 :   d }|r@ |          }|j         r.|j        r'|j         |           j        k    r|j         |j        f}|r@ |          }|j         r.|j        r'|j         |           j        k    r|j         |j        f} 	| ||          \  }}||fS )N)r   )r   r   hostname)
r   scratch_file	index_urlextra_index_urlr   parsedr  _headersr   r  s
           r%   _download_distz0get_modules_from_package.<locals>._download_dist  s     	:Xi((F :6? :v((K K!8" 8" 9 	:Xo..F :6? :v((K K!8" 8" 9&;sLtDDDxrZ   c                 0   t           j        dddddddg}| +|d| gz  }| t          k    r 	|           j        }|r|d	|gz  }||d
|d	 	|          j        gz  }|             }n-t	          |          d         }t	          |          d         |d<   |                    d          dd         \  }}t          |          }t          |          }|dk    r|                    d           d||fcxk    rdk     rn n|                    d           |S )N-mr  wheelz-vvvz	--no-depsz--no-cache-dirz--disable-pip-version-check--index-urlz--trusted-hostz--extra-index-urlpip_versionpython_executabler   r8      
   z--progress-bar=off)      )   rx   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_argsz1get_modules_from_package.<locals>._get_wheel_args,  sk   N)	
  ]I..DM))#8I..7 9-x88D&#_6F))2 D ;**,,KKs))M2K3ii 34DG*0055ac:	9	NN		NN	??KK,---y),6666w66666KK:;;;rZ   Fc                     !|||          | gz   } %|          }t                               dt          j                            |          d                    |                     	  "||                              d          }nX# t          $ rK}	t          |	dd                              d          }t           	                    |           |s Y d }	~	nd }	~	ww xY wt                               d	| 
           g }
g }|
                                }t          |          D ]O\  }}|                                }|                    d          r2|                                }|d         }|
                    |           a|                    d          r@|                                }|d         }t!          |          dk    r3|                    d          r|                    d          r	|d         }nt!          |          dk    r|d                             d          r|                    d          r|d         } &|          j        si||dz                                            }|                    d          r7d|v r3|                    d          d                                         d         }n|}|
                    |           |                    d          r5d|v r1|                                d         }|
                    |           |                    d          r9d|v r5d |                                D             \  }|                    |           Q|
s|
|z  }
t'          t(                              |
                    }
|
s+t           	                    d|           t-          d          t!          |
          dk    r6|
\  }}|                    |          r|
d= n|                    |          r|
d= t!          |
          dk    rt                               d ||
!           |
d         } $t          j                            t          j                            |          d"                    }	 |\  }n# t.          $ r	 |rd#}n Y nw xY w|                    d$          \  }}}|                    d%d&          }|                    d'          s|                    d(          sd)}t          j                            |                                          |                    d*d          d                                         k    r|}nNt          j                            |t          j                            |                    }  ||||          \  }} #||+          }d,                    ||g          }|||d-}|S ).N)dirzwheeling and dealingr   )scratch_dirr   )stderrri   r   outputrZ   zwheel command completed ok)	dist_namezDownloading from URL r/  zDownloading ()   rx   zAdded z to build trackerr   z
Source in zwhich satisfies requirementz from file://c                 <    g | ]}|                     d           |S )zfile://r~   r   xs     r%   r   z9get_modules_from_package.<locals>.get.<locals>.<listcomp>  s)    MMMQ\\)5L5LM!MMMrZ   zcould not find download link)outzfailed to collect distr,  zmore than 1 link collected)rI  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listr3  fromkeysr   
ValueError	partitionr   r   lowerrsplit)'r>  r!  r5  r"  tmpdirignore_errorsr   r;  rI  errrJ  local_linkslinesilinepartslinklast	next_linefirstsecondwhlswhlr   _seprO  r  r  r   r$  r  r   r%  r8  r   r  r   r   r   s'                                  r%   getz%get_modules_from_package.<locals>.getQ  s(    y#??9+Mg&)))"44$ 	 	! 	! 	!	,V6 6 66<fWoo C  	 	 	#x--44W==CNN3      	
 	1YGGG   '' !	) !	)GAt::<<D677 )

QxT""""00 )

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

30B0B20F0L0L0N0NNN!w||K9I9I#9N9NOO#1>#|Y2A$C $C ''vKKKHxxH 566Hcx@@s+   -!B 
C$ACC$R# #R65R6c                    dd l }t                              d            |           }|                                }d |D             \  }d |D             \  }|                    |                              d                                                                          }|                    |                              d          }d |D             }|                    d|j	                  }	|                    d	|j	                  }
|	
                    |          }|

                    |          }|                    d
          }|                    d          }|||fS )Nr   zfinding import names)r   c                 <    g | ]}|                     d           |S )ztop_level.txtrT  rG  s     r%   r   zKget_modules_from_package.<locals>.discover_import_names.<locals>.<listcomp>  s)    AAAAQZZ%@%@AaAAArZ   c                 <    g | ]}|                     d           |S )z.dist-info/METADATAro  rG  s     r%   r   zKget_modules_from_package.<locals>.discover_import_names.<locals>.<listcomp>  s*    GGGAQZZ0E%F%FGaGGGrZ   r   c                 <    g | ]}|                     d           |S )rv   rF  )r   ns     r%   r   zKget_modules_from_package.<locals>.discover_import_names.<locals>.<listcomp>  s)    FFFaALL4E4EFFFFrZ   z^Version: (?P<version>.+)$z^Name: (?P<name>.+)$r   r   )rer!   r  namelistr   rQ  r3   r   compile	MULTILINEsearchgroup)whl_filers  zipfilert  top_level_fnamemetadata_fname	all_namesr  public_namesversion_patternname_patternversion_match
name_matchr   r5   r   s                  r%   discover_import_namesz7get_modules_from_package.<locals>.discover_import_names  si   			+,,,'x(((##%%AAAAA	 HGGGG	LL1188 UWWZZ\\ 	<<//66w??FF9FFF**%B%'\3 3zz"92<HH'..x88!((22
&,,Y77 &&v..^[88rZ   z.whl)r\  r'   NN)r  r  )NNNNF)rz  r   tempfiler   
subprocessr   r   r   r  urllib.parser   urllibr   packaging.requirementsr   rT  r   r'   r:   r   r   r   r   rmtree)packager   rl  r  r\  	file_namer   ry  rv   r   r   import_namesimport_versionrn   r   r   r%  r  r8  r   r  r   r  r   r  r   r  s                 @@@@@@@@@@@@@r%   r   r     s           ////////NNN%%%%%%))))))222222! ! ! ! ! ! !6	 	 	 	 	 		 	 	           "# # # # # #L  h h h h h h h h h h h h hT9 9 9 9 94 WYYF  Ow~~g&& 	((11IVY77I(.)T4HHKHaaHH	 k'""(s76***<1F1F2 2.L.,
M&55rZ   c                      e Zd ZdZeefdZed             Zej	        d             Ze
d             Ze
dd            Ze
dee         fd	            Ze
dee         fd
            Z	 	 ddee         dee         defdZdee         fdZ	 ddeeef         fdZd Z	 ddZd Z	 ddZ	 ddZdeeef         fdZdS )r   z
    plugins manager class
    c                     t          j        d|          }t          j        d|          }t           j                            ||          | _        d S )NMODELSCOPE_CACHEMODELSCOPE_PLUGINS_FILE)r   getenvr'   r   
_file_path)self	cache_dirplugins_files      r%   __init__zPluginsManager.__init__  sC     I0)<<	y!:LII',,y,??rZ   c                     | j         S r  r  r  s    r%   r   zPluginsManager.file_path  s
    rZ   c                     || _         d S r  r  )r  values     r%   r   zPluginsManager.file_path  s    rZ   c                     |                      d          d         dk    rFt          |           \  }}}t                              |          \  }}|r
||k    rd|fS |sd|fS d|fS t                              |           S )a   Check 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?  rj  FT)r   r   r   _check_plugin_installed)r  rv   r   rn   local_installedr   s         r%   r   z%PluginsManager.check_plugin_installed   s     ==b!U**.Fw.O.O+A~|'5'M'M( ($OW &>W#<#<g~%$ &g~%''!99'BBBrZ   Nc                 R   ddl m}  ||           }	 t          j                     t          j                            |j                  }|j        }d}|j        D ]}|	                    |          }|sd} nn# t          j        j
        $ r d}d}Y nw xY w|r|
||k    rd|fS ||fS )Nr   r   TFr   )r  r   rl   rz   r  distributionr   r   	specifiercontainsPackageNotFoundError)	r  verified_versionr   r   distr   r   r   installed_valid_versions	            r%   r  z&PluginsManager._check_plugin_installed  s    666666k'""	')))%2238<<DlG I   *.--*@*@'.  %IE !6 	 	 	GIII	  	&)5:Jg:U:U***g%%s   A%A9 9BBcommand_argsc                 N   t          j                     | dk    r*|                    d           |                    d           t                              |          \  }}t
          j        dd| g|z   }t          j        |          }|j	        }t          j                     |||fS )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.htmlr'  r  )
rl   rz   rf   r   _parse_pip_argsr+   r1  r  run
returncode)commandr  optionsr   cmdr  r   s          r%   r   zPluginsManager.pip_command8  s    ( 	#%%%i%%%S   '66|DD~tUG4|C$$' 	#%%%GT))rZ   c                    ddl m}  |ddg           }g }dddddddd}d}|t          |           k     r| |         }||v r~||         }|d	z   t          |           k     r_|W| |d	z            }t          ||          }	t	          |	t
                    r|	                    |           nt          |||           |d
z  }n0|                    d          r|d	z  }|                    |           |d	z  }|t          |           k     ||fS )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  )-ir)  z-rz--requirementz--srcr  z--find-linksrx   r,  -)	typesr  re   r{   
isinstancerV  rf   setattrr~   )
r  r  r  r   flags_with_valuera  argattrr  currents
             r%   r  zPluginsManager._parse_pip_args`  s{     	*)))))!/
 
 

  & + 
 
 #l####q/C&&&',q53|,,,,' ,QU 3")'4"8"8%gt44 :#NN51111#GT5999FA - $$ !QC   FA) #l####, }rZ   Finstall_argsr!  r0   c                 `   t          |          dk    rdg fS ||d|gz  }|dur|dgz  }t                              d|          \  }}}|dk    r\t                              dd                    |           d	           |                     ||          }|                     |           ||fS )
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   Nr  Fr  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   zPluginsManager.install_plugins  s     |!!b5L T9--Lu$$TF"L%3%?%?&
 &
"Wd
 !KKDsxx~~DDDEEE
 !% 4 4T7 C C$$%6777L((rZ   r   c                 $    g }t          |          dk    rMj        }j        }|D ]<}|t          j                            ||          ddd}|                    |           = fd}|D ]"}	 ||	          }|                    |           #|S )a  
        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                    ddl m} | j        dd}|                     d          d         dk    r ||           j        }                     |           \  }}|r||d<   | |d	<   n#t                              d
|  d           d|d<   |S )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_infoz8PluginsManager.parse_args_info.<locals>.get_package_info  s    $$$$$$$( L !!#&&r*e33#tL116(,(C(C) )%g -*1Y''3V$$]< ] ] ]   +-Y'rZ   )re   r  r   r   r'   r   rf   )
r  r   r  r  r  r   r   r  r  r  s
   ` `       r%   r  zPluginsManager.parse_args_info  s      t99>>oG"/L+ 7 7'7<<==!	    "((6666	  	  	  	  	  	 4  	3 	3G++G44L$$\2222  rZ   uninstall_argsc                     ||dgz  }t                               d|          \  }}}|dk    rFt                              dd                    |           d           |                     |           ||fS )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

        Nz-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_pluginsz PluginsManager.uninstall_plugins  s     tf$N%3%?%?&
 &
"Wd
 !KKFsxx~~FFFGGG
 ))$///N**rZ   c                     t                               d| j                    t          j                            | j                  r4t                              | j                  }t          j	        |          }ni }|S )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_filez%PluginsManager._get_plugins_from_file  sm     	HHHIII7>>$.)) 	$'.||DN'C'C$!%,D!E!E!#!!rZ   c                    |D ]}|                     d          }||v r|s}||         }ddlm}  ||d         dk    r|d         nd          } ||d         dk    r|d         nd          }	|d         }
|d         dk    r|
dk    r|d         }
||	k    r|n|}|
|d<   |d         dk    r|                     |          \  }}||d<   |||<   |S )Nr   r   )parser   r   z0.0.0r   )poppackaging.versionr  r   )r  new_plugins_listr  overrideitemrn   original_itemparse_versionitem_versionorigin_versionr   r   r   s                r%   _update_pluginszPluginsManager._update_plugins&  sG    % 	4 	4D88F++L 111(1 2< @DDDDDD,}'+I"'<'<DOO' K  K!.$Y/255 "),,;B"D "D F| (B..42::(0D+n<<tt-#V I"$$,0,G,G -" -") '")Y/3|,,!!rZ   c           	          t          d                    ddd                     t          d           |                                D ]5\  }}t          d                    ||d         |d                              6d S )	Nz{:<15} |{:<10}  |{:<100}NAMEVERSIONDESCRIPTIONr   z{:<15} |{:<10} |{:<100}r   r   )printformatitems)r  r  kvs       r%   _print_plugins_infoz"PluginsManager._print_plugins_infoH  s    (//	0=? ? 	@ 	@ 	@b			&,,.. 	P 	PDAq+221a	lAfINNOOOO	P 	PrZ   c                 |   |                                  }t          j        t                    }|                     ||          }|du r|                     |           |S g }|D ])}||         d         dk    r|                    |           *|D ]}|                    |           |                     |           |S )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_pluginszPluginsManager.list_pluginsO  s     "88:: "&/?!@!@!112H2DF F t$$%7888%%  & 	0 	0D!$'	2b88"))$///& 	) 	)D""4((((  !3444!!rZ   c                 L   |                                  }|i k    r,|                    t          j        t                               |                     |||          }t          j        |          }t          	                    |
                                | j                   |S )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  z"PluginsManager.update_plugins_filet  s      "88:: ##.> ? ?@@@!11,2DhP P #'*-?"@"@-4466GGG&&rZ   package_namesc                 @   |                                  }t          |          t          u rt          |          }|D ]}||v r|                    |           t          j        |          }t                              |	                                | j
                   |S )zremove the plugins from file
        Args:
            package_names:  package name

        Returns:
            local_plugins_info_json: the json version of updated plugins info

        )r  typer*   rV  r  r  r  r  r   r  r   )r  r  r  r  r  s        r%   r  z'PluginsManager.remove_plugins_from_file  s     "88::#%% //M! 	- 	-D)))"&&t,,,"&*-?"@"@-4466GGG&&rZ   r  )NFF)__name__
__module____qualname____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  rV  r   rZ   r%   r   r     s5        
 /.@ @ @ @   X       C C \C2 & & & \&: %*3i%* %* %* \%*N :d3i : : : \:| 48%*") ")&*3i")#+C=") 03") ") ") ")H9!DI 9! 9! 9! 9!z "'+ +*/T	*:+ + + +<
" 
" 
"  	 "  "  "  "DP P P #" #" #" #"P ' ' ' '<'S$Y'' ' ' ' ' 'rZ   r   c                   R    e Zd ZdZeefdZd Zd Zd Z	d Z
d Zed             Zd	S )
EnvsManagerenvsc                    t          j        d|          }t           j                            |t          j        |          | _        t          ||          }ddlm	}  ||          }|
                    dg           | _        |
                    dd          | _        ddl}|                    d	dd	d
          | _        dS )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  rl  r   r   venv
EnvBuilderenv_builder)r  model_idmodel_revisionr  r   r  cfgr  s           r%   r  zEnvsManager.__init__  s     I0)<<	w||I{/?JJ%hHHH	444444k)$$wwy"--GGNE::??!%	 +  rZ   c                     | j         S r  )r  r  s    r%   get_env_dirzEnvsManager.get_env_dir  s
    |rZ   c                 N    t           j                            | j        dd          S )Nbinactivate)r   r'   r   r  r  s    r%   get_activate_dirzEnvsManager.get_activate_dir  s    w||DL%<<<rZ   c                 @    t          | j                  s| j        rdS dS rr   )re   r   r   r  s    r%   check_if_need_envzEnvsManager.check_if_need_env  s(    t| 	 1 	45rZ   c                 @   t           j                            | j                  st          j        | j                   	 | j                            | j                   d S # t          $ r3}|                                  t          d| j         d|           d }~ww xY w)Nz Failed to create virtual env at z with error: )
r   r'   r   r  r   r  creater   	clean_envr   )r  rj   s     r%   
create_envzEnvsManager.create_env  s    w~~dl++ 	&K%%%	##DL11111 	 	 	NN"Q4<QQaQQ  	s   A   
B*.BBc                     t           j                            | j                  r!| j                            | j                   d S d S r  )r   r'   r   r  r  clear_directoryr  s    r%   r$  zEnvsManager.clean_env  sC    7>>$,'' 	;,,T\:::::	; 	;rZ   c                     dd l } |j        |           \  }}t                              d                    ||                     |dk    r#t          d                    | |                    |S )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_processzEnvsManager.run_process  s    33C88=DDF  	 	 	Q;;>EE! !" " " rZ   N)r  r  r   r   r   r  r  r  r  r!  r%  r$  r  r*  r  rZ   r%   r	  r	    s        D !7.   2  = = =  	 	 	; ; ; 	 	 \	 	 	rZ   r	  __main__z
pai-easycvr  r  r  )r0   N)Jr  r   rl   r  r   r|   r   r  r+   
contextlibr   r   r   r   typingr   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*   ry   r&   r.   r6   r@   rY   rU   rW   rS   r   r   r   r   r   r   r   r   r2  r   objectr   r	  r  import_namer  rn   r  rZ   r%   <module>r7     s  
           				       



 % % % % % %             < < < < < < < < < < < < < < < <  ( ( ( ( ( ( / / / / / / 7 7 7 7 7 7 < < < < < < @ @ @ @ @ @ . . . . . .	
,....00 0 ',,24MNN(  d   / ',,yty{{M9MM   3     $ 3      /	" 	"	"3;C=	" 	" 	" 	"$ $x} $ $ $ $@ S	 T#Y    B# #DI #c # # # #L $s)    6 @D?C7C 7Cs 7C*23s8*<7C*23s8*<7CHL7C 7C 7C 7CtK K K8    "  "  "F%+ %+ %+P
49 
 
 
 
0
6S	 
6 
6 
6 
6 49               +T6 T6 T6n~' ~' ~' ~' ~'V ~' ~' ~'BA A A A A& A A AH z!!8*---0H0H1 1-K rZ   