-
-
Save zodsoft/430669d2c253f74fd9d092f7cd0bcd1c to your computer and use it in GitHub Desktop.
[Python] Creates a relative symlink based on two directories. If only one directory is passed as an argument, the symlink will be created in the current working directory [Mac, Terminal, Symbolic Link, Absolute]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
################################# | |
# ~~~~~symlink | |
# | |
# version: 0.11 | |
# | |
# creates a relative symbolic link pointing to the | |
# directory specified in the argument after '-to'. | |
# | |
# usage: | |
# ./symlink.py -to /some/dir/to/link/to -from /another/dir/to/create/link/in | |
# or | |
# ./symlink.py -to /some/dir | |
# (creates the link in cwd) | |
# | |
# Symlinks can be used as folder to point to, too. | |
# | |
################################ | |
import argparse, os | |
import relpath | |
__author__ = 'powder' | |
parser = argparse.ArgumentParser(description='This is a script to create symlinks based on two absolute folders.') | |
parser.add_argument('-to','--tofolder', help='The folder to link to',required=True) | |
parser.add_argument('-from','--fromfolder',help='The folder where the symlink should be created', required=False) | |
args = parser.parse_args() | |
#print ("To folder: %s" % args.tofolder ) | |
#print ("From folder: %s" % args.fromfolder ) | |
tofolder = args.tofolder | |
if os.path.islink(tofolder): | |
tofolder = os.path.realpath(tofolder); | |
if args.fromfolder: | |
fromfolder = args.fromfolder | |
else: | |
fromfolder = os.getcwd() | |
relativepath = relpath.relpath(tofolder, fromfolder) | |
print('Creating symlink to '+relativepath) | |
fromfolderWithFoldername = fromfolder + '/' + os.path.basename(tofolder) | |
#print('fromfolderWithFoldername: '+fromfolderWithFoldername) | |
os.symlink(relativepath, fromfolderWithFoldername) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment