From eda21448fb019e43ddd8579468bcc8a067484776 Mon Sep 17 00:00:00 2001 From: z00793368 Date: Thu, 27 Jul 2023 20:27:00 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=8F=96=E5=8C=85=E5=90=8D?= =?UTF-8?q?=E5=A4=A7=E5=B0=8F=E5=86=99=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- script/gspylib/os/gsplatform.py | 52 ++++++++++++++++++++++++++- script/os_platform/linux_platform.py | 53 ++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+), 1 deletion(-) diff --git a/script/gspylib/os/gsplatform.py b/script/gspylib/os/gsplatform.py index 1e12c30..7e513d7 100644 --- a/script/gspylib/os/gsplatform.py +++ b/script/gspylib/os/gsplatform.py @@ -1566,11 +1566,61 @@ class LinuxPlatform(GenericPlatform): fileName = os.path.normpath(fileName) if not os.path.exists(fileName): - raise Exception(ErrorCode.GAUSS_502["GAUSS_50201"] % fileName) + try: + directory_to_search = os.path.join(dirName, "./../../../") + matched_files = self.compare_files_in_directory(directory_to_search, fileName) + + if len(matched_files) == 1: + return matched_files[0] + elif len(matched_files) > 1: + print("Multiple matching files found," + "please select one:") + + for i, file in enumerate(matched_files, 1): + print(f"{i}. {file}") + + while True: + try: + choice = int(input("Please enter the serial number" + "of the option:")) + if 1 <= choice <= len(matched_files): + return matched_files[choice - 1] + else: + print("Invalid input: Please re-enter") + except ValueError: + print("Invalid input: Please re-enter") + else: + raise Exception("No matching files found in the directory.") + except Exception as e: + raise Exception(ErrorCode.GAUSS_502["GAUSS_50201"] % fileName) + if not os.path.isfile(fileName): raise Exception(ErrorCode.GAUSS_502["GAUSS_50210"] % fileName) return fileName + def compare_files_in_directory(directory, file_to_compare): + """ + function: The function is to recursively search for files in the + specified directory and compare them with the given file, + input: + directory: Directory path to traverse + file_to_compare: The file path to compare with files in the directory + output: + matched_files + """ + matched_files = [] + + for root, dirs, files in os.walk(directory): + for file in files: + file_path = os.path.join(root, file) + if os.path.isfile(file_path) and file_to_compare.lower() == file_path.lower(): + matched_files.append(file_path) + + if matched_files: + return matched_files + else: + raise Exception(ErrorCode.GAUSS_502["GAUSS_50201"] % file_to_compare) + def setKeyValueInSshd(self, key, value): """ function: Set a (key, value) pair into /etc/ssh/sshd_config, diff --git a/script/os_platform/linux_platform.py b/script/os_platform/linux_platform.py index a46ed0a..4e4c412 100644 --- a/script/os_platform/linux_platform.py +++ b/script/os_platform/linux_platform.py @@ -243,7 +243,60 @@ class LinuxPlatform(object): package_name_list.append(package_name) if os.path.exists(file_name) and os.path.isfile(file_name): return file_name + + for file_name in file_name_list: + try: + directory_to_search = os.path.join(dir_name, "./../../../") + matched_files = self.compare_files_in_directory(directory_to_search, file_name) + + if len(matched_files) == 1: + return matched_files[0] + elif len(matched_files) > 1: + print("Multiple matching files found," + "please select one:") + + for i, file in enumerate(matched_files, 1): + print(f"{i}. {file}") + + while True: + try: + choice = int(input("Please enter the serial number" + "of the option:")) + if 1 <= choice <= len(matched_files): + return matched_files[choice - 1] + else: + print("Invalid input: Please re-enter") + except ValueError: + print("Invalid input: Please re-enter") + else: + raise Exception("No matching files found in the directory.") + except Exception as e: + raise Exception(ErrorCode.GAUSS_502["GAUSS_50201"] % file_name) + raise Exception(ErrorCode.GAUSS_502["GAUSS_50201"] % package_name_list) + + def compare_files_in_directory(directory, file_to_compare): + """ + function: The function is to recursively search for files in the + specified directory and compare them with the given file, + input: + directory: Directory path to traverse + file_to_compare: The file path to compare with files in the directory + output: + matched_files + """ + matched_files = [] + + for root, dirs, files in os.walk(directory): + for file in files: + file_path = os.path.join(root, file) + if os.path.isfile(file_path) and file_to_compare.lower() == file_path.lower(): + matched_files.append(file_path) + + if matched_files: + return matched_files + else: + raise Exception(ErrorCode.GAUSS_502["GAUSS_50201"] % file_to_compare) def getGrepCmd(self): """