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 45 46 47 48 49 50 51 52
|
import os
import os.path
import sys
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 )
if os.path.isdir( sys.argv[2] ):
print("Removing link target dir:" + sys.argv[2])
if os.path.islink(sys.argv[2]):
os.unlink(sys.argv[2])
else:
shutil.rmtree(sys.argv[2])
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)
|