about summary refs log tree commit diff stats
ModeNameSize
-rw-r--r--.hgtags675log stats plain blame
-rw-r--r--LICENSE1192log stats plain blame
-rw-r--r--Makefile1542log stats plain blame
-rw-r--r--README1096log stats plain blame
-rw-r--r--client.c8862log stats plain blame
-rw-r--r--config.arg.h2606log stats plain blame
-rw-r--r--config.default.h2613log stats plain blame
-rw-r--r--config.mk472log stats plain blame
-rw-r--r--draw.c4588log stats plain blame
-rw-r--r--dwm.13239log stats plain blame
-rw-r--r--dwm.h5796log stats plain blame
-rw-r--r--dwm.png373log stats plain blame
-rw-r--r--event.c8575log stats plain blame
-rw-r--r--main.c7547log stats plain blame
-rw-r--r--tag.c2553log stats plain blame
-rw-r--r--util.c1164log stats plain blame
-rw-r--r--view.c4726log stats plain blame
# This file is part of ranger, the console file manager.
# License: GNU GPL version 3, see the file "AUTHORS" for details.

from __future__ import (absolute_import, division, print_function)

from os import symlink, sep


def relative_symlink(src, dst):
    common_base = get_common_base(src, dst)
    symlink(get_relative_source_file(src, dst, common_base), dst)


def get_relative_source_file(src, dst, common_base=None):
    if common_base is None:
        common_base = get_common_base(src, dst)
    return '../' * dst.count('/', len(common_base)) + src[len(common_base):]


def get_common_base(src, dst):
    if not src or not dst:
        return '/'
    i = 0
    while True:
        new_i = src.find(sep, i + 1)
        if new_i == -1:
            break
        if not dst.startswith(src[:new_i + 1]):
            break
        i = new_i
    return src[:i + 1]