Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CTX-5485: Changes regarding decision to not use anymore coretex cli tool which was installed via pip. #249

Draft
wants to merge 15 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 15 additions & 26 deletions coretex/cli/main.py → cli_entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@

from importlib.metadata import version as getLibraryVersion

import sys
import click

from .commands.login import login
from .commands.model import model
from .commands.node import node
from .commands.task import run
from .commands.project import project
from coretex.cli.commands.login import login
from coretex.cli.commands.model import model
from coretex.cli.commands.node import node
from coretex.cli.commands.task import run
from coretex.cli.commands.project import project

from .modules import ui, utils
from .modules.intercept import ClickExceptionInterceptor
from ..utils.process import CommandException
from coretex.cli.modules import ui, utils
from coretex.cli.modules.intercept import ClickExceptionInterceptor


@click.command()
Expand All @@ -38,24 +38,10 @@ def version() -> None:

@click.command()
def update() -> None:
currentVersion = utils.fetchCurrentVersion()
latestVersion = utils.fetchLatestVersion()

if currentVersion is None or latestVersion is None:
return

if latestVersion > currentVersion:
try:
ui.progressEcho("Updating coretex...")
utils.updateLib()
ui.successEcho(
f"Coretex successfully updated from {utils.formatCliVersion(currentVersion)} "
f"to {utils.formatCliVersion(latestVersion)}!"
)
except CommandException:
ui.errorEcho("Failed to update coretex.")
else:
ui.stdEcho("Coretex version is up to date.")
ui.stdEcho(
"This command isn't implemented yet for compiled version but will be available soon."
"Thanks for your patience!"
)


@click.group(cls = ClickExceptionInterceptor)
Expand All @@ -70,3 +56,6 @@ def cli() -> None:
cli.add_command(run)
cli.add_command(version)
cli.add_command(update)

if getattr(sys, 'frozen', False):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add comment explaining why this is needed

cli(sys.argv[1:])
5 changes: 2 additions & 3 deletions coretex/cli/commands/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
from ..modules import node as node_module
from ..modules.node import NodeStatus
from ..modules.user import initializeUserSession
from ..modules.utils import onBeforeCommandExecute, checkEnvironment
from ..modules.utils import onBeforeCommandExecute
from ..modules.update import activateAutoUpdate, getNodeStatus
from ...utils import docker
from ...configuration import UserConfiguration, NodeConfiguration, InvalidConfiguration, ConfigurationNotFound
from ...configuration import NodeConfiguration, InvalidConfiguration, ConfigurationNotFound


@click.command()
Expand Down Expand Up @@ -183,7 +183,6 @@ def config(advanced: bool) -> None:
@onBeforeCommandExecute(docker.isDockerAvailable)
@onBeforeCommandExecute(initializeUserSession)
@onBeforeCommandExecute(node_module.checkResourceLimitations)
@onBeforeCommandExecute(checkEnvironment)
def node() -> None:
pass

Expand Down
2 changes: 1 addition & 1 deletion coretex/cli/commands/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from ..modules.project_utils import getProject
from ..._folder_manager import folder_manager
from ..._task import TaskRunWorker, executeRunLocally, readTaskConfig, runLogger
from ...configuration import UserConfiguration, NodeConfiguration
from ...configuration import UserConfiguration
from ...entities import TaskRun, TaskRunStatus
from ...resources import PYTHON_ENTRY_POINT_PATH
from ..._task import TaskRunWorker, executeRunLocally, readTaskConfig, runLogger
Expand Down
4 changes: 2 additions & 2 deletions coretex/cli/modules/cron.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ def jobExists(script: str) -> bool:
return any(script in line for line in existingLines)


def scheduleJob(scriptName: str) -> None:
def scheduleJob(commandName: str) -> None:
existingLines = getExisting()
cronJob = f"*/30 * * * * {CONFIG_DIR / scriptName} >> {CONFIG_DIR}/logs/ctx_autoupdate.log 2>&1\n"
cronJob = f"*/30 * * * * {commandName} >> {CONFIG_DIR}/logs/ctx_autoupdate.log 2>&1\n"
existingLines.append(cronJob)

tempCronFilePath = CONFIG_DIR / "temp.cron"
Expand Down
2 changes: 1 addition & 1 deletion coretex/cli/modules/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ def promptRam(ramLimit: int) -> int:
if nodeRam < config_defaults.MINIMUM_RAM:
ui.errorEcho(
f"ERROR: Configured RAM ({nodeRam}GB) is lower than "
"the minimum Node RAM requirement ({config_defaults.MINIMUM_RAM}GB)."
f"the minimum Node RAM requirement ({config_defaults.MINIMUM_RAM}GB)."
)
return promptRam(ramLimit)

Expand Down
34 changes: 3 additions & 31 deletions coretex/cli/modules/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,10 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>.

from enum import IntEnum
from pathlib import Path

import requests

from .utils import getExecPath
from .cron import jobExists, scheduleJob
from ..resources import RESOURCES_DIR
from ...utils import command
from ...configuration import DEFAULT_VENV_PATH


UPDATE_SCRIPT_NAME = "update_node.sh"
Expand All @@ -39,34 +34,11 @@ class NodeStatus(IntEnum):
reconnecting = 5


def generateUpdateScript() -> str:
dockerExecPath = getExecPath("docker")
gitExecPath = getExecPath("git")
bashScriptTemplatePath = RESOURCES_DIR / "update_script_template.sh"

with bashScriptTemplatePath.open("r") as scriptFile:
bashScriptTemplate = scriptFile.read()

return bashScriptTemplate.format(
dockerPath = dockerExecPath,
gitPath = gitExecPath,
venvPath = DEFAULT_VENV_PATH
)


def dumpScript(updateScriptPath: Path) -> None:
with updateScriptPath.open("w") as scriptFile:
scriptFile.write(generateUpdateScript())

command(["chmod", "+x", str(updateScriptPath)], ignoreStdout = True)


def activateAutoUpdate() -> None:
updateScriptPath = DEFAULT_VENV_PATH.parent / UPDATE_SCRIPT_NAME
dumpScript(updateScriptPath)
commandName = "coretex node update -n"

if not jobExists(str(updateScriptPath)):
scheduleJob(UPDATE_SCRIPT_NAME)
if not jobExists(str(commandName)):
scheduleJob(commandName)


def getNodeStatus() -> NodeStatus:
Expand Down
51 changes: 0 additions & 51 deletions coretex/cli/modules/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,75 +16,24 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>.

from typing import List, Any, Tuple, Optional, Callable
from pathlib import Path
from functools import wraps
from importlib.metadata import version as getLibraryVersion

import sys
import venv
import shutil
import logging
import platform

from py3nvml import py3nvml

import click
import requests

from . import ui
from ...configuration import DEFAULT_VENV_PATH
from ...utils.process import command


def formatCliVersion(version: Tuple[int, int, int]) -> str:
return ".".join(map(str, version))


def fetchCtxSource() -> Optional[str]:
_, output, _ = command([sys.executable, "-m", "pip", "freeze"], ignoreStdout = True, ignoreStderr = True)
packages = output.splitlines()

for package in packages:
if "coretex" in package:
return package.replace(" ", "")

return None


def createEnvironment(venvPython: Path) -> None:
if DEFAULT_VENV_PATH.exists():
shutil.rmtree(DEFAULT_VENV_PATH)

venv.create(DEFAULT_VENV_PATH, with_pip = True)

if platform.system() == "Windows":
venvPython = DEFAULT_VENV_PATH / "Scripts" / "python.exe"

ctxSource = fetchCtxSource()
if ctxSource is not None:
command([str(venvPython), "-m", "pip", "install", ctxSource], ignoreStdout = True, ignoreStderr = True)


def checkEnvironment() -> None:
venvPython = DEFAULT_VENV_PATH / "bin" / "python"
venvActivate = DEFAULT_VENV_PATH / "bin" / "activate"
venvCoretex = DEFAULT_VENV_PATH / "bin" / "coretex"

if not venvActivate.exists() or not venvPython.exists():
createEnvironment(venvPython)
return

try:
command([str(venvCoretex), "version"], check = True, ignoreStderr = True, ignoreStdout = True)
except Exception:
createEnvironment(venvPython)
return


def updateLib() -> None:
command([sys.executable, "-m", "pip", "install", "--no-cache-dir", "--upgrade", "coretex"], ignoreStdout = True, ignoreStderr = True)


def parseLibraryVersion(version: str) -> Optional[Tuple[int, int, int]]:
parts = version.split(".")

Expand Down
18 changes: 0 additions & 18 deletions coretex/cli/resources/__init__.py

This file was deleted.

21 changes: 0 additions & 21 deletions coretex/cli/resources/resources.py

This file was deleted.

12 changes: 0 additions & 12 deletions coretex/cli/resources/update_script_template.sh

This file was deleted.

3 changes: 0 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,3 @@ exclude = ["tests*", "docs_images*", "doxygen-awesome-css*"]

[tool.setuptools.package-data]
"*" = ["py.typed", "resources/*"]

[project.scripts]
coretex = "coretex.cli.main:cli"
Loading