From fa1307e62c1af2190a7adcde0fd1d8616df70065 Mon Sep 17 00:00:00 2001 From: allegroai <> Date: Sun, 23 Jan 2022 10:40:05 +0200 Subject: [PATCH] Add agent.poetry_version to specify poetry version (and force installation of poetry if missing) --- .../backend_api/config/default/agent.conf | 3 +++ clearml_agent/commands/worker.py | 4 +-- clearml_agent/helper/package/poetry_api.py | 27 +++++++++++++++++++ docs/clearml.conf | 3 +++ 4 files changed, 35 insertions(+), 2 deletions(-) diff --git a/clearml_agent/backend_api/config/default/agent.conf b/clearml_agent/backend_api/config/default/agent.conf index 9d07cdc..73ef530 100644 --- a/clearml_agent/backend_api/config/default/agent.conf +++ b/clearml_agent/backend_api/config/default/agent.conf @@ -11,6 +11,7 @@ # Set GIT user/pass credentials (if user/pass are set, GIT protocol will be set to https) # leave blank for GIT SSH credentials (set force_git_ssh_protocol=true to force SSH protocol) + # Notice: GitHub personal token is equivalent to password, you can put it directly into `git_pass` # git_user: "" # git_pass: "" # git_host: "" @@ -48,6 +49,8 @@ # specify pip version to use (examples "<20", "==19.3.1", "", empty string will install the latest version) pip_version: "<20.2", + # specify poetry version to use (examples "<2", "==1.1.1", "", empty string will install the latest version) + # poetry_version: "<2", # virtual environment inheres packages from system system_site_packages: false, diff --git a/clearml_agent/commands/worker.py b/clearml_agent/commands/worker.py index 6beb168..9aecf8e 100644 --- a/clearml_agent/commands/worker.py +++ b/clearml_agent/commands/worker.py @@ -2602,8 +2602,8 @@ class Worker(ServiceCommandSection): print('Poetry Enabled: Ignoring requested python packages, using repository poetry lock file!') api.install() return api - except Exception: - self.log.error("failed installing poetry requirements:") + except Exception as ex: + self.log.error("failed installing poetry requirements: {}".format(ex)) return None def install_requirements( diff --git a/clearml_agent/helper/package/poetry_api.py b/clearml_agent/helper/package/poetry_api.py index a32b176..fa1a5fc 100644 --- a/clearml_agent/helper/package/poetry_api.py +++ b/clearml_agent/helper/package/poetry_api.py @@ -5,6 +5,7 @@ import attr import sys import os from pathlib2 import Path + from clearml_agent.helper.process import Argv, DEVNULL, check_if_command_exists from clearml_agent.session import Session, POETRY @@ -81,6 +82,32 @@ class PoetryConfig: @_guard_enabled def initialize(self, cwd=None): if not self._initialized: + if self.session.config.get("agent.package_manager.poetry_version", None) is not None: + version = str(self.session.config.get("agent.package_manager.poetry_version")) + print('Upgrading Poetry package {}'.format(version)) + # first upgrade pip if we need to + try: + from clearml_agent.helper.package.pip_api.venv import VirtualenvPip + pip = VirtualenvPip( + session=self.session, python=self._python, + requirements_manager=None, path=None, interpreter=self._python) + pip.upgrade_pip() + except Exception as ex: + self.log.warning("failed upgrading pip: {}".format(ex)) + + # now install poetry + try: + version = version.replace(' ', '') + if ('=' in version) or ('~' in version) or ('<' in version) or ('>' in version): + version = version + elif version: + version = "==" + version + argv = Argv(self._python, "-m", "pip", "install", "poetry{}".format(version), + "--upgrade", "--disable-pip-version-check") + print(argv.get_output()) + except Exception as ex: + self.log.warning("failed upgrading poetry: {}".format(ex)) + self._initialized = True try: self._config("--local", "virtualenvs.in-project", "true", cwd=cwd) diff --git a/docs/clearml.conf b/docs/clearml.conf index 24667aa..b8e6ae0 100644 --- a/docs/clearml.conf +++ b/docs/clearml.conf @@ -15,6 +15,7 @@ api { agent { # Set GIT user/pass credentials (if user/pass are set, GIT protocol will be set to https) # leave blank for GIT SSH credentials (set force_git_ssh_protocol=true to force SSH protocol) + # Notice: GitHub personal token is equivalent to password, you can put it directly into `git_pass` git_user="" git_pass="" # Limit credentials to a single domain, for example: github.com, @@ -60,6 +61,8 @@ agent { # specify pip version to use (examples "<20", "==19.3.1", "", empty string will install the latest version) # pip_version: "<20" + # specify poetry version to use (examples "<2", "==1.1.1", "", empty string will install the latest version) + # poetry_version: "<2", # virtual environment inheres packages from system system_site_packages: false,