__init__.py 1000 B

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