Skip to content

Instantly share code, notes, and snippets.

@berndweiss
Forked from leetrout/aREADME.rst
Created October 7, 2012 07:33
Show Gist options
  • Save berndweiss/3847428 to your computer and use it in GitHub Desktop.
Save berndweiss/3847428 to your computer and use it in GitHub Desktop.
Clone all your github repos into a local directory

USAGE

python ghclone.py

Follow the prompts

Notes

You should make sure you have ssh keys setup or you'll have to enter your username and password every time.

import getpass
import json
import os
from subprocess import call
import urllib2
cred = {
'user': '',
'pass': ''
}
password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
API_BASE_URL = "https://api.github.com"
def clone():
password_mgr.add_password(None, API_BASE_URL, cred['user'], cred['pass'])
handler = urllib2.HTTPBasicAuthHandler(password_mgr)
opener = urllib2.build_opener(handler)
page = opener.open(API_BASE_URL + '/user/repos')
data = page.read()
page.close()
repos = json.loads(data)
os.chdir(target_dir)
cnt = 0
for repo in repos:
url = repo['git_url']
call(['git', 'clone', url])
print("cloned %s" % url)
cnt += 1
print('cloned %d repos in %s' % (cnt, target_dir))
if __name__ == '__main__':
cred['user'] = raw_input('Username: ')
cred['pass'] = getpass.getpass('Password: ')
cwd = os.getcwd()
target_dir = os.path.abspath(os.path.expanduser(
raw_input('Target directory (%s): ' % cwd) or cwd))
clone()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment