Last active
August 29, 2015 14:26
-
-
Save 1stvamp/2e86414634b9bbf05b51 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
plugins_repos: | |
- https://github.com/juju/plugins.git | |
- https://github.com/bloodearnest/plugins.git#juju-local-dns | |
- https://github.com/bloodearnest/plugins.git#juju-add-local-env |
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 errno | |
from hashlib import md5 | |
from os import listdir, makedirs, path, symlink, unlink | |
import subprocess | |
import sys | |
import yaml | |
CONFIG_PATH = '~/.juju_plugins.yaml' | |
PLUGINS_DIR = '~/.juju_plugins' | |
CHECKOUTS_DIR = '~/.juju_plugins_checkouts' | |
PLUGINS_REPOS = ['https://github.com/juju/plugins.git'] | |
def load_config(config_path=CONFIG_PATH): | |
with file(path.expanduser(config_path)) as f: | |
config = yaml.load(f) | |
config.setdefault('plugins_repos', PLUGINS_REPOS) | |
config.setdefault('plugins_dir', PLUGINS_DIR) | |
config.setdefault('checkouts_dir', CHECKOUTS_DIR) | |
return config | |
def pluginate(config): | |
seen_commands = [] | |
for repo in config['plugins_repos']: | |
parts = repo.split('#') | |
if len(parts) > 1: | |
repo = parts[0] | |
branch = parts[1] | |
else: | |
branch = 'master' | |
plugins_dir = path.expanduser(config['plugins_dir']) | |
mkdir_p(plugins_dir, 0o744) | |
checkouts_dir = path.expanduser(config['checkouts_dir']) | |
mkdir_p(checkouts_dir, 0o774) | |
repo_hash = md5() | |
repo_hash.update('{}#{}'.format(repo, branch)) | |
co_path = path.join(checkouts_dir, repo_hash.hexdigest()) | |
if not path.exists(co_path): | |
base_path = checkouts_dir | |
cmds = ['git clone -b {} {} {}'.format(branch, repo, | |
repo_hash.hexdigest())] | |
else: | |
base_path = co_path | |
cmds = ['git fetch --all', | |
'git reset --hard origin/{}'.format(branch)] | |
for cmd in cmds: | |
pipes = subprocess.Popen(cmd, cwd=base_path, | |
stdout=sys.stdout, | |
stderr=sys.stderr, | |
shell=True) | |
pipes.wait() | |
new_commands = set(listdir(co_path)).difference(set(seen_commands)) | |
symlink_commands(co_path, new_commands, plugins_dir) | |
map(lambda f: seen_commands.append(f), new_commands) | |
print("\nDon't forget to add to your .bashrc:\n\nexport" | |
" PATH=$PATH:{}".format(plugins_dir)) | |
def symlink_commands(base, commands, plugins_dir): | |
for f in commands: | |
file_path = path.join(base, f) | |
if '.' not in f and path.isfile(file_path): | |
dest = path.join(plugins_dir, f) | |
if path.exists(dest): | |
unlink(dest) | |
symlink(file_path, dest) | |
def mkdir_p(dir_path, perms): | |
if not path.exists(dir_path): | |
try: | |
makedirs(dir_path, perms) | |
except OSError as exc: | |
if exc.errno == errno.EEXIST and path.isdir(dir_path): | |
pass | |
else: | |
raise | |
if __name__ == '__main__': | |
config_path = CONFIG_PATH | |
if len(sys.argv) > 1: | |
config_path = sys.argv[1] | |
pluginate(load_config(config_path)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment