From 6c6ae63821e60c6560d8e5b56eabbbc642ad4e55 Mon Sep 17 00:00:00 2001 From: Peter Ross Date: Thu, 8 Jun 2023 17:03:30 +1000 Subject: [PATCH] utils: add symlink_or_copyfile --- icefall/utils.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/icefall/utils.py b/icefall/utils.py index d002982ec..f24a7165e 100644 --- a/icefall/utils.py +++ b/icefall/utils.py @@ -28,6 +28,7 @@ from contextlib import contextmanager from dataclasses import dataclass from datetime import datetime from pathlib import Path +from shutil import copyfile from typing import Dict, Iterable, List, Optional, TextIO, Tuple, Union import k2 @@ -1881,3 +1882,20 @@ def is_cjk(character): ] ] ) + + +def symlink_or_copyfile(exp_dir: Path, src: str, dst: str): + """ + In the experiment directory, create a symlink pointing to src named dst. + If symlink creation fails (Windows?), fall back to copyfile.""" + + dir_fd = os.open(exp_dir, os.O_RDONLY) + try: + os.remove(dst, dir_fd=dir_fd) + except FileNotFoundError: + pass + try: + os.symlink(src=src, dst=dst, dir_fd=dir_fd) + except OSError: + copyfile(src=exp_dir / src, dst=exp_dir / dst) + os.close(dir_fd)