From 1020c0162ee6dfbb19bca28f424cb4d9ea734e40 Mon Sep 17 00:00:00 2001 From: FuzTheCat <96272894+FuzTheCat@users.noreply.github.com> Date: Sat, 23 Dec 2023 17:59:48 +1000 Subject: [PATCH 1/3] Updated files.py Check path is a file before opening. --- pilot/helpers/files.py | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/pilot/helpers/files.py b/pilot/helpers/files.py index 9d7214441..af879e51b 100644 --- a/pilot/helpers/files.py +++ b/pilot/helpers/files.py @@ -1,6 +1,6 @@ from pathlib import Path import os -from typing import Optional, Union +from typing import Optional, Union, Dict from utils.style import color_green @@ -34,7 +34,7 @@ def update_file(path: str, new_content: Union[str, bytes]): def get_file_contents( path: str, project_root_path: str -) -> dict[str, Union[str, bytes]]: +) -> Dict[str, Union[str, bytes]]: """ Get file content and metadata. @@ -50,26 +50,35 @@ def get_file_contents( will be a Python string. If that fails, it will be treated as a binary file and `content` will be a Python bytes object. """ + # Normalize the path to avoid issues with different path separators + full_path = os.path.normpath(os.path.join(project_root_path, path)) + + # Check if the path is a file + if not os.path.isfile(full_path): + raise ValueError(f"The path provided is not a file: {full_path}") + try: # Assume it's a text file using UTF-8 encoding - file_content = open(path, "r", encoding="utf-8").read() + with open(full_path, "r", encoding="utf-8") as file: + file_content = file.read() except UnicodeDecodeError: # If that fails, we'll treat it as a binary file - file_content = open(path, "rb").read() + with open(full_path, "rb") as file: + file_content = file.read() except FileNotFoundError: - raise ValueError(f"File not found: {path}") + raise ValueError(f"File not found: {full_path}") - file_name = os.path.basename(path) - relative_path = str(Path(path).parent.relative_to(project_root_path)) + file_name = os.path.basename(full_path) + relative_path = os.path.relpath(os.path.dirname(full_path), project_root_path) - if relative_path == ".": - relative_path = "" + if relative_path == '.': + relative_path = '' return { "name": file_name, "path": relative_path, "content": file_content, - "full_path": path, + "full_path": full_path, } From 89120dbffe9dcf30743f50e87d0f48e28354f01c Mon Sep 17 00:00:00 2001 From: LeonOstrez <41999013+LeonOstrez@users.noreply.github.com> Date: Tue, 26 Dec 2023 09:51:12 +0100 Subject: [PATCH 2/3] Update files.py --- pilot/helpers/files.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/pilot/helpers/files.py b/pilot/helpers/files.py index af879e51b..84de8700b 100644 --- a/pilot/helpers/files.py +++ b/pilot/helpers/files.py @@ -1,6 +1,6 @@ from pathlib import Path import os -from typing import Optional, Union, Dict +from typing import Optional, Union from utils.style import color_green @@ -34,7 +34,7 @@ def update_file(path: str, new_content: Union[str, bytes]): def get_file_contents( path: str, project_root_path: str -) -> Dict[str, Union[str, bytes]]: +) -> dict[str, Union[str, bytes]]: """ Get file content and metadata. @@ -51,11 +51,7 @@ def get_file_contents( binary file and `content` will be a Python bytes object. """ # Normalize the path to avoid issues with different path separators - full_path = os.path.normpath(os.path.join(project_root_path, path)) - - # Check if the path is a file - if not os.path.isfile(full_path): - raise ValueError(f"The path provided is not a file: {full_path}") + full_path = os.path.normpath(path) try: # Assume it's a text file using UTF-8 encoding @@ -67,9 +63,11 @@ def get_file_contents( file_content = file.read() except FileNotFoundError: raise ValueError(f"File not found: {full_path}") + except Exception as e: + raise ValueError(f"Exception in get_file_contents: {e}") - file_name = os.path.basename(full_path) - relative_path = os.path.relpath(os.path.dirname(full_path), project_root_path) + file_name = os.path.basename(path) + relative_path = str(Path(path).parent.relative_to(project_root_path)) if relative_path == '.': relative_path = '' From 41af0e736765a014fce317883960f912cc7456ec Mon Sep 17 00:00:00 2001 From: LeonOstrez <41999013+LeonOstrez@users.noreply.github.com> Date: Tue, 26 Dec 2023 09:55:27 +0100 Subject: [PATCH 3/3] Update test_files.py --- pilot/test/helpers/test_files.py | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/pilot/test/helpers/test_files.py b/pilot/test/helpers/test_files.py index 3ac56c8f4..b3649b320 100644 --- a/pilot/test/helpers/test_files.py +++ b/pilot/test/helpers/test_files.py @@ -95,6 +95,7 @@ def np(path: str) -> str: return str(Path(path)) mock_os.path.join = os.path.join + mock_os.path.normpath = os.path.normpath mock_os.path.basename = os.path.basename mock_walk = mock_os.walk @@ -103,7 +104,7 @@ def np(path: str) -> str: (np("/fake/root/foo"), [], ["foo.txt"]), (np("/fake/root/bar"), [], ["bar.txt"]), ] - mock_open.return_value.read.side_effect = [ + mock_open.return_value.__enter__.return_value.read.side_effect = [ "file.txt", "foo.txt - 無為", UnicodeDecodeError("utf-8", b"\xff\xff\xff", 0, 1, "invalid start byte"), @@ -132,18 +133,6 @@ def np(path: str) -> str: }, ] mock_walk.assert_called_once_with(np("/fake/root")) - mock_open.assert_has_calls( - [ - call(np("/fake/root/file.txt"), "r", encoding="utf-8"), - call().read(), - call(np("/fake/root/foo/foo.txt"), "r", encoding="utf-8"), - call().read(), - call(np("/fake/root/bar/bar.txt"), "r", encoding="utf-8"), - call().read(), - call(np("/fake/root/bar/bar.txt"), "rb"), - call().read(), - ] - ) def test_get_directory_contents_live():