public
Created — forked from kennethreitz/sync.py

GitHub Syncer. Clones or Pulls all GitHub repos (including watched list).

  • Download Gist
sync.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
#!/usr/bin/env python
# -*- coding: utf-8 -*-
 
"""Kenneth Reitz's GitHub Syncer
 
This script uses the GitHub API to get a list of all forked, mirrored, public, and
private repos in your GitHub account. If the repo already exists locally, it will
update it via git-pull. Otherwise, it will properly clone the repo.
 
It will organize your repos into the following directory structure:
 
+ repos
├── forks (public fork repos)
├── mirrors (public mirror repos)
├── private (private repos)
├── public (public repos)
├── watched (public watched repos)
└── sync.py (this script)
 
Requires Ask Solem's github2 (http://pypi.python.org/pypi/github2).
 
Inspired by Gisty (http://github.com/swdyh/gisty).
"""
 
import os
from commands import getoutput as cmd
 
from github2.client import Github
 
 
# GitHub configurations
GITHUB_USER = cmd('git config github.user')
GITHUB_TOKEN = cmd('git config github.token')
 
# API Object
github = Github(username=GITHUB_USER, api_token=GITHUB_TOKEN)
 
 
# repo slots
repos = {}
 
repos['watched'] = [r for r in github.repos.watching(GITHUB_USER)]
repos['private'] = []
repos['mirrors'] = []
repos['public'] = []
repos['forks'] = []
 
# Collect GitHub repos via API
for repo in github.repos.list():
 
if repo.private:
repos['private'].append(repo)
elif repo.fork:
repos['forks'].append(repo)
elif 'mirror' in repo.description.lower():
# mirrors owned by self if mirror in description...
repos['mirrors'].append(repo)
else:
repos['public'].append(repo)
 
 
for org, repos in repos.iteritems():
for repo in repos:
# create org directory (safely)
try:
os.makedirs(org)
except OSError:
pass
# enter org dir
os.chdir(org)
# I own the repo
private = True if org in ('private', 'fork', 'mirror') else False
 
# just `git pull` if it's already there
if os.path.exists(repo.name):
os.chdir(repo.name)
print('Updating repo: %s' % (repo.name))
os.system('git pull')
os.chdir('..')
else:
if private:
print('Cloning private repo: %s' % (repo.name))
os.system('git clone git@github.com:%s/%s.git' % (repo.owner, repo.name))
else:
print('Cloning repo: %s' % (repo.name))
os.system('git clone git://github.com/%s/%s.git' % (repo.owner, repo.name))
# return to base
os.chdir('..')
print

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.