From ee064c96ba09ee104e8899a7cff2f4a245ab7e47 Mon Sep 17 00:00:00 2001 From: Dario Del Piano Date: Sun, 28 Jul 2024 23:58:21 +0200 Subject: [PATCH] changes per PR review --- .../ch_cli_tools/migration.py | 88 ++++++++++--------- .../1_migration.json} | 0 .../ch_cli_tools/utils.py | 28 ++---- .../applications/migration_app/Dockerfile | 2 +- 4 files changed, 57 insertions(+), 61 deletions(-) rename tools/deployment-cli-tools/ch_cli_tools/{config/migration.json => migrations/1_migration.json} (100%) diff --git a/tools/deployment-cli-tools/ch_cli_tools/migration.py b/tools/deployment-cli-tools/ch_cli_tools/migration.py index 3beae072..862648c0 100644 --- a/tools/deployment-cli-tools/ch_cli_tools/migration.py +++ b/tools/deployment-cli-tools/ch_cli_tools/migration.py @@ -13,48 +13,56 @@ def perform_migration(base_root, accept_all=False): all_files_detected = [] - f = open(os.path.join(HERE, "config", "migration.json"), "r") - migration_json = json.load(f) + for file in os.listdir(os.path.join(HERE, "migrations")): + f = open(os.path.join(HERE, "migrations", file), "r") + migration_json = json.load(f) - app_base_path = os.path.join(base_root, APPS_PATH) + sub_paths = [] + if isinstance(base_root, list): + app_base_path = [ + os.path.join(base_root_path, APPS_PATH) + for base_root_path in base_root + ] + for app_path in app_base_path: + sub_paths.extend(get_sub_paths(app_path)) + else: + app_base_path = os.path.join(base_root, APPS_PATH) + sub_paths = get_sub_paths(app_base_path) - # Iterate over all the applications to check if they need to be migrated - for app_path in get_sub_paths(app_base_path): - # Iterate the folders and files to check if they need to be migrated - for sub_path in TO_CHECK: - to_check = os.path.join(app_path, sub_path) - if os.path.isdir(to_check): - for migration_obj in migration_json["deprecated"]: - files = search_word_in_folder(to_check, migration_obj["keyword"]) - for file in files: - file_path = os.path.join(to_check, file) - print("#########################################") - print(f"Running migration on {file_path}") - print("#########################################") - all_files_detected.append(file_path) - for word_to_replace in migration_obj["to_be_replaced"]: - read_file_and_replace( - file_path, - word_to_replace["old"], - word_to_replace["new"], - accept_all, - ) - elif os.path.isfile(to_check): - for migration_obj in migration_json["deprecated"]: - if len(search_word_in_file(to_check, migration_obj["keyword"])) > 0: - print("#########################################") - print(f"Running migration on {to_check}") - print("#########################################") - all_files_detected.append(to_check) - for word_to_replace in migration_obj["to_be_replaced"]: - read_file_and_replace( - to_check, - word_to_replace["old"], - word_to_replace["new"], - accept_all, - ) - else: - print(f'Path {to_check} does not exist') + for app_path in sub_paths: + # Iterate the folders and files to check if they need to be migrated + for sub_path in TO_CHECK: + to_check = os.path.join(app_path, sub_path) + if os.path.isdir(to_check): + for migration_obj in migration_json["deprecated"]: + files = search_word_in_folder(to_check, migration_obj["keyword"]) + for file in files: + file_path = os.path.join(to_check, file) + print("#########################################") + print(f"Running migration on {file_path}") + print("#########################################") + all_files_detected.append(file_path) + for word_to_replace in migration_obj["to_be_replaced"]: + read_file_and_replace( + file_path, + word_to_replace["old"], + word_to_replace["new"], + accept_all, + ) + elif os.path.isfile(to_check): + for migration_obj in migration_json["deprecated"]: + if len(search_word_in_file(to_check, migration_obj["keyword"])) > 0: + print("#########################################") + print(f"Running migration on {to_check}") + print("#########################################") + all_files_detected.append(to_check) + for word_to_replace in migration_obj["to_be_replaced"]: + read_file_and_replace( + to_check, + word_to_replace["old"], + word_to_replace["new"], + accept_all, + ) print("=========================================") print("=== Cloud Harness migration completed ===") print("=========================================") diff --git a/tools/deployment-cli-tools/ch_cli_tools/config/migration.json b/tools/deployment-cli-tools/ch_cli_tools/migrations/1_migration.json similarity index 100% rename from tools/deployment-cli-tools/ch_cli_tools/config/migration.json rename to tools/deployment-cli-tools/ch_cli_tools/migrations/1_migration.json diff --git a/tools/deployment-cli-tools/ch_cli_tools/utils.py b/tools/deployment-cli-tools/ch_cli_tools/utils.py index 375b35ec..0bb6b588 100644 --- a/tools/deployment-cli-tools/ch_cli_tools/utils.py +++ b/tools/deployment-cli-tools/ch_cli_tools/utils.py @@ -398,32 +398,20 @@ def filter_empty_strings(value): def search_word_in_file(file, word): - p = subprocess.Popen('grep -l %s %s'% (word, file), shell=True, - stdout=subprocess.PIPE) - output, _ = p.communicate() - output = output.decode("utf-8") + if os.path.isdir(file): + return [] matches = [] - if p.returncode == 1: # no matches found - pass - elif p.returncode == 0: # matches found - matches = output.split('\n') - else: - raise Exception(f'Migration Error: {file} grep failed with return code {p.returncode} and output {output}') + with open(file) as f: + if word in f.read(): + matches.append(file) return list(filter(filter_empty_strings, matches)) def search_word_in_folder(folder, word): - p = subprocess.Popen('grep -rl %s %s'% (word, '*'), shell=True, - stdout=subprocess.PIPE, cwd=folder) - output, _ = p.communicate() - output = output.decode("utf-8") matches = [] - if p.returncode == 1: # no matches found - pass - elif p.returncode == 0: # matches found - matches = output.split('\n') - else: - raise Exception(f'Migration Error: {folder} grep failed with return code {p.returncode} and output {output}') + files = glob.glob(folder + '/**/*', recursive = True) + for file in files: + matches.extend(search_word_in_file(file, word)) return list(filter(filter_empty_strings, matches)) diff --git a/tools/deployment-cli-tools/tests/resources/applications/migration_app/Dockerfile b/tools/deployment-cli-tools/tests/resources/applications/migration_app/Dockerfile index 12e4f487..721ff5e5 100644 --- a/tools/deployment-cli-tools/tests/resources/applications/migration_app/Dockerfile +++ b/tools/deployment-cli-tools/tests/resources/applications/migration_app/Dockerfile @@ -1,4 +1,4 @@ ARG CLOUDHARNESS_BASE_DEBIAN FROM $CLOUDHARNESS_BASE_DEBIAN -RUN apk add --no-cache bash +RUN apt install --no-cache bash