From 0cba83b0638f8ee7dd884337728d80e925380df0 Mon Sep 17 00:00:00 2001 From: chunlin Date: Mon, 6 Jan 2025 19:58:37 +0800 Subject: [PATCH 1/2] add key check when use config file --- src/configService.py | 8 ++++++++ src/toolService.py | 8 +++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/configService.py b/src/configService.py index c7481b3..45dac80 100644 --- a/src/configService.py +++ b/src/configService.py @@ -18,6 +18,14 @@ class ConfigService: if not os.path.isfile(config_path): print("config_path not found, switch failed.") return + contents = self.tool.read_file(config_file) + # keys should contains in config + keys = ["DOWNLOAD","DEPENDENCY","ENV","APP","BUILD","RUN"] + for key in keys: + if f"[{key}]" not in contents: + print(f"key [{key}] not found, switch failed.") + self.tool.del_file(self.meta_path) + return self.tool.write_file(self.meta_path, config_file.strip()) print("Successfully switched. config file saved in file .meta") diff --git a/src/toolService.py b/src/toolService.py index 0942dfc..0cb2567 100644 --- a/src/toolService.py +++ b/src/toolService.py @@ -42,6 +42,12 @@ class ToolService: with open(filename, 'w') as f: f.write(content) + def del_file(self, filepath): + """ + 删除文件。 + """ + if os.path.exists(filepath): + os.remove(filepath) def mkdirs(self, path): """ @@ -66,4 +72,4 @@ class ToolService: response = requests.get(url, stream=True) return response.status_code == 200 except requests.exceptions.RequestException as e: - return False \ No newline at end of file + return False -- Gitee From 57a0c392ecd9e525b541d003d298681308826b16 Mon Sep 17 00:00:00 2001 From: chunlin Date: Mon, 6 Jan 2025 20:06:01 +0800 Subject: [PATCH 2/2] no need to del old meta file --- src/configService.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/configService.py b/src/configService.py index 45dac80..ad73f3a 100644 --- a/src/configService.py +++ b/src/configService.py @@ -23,9 +23,8 @@ class ConfigService: keys = ["DOWNLOAD","DEPENDENCY","ENV","APP","BUILD","RUN"] for key in keys: if f"[{key}]" not in contents: - print(f"key [{key}] not found, switch failed.") - self.tool.del_file(self.meta_path) - return + print(f"key [{key}] not found in {config_file}, switch failed.") + return self.tool.write_file(self.meta_path, config_file.strip()) print("Successfully switched. config file saved in file .meta") -- Gitee