__init__.py 984 B

123456789101112131415161718192021222324
  1. import os
  2. import sys
  3. def symlink(source, link_name, relative = False, override = False, backup = True, backup_suffix = "~"):
  4. if relative:
  5. source = os.path.relpath(source, os.path.dirname(link_name))
  6. # os.path.lexists() returns True if path refers to an existing path and
  7. # True for broken symbolic links.
  8. if override and os.path.lexists(link_name):
  9. if backup:
  10. backup_name_prefix = link_name + backup_suffix
  11. backup_name = backup_name_prefix
  12. backup_index = 0
  13. while os.path.lexists(backup_name):
  14. backup_index += 1
  15. backup_name = backup_name_prefix + str(backup_index)
  16. os.rename(link_name, backup_name)
  17. else:
  18. if os.path.isdir(link_name):
  19. os.rmdir(link_name)
  20. else:
  21. os.remove(link_name)
  22. if not os.path.lexists(link_name) or os.readlink(link_name) != source:
  23. os.symlink(source, link_name)