Last active
December 14, 2015 22:38
-
-
Save dgrant/5159432 to your computer and use it in GitHub Desktop.
This script will create symlinks in specific "binpath" for all executables in sub-directories of a specified "gistpath"
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/env python3 | |
""" | |
create symlinks in binpath for all exectuables in sub-directories | |
within gistpath | |
""" | |
import argparse | |
import os | |
import shutil | |
def is_exe(path): | |
""" return true if the path is executable """ | |
return os.access(path, os.X_OK) | |
def create_gist_links(gistpath, binpath, noclobber=False): | |
""" | |
create symlinks in binpath for all exectuables in sub-directories | |
within gistpath | |
""" | |
gistpath = os.path.abspath(gistpath) | |
binpath = os.path.abspath(binpath) | |
if not noclobber: | |
if os.path.exists(binpath): | |
shutil.rmtree(binpath) | |
if not os.path.exists(binpath): | |
os.makedirs(binpath) | |
for path in os.listdir(gistpath): | |
fullpath = os.path.join(gistpath, path) | |
if os.path.isdir(fullpath) and os.path.exists(os.path.join(fullpath, '.git')): | |
for binname in os.listdir(fullpath): | |
fullbin = os.path.join(fullpath, binname) | |
if binname != '.git' and not os.path.isdir(fullbin) and is_exe(fullbin): | |
os.symlink(fullbin, os.path.join(binpath, binname)) | |
def main(): | |
""" main """ | |
parser = argparse.ArgumentParser() | |
parser.add_argument('gistpath', metavar='GIST_PATH', | |
help='Path where the gists are checked out') | |
parser.add_argument('binpath', metavar='BIN_PATH', | |
help='Path where the symlinks should go') | |
parser.add_argument('--noclobber', '-n', default=False, | |
action='store_true', | |
help='Do not clobber existing symlinks') | |
args = parser.parse_args() | |
create_gist_links(args.gistpath, args.binpath, args.noclobber) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment