Skip to content
Snippets Groups Projects
symlink_absolute.py 1.03 KiB
Newer Older
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import os
import sys


def handle_files(files):
    for file in files:
        if os.path.islink(file):
            real = os.path.realpath(file)
            old = file + ".old"

            os.rename(file, old)
            os.symlink(real, file)
            os.remove(old)

            print(file, "is now absolute pointing at", real)
        else:
            print(file, "is not a symlink or is broken")


def usage():
    print("This script is useful to transform relative symlinks into absolute.")
    print("As a side-effect if the symlink points to chain of symlinks these "
          "will be resolved such that the new symlink points directly to the "
          "original file")
    print("\nSee also: symlink_relative.py\n")
    print("\nUsage:")
    print("  ", sys.argv[0], "<symlink1> [<symlink2> ...]\n")


def main():
    files = sys.argv[1:]
    if not files:
        usage()
        sys.exit(1)

    handle_files(files)


if __name__ == "__main__":
    main()

# vim: ai sts=4 et sw=4