Created
August 30, 2016 12:40
-
-
Save sevas/370b0ad1356d5a916b00f056cd9ea288 to your computer and use it in GitHub Desktop.
cross platform utility to make symlinks, useful to call from build events in cmake build scripts
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
import sys | |
import os | |
import subprocess | |
import argparse | |
import platform | |
def link(source, dest): | |
if not os.path.exists(source): | |
print("--- directory to be linked: {} does not exist".format(source)) | |
return 1 | |
print("--- linking {} to {}".format(source, dest)) | |
platform_name = platform.system() | |
if platform_name in ["Linux", "Darwin"]: | |
raise NotImplementedError() | |
elif platform_name == "Windows": | |
if os.path.exists(dest): | |
print("--- delete existing junction: {}".format(dest)) | |
ret = subprocess.call("fsutil reparsepoint delete {}".format(dest)) | |
if ret != 0: | |
print("!!! {} is not a junction point. Aborting".format(dest)) | |
return 1 | |
if os.path.exists(dest) and os.path.isdir(dest) and not os.listdir(dest): | |
print("--- delete leftover mount point at {}".format(dest)) | |
os.rmdir(dest) | |
else: | |
print("!!! {} is not an empty directory. Aborting".format(dest)) | |
return 1 | |
print("--- make new junction to {}".format(source)) | |
subprocess.call("mklink /J {} {}".format(dest, source), shell=True) | |
return 0 | |
def main(): | |
parser = argparse.ArgumentParser(prog='symlink.py') | |
parser.add_argument('-s', '--source', required=True, type=str) | |
parser.add_argument('-d', '--destination', required=True, type=str) | |
args = parser.parse_args() | |
return link(os.path.abspath(args.source), os.path.abspath(args.destination)) | |
if __name__ == '__main__': | |
sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment