mirror of
https://github.com/k2-fsa/icefall.git
synced 2025-12-11 06:55:27 +00:00
utils: add symlink_or_copyfile
This commit is contained in:
parent
3ae47a4940
commit
6c6ae63821
@ -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)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user