o
    ॵig                  	   @   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_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              
   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   previous_dir r#   L/home/ubuntu/.local/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   
"r3   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,   r3   addGLOBAL_PLUGINS_FILENAME)requirement_pathr   pluginr#   r#   r$   discover_pluginsa   s8   
r<   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>   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_pluginsr#   r#   r$   import_all_plugins   s   rT   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   >   easycv.coreeasycv.hookseasycv.modelseasycv.toolkiteasycv.predictorseasycv.toolkit.modelscopezeasycv.toolkit.*zeasycv.*rK   Plugin %s availablePlugin  could not be loaded: )lenr   r   r)   r&   appendrN   r   r    ModuleNotFoundErrorerror)r   rS   cwdr2   er#   r#   r$   rP      s,   
	"rP   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

    r[   r\   r]   N)r   r   r)   r&   r_   r<   	importlibimport_moduler   r    r`   ra   )r:   rS   rb   r2   rc   r#   r#   r$   rR      s   

"rR   package_namerL   rM   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.
    rf   pattern_setr.   c                 S   s   |D ]
}t | |r dS qdS NTFr   )rf   rg   patternr#   r#   r$   fn_in   s
   
z+import_module_and_submodules.<locals>.fn_inNr4   __path__r   r   _testrK   z not imported: z, but should continue   zPackage not installed)r5   r(   r   boolrd   invalidate_cachesr,   re   getattrpkgutiliter_modulesr&   
startswithrN   
SystemExitr   warning	Exceptionr^   splitr`   )rf   rL   rM   rj   moduler&   path_stringmodule_finderr   rl   
subpackagerc   r#   r#   r$   rN      sT   



"rN   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_installedr_   r^   pip_commandImportError)	r:   install_listr1   requirementsreq	installedrl   status_codeargsr#   r#   r$    install_module_from_requirements  s.   
	
r   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

    )rd   utilspec_from_file_locationmodule_from_specloaderexec_module)r2   	file_pathspecry   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/   )r   rl   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copyfilerd   rp   )	file_listfile_prefixr2   r   dest_dirr   file_dir
target_dir	init_filetarget_file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)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>r4   z.pyr   N)pathlibr   r   traversal_files	file_dirsrequirement_dirsinstall_requirements_by_filesr   r)   r&   r_   stemr   r   r   r   r   rd   re   )
r   r   file_scannerr   r   r2   MODULE_PATH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)r   r   r_   install_pluginsEnvironmentErrorr   )r   plugins_manageruninstalled_pluginsr;   plugin_installedr   statusrl   r#   r#   r$   install_requirements_by_names  s    	
r   r   c                 C   s   | D ]}t | qdS )z install the requriements by files

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

     No returns, raise error if failed

    N)r   )r   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_packagerO   rP   )r   modulesr;   r2   module_versionrl   r#   r#   r$   register_plugins_repo  s   r   c                 C   s6   |r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;Multi component keys in the hub are registered in same fileN)r   KeyErrorr   rv   )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    writer   )r   r-   r   authpassword_mgrusernamepassword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)r   r#   	blocksizer1   r#   r$   <lambda>  s   
 zDget_modules_from_package.<locals>.compute_checksum.<locals>.<lambda>    zcomputed checksum)result)rq   r   debugr/   iterupdate	hexdigest)r   r   r   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   rw   r  __version__)rd   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   r4      
   z--progress-bar=off)      )   rn   z --use-deprecated=legacy-resolver)r)   
executableDEFAULT_INDEXr  dictrx   intr_   )r
  envr  r   r  r  	pip_major	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_dirr   )stderrrb   r~   outputr   zwheel command completed ok)	dist_namezDownloading from URL r  zDownloading ()   rn   zAdded z to build trackerr   z
Source in zwhich satisfies requirementz from file://c                 S      g | ]	}| d r|qS )zfile://rt   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ChildProcessErrorrq   rv   r   	enumerater0   rt   rx   r_   r^   endswithschemelistr  fromkeysrw   
ValueError	partitionr   r   lowerrsplit)r#  r
  r  r  tmpdirignore_errorsr   r   r/  errr0  local_linkslinesilinepartslinklast	next_linefirstsecondwhlswhlr   _sepr5  r   r   r	  r  r   )r   r  r  r   r  r   r   r   r#   r$   getM  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/METADATArS  r,  r#   r#   r$   r     r.  r~   c                 S   s   g | ]	}| d s|qS )rl   r+  )r   nr#   r#   r$   r     r.  z^Version: (?P<version>.+)$z^Name: (?P<name>.+)$r   r   )rer   r   namelistr   r7  r0   r   compile	MULTILINEsearchgroup)whl_filerU  zipfilerV  top_level_fnamemetadata_fname	all_namesr  public_namesversion_patternname_patternversion_match
name_matchr   r2   r   r#   r$   discover_import_names  s0   







z7get_modules_from_package.<locals>.discover_import_namesz.whl)rB  r&   NN)r   r   )NNNNF)r\  r   tempfiler   
subprocessr   r   r   r  urllib.parser   urllibr   pip._internal.utils.packagingr   r:  r   r&   r6   r   r   r   r   rmtree)packager   rR  re  rB  	file_namer   r[  rl   r   r   import_namesimport_versionrf   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 )+r   z
    plugins manager class
    c                 C   s,   t d|}t d|}t j||| _d S )NMODELSCOPE_CACHEMODELSCOPE_PLUGINS_FILE)r   getenvr&   r   
_file_path)self	cache_dirplugins_filer#   r#   r$   __init__  s   zPluginsManager.__init__c                 C      | j S r   rt  ru  r#   r#   r$   r     s   zPluginsManager.file_pathc                 C   s
   || _ d S r   rz  )ru  valuer#   r#   r$   r     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

        r4   r$  rP  FT)rx   r   r   _check_plugin_installed)rm  rl   r   rf   local_installedr   r#   r#   r$   r     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   )rk  r   r  rd   reloadpkg_resourcesworking_setby_keyr   r   	specifiercontainsr   )
rm  verified_versionr   r  r   package_meta_infor   r   r   installed_valid_versionr#   r#   r$   r}    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_commandr   -fzAhttps://modelscope.oss-cn-beijing.aliyuncs.com/releases/repo.html)pip._internal.commandsr  rd   r  r  r_   
parse_argsmain)commandr  r  optionsr   r   r#   r#   r$   r   3  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-iFr  r   The plugins r   z is installed)r^   r   r   r   r    r   parse_args_infoupdate_plugins_file)ru  r  r
  force_updater   r  r   installed_packager#   r#   r$   r   V  s   


zPluginsManager.install_pluginsr   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   r4   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
  rx   r   r   r   rv   )rf   r   package_infor   r   r  ru  r#   r$   get_package_info  s&   

z8PluginsManager.parse_args_info.<locals>.get_package_info)r^   src_dirrequirmentsr   r&   r   r_   )
ru  r   r  r  r  r   r   r  r  rm  r#   r  r$   r  z  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   r  r   z is uninstalled)r   r   r   r    r   remove_plugins_from_file)ru  r  is_yesr   r  r   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    r   r   r&   r   storager   jsonloads)ru  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   )popr  r  r   )ru  new_plugins_listr  overrideitemrf   original_itemr  item_version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VERSIONDESCRIPTIONr   z{:<15} |{:<10} |{:<100}r   r   )printformatitems)ru  r  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   )r  copydeepcopyOFFICIAL_PLUGINSr  r  r_   r  )ru  show_allr  local_official_pluginsnot_installed_listr  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

        )r  rO   r  r  r  r  r  dumpsr  r   encoder   )ru  plugins_listr  r  local_plugins_info_jsonr#   r#   r$   r  -  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

        )r  typer(   r<  r  r  r  r  r   r  r   )ru  r  r  r  r  r#   r#   r$   r  K  s   

z'PluginsManager.remove_plugins_from_filer   )NFF)__name__
__module____qualname____doc__MODELSCOPE_FILE_DIRPLUGINS_FILENAMErx  propertyr   setterstaticmethodr   r}  r	   r(   r   r
   r   r   r  r   r  r  r  r  r  r  r<  r  r#   r#   r#   r$   r     sT    



$
$=


"	
(

r   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 )EnvsManager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
        rq  )revisionr   )read_configr   r   FNT)system_site_packagesclearsymlinkswith_pip)r   rs  r&   r   r  r   env_dirr   modelscope.utils.hubr  rR  r   r   venv
EnvBuilderenv_builder)ru  model_idmodel_revisionrv  r   r  cfgr  r#   r#   r$   rx  i  s   zEnvsManager.__init__c                 C   ry  r   )r  r{  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  r{  r#   r#   r$   get_activate_dir  s   zEnvsManager.get_activate_dirc                 C   s   t | js| jr
dS dS rh   )r^   r   r   r{  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  createrw   	clean_envr   )ru  rc   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  clear_directoryr{  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: {})rh  getstatusoutputr   r   r  rw   )cmdrh  r   r   r#   r#   r$   run_process  s   zEnvsManager.run_processN)r  r  r  r   r   r  rx  r  r  r  r  r  r  r  r#   r#   r#   r$   r  f  s    
r  __main__z
pai-easycvr  r   rf  )r.   N)Ir  r   rd   r   rr   r   r)   
contextlibr   r   r   r   typingr   r   r	   r
   r   r   r  r  r=   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  r7   homer9   rQ   r(   ro   r%   r,   r3   r<   rT   rP   rR   rN   r   r   r   r   r   r   r   r   r  r   objectr   r  r  import_namerp  rf   r#   r#   r#   r$   <module>   s    

 !&


:#(     
D
