#!/usr/bin/python # Python 2 or 3 compatible # If arguments are symlinks then replace with their eventual target. # If a cycle is detected then the beginning of the cycle is returned. # # Whether the target actually exists is not checked. Also arguments that aren't # even valid files are left alone. # Iain Murray, June 2009, June 2012. import os, sys def follow(file): try: next = os.readlink(file) if not os.path.isabs(next): next = os.path.join(os.path.dirname(file), next) return next except: return file for file in sys.argv[1:]: mem = set([file]) while True: file = follow(file) if file in mem: break else: mem.add(file) print(file)