Created
December 11, 2015 01:04
-
-
Save ravikiranj/eb9836ef19c39abc530c to your computer and use it in GitHub Desktop.
Checkout all github repositories of a user
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 | |
""" | |
Usage: ./checkout_github_repos.py -u <github_user> -o <output_dir> | |
Example: ./checkout_github_repos.py -u ravikiranj -o . | |
""" | |
import argparse | |
import os | |
import sys | |
import traceback | |
import requests | |
from subprocess import call | |
import logging | |
logger = logging.getLogger(__name__) | |
logging.basicConfig(level=logging.INFO) | |
def get_github_repos(user): | |
url = "https://api.github.com/users/%s/repos?per_page=200" % (user) | |
r = requests.get(url) | |
repos = [] | |
status = "FAIL" | |
if r.status_code != 200: | |
return {"status": status, "repos": repos} | |
data = r.json() | |
status = "OK" | |
for repo in data: | |
if "clone_url" in repo: | |
repos.append(repo["clone_url"]) | |
return {"status": status, "repos": repos} | |
#end | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser(description='Checkout github repositories for a user') | |
parser.add_argument('-u', '--user', help='github user', required=True) | |
parser.add_argument('-o', '--output', help='directory to checkout github repositories', required=True) | |
try: | |
args = parser.parse_args() | |
output_dir = args.output | |
user = args.user | |
if not os.path.isdir(output_dir): | |
raise Exception("Directory = %s is not a valid directory" % output_dir) | |
if not os.access(output_dir, os.W_OK): | |
raise Exception("You don't have write permission on directory = %s" % output_dir) | |
result = get_github_repos(user) | |
if result["status"] != "OK": | |
raise Exception("Couldn't find repos for user = %s" % user) | |
count = 1 | |
repos = result["repos"] | |
total_repos = len(result["repos"]) | |
for repo in result["repos"]: | |
logger.info("Cloning repository %d out of %d" % (count, total_repos)) | |
call(["git", "clone", repo]) | |
count += 1 | |
except Exception as ex: | |
logger.info(ex.message) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment