o
    W+ i                  	   @   s  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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+ ddZ,g Z-edGde.de/fddZ0ede.fddZ1e*fde.dee. fddZ2dHdee. fd d!Z3dHdee. dee. fd"d#Z4dHdee. dee. fd$d%Z5dHdee. fd&d'Z6		dId(e.d)eee.  d*eee.  ddfd+d,Z7d-d. Z8d/d0 Z9d1d2 Z:d3d4 Z;dee. fd5d6Z<d7ee. fd8d9Z=dee. ddfd:d;Z>dGdJd<d=Z?d>Z@d?d@ ZAG dAdB dBeBZCG dCdD dDeBZDeEdEkrZe=dg eAdF\ZFZGZHdS dS )K    N)contextmanagerfnmatchPath)AnyIterableListOptionalSetUnion)snapshot_download)LocalStorage)FilesAstScanning)DEFAULT_MODEL_REVISION)get_modelscope_cache_dir)
get_loggerZmodelscope_modulesz.modelscope_pluginsZadaseqzZProvide hundreds of additions NERs algorithms, check: https://github.com/modelscope/AdaSeq )namedescversionurlz.modelscopepluginsFnew_dirverbosec              
   c   st    t  }|rtd|   t |  zdV  W |r#td|  t | dS |r4td|  t | 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   Zprevious_dir r    d/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/modelscope/utils/plugins.pypushd2   s   
r"   pathc              	   c   sL    t |  } t| } tjd|  zdV  W tj|  dS tj|  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#   r    r    r!   push_python_pathE   s   r)   filenamereturnc                 c   sL    t | }|D ]}| }|r|V  qW d   dS 1 sw   Y  dS )z$
    Discover plugins from file
    N)openstrip)r*   fmodule_namer    r    r!   discover_file_pluginsU   s   
"r0   c                 c   s    t  }| du rTtjtr6td ttD ]}||v rq|V  || qW d   n1 s1w   Y  tjtrPttD ]}||v rGq@|V  || q@dS dS tj| rnt| D ]}||v req^|V  || q^dS dS )za
    Discover plugins

        Args:
        requirement_path: The file path of requirement

    N.)	setr   r#   isfileLOCAL_PLUGINS_FILENAMEr)   r0   addGLOBAL_PLUGINS_FILENAME)requirement_pathr   pluginr    r    r!   discover_pluginsa   s8   
r9   c                 C   sF   t dh dh dd g }|tt |t|  |t  |S )zM
    Imports default plugins, input plugins and file discovered plugins.
    
modelscope>   zmodelscope.metrics.builderzmodelscope.pipelines.builderz modelscope.preprocessors.builderzmodelscope.trainers.builderzmodelscope.models.builder>   zmodelscope.models.*zmodelscope.trainers.*zmodelscope.utilszmodelscope.preprocessors.*zmodelscope.metrics.*zmodelscope.pipelines.*zmodelscope.exporterszmodelscope.msdatasetsincludeexclude)import_module_and_submodulesextendimport_pluginsDEFAULT_PLUGINSimport_file_plugins)r   imported_pluginsr    r    r!   import_all_plugins   s   rD   c                 C   s   g }| du st | dkr|S t }|tjvrtj| | D ]7}zt|h dddhd td| || W q t	yV } zt
d| d	|  W Y d}~qd}~ww |S )
z6
    Imports the plugins listed in the arguments.
    Nr   >   zeasycv.modelszeasycv.toolkit.modelscopezeasycv.toolkitzeasycv.predictorszeasycv.corezeasycv.hookszeasycv.toolkit.*zeasycv.*r;   Plugin %s availablePlugin  could not be loaded: )lenr   r   r&   r#   appendr>   r   r   ModuleNotFoundErrorerror)r   rC   cwdr/   er    r    r!   r@      s,   
	"r@   c                 C   s   g }t  }|tjvrtj| t| D ]1}zt| t	d| || W q t
yF } ztd| d|  W Y d}~qd}~ww |S )z
    Imports the plugins found with `discover_plugins()`.

    Args:
        requirement_path: The file path of requirement

    rE   rF   rG   N)r   r   r&   r#   rI   r9   	importlibimport_moduler   r   rJ   rK   )r7   rC   rL   r/   rM   r    r    r!   rB      s   

"rB   package_namer<   r=   c                 C   s  |r|nt  }|r|nt  }dtdtt dtfdd}|| |s'|| |r'dS t  td zBt| }t|dg }|s@d	n|d
 }t	
|D ]'\}}}	|rV|j|krVqI|dr\qI|drbqI|  d| }
t|
||d qIW nI ty } zt|  dt| d W Y d}~n7d}~w ty } zt|  dt|  t| ddkrtdW Y d}~nd}~ww W d   dS W d   dS W d   dS 1 sw   Y  dS )z?
    Import all public submodules under the given package.
    rP   pattern_setr+   c                 S   s   |D ]
}t | |r dS qdS NTFr   )rP   rQ   patternr    r    r!   fn_in   s
   
z+import_module_and_submodules.<locals>.fn_inNr1   __path__r   r   _testr;   z not imported: z, but should continue   zPackage not installed)r2   r%   r   boolrN   invalidate_cachesr)   rO   getattrpkgutiliter_modulesr#   
startswithr>   
SystemExitr   warning	ExceptionrH   splitrJ   )rP   r<   r=   rT   moduler#   Zpath_stringmodule_finderr   rV   Z
subpackagerM   r    r    r!   r>      sT   



"r>   c           	      C   s   g }t | ddd&}|  }|D ]}|dkrqt|\}}|s'|| qW d   n1 s2w   Y  t|dkrQtd|\}}}|dkrSt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 )	r,   read
splitlinesPluginsManagercheck_plugin_installedrI   rH   pip_commandImportError)	r7   Zinstall_listr.   requirementsreq	installedrV   status_codeargsr    r    r!    install_module_from_requirements  s.   
	
rt   c                 C   s*   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

    )rN   utilspec_from_file_locationmodule_from_specloaderexec_module)r/   	file_pathspecrc   r    r    r!   import_module_from_file8  s   
r|   c                 C   s   dd }t jt|}| D ]K}t j|}t j||}t j|dd t j|d}t j|s4|| t j|t j|}	t j||}
t j|	rRt	|
|	sXt
|
|	 qt  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                 S   s0   t | d	}W d    d S 1 sw   Y  d S )Nw)r,   )rz   rV   r    r    r!   create_empty_fileU  s   "z3create_module_from_files.<locals>.create_empty_fileT)exist_okz__init__.pyN)r   r#   joinBASE_MODULE_DIRdirnamemakedirsexistsbasenamefilecmpcmpshutilcopyfilerN   rZ   )	file_listZfile_prefixr/   r~   dest_dirrz   Zfile_dir
target_dirZ	init_fileZtarget_fileZsrc_filer    r    r!   create_module_from_filesH  s"   r   c           
         s   ddl m} t }|j d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|D }t| | |D ]}|d |dd	tjd }	t|	 qMd
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)Zinclude_initc                    s&   g | ]}|  tjtj d qS )r   )replacerstripr   sep).0file	model_dirr    r!   
<listcomp>  s    z0import_module_from_model_dir.<locals>.<listcomp>r1   z.pyr   N)pathlibr   r   Ztraversal_files	file_dirsZrequirement_dirsinstall_requirements_by_filesr   r&   r#   rI   stemr   r   r   r   r   rN   rO   )
r   r   Zfile_scannerr   ro   r/   ZMODULE_PATHZrelative_file_dirsr   	submoduler    r   r!   import_module_from_model_dirk  s,   	



r   c                 C   sp   t  }g }| D ]}||\}}|s|| q||\}}|dkr6t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)rk   rl   rI   install_pluginsEnvironmentErrorr   )r   Zplugins_managerZuninstalled_pluginsr8   plugin_installedr   statusrV   r    r    r!   install_requirements_by_names  s    	
r   ro   c                 C   s   | D ]}t | q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)rt   )ro   requirementr    r    r!   r     s   	
r   c                 C   sF   | dur!t |  g }| D ]}t|\}}}|| qt| dS dS )z, Try to install and import plugins from repoN)r   get_modules_from_packager?   r@   )r   modulesr8   r/   module_versionrV   r    r    r!   register_plugins_repo  s   r   c                 C   sH   |r"t d|  d zt|  W dS  ty!   t d Y dS 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_remoter    r    r!   register_modelhub_repo  s   
r   zhttps://pypi.org/simple/c              	      sl  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	dfdd	dd fddfdd					d  	fdd	}fdd}	 }| dr	 tj| stj| }tj||}| |dd\}}n| }n	 || }	|	j} || |d}
|
d }||\}}}t| |||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)get_requirementc                    s   |d ur$   }|\}}| j}|d |||  |} |}	n  }	|	j| |d}
|
 }t|d}||
  W d    ||fS 1 sMw   Y  ||fS )N)datawb)	HTTPPasswordMgrWithDefaultRealmnetlocadd_passwordHTTPBasicAuthHandlerbuild_openerr,   r   writeri   )r   r*   r   authpassword_mgrusernamepasswordZtop_level_urlhandleropenerresheadersfp)urllib2r   r    r!   urlretrieve  s    


z-get_modules_from_package.<locals>.urlretrievesha256    c                    s   t |}| }tjd| |d t| dt fdddD ]}|| q W d    n1 s2w   Y  | }tjd|d |S )	Nzcomputing checksumtarget	algorithmrbc                      s
     S N)ri   r    	blocksizer.   r    r!   <lambda>	  s   
 zDget_modules_from_package.<locals>.compute_checksum.<locals>.<lambda>    zcomputed checksum)result)r[   r   debugr,   iterupdate	hexdigest)r   r   r   Zhashtypehash_chunkr   )hashlibr   r!   compute_checksum  s   
z2get_modules_from_package.<locals>.compute_checksumc                  S   s8   zdd l } | jdW S  ty   Y nw dd l}|jS )Nr   pip)importlib.metadatametadatar   ra   r   __version__)rN   r   r    r    r!   _get_pip_version  s   z2get_modules_from_package.<locals>._get_pip_versionc                    s   d }|r |}|j r|jr|j | jkr|j |jf}|r6 |}|j r6|jr6|j | jkr6|j |jf}| ||d\}}||fS )N)r   )r   r   hostname)r   scratch_file	index_urlextra_index_urlr   parsedr   _headers)r   r   r    r!   _download_dist  s&   z0get_modules_from_package.<locals>._download_distc                    s   t jdddddddg}| d ur&|d| g7 }| tkr&| j}|r&|d	|g7 }|d ur5|d
|d	|jg7 }|d u r=  }nt|d }t|d |d< |ddd \}}t|}t|}|dkrg|d d||f  krsdk r{n |S |d |S )Nz-mr   wheelz-vvvz	--no-depsz--no-cache-dirz--disable-pip-version-checkz--index-urlz--trusted-hostz--extra-index-urlpip_versionpython_executabler   r1      
   z--progress-bar=off)      )   rX   z --use-deprecated=legacy-resolver)r&   
executableDEFAULT_INDEXr   dictrb   intrI   )r   envr   rs   r   r   Z	pip_majorZ	pip_minor)r   r   r    r!   _get_wheel_args+  sB   



z1get_modules_from_package.<locals>._get_wheel_argsFc              
      s  |||| g }|d}t jdtj|d|d z| |dd}W n# tyL }	 zt|	ddd}t 	| |sB W Y d }	~	nd }	~	ww t jd	| d
 g }
g }|
 }t|D ]\}}| }|dr{| }|d }|
| q`|dr| }|d }t|dkr|dr|dr|d }n:t|dkr|d dr|dr|d }|js||d   }|drd|v r|dd  d }n|}|
| q`|drd|v r| d }|
| q`|drd|v rdd | D \}|| q`|
s|
|7 }
tt|
}
|
s)t j	d|d tdt|
d krG|
\}}||r>|
d= n	||rG|
d= t|
dkrVt jd!||
d" |
d }tjtj|d#}z|\}W n ty}   |rzd$}n Y nw |d%\}}}|d&d'}|d(s|d)sd*}tj| |d+dd  kr|}ntj|tj|}||||\}}||d,}d-||g}|||d.}|S )/N)dirzwheeling and dealingr   )scratch_dirrs   )stderrrL   rf   outputr   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                 S      g | ]	}| d r|qS )zfile://r^   r   xr    r    r!   r         z9get_modules_from_package.<locals>.get.<locals>.<listcomp>zcould not find download link)outzfailed to collect distr   zmore than 1 link collected)r  linksz*.whlr   #z/%2Bf/z/+f/zmd5=zsha256=md5/r   =)r#   r   checksum)r   r   r   r#   abspathr   decodeChildProcessErrorr[   r`   rj   	enumerater-   r^   rb   rI   rH   endswithschemelistr   fromkeysra   
ValueError	partitionr   r   lowerrsplit)r   r   r   r   tmpdirignore_errorsrs   r   r  errr  Zlocal_linkslinesilinepartslinklast	next_linefirstsecondZwhlswhlr   Z_sepr  r   r   r   r   r   )r   r   r   r   r   r   r   r   r    r!   getP  s   








&z%get_modules_from_package.<locals>.getc                    s   dd l }td  | d}| }dd |D \}dd |D \}||d  }||d}dd |D }|d	|j	}	|d
|j	}
|	
|}|

|}|d}|d}|||fS )Nr   zfinding import names)r   c                 S   r   )ztop_level.txtr  r  r    r    r!   r     r  zKget_modules_from_package.<locals>.discover_import_names.<locals>.<listcomp>c                 S   r   )z.dist-info/METADATAr&  r  r    r    r!   r     r  rf   c                 S   s   g | ]	}| d s|qS )rV   r  )r   nr    r    r!   r     r  z^Version: (?P<version>.+)$z^Name: (?P<name>.+)$r   r   )rer   r   namelistri   r  r-   rj   compile	MULTILINEsearchgroup)whl_filer(  zipfiler)  Ztop_level_fnameZmetadata_fnameZ	all_namesr   Zpublic_namesZversion_patternZname_patternZversion_matchZ
name_matchr   r/   r   r    r!   discover_import_names  s0   







z7get_modules_from_package.<locals>.discover_import_namesz.whl)r  r#   NN)r   r   )NNNNF)r/  r   tempfiler   
subprocessr   r   r   r   urllib.parser   urllibr   pip._internal.utils.packagingr   r  r   r#   r3   r   r   r   r   rmtree)packager   r%  r0  r  	file_namerz   r.  rV   rp   r   Zimport_namesimport_versionrP   r    )r   r   r   r   r   r   r   r   r   r   r   r   r   r!   r     sL   &j



r   c                   @   s  e Zd ZdZeefddZedd Zej	dd Ze
dd Ze
d(d
dZe
dee fddZ			d)dee dee defddZdee fddZ	d*deeef fddZdd Z	d*ddZdd  Z	d*d!d"Z	d*d#d$Zd%eeef fd&d'Zd	S )+rk   z
    plugins manager class
    c                 C   s,   t d|}t d|}t j||| _d S )NMODELSCOPE_CACHEZMODELSCOPE_PLUGINS_FILE)r   getenvr#   r   
_file_path)self	cache_dirZplugins_filer    r    r!   __init__  s   zPluginsManager.__init__c                 C      | j S r   r=  r>  r    r    r!   rz     s   zPluginsManager.file_pathc                 C   s
   || _ d S r   rB  )r>  valuer    r    r!   rz     s   
c                 C   s`   |  dd dkr+t| \}}}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

        r1   r   r$  FT)rb   r   rk   _check_plugin_installed)r8  rV   r   rP   Zlocal_installedr   r    r    r!   rl     s   
z%PluginsManager.check_plugin_installedNc           
      C   s   ddl m}m} || }z$tt tjj|j }|j	}d}|j
D ]}||}	|	s.d} nq!W n ty=   d}d}Y nw |rL|d urL||krLd|fS ||fS )Nr   )r   
specifiersTFr   )r6  r   rF  rN   reloadpkg_resourcesworking_setby_keyr   r   	specifiercontainsr   )
r8  Zverified_versionr   rF  rp   Zpackage_meta_infor   rq   r{   Zinstalled_valid_versionr    r    r!   rE    s*   


z&PluginsManager._check_plugin_installedcommand_argsc                 C   sf   ddl m} tt | dkr|d |d || } | |\}}| |}tt |||fS )a  

        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_commandrh   -fzAhttps://modelscope.oss-cn-beijing.aliyuncs.com/releases/repo.html)pip._internal.commandsrN  rN   rG  rH  rI   
parse_argsmain)commandrM  rN  optionsrs   rr   r    r    r!   rm   6  s   




zPluginsManager.pip_commandFinstall_argsr   r+   c                 C   s   t |dkr
dg fS |dur|d|g7 }|dur|dg7 }td|\}}}|dkrAt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   Nz-iFrO  rh   The plugins r   z is installed)rH   rk   rm   r   r   r   parse_args_infoupdate_plugins_file)r>  rU  r   Zforce_updaterr   rT  rs   installed_packager    r    r!   r   Y  s   


zPluginsManager.install_pluginsrs   c           
         sv   g }t |dkr$ j} j}|D ]}|tj||ddd}|| q fdd}|D ]}	||	}|| q-|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                    sz   ddl m} |  jdd}| dd dkr|| j} | \}}|r.||d< | |d	< |S td
|  d d|d< |S )Nr   r   r   )r   r   r   r1   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   rb   r   rl   r   r`   )rP   r   package_infor   r   rT  r>  r    r!   get_package_info  s&   

z8PluginsManager.parse_args_info.<locals>.get_package_info)rH   src_dirZrequirmentsr   r#   r   rI   )
r>  rs   rT  rY  r^  ro   r   r[  r]  r8  r    r\  r!   rW  }  s    zPluginsManager.parse_args_infouninstall_argsc                 C   sV   |dur	|dg7 }t d|\}}}|dkr't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   rV  r   z is uninstalled)rk   rm   r   r   r   remove_plugins_from_file)r>  r_  Zis_yesrr   rT  rs   r    r    r!   uninstall_plugins  s   


z PluginsManager.uninstall_pluginsc                 C   sB   t d| j  tj| jrt| j}t	|}|S i }|S )z  get plugins from file

        z!Loading plugins information from )
r   r   rz   r   r#   r   storageri   jsonloads)r>  Zlocal_plugins_info_byteslocal_plugins_infor    r    r!   _get_plugins_from_file  s   
z%PluginsManager._get_plugins_from_filec                 C   s   |D ]f}| d}||v rS|sS|| }ddlm} ||d dkr$|d nd}||d dkr2|d nd}	|d }
|d dkrG|
dkrG|d }
||	krM|n|}|
|d< |d dkrd| |\}}||d< |||< q|S )Nr   r   )parse_versionr   r   z0.0.0r   )poprH  rh  rl   )r>  Znew_plugins_listrf  overrideitemrP   Zoriginal_itemrh  Zitem_versionZorigin_versionr   r   r   r    r    r!   _update_plugins  s2   

zPluginsManager._update_pluginsc                 C   sJ   t dddd t d | D ]\}}t d||d |d  qd S )	Nz{:<15} |{:<10}  |{:<100}NAMEVERSIONZDESCRIPTIONr   z{:<15} |{:<10} |{:<100}r   r   )printformatitems)r>  rf  kvr    r    r!   _print_plugins_info  s   
z"PluginsManager._print_plugins_infoc                 C   s~   |   }tt}| ||}|du r| | |S g }|D ]}|| d dkr-|| q|D ]}|| q0| | |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   )rg  copydeepcopyOFFICIAL_PLUGINSrl  rt  rI   ri  )r>  Zshow_allrf  Zlocal_official_pluginsZnot_installed_listrk  r    r    r!   list_plugins  s"   



zPluginsManager.list_pluginsc                 C   sN   |   }|i kr|tt | |||}t|}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

        )rg  r?   ru  rv  rw  rl  rd  dumpsrc  r   encoderz   )r>  Zplugins_listrj  rf  local_plugins_info_jsonr    r    r!   rX  0  s   
z"PluginsManager.update_plugins_filepackage_namesc                 C   sX   |   }t|tu rt|}|D ]}||v r|| qt|}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

        )rg  typer%   r  ri  rd  ry  rc  r   rz  rz   )r>  r|  rf  rk  r{  r    r    r!   ra  N  s   

z'PluginsManager.remove_plugins_from_filer   )NFF)__name__
__module____qualname____doc__MODELSCOPE_FILE_DIRPLUGINS_FILENAMEr@  propertyrz   setterstaticmethodrl   rE  r	   r%   rm   r
   r   r   rW  r   rb  rg  rl  rt  rx  rX  r  ra  r    r    r    r!   rk     sT    



$
$=


"	
(

rk   c                   @   sR   e Zd ZdZeefddZdd Zdd Zdd	 Z	d
d Z
dd Zedd ZdS )EnvsManagerZenvsc                 C   s|   t d|}t j|tj|| _t||d}ddlm	} ||}|
dg | _|
dd| _ddl}|jd	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)Zsystem_site_packagesclearsymlinksZwith_pip)r   r<  r#   r   r  r   env_dirr   Zmodelscope.utils.hubr  r%  r   r   venvZ
EnvBuilderenv_builder)r>  Zmodel_idZmodel_revisionr?  r   r  cfgr  r    r    r!   r@  l  s   zEnvsManager.__init__c                 C   rA  r   )r  rC  r    r    r!   get_env_dir  s   zEnvsManager.get_env_dirc                 C   s   t j| jddS )Nbinactivate)r   r#   r   r  rC  r    r    r!   get_activate_dir  s   zEnvsManager.get_activate_dirc                 C   s   t | js| jr
dS dS rR   )rH   r   r   rC  r    r    r!   check_if_need_env  s   zEnvsManager.check_if_need_envc              
   C   sf   t j| jst | j z
| j| j W d S  ty2 } z|   t	d| j d| d }~ww )Nz Failed to create virtual env at z with error: )
r   r#   r   r  r   r  createra   	clean_envr   )r>  rM   r    r    r!   
create_env  s   zEnvsManager.create_envc                 C   s$   t j| jr| j| j d S d S r   )r   r#   r   r  r  Zclear_directoryrC  r    r    r!   r    s   zEnvsManager.clean_envc                 C   sD   dd l }|| \}}td|| |dkr td| ||S )Nr   z&The status and the results are: {}, {}z,running the cmd: {} failed, with message: {})r3  getstatusoutputr   r   rp  ra   )cmdr3  r   r   r    r    r!   run_process  s   zEnvsManager.run_processN)r  r  r  r   r   r  r@  r  r  r  r  r  r  r  r    r    r    r!   r  i  s    
r  __main__z
pai-easycvr~  r   r1  )r+   N)Iru  r   rN   r   r\   r   r&   
contextlibr   r   r   r   typingr   r   r	   r
   r   r   rd  rH  r:   r   Zmodelscope.fileio.filer   Zmodelscope.utils.ast_utilsr   Zmodelscope.utils.constantr   Zmodelscope.utils.file_utilsr   Zmodelscope.utils.loggerr   r   rc  r  ZMODELSCOPE_DYNAMIC_MODULEr#   r   r   r  rw  r4   homer6   rA   r%   rY   r"   r)   r0   r9   rD   r@   rB   r>   rt   r|   r   r   r   r   r   r   r   r   objectrk   r  r  import_namer:  rP   r    r    r    r!   <module>   s    

 !&


:#(     
D
