Instantly share code, notes, and snippets.

Embed
What would you like to do?
Clones all repositories a user owns into the current directory
#!/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)
print
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment