Created
July 28, 2014 17:50
-
-
Save knifewine/1407cc6193de639d13e9 to your computer and use it in GitHub Desktop.
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 python | |
import os, subprocess | |
#TODO: make this script generic so it can take options for what env var to get choices from, what env var to write them to, and where the target symlink should be | |
home = os.path.expanduser("~") | |
symlink_path = os.path.join(home, 'cassandra_for_ccm') | |
current_cass_dir = os.environ.get('CASSANDRA_DIR', None) | |
if not current_cass_dir == symlink_path: | |
print "please set CASSANDRA_DIR to {}".format(symlink_path) | |
exit(1) | |
possible_dirs = os.environ.get('CASSANDRA_DIRS', None) | |
if possible_dirs is None: | |
print "CASSANDRA_DIRS is not defined." | |
print " this should be a colon-delimited list" | |
exit(1) | |
if os.path.exists(symlink_path) and os.path.islink(symlink_path): | |
print "CASSANDRA_DIR is currently linked to {}\n".format(os.readlink(symlink_path)) | |
dir_array = possible_dirs.split(":") | |
for idx, dirname in enumerate(dir_array, 1): | |
git_label = subprocess.check_output(["git", "status"], cwd=dirname).split("\n")[0] | |
print "{idx}. {dirname} ({git_label})".format(idx=idx, dirname=dirname, git_label=git_label) | |
choice = raw_input("\nplease choose a number (enter to quit): ") | |
try: | |
int_choice = int(choice) | |
except ValueError: | |
if len(choice) == 0: | |
print "quitting" | |
exit(0) | |
print "invalid choice" | |
exit(1) | |
if (int_choice <= 0) or int_choice > len(dir_array): | |
print "invalid number" | |
exit(1) | |
if os.path.exists(symlink_path) and os.path.islink(symlink_path): | |
os.unlink(symlink_path) | |
os.symlink(dir_array[int_choice-1], symlink_path) | |
print "CASSANDRA_DIR is now linked to {}\n".format(os.readlink(symlink_path)) | |
if not os.path.exists(symlink_path): | |
os.symlink(dir_array[int_choice-1], symlink_path) | |
print "CASSANDRA_DIR is now linked to {}\n".format(os.readlink(symlink_path)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment