mkSymlink.py 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. #!/usr/bin/env python3
  2. import os
  3. import sys
  4. BASE_DIRECTORY = 'files'
  5. HOME_DIRECTORY = os.getenv("HOME")
  6. def recurseInstall(path=''):
  7. def homepath(entry):
  8. return os.path.join(HOME_DIRECTORY, path, entry)
  9. def gitpath(entry):
  10. return os.path.join(BASE_DIRECTORY, path, entry)
  11. def link(entry):
  12. os.symlink(os.path.join(os.getcwd(), gitpath(entry)), homepath(entry))
  13. for entry in os.listdir(os.path.join(BASE_DIRECTORY, path)):
  14. if os.path.exists(homepath(entry)):
  15. # The file/directory already exists
  16. if os.path.isdir(entry): # Directory: recurse on it
  17. recurseInstall(os.path.join(path, entry))
  18. else: # File: backup it, symlink it
  19. os.rename(homepath(entry), homepath(entry)+'.bck')
  20. link(entry)
  21. else:
  22. link(entry)
  23. if __name__ == '__main__':
  24. if not HOME_DIRECTORY:
  25. print("Couldn't find home directory.", file=sys.stderr)
  26. sys.exit(1)
  27. recurseInstall()