Skip to content

Commit cfb86c2

Browse files
committed
add new ensure_symlink util function to help prevent FileExists errors
Signed-off-by: Michael Oviedo <mikeovi@amazon.com>
1 parent f65ba51 commit cfb86c2

File tree

2 files changed

+54
-16
lines changed

2 files changed

+54
-16
lines changed

osbenchmark/paths.py

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,33 +21,43 @@
2121
# KIND, either express or implied. See the License for the
2222
# specific language governing permissions and limitations
2323
# under the License.
24+
import logging
2425
import os
2526
import sys
26-
from osbenchmark.utils.io import ensure_dir
27+
from osbenchmark.utils.io import ensure_dir, ensure_symlink
2728

2829
def benchmark_confdir():
30+
logger = logging.getLogger("opensearch_benchmark")
2931
default_home = os.path.expanduser("~")
3032
old_path = os.path.join(default_home, ".benchmark")
3133
new_path = os.path.join(default_home, ".osb")
3234

33-
# ensure both directories exist
34-
ensure_dir(old_path)
35-
ensure_dir(new_path)
35+
try:
36+
# Ensure .benchmark directory exists
37+
ensure_dir(old_path)
38+
logger.info(f"Ensured directory exists: {old_path}")
3639

37-
# Create symlink from .osb to .benchmark if it doesn't exist
38-
if not os.path.islink(new_path):
39-
try:
40-
os.symlink(old_path, new_path, target_is_directory=True)
41-
except OSError as e:
42-
error_message = (
43-
f"OSError: Failed to create symlink from {new_path} to {old_path}\n"
44-
f"Error type: {type(e).__name__}\n"
45-
f"Error message: {str(e)}\n"
46-
)
47-
print(error_message, file=sys.stderr)
40+
# Ensure symlink from .osb to .benchmark
41+
ensure_symlink(old_path, new_path)
4842

49-
return os.path.join(os.getenv("BENCHMARK_HOME", default_home), ".osb")
43+
final_path = os.path.join(os.getenv("BENCHMARK_HOME", default_home), ".osb")
5044

45+
return final_path
46+
47+
except Exception as e:
48+
error_message = (
49+
f"Error in benchmark_confdir:\n"
50+
f"Error type: {type(e).__name__}\n"
51+
f"Error message: {str(e)}\n"
52+
f"Current user: {os.getlogin()}\n"
53+
f"Current working directory: {os.getcwd()}\n"
54+
f"Python version: {sys.version}\n"
55+
f"Operating system: {sys.platform}\n"
56+
f"Permissions of {old_path}: {oct(os.stat(old_path).st_mode) if os.path.exists(old_path) else 'N/A'}\n"
57+
f"Permissions of parent of {new_path}: {oct(os.stat(os.path.dirname(new_path)).st_mode)}"
58+
)
59+
print(error_message)
60+
raise
5161

5262
def benchmark_root():
5363
return os.path.dirname(os.path.realpath(__file__))

osbenchmark/utils/io.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,34 @@ def ensure_dir(directory, mode=0o777):
236236
if directory:
237237
os.makedirs(directory, mode, exist_ok=True)
238238

239+
def ensure_symlink(source, link_name):
240+
"""
241+
Ensure that a symlink exists from link_name to source.
242+
If link_name already exists, it will be updated or replaced as necessary.
243+
244+
:param source: The target of the symlink
245+
:param link_name: The path where the symlink should be created
246+
"""
247+
logger = logging.getLogger(__name__)
248+
if os.path.exists(link_name):
249+
if os.path.islink(link_name):
250+
if os.readlink(link_name) != source:
251+
os.remove(link_name)
252+
os.symlink(source, link_name)
253+
logger.info("Updated symlink: %s -> %s", link_name, source)
254+
else:
255+
logger.info("Symlink already correct: %s -> %s", link_name, source)
256+
elif os.path.isdir(link_name):
257+
shutil.rmtree(link_name)
258+
os.symlink(source, link_name)
259+
logger.info("Replaced directory with symlink: %s -> %s", link_name, source)
260+
else:
261+
os.remove(link_name)
262+
os.symlink(source, link_name)
263+
logger.info("Replaced file with symlink: %s -> %s", link_name, source)
264+
else:
265+
os.symlink(source, link_name)
266+
logger.info("Created symlink: %s -> %s", link_name, source)
239267

240268
def _zipdir(source_directory, archive):
241269
for root, _, files in os.walk(source_directory):

0 commit comments

Comments
 (0)