Last active
May 5, 2017 05:25
-
-
Save Niximacco/2cef33ff68941292e2e58ff3a479725a to your computer and use it in GitHub Desktop.
Clones all repositories a user owns into the current directory
This file contains hidden or 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 | |
import os | |
import urllib2 | |
import json | |
import sys | |
import time | |
import subprocess | |
from subprocess import call | |
class bcolors: | |
PULLED = '\033[94m' | |
EXISTS = '\033[92m' | |
DOESNTEXIST = '\033[93m' | |
END = '\033[0m' | |
# Set Github Key variable to the Environment Variable named "GITHUB_KEY" | |
githubKey = os.environ['GITHUB_KEY'] | |
# Call the repos endpoint from github, get the user's repos, parse this into JSON | |
request = urllib2.Request("https://api.github.com/user/repos", headers={"Authorization" : "token " + githubKey}) | |
contents = urllib2.urlopen(request).read() | |
reposJson = json.loads(contents) | |
# Iterate all of the repos the user owns | |
for repo in reposJson: | |
repoName = repo['name'] | |
# Determine if the repo already exists | |
if os.path.isdir(repoName): | |
# Repo exists, print it out in green. | |
print bcolors.EXISTS + repoName + bcolors.END, | |
sys.stdout.flush() | |
else: | |
# Repo doesn't exist, print the name in orange | |
print bcolors.DOESNTEXIST + repoName + bcolors.END, | |
sys.stdout.flush() | |
# clone the repo using ssh and no output | |
call (["git", "clone", repo['git_url']], stdout=open(os.devnull, "w"), stderr=subprocess.STDOUT) | |
# Print the repo in blue when it is finished pulling | |
print "\r" + bcolors.PULLED + repoName + bcolors.END, | |
sys.stdout.flush() | |
# Wait a second to make it look good | |
time.sleep(1) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment