|
29 | 29 | import mmap |
30 | 30 | import shutil |
31 | 31 | import subprocess |
| 32 | +import sys |
32 | 33 | import tarfile |
33 | 34 | import zipfile |
34 | 35 | import urllib.error |
@@ -236,6 +237,34 @@ def ensure_dir(directory, mode=0o777): |
236 | 237 | if directory: |
237 | 238 | os.makedirs(directory, mode, exist_ok=True) |
238 | 239 |
|
| 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}") |
239 | 268 |
|
240 | 269 | def _zipdir(source_directory, archive): |
241 | 270 | for root, _, files in os.walk(source_directory): |
|
0 commit comments