From 4e8e8161c35673366a567a179a097accd3dc32a1 Mon Sep 17 00:00:00 2001 From: Vinicius Date: Fri, 19 May 2023 12:31:19 -0300 Subject: [PATCH] Copy files and directories implemented --- danix/danixfs.py | 8 ++++++++ danix/db/models.py | 30 +++++++++++++++++++++++++++++- danix/main.py | 4 ++++ danix/utils.py | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 73 insertions(+), 1 deletion(-) diff --git a/danix/danixfs.py b/danix/danixfs.py index ee708a0..f266d3a 100644 --- a/danix/danixfs.py +++ b/danix/danixfs.py @@ -3,6 +3,14 @@ from sh import du from settings import MAIN_REPO class Danix(): + @staticmethod + def copy(environent_is_first, filesystem_name, environment_dir, host_directory): + + if environent_is_first: + return os.system(f"cp -r {MAIN_REPO}{filesystem_name}/danixfs{environment_dir} {host_directory} >/dev/null 2>&1") + else: + return os.system(f"cp -r {host_directory} {MAIN_REPO}{filesystem_name}/danixfs{environment_dir} >/dev/null 2>&1") + @staticmethod def remove_snapshot(snapshot_name): return os.system(f"rm -r {MAIN_REPO}.snapshots/{snapshot_name} >/dev/null 2>&1") diff --git a/danix/db/models.py b/danix/db/models.py index 69264bc..a7d24b2 100644 --- a/danix/db/models.py +++ b/danix/db/models.py @@ -5,7 +5,7 @@ import settings from danixfs import Danix from datetime import datetime from settings import SNAPSHOT_LIMIT -from utils import get_size_in_mb_or_gb, print_snapshot_list_header, print_footer, print_environment_list_header, is_unique_database_tuple, get_message, check_equal_sentence, check_not_equal_sentence +from utils import separate, get_size_in_mb_or_gb, print_snapshot_list_header, print_footer, print_environment_list_header, is_unique_database_tuple, get_message, check_equal_sentence, check_not_equal_sentence from django.core.exceptions import ValidationError def get_queryset_filtered(model, sub_attribute): @@ -22,6 +22,34 @@ class Environment(models.Model): created = models.DateField(default=datetime.now()) template = models.TextField(default="") + + @staticmethod + def copy(path): + + environent_is_first , environment_uuid, environment_dir, host_directory = separate(path) + + if environment_uuid and environment_dir and host_directory: + + environment = get_queryset_filtered(Environment, environment_uuid) + filesystem_name = environment.first().filesystem_name + + resp = Danix.copy( + environent_is_first, + filesystem_name, + environment_dir, + host_directory + ) + + if check_equal_sentence(resp, 0): + get_message("🟢 Elements copyed successfuly!", True, 0) + else: + get_message("🔴 Copy error!", True, 1) + else: + if environent_is_first: + get_message(f"🔴 Syntax error or element exist in {host_directory}!", True, 1) + else: + get_message(f"🔴 Syntax error or path not exist in subsystem!", True, 1) + @staticmethod def navigate(filesystem_name): diff --git a/danix/main.py b/danix/main.py index 98e0dd8..13b3775 100755 --- a/danix/main.py +++ b/danix/main.py @@ -25,6 +25,7 @@ usages.add_argument("-n", "--navigate", help="Navigate inside the en usages.add_argument("-sr", "--snapshotremove", help="Remove snapshot", required=False) usages.add_argument("-sc", "--snapshotcreate", help="Create snapshot", required=False) usages.add_argument("-sb", "--snapshotback" , help="Back snapshot", required=False) +usages.add_argument("-c", "--copy" , help="Copy files and directories", required=False) usages.add_argument("-o", "--option", choices=["clike", "java", "python", "ruby", "lua", "go", "ada"], required=False) @@ -98,6 +99,9 @@ if check_system_configuration(): exit(0) print("🔴 [Danix]: System abort!") + + if args.copy: + Environment.copy(args.copy) else: print("🔴 Danix system is not configured!") print("Plase run 'make config'") \ No newline at end of file diff --git a/danix/utils.py b/danix/utils.py index 652a298..659b8d1 100644 --- a/danix/utils.py +++ b/danix/utils.py @@ -1,6 +1,38 @@ import os from settings import MAIN_REPO, MAIN_DIR, BASE_DIR +def separate(path): + list_path = path.split(" ") + + environment_path = None + environment_uuid = None + host_path = None + environent_is_first = False + + if len(list_path) >= 2: + + if list_path[1]: + if str(list_path[1]).__contains__(":"): + + environment_uuid = list_path[1].split(":")[0] + environment_path = list_path[1].split(":")[1] + + else: + host_path = list_path[1] + + if list_path[0]: + + if str(list_path[0]).__contains__(":"): + + environent_is_first = True + environment_uuid = list_path[0].split(":")[0] + environment_path = list_path[0].split(":")[1] + + else: + host_path = list_path[0] + + return environent_is_first, environment_uuid, environment_path, host_path + def check_create_dir(): mainrepo_resp = os.system(f"cd {MAIN_REPO} >/dev/null 2>&1") snapshot_resp = os.system(f" cd {MAIN_REPO}.snapshots >/dev/null 2>&1")