From 55a35098bdba636e9a9c8114b0edf190d6258540 Mon Sep 17 00:00:00 2001 From: Bogdan Tintor Date: Tue, 3 Sep 2024 10:52:13 +0200 Subject: [PATCH] CTX-6563: Saving changes --- coretex/cli/commands/task.py | 24 ++--- coretex/cli/modules/task.py | 102 ------------------ .../entities/repository/coretex_repository.py | 7 +- 3 files changed, 15 insertions(+), 118 deletions(-) delete mode 100644 coretex/cli/modules/task.py diff --git a/coretex/cli/commands/task.py b/coretex/cli/commands/task.py index b50db7dc..c468dcb2 100644 --- a/coretex/cli/commands/task.py +++ b/coretex/cli/commands/task.py @@ -22,7 +22,6 @@ import webbrowser from ..modules import ui -from ..modules import task as task_utils from ..modules.project_utils import getProject from ..modules.user import initializeUserSession from ..modules.utils import onBeforeCommandExecute @@ -31,6 +30,7 @@ from ..._task import TaskRunWorker, executeRunLocally, readTaskConfig, runLogger from ...configuration import UserConfiguration from ...entities import Task, TaskRun, TaskRunStatus +from ...entities.repository import CORETEX_METADATA_PATH from ...resources import PYTHON_ENTRY_POINT_PATH @@ -116,10 +116,7 @@ def run(path: str, name: Optional[str], description: Optional[str], snapshot: bo @click.command() @click.argument("id", type = int, default = None, required = False) def pull(id: Optional[int]) -> None: - initialMetadataPath = Path(f"{id}/.metadata.json") - coretexMetadataPath = Path(f"{id}/.coretex.json") - - if id is None and not coretexMetadataPath.exists(): + if id is None and not CORETEX_METADATA_PATH.exists(): id = ui.clickPrompt(f"There is no existing Task repository. Please specify id of Task you want to pull:", type = int) if id is not None: @@ -129,13 +126,12 @@ def pull(id: Optional[int]) -> None: ui.errorEcho(f"Failed to fetch Task id {id}. Reason: {ex.response}") return - if not coretexMetadataPath.exists(): + if not CORETEX_METADATA_PATH.exists(): task.pull() - task_utils.createMetadata(initialMetadataPath, coretexMetadataPath) - task_utils.fillTaskMetadata(task, initialMetadataPath, coretexMetadataPath) + task.createMetadata() + task.fillMetadata() else: - remoteMetadata = task.getMetadata() - differences = task_utils.checkMetadataDifference(remoteMetadata, coretexMetadataPath) + differences = task.getDiff() if len(differences) == 0: ui.stdEcho("Your repository is already updated.") return @@ -143,16 +139,16 @@ def pull(id: Optional[int]) -> None: ui.stdEcho("There are conflicts between your and remote repository.") for diff in differences: ui.stdEcho(f"File: {diff['path']} differs") - ui.stdEcho(f" Local checksum: {diff['local_checksum']}") - ui.stdEcho(f" Remote checksum: {diff['remote_checksum']}") + ui.stdEcho(f"\tLocal checksum: {diff['local_checksum']}") + ui.stdEcho(f"\tRemote checksum: {diff['remote_checksum']}") if not ui.clickPrompt("Do you want to pull the changes and update your local repository? (Y/n):", type = bool, default = True): ui.stdEcho("No changes were made to your local repository.") return task.pull() - task_utils.createMetadata(initialMetadataPath, coretexMetadataPath) - task_utils.fillTaskMetadata(task, initialMetadataPath, coretexMetadataPath) + task.createMetadata() + task.fillMetadata() ui.stdEcho("Repository updated successfully.") diff --git a/coretex/cli/modules/task.py b/coretex/cli/modules/task.py deleted file mode 100644 index 4cf24d44..00000000 --- a/coretex/cli/modules/task.py +++ /dev/null @@ -1,102 +0,0 @@ -# Copyright (C) 2023 Coretex LLC - -# This file is part of Coretex.ai - -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License as -# published by the Free Software Foundation, either version 3 of the -# License, or (at your option) any later version. - -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Affero General Public License for more details. - -# You should have received a copy of the GNU Affero General Public License -# along with this program. If not, see . - -from typing import Dict, Any, Tuple, List -from pathlib import Path -from zipfile import ZipFile - -import os -import json - -from ...entities import Task -from ...networking import networkManager, NetworkRequestError -from ...utils.misc import generateSha256Checksum - - -def pull(id: int) -> None: - params = { - "sub_project_id": id - } - - zipFilePath = f"{id}.zip" - response = networkManager.download(f"workspace/download", zipFilePath, params) - - if response.hasFailed(): - raise NetworkRequestError(response, ">> [Coretex] Task download has failed") - - with ZipFile(zipFilePath) as zipFile: - zipFile.extractall(str(id)) - - # remove zip file after extract - os.unlink(zipFilePath) - - -def createMetadata(initialMetadataPath: Path, coretexMetadataPath: Path) -> None: - with open(initialMetadataPath, "r") as initialMetadataFile: - initialMetadata = json.load(initialMetadataFile) - - # if backend returns null for checksum of file, generate checksum - for file in initialMetadata: - if file["checksum"] is None: - filePath = initialMetadataPath.parent.joinpath(file["path"]) - if filePath.exists(): - file["checksum"] = generateSha256Checksum(filePath) - - newMetadata = { - "checksums": initialMetadata - } - - with open(coretexMetadataPath, "w") as coretexMetadataFile: - json.dump(newMetadata, coretexMetadataFile, indent = 4) - - -def fillTaskMetadata(task: Task, initialMetadataPath: Path, coretexMetadataPath: Path) -> None: - metadata = task.encode() - - with coretexMetadataPath.open("r") as coretexMetadataFile: - existing_metadata = json.load(coretexMetadataFile) - - existing_metadata.update(metadata) - - with coretexMetadataPath.open("w") as coretexMetadataFile: - json.dump(existing_metadata, coretexMetadataFile, indent=4) - - initialMetadataPath.unlink() - - -def checkMetadataDifference(remoteMetadata: list, coretexMetadataPath: Path) -> List[Dict[str, Any]]: - with coretexMetadataPath.open("r") as localMetadataFile: - localMetadata = json.load(localMetadataFile) - - localChecksums = {file['path']: file['checksum'] for file in localMetadata['checksums']} - - differences = [] - - for remoteFile in remoteMetadata: - remotePath = remoteFile['path'] - remoteChecksum = remoteFile['checksum'] - - localChecksum = localChecksums.get(remotePath) - - if localChecksum != remoteChecksum: - differences.append({ - 'path': remotePath, - 'local_checksum': localChecksum, - 'remote_checksum': remoteChecksum - }) - - return differences diff --git a/coretex/entities/repository/coretex_repository.py b/coretex/entities/repository/coretex_repository.py index a02bd046..e58fd67c 100644 --- a/coretex/entities/repository/coretex_repository.py +++ b/coretex/entities/repository/coretex_repository.py @@ -87,7 +87,7 @@ def getRemoteMetadata(self) -> List: response = networkManager.get(f"{self.endpoint}/metadata", params) if response.hasFailed(): - return NetworkRequestError(response, "Failed to fetch task metadata.") + raise NetworkRequestError(response, "Failed to fetch task metadata.") return response.getJson(list, force = True) @@ -123,7 +123,7 @@ def fillMetadata(self) -> None: INITIAL_METADATA_PATH.unlink() - def checkDiff(self) -> List[Dict[str, Any]]: + def getDiff(self) -> List[Dict[str, Any]]: with CORETEX_METADATA_PATH.open("r") as localMetadataFile: localMetadata = json.load(localMetadataFile) @@ -146,3 +146,6 @@ def checkDiff(self) -> List[Dict[str, Any]]: }) return differences + + # def updateRepository(self, differences: List[Dict[str, Any]]) -> None: + # pass