From 10914e7f87d4608f18eefe32918f44c41dab1162 Mon Sep 17 00:00:00 2001 From: Michael Oviedo Date: Thu, 2 Jan 2025 21:36:16 +0000 Subject: [PATCH 1/4] update rename ~/.benchmark to ~/.osb and add symlink Signed-off-by: Michael Oviedo --- osbenchmark/paths.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/osbenchmark/paths.py b/osbenchmark/paths.py index 305275fe..a3831492 100644 --- a/osbenchmark/paths.py +++ b/osbenchmark/paths.py @@ -26,7 +26,11 @@ def benchmark_confdir(): default_home = os.path.expanduser("~") - return os.path.join(os.getenv("BENCHMARK_HOME", default_home), ".benchmark") + old_path = os.path.join(default_home, ".benchmark") + new_path = os.path.join(default_home, ".osb") + if os.path.exists(old_path) and not os.path.exists(new_path): + os.symlink(old_path, new_path) + return os.path.join(os.getenv("BENCHMARK_HOME", default_home), ".osb") def benchmark_root(): From bb2df8306102a8dfcfa70fd54e8914874c178451 Mon Sep 17 00:00:00 2001 From: Michael Oviedo Date: Fri, 3 Jan 2025 20:24:03 +0000 Subject: [PATCH 2/4] add case if .benchmark does not exist Signed-off-by: Michael Oviedo --- osbenchmark/paths.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/osbenchmark/paths.py b/osbenchmark/paths.py index a3831492..53481a78 100644 --- a/osbenchmark/paths.py +++ b/osbenchmark/paths.py @@ -28,8 +28,22 @@ def benchmark_confdir(): default_home = os.path.expanduser("~") old_path = os.path.join(default_home, ".benchmark") new_path = os.path.join(default_home, ".osb") - if os.path.exists(old_path) and not os.path.exists(new_path): - os.symlink(old_path, new_path) + + # Create .benchmark directory if it doesn't exist + if not os.path.exists(old_path): + os.makedirs(old_path, exist_ok=True) + + # Create .osb directory if it doesn't exist + if not os.path.exists(new_path): + os.makedirs(new_path, exist_ok=True) + + # Create symlink from .osb to .benchmark if it doesn't exist + if not os.path.islink(new_path): + try: + os.symlink(old_path, new_path, target_is_directory=True) + except OSError: + print(f"Warning: Failed to create symlink from {new_path} to {old_path}") + return os.path.join(os.getenv("BENCHMARK_HOME", default_home), ".osb") From f65ba51d5cf5aa55683ad5c07004b5821b06ff09 Mon Sep 17 00:00:00 2001 From: Michael Oviedo Date: Tue, 7 Jan 2025 18:39:09 +0000 Subject: [PATCH 3/4] Use ensure_dir to ensure directories exist + output more verbose error message Signed-off-by: Michael Oviedo --- osbenchmark/paths.py | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/osbenchmark/paths.py b/osbenchmark/paths.py index 53481a78..986c9ec2 100644 --- a/osbenchmark/paths.py +++ b/osbenchmark/paths.py @@ -22,27 +22,29 @@ # specific language governing permissions and limitations # under the License. import os - +import sys +from osbenchmark.utils.io import ensure_dir def benchmark_confdir(): default_home = os.path.expanduser("~") old_path = os.path.join(default_home, ".benchmark") new_path = os.path.join(default_home, ".osb") - # Create .benchmark directory if it doesn't exist - if not os.path.exists(old_path): - os.makedirs(old_path, exist_ok=True) - - # Create .osb directory if it doesn't exist - if not os.path.exists(new_path): - os.makedirs(new_path, exist_ok=True) + # ensure both directories exist + ensure_dir(old_path) + ensure_dir(new_path) # Create symlink from .osb to .benchmark if it doesn't exist if not os.path.islink(new_path): try: os.symlink(old_path, new_path, target_is_directory=True) - except OSError: - print(f"Warning: Failed to create symlink from {new_path} to {old_path}") + except OSError as e: + error_message = ( + f"OSError: Failed to create symlink from {new_path} to {old_path}\n" + f"Error type: {type(e).__name__}\n" + f"Error message: {str(e)}\n" + ) + print(error_message, file=sys.stderr) return os.path.join(os.getenv("BENCHMARK_HOME", default_home), ".osb") From 4656f0d33f65481306c9e627e7c1b2d6910e2c88 Mon Sep 17 00:00:00 2001 From: Michael Oviedo Date: Tue, 7 Jan 2025 19:26:27 +0000 Subject: [PATCH 4/4] add new ensure_symlink util function to help prevent FileExists errors Signed-off-by: Michael Oviedo --- osbenchmark/paths.py | 39 +++++++++++++++++++++++---------------- osbenchmark/utils/io.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 16 deletions(-) diff --git a/osbenchmark/paths.py b/osbenchmark/paths.py index 986c9ec2..43836895 100644 --- a/osbenchmark/paths.py +++ b/osbenchmark/paths.py @@ -23,31 +23,38 @@ # under the License. import os import sys -from osbenchmark.utils.io import ensure_dir +from osbenchmark.utils.io import ensure_dir, ensure_symlink def benchmark_confdir(): default_home = os.path.expanduser("~") old_path = os.path.join(default_home, ".benchmark") new_path = os.path.join(default_home, ".osb") - # ensure both directories exist - ensure_dir(old_path) - ensure_dir(new_path) + try: + # Ensure .benchmark directory exists + ensure_dir(old_path) - # Create symlink from .osb to .benchmark if it doesn't exist - if not os.path.islink(new_path): - try: - os.symlink(old_path, new_path, target_is_directory=True) - except OSError as e: - error_message = ( - f"OSError: Failed to create symlink from {new_path} to {old_path}\n" - f"Error type: {type(e).__name__}\n" - f"Error message: {str(e)}\n" - ) - print(error_message, file=sys.stderr) + # Ensure symlink from .osb to .benchmark + ensure_symlink(old_path, new_path) - return os.path.join(os.getenv("BENCHMARK_HOME", default_home), ".osb") + final_path = os.path.join(os.getenv("BENCHMARK_HOME", default_home), ".osb") + return final_path + + except Exception as e: + error_message = ( + f"Error in benchmark_confdir:\n" + f"Error type: {type(e).__name__}\n" + f"Error message: {str(e)}\n" + f"Current user: {os.getlogin()}\n" + f"Current working directory: {os.getcwd()}\n" + f"Python version: {sys.version}\n" + f"Operating system: {sys.platform}\n" + f"Permissions of {old_path}: {oct(os.stat(old_path).st_mode) if os.path.exists(old_path) else 'N/A'}\n" + f"Permissions of parent of {new_path}: {oct(os.stat(os.path.dirname(new_path)).st_mode)}" + ) + print(error_message) + raise def benchmark_root(): return os.path.dirname(os.path.realpath(__file__)) diff --git a/osbenchmark/utils/io.py b/osbenchmark/utils/io.py index 2b740f1e..e1b59670 100644 --- a/osbenchmark/utils/io.py +++ b/osbenchmark/utils/io.py @@ -236,6 +236,34 @@ def ensure_dir(directory, mode=0o777): if directory: os.makedirs(directory, mode, exist_ok=True) +def ensure_symlink(source, link_name): + """ + Ensure that a symlink exists from link_name to source. + If link_name already exists, it will be updated or replaced as necessary. + + :param source: The target of the symlink + :param link_name: The path where the symlink should be created + """ + logger = logging.getLogger(__name__) + if os.path.exists(link_name): + if os.path.islink(link_name): + if os.readlink(link_name) != source: + os.remove(link_name) + os.symlink(source, link_name) + logger.info("Updated symlink: %s -> %s", link_name, source) + else: + logger.info("Symlink already correct: %s -> %s", link_name, source) + elif os.path.isdir(link_name): + shutil.rmtree(link_name) + os.symlink(source, link_name) + logger.info("Replaced directory with symlink: %s -> %s", link_name, source) + else: + os.remove(link_name) + os.symlink(source, link_name) + logger.info("Replaced file with symlink: %s -> %s", link_name, source) + else: + os.symlink(source, link_name) + logger.info("Created symlink: %s -> %s", link_name, source) def _zipdir(source_directory, archive): for root, _, files in os.walk(source_directory):