Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 83 lines (70 sloc) 2.25 KB
#!/usr/bin/env python
"""
This program takes a supplied path and expands it to explicitly show symlinks.
It can show non-relative symlink values with the '-f' option.
It can show the actual, symlink-less path with the '-a' option.
Examples:
> realpath /tmp/pathtest/stuff
/tmp[private/tmp]/pathtest/stuff[../../../Users/admindude/Documents/stuff]
> realpath -f /tmp/pathtest/stuff
/tmp[/private/tmp]/pathtest/stuff[/Users/admindude/Documents/stuff]
> realpath -a /tmp/pathtest/stuff
/Users/admindude/Documents/stuff
"""
import os, sys
from optparse import OptionParser
# set up terminal colors
class color:
PURPLE = '\033[95m'
CYAN = '\033[96m'
DARKCYAN = '\033[36m'
BLUE = '\033[94m'
GREEN = '\033[92m'
YELLOW = '\033[93m'
RED = '\033[91m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
END = '\033[0m'
# method to contruct the interleaved path
def interleaved_path(path):
expanded_path = list()
split_path = path.split("/")
path_str = ""
for item in split_path:
this_level = "/" + item
path_str += this_level
expanded_path.append(this_level)
if os.path.islink(path_str):
if full:
expanded_path.append(color.BOLD + "[" + os.path.realpath(path_str) + "]"+ color.END)
else:
expanded_path.append(color.BOLD + "[" + os.readlink(path_str) + "]"+ color.END)
return ("".join(expanded_path[1:]))
# method to parse the path, according to selected method
def parse_path(path):
if os.path.exists(path):
if actual:
print os.path.realpath(path)
else:
print interleaved_path(path)
else:
print "Path does not exist or contains a broken symlink."
sys.exit(1)
# main loop
def main():
global actual, full
usage = "usage: %prog [options] path"
parser = OptionParser(usage=usage)
parser.add_option("-f", "--full", action="store_true", dest="full", default=False,
help="show full symlink paths")
parser.add_option("-a", "--actual", action="store_true", dest="actual", default=False,
help="show the actual, non-interleaved path")
(options, args) = parser.parse_args()
actual = options.actual
full = options.full
if len(args) != 1:
parser.error("no path specified")
else:
parse_path(args[0])
if __name__ == "__main__":
main()