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 all 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
19 changes: 19 additions & 0 deletions .build/compile.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash
set -e

# Create a Python virtual environment
python -m venv compile_env

# Activate the virtual environment
source compile_env/bin/activate

# Install the current package and pyinstaller
pip install .. pyinstaller

# Run PyInstaller with the specified options
pyinstaller ../main.py --onedir --name coretex --copy-metadata readchar --copy-metadata coretex --add-data "../coretex/resources/_coretex.py:coretex/resources" --workpath coretex --noconfirm

# Deactivate and remove the virtual environment
deactivate
rm -rf compile_env
rm -rf coretex.spec
Binary file added .build/coretex.pkg
Binary file not shown.
38 changes: 38 additions & 0 deletions .build/deb-package.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/bin/bash
set -e

# Define package name and directory structure
PACKAGE_NAME="coretex"
PACKAGE_DIR="${PACKAGE_NAME}"
Copy link
Collaborator

Choose a reason for hiding this comment

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

This should be "dist/SOMETHING_SOMETHING"

DEBIAN_DIR="${PACKAGE_DIR}/DEBIAN"
BIN_DIR="${PACKAGE_DIR}/usr/local/bin"

# Create the necessary directory structure
mkdir -p "${DEBIAN_DIR}"
mkdir -p "${BIN_DIR}"

# Create a Python virtual environment
python -m venv compile_env
Copy link
Collaborator

Choose a reason for hiding this comment

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

Rename the venv to debian-package-venv


# Activate the virtual environment
source compile_env/bin/activate

# Install the toml package (for generate_control.py)
pip install toml

# Move the entire dist/main directory contents directly to /usr/local/bin
mv dist/coretex/* "${BIN_DIR}/"
rm -rf dist/

# Generate the control file using Python (assuming you have the Python script ready)
python generate_control.py

# Deactivate and remove the virtual environment
deactivate
rm -rf compile_env

# Build the .deb package
dpkg-deb --build "${PACKAGE_DIR}"

# Remove unnecessary dir's
rm -rf "${PACKAGE_DIR}"
32 changes: 32 additions & 0 deletions .build/generate_control.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import toml


def main() -> None:
# Load data from pyproject.toml
with open("../pyproject.toml", "r") as f:
pyproject = toml.load(f)

# Extract necessary information
packageName = pyproject["project"]["name"]
version = pyproject["project"]["version"]
description = pyproject["project"]["description"]

# Create control file content
controlContent = (
f"Package: {packageName}\n"
f"Version: {version}\n"
"Section: base\n"
"Priority: optional\n"
"Architecture: all\n"
"Depends: python3\n"
"Maintainer: Your Name < [email protected] >\n"
f"Description: {description}\n"
)

# Write to control file
with open(f"{packageName}/DEBIAN/control", "w") as controlFile:
controlFile.write(controlContent)


if __name__ == "__main__":
main()
16 changes: 16 additions & 0 deletions .build/macos-package.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash
set -e

# Extract version from pyproject.toml
VERSION=$(grep '^version = ' ../pyproject.toml | sed 's/version = "\(.*\)"/\1/')

# Define package name and directory structure
PACKAGE_NAME="coretex"
PAYLOAD_DIR="dist/coretex/"

# Build the .pkg package using pkgbuild
pkgbuild --root "${PAYLOAD_DIR}" --identifier ai.coretex --version "${VERSION}" --install-location "/usr/local/bin" "${PACKAGE_NAME}.pkg"

# Clean up
rm -rf coretex/
rm -rf dist/
Empty file added .build/windows-package.sh
Empty file.
8 changes: 6 additions & 2 deletions coretex/cli/commands/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
from ..modules import node as node_module
from ..modules.node import NodeStatus, getNodeStatus
from ..modules.user import initializeUserSession
from ..modules.utils import onBeforeCommandExecute, checkEnvironment
from ..modules.update import activateAutoUpdate
from ..modules.update import activateAutoUpdate, getNodeStatus
from ..modules.utils import onBeforeCommandExecute
from ...utils import docker
from ...configuration import NodeConfiguration, InvalidConfiguration, ConfigurationNotFound

Expand Down Expand Up @@ -208,8 +208,12 @@ def logs(tail: Optional[int], follow: bool, timestamps: bool) -> None:
@click.group()
@onBeforeCommandExecute(docker.isDockerAvailable, excludeSubcommands = ["status"])
@onBeforeCommandExecute(initializeUserSession)
<<<<<<< HEAD
@onBeforeCommandExecute(node_module.checkResourceLimitations)
=======
@onBeforeCommandExecute(node_module.checkResourceLimitations, excludeSubcommands = ["status"])
@onBeforeCommandExecute(checkEnvironment)
>>>>>>> develop
def node() -> None:
pass

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 @@ -302,7 +302,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
41 changes: 11 additions & 30 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,31 +34,17 @@ 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:
commandName = "coretex node update -n"

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

def activateAutoUpdate() -> None:
updateScriptPath = DEFAULT_VENV_PATH.parent / UPDATE_SCRIPT_NAME
dumpScript(updateScriptPath)

if not jobExists(str(updateScriptPath)):
scheduleJob(UPDATE_SCRIPT_NAME)
def getNodeStatus() -> NodeStatus:
try:
response = requests.get(f"http://localhost:21000/status", timeout = 1)
status = response.json()["status"]
return NodeStatus(status)
except:
return NodeStatus.inactive
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.

Loading
Loading