Skip to content

Commit 73197e5

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 73197e5

File tree

2 files changed

+33
-16
lines changed

2 files changed

+33
-16
lines changed

osbenchmark/paths.py

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,33 +22,21 @@
2222
# specific language governing permissions and limitations
2323
# under the License.
2424
import os
25-
import sys
26-
from osbenchmark.utils.io import ensure_dir
25+
from osbenchmark.utils.io import ensure_dir, ensure_symlink
2726

2827
def benchmark_confdir():
2928
default_home = os.path.expanduser("~")
3029
old_path = os.path.join(default_home, ".benchmark")
3130
new_path = os.path.join(default_home, ".osb")
3231

33-
# ensure both directories exist
32+
# Ensure .benchmark directory exists
3433
ensure_dir(old_path)
35-
ensure_dir(new_path)
3634

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)
35+
# Ensure symlink from .osb to .benchmark
36+
ensure_symlink(old_path, new_path)
4837

4938
return os.path.join(os.getenv("BENCHMARK_HOME", default_home), ".osb")
5039

51-
5240
def benchmark_root():
5341
return os.path.dirname(os.path.realpath(__file__))
5442

osbenchmark/utils/io.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import mmap
3030
import shutil
3131
import subprocess
32+
import sys
3233
import tarfile
3334
import zipfile
3435
import urllib.error
@@ -236,6 +237,34 @@ def ensure_dir(directory, mode=0o777):
236237
if directory:
237238
os.makedirs(directory, mode, exist_ok=True)
238239

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

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

0 commit comments

Comments
 (0)