Advertisement
Guest User

Untitled

a guest
May 29th, 2015
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 KB | None | 0 0
  1. # Small python script to replace full path in prompt with symlinks where
  2. # appropriate. Set your shell prompt to this script's output with
  3. ######################################################################
  4. # export PROMPT_COMMAND='PS1="$(python /path-to/symlinkifypath.py)"' #
  5. ######################################################################
  6. # in your .bashrc.
  7.  
  8. # Set 'symLinksDir' and 'tilde' as per your needs below.
  9. # Please make sure all your symlinks are the complete path - no symlinks
  10. # inside another.. This script works for symlinks that start from the root
  11. # directory, it replaces the set of characters starting from the beginning of
  12. # the complete path with your symlink name.
  13.  
  14. import os
  15. from os.path import isfile,islink,join
  16. from commands import getoutput
  17. from socket import gethostname
  18. import operator
  19.  
  20. # Set this to where you store symbolic links
  21. symLinksDir = '/home/jmazz'
  22. # May not wish to use '~' if your symLinksDir is not your home directory
  23. tilde = '~'
  24.  
  25. # Get hostname, username, cwd, pwd
  26. hostname = gethostname()
  27. username = os.environ['USER']
  28. pwd = os.getcwd()
  29. homedir = os.path.expanduser('~')
  30. pwd = pwd.replace(homedir, '~', 1)
  31.  
  32. # Build list of directories in symLinksDir
  33. dirs = []
  34. for (dirpath, dirnames, filenames) in os.walk(symLinksDir):
  35. dirs.extend(dirnames)
  36. break
  37.  
  38. # Build dict of symlinks. {key,value} pair as
  39. # {linkName, linkPath}
  40. symLinks = {}
  41. for potentialLink in dirs:
  42. if islink(join(symLinksDir,potentialLink)):
  43. symLink = potentialLink
  44. path = join(symLinksDir,potentialLink)
  45. symLinks.update({symLink:os.readlink(path)})
  46.  
  47. # Build dict storing lengths of linkPath for each linkName
  48. # {linkName, len(linkPath)}
  49. symLinksLens = {}
  50. for key in symLinks.keys():
  51. symLinksLens.update({key:len(symLinks[key])})
  52.  
  53. # Sort symLinksLens by values; gives list of tuples of the form
  54. # (linkName, len(linkPath))
  55. sorted_symLinksLens = sorted(symLinksLens.items(), key=operator.itemgetter(1))
  56. # Highest to lowest
  57. sorted_symLinksLens.reverse()
  58.  
  59. # List of linkNames, in order from highest to lowest string length of linkPath
  60. linkNames = []
  61. for link in sorted_symLinksLens:
  62. linkNames.append(link[0])
  63.  
  64. # Check if full path contains any of the symLinks, and alter pwd accordingly
  65. for linkName in linkNames:
  66. sL = symLinks[linkName]
  67. if sL == pwd[:len(sL)]:
  68. pwd = '%s/%s%s' % (tilde,linkName,pwd[len(sL):])
  69. break
  70.  
  71. # Print string for the shell prompt
  72. print '%s@%s:%s$ ' % (username, hostname, pwd)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement