2020-10-15 20:23:46 +00:00
|
|
|
from typing import Any
|
|
|
|
|
2019-10-25 19:28:44 +00:00
|
|
|
from pathlib2 import Path
|
|
|
|
|
2020-12-22 21:00:57 +00:00
|
|
|
from clearml_agent.helper.base import select_for_platform, rm_tree, ExecutionInfo
|
|
|
|
from clearml_agent.helper.package.base import PackageManager
|
|
|
|
from clearml_agent.helper.process import Argv, PathLike
|
|
|
|
from clearml_agent.session import Session
|
2019-10-25 19:28:44 +00:00
|
|
|
from ..pip_api.system import SystemPip
|
|
|
|
from ..requirements import RequirementsManager
|
|
|
|
|
|
|
|
|
|
|
|
class VirtualenvPip(SystemPip, PackageManager):
|
2020-10-15 20:23:46 +00:00
|
|
|
def __init__(self, session, python, requirements_manager, path, interpreter=None, execution_info=None, **kwargs):
|
|
|
|
# type: (Session, float, RequirementsManager, PathLike, PathLike, ExecutionInfo, Any) -> ()
|
2019-10-25 19:28:44 +00:00
|
|
|
"""
|
|
|
|
Program interface to virtualenv pip.
|
|
|
|
Must be given either path to virtualenv or source command.
|
|
|
|
Either way, ``self.source`` is exposed.
|
2020-05-31 11:02:42 +00:00
|
|
|
:param session: a Session object for communication
|
2019-10-25 19:28:44 +00:00
|
|
|
:param python: interpreter path
|
|
|
|
:param path: path of virtual environment to create/manipulate
|
|
|
|
:param python: python version
|
|
|
|
:param interpreter: path of python interpreter
|
|
|
|
"""
|
|
|
|
super(VirtualenvPip, self).__init__(
|
2020-05-31 11:02:42 +00:00
|
|
|
session=session,
|
|
|
|
interpreter=interpreter or Path(
|
|
|
|
path, select_for_platform(linux="bin/python", windows="scripts/python.exe"))
|
2019-10-25 19:28:44 +00:00
|
|
|
)
|
|
|
|
self.path = path
|
|
|
|
self.requirements_manager = requirements_manager
|
2019-11-08 20:36:24 +00:00
|
|
|
self.python = python
|
2019-10-25 19:28:44 +00:00
|
|
|
|
|
|
|
def _make_command(self, command):
|
2020-01-27 14:23:15 +00:00
|
|
|
return self.session.command(self.bin, "-m", "pip", "--disable-pip-version-check", *command)
|
2019-10-25 19:28:44 +00:00
|
|
|
|
|
|
|
def load_requirements(self, requirements):
|
|
|
|
if isinstance(requirements, dict) and requirements.get("pip"):
|
|
|
|
requirements["pip"] = self.requirements_manager.replace(requirements["pip"])
|
|
|
|
super(VirtualenvPip, self).load_requirements(requirements)
|
2020-06-17 22:55:14 +00:00
|
|
|
self.requirements_manager.post_install(self.session)
|
2019-10-25 19:28:44 +00:00
|
|
|
|
|
|
|
def create_flags(self):
|
|
|
|
"""
|
|
|
|
Configurable environment creation arguments
|
|
|
|
"""
|
|
|
|
return Argv.conditional_flag(
|
|
|
|
self.session.config["agent.package_manager.system_site_packages"],
|
|
|
|
"--system-site-packages",
|
2022-02-27 09:25:25 +00:00
|
|
|
) + ("--python", self._bin)
|
2019-10-25 19:28:44 +00:00
|
|
|
|
|
|
|
def install_flags(self):
|
|
|
|
"""
|
|
|
|
Configurable package installation creation arguments
|
|
|
|
"""
|
|
|
|
return super(VirtualenvPip, self).install_flags() + Argv.conditional_flag(
|
|
|
|
self.session.config["agent.package_manager.force_upgrade"], "--upgrade"
|
|
|
|
)
|
|
|
|
|
|
|
|
def create(self):
|
|
|
|
"""
|
|
|
|
Create virtualenv.
|
|
|
|
Only valid if instantiated with path.
|
|
|
|
Use self.python as self.bin does not exist.
|
|
|
|
"""
|
2022-02-27 09:25:25 +00:00
|
|
|
# Log virtualenv information to stdout
|
|
|
|
self.session.command(
|
|
|
|
self.python, "-m", "virtualenv", "--version"
|
|
|
|
)
|
2019-10-25 19:28:44 +00:00
|
|
|
self.session.command(
|
|
|
|
self.python, "-m", "virtualenv", self.path, *self.create_flags()
|
|
|
|
).check_call()
|
|
|
|
return self
|
|
|
|
|
|
|
|
def remove(self):
|
|
|
|
"""
|
|
|
|
Delete virtualenv.
|
|
|
|
Only valid if instantiated with path.
|
|
|
|
"""
|
|
|
|
rm_tree(self.path)
|