Skip to content

Commit

Permalink
changes per PR review
Browse files Browse the repository at this point in the history
  • Loading branch information
ddelpiano committed Jul 28, 2024
1 parent 52d927c commit ee064c9
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 61 deletions.
88 changes: 48 additions & 40 deletions tools/deployment-cli-tools/ch_cli_tools/migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("=========================================")
Expand Down
28 changes: 8 additions & 20 deletions tools/deployment-cli-tools/ch_cli_tools/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))


Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ARG CLOUDHARNESS_BASE_DEBIAN
FROM $CLOUDHARNESS_BASE_DEBIAN

RUN apk add --no-cache bash
RUN apt install --no-cache bash

0 comments on commit ee064c9

Please sign in to comment.