123456789101112131415161718192021222324 |
- import os
- import sys
- def symlink(source, link_name, relative = False, override = False, backup = True, backup_suffix = "~"):
- if relative:
- source = os.path.relpath(source, os.path.dirname(link_name))
- # os.path.lexists() returns True if path refers to an existing path and
- # True for broken symbolic links.
- if override and os.path.lexists(link_name):
- if backup:
- backup_name_prefix = link_name + backup_suffix
- backup_name = backup_name_prefix
- backup_index = 0
- while os.path.lexists(backup_name):
- backup_index += 1
- backup_name = backup_name_prefix + str(backup_index)
- os.rename(link_name, backup_name)
- else:
- if os.path.isdir(link_name):
- os.rmdir(link_name)
- else:
- os.remove(link_name)
- if not os.path.lexists(link_name) or os.readlink(link_name) != source:
- os.symlink(source, link_name)
|