File: make_rel_symlink.py

package info (click to toggle)
lirc 0.10.1-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 8,364 kB
  • sloc: ansic: 26,717; cpp: 9,066; sh: 5,521; python: 4,362; makefile: 1,034; xml: 63
file content (44 lines) | stat: -rw-r--r-- 1,115 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import os
import os.path
import sys
import pdb
import shutil

def relative_ln_s( from_, to_ ):
    """

    This is just so dirty & boring: create a relative symlink, making the
    to_ path relative to from_. No errorchecks. Both arguments must be
    files, a destination directory doesn't work (I think). An existing
    file in to_ will be removed.

    """
    prefix = os.path.commonprefix( [ to_, from_ ] )
    if prefix == '':
        prefix = '/'
    source = from_.split(prefix )[ 1 ]
    dest = to_.split(prefix)[1]
    level = len(dest.split('/')) - 1
    path =  ('../' * level) + source
    return path

USAGE = 'Usage: make_rel_symlink [-p]  <sourcefile> <destfile>'

just_print = False;
if sys.argv[1] == "-p":
    just_print = True;
    sys.argv = sys.argv[ 1:]

if len(sys.argv) != 3:
    print(USAGE)
    sys.exit(1)

link_path = relative_ln_s(sys.argv[1], sys.argv[2])
if just_print:
    print(link_path)
else:
    os.chdir( os.path.dirname( sys.argv[2]))
    target = os.path.basename( sys.argv[2])
    if os.path.exists( target ):
        os.unlink( target)
    os.symlink( link_path, target)