Created
December 17, 2010 21:41
-
-
Save ymasory/745755 to your computer and use it in GitHub Desktop.
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/python | |
# | |
# Automatically synchronize your workspace with GitHub using the GitHub API. | |
# | |
# Prerequisites: | |
# - Install pip. | |
# On Ubuntu: sudo apt-get install python-pip | |
# - Using pip, install github2 Python bindings. | |
# sudo pip install github2 | |
# - Install curl. | |
# On Ubuntu: sudo apt-get install curl | |
# - Create an environment variable called 'work' with the absolute path of your | |
# workspace. | |
# e.g., export work="$HOME/workspace" | |
# - Replace the question marks in the token variable with your API token found | |
# on GitHub.com in Account Settings -> Account Admin. | |
# - Replace the question marks in the uname variable with your GitHub username. | |
# | |
# Usage: | |
# - python path/to/sync-github.py | |
# | |
# Copyright (C) 2010 Yuvi Masory (ymasory@gmail.com) | |
# This program comes with ABSOLUTELY NO WARRANTY; | |
# This is free software, and you are welcome to redistribute it under the | |
# conditions of the GNU General Public License version 3 | |
# (http://www.gnu.org/licenses/gpl-3.0.txt). | |
import os.path, subprocess, sys, re | |
from github2.client import Github | |
#-- FILL THESE IN --# | |
token = '???????' #your GitHub API token | |
uname = '???????' #your GitHub username | |
#-- figure out what groups you belong to --# | |
groups = [uname] | |
orgcmd = ['curl', '-su', "'" + uname + ':' + token + "'", | |
'http://github.com/api/v2/xml/user/show/' + uname + '/organizations'] | |
(stdout, stderr) = subprocess.Popen(orgcmd, stdout=subprocess.PIPE).communicate() | |
iterator = re.compile('<login>(.*?)</login>').finditer(stdout) | |
for match in iterator: | |
groups.append(match.group(1)) | |
env_var = 'work' #environemnt variable of your workspace | |
def sync(): | |
try: | |
workspace = os.environ[env_var] | |
except: | |
print 'workspace var not set' | |
sys.exit(1) | |
if not os.path.isabs(workspace): | |
print 'workspace var must be absolute' | |
sys.exit(1) | |
if not os.path.exists(workspace): | |
print 'CREATE WORKSPACE' | |
os.mkdir(workspace) | |
def mkGroup(group): | |
path = workspace + os.sep + group | |
if not os.path.exists(path): | |
print 'CREATE GROUP SUB-WORKSPACE ' + path | |
os.mkdir(path) | |
map(mkGroup, groups) | |
for group in groups: | |
print 'SYNC GROUP ' + group | |
dest = workspace + os.sep + group | |
os.chdir(dest) | |
github = Github(username=uname, api_token=token) | |
repos = github.repos.list(group) | |
file_names = [f for f in os.listdir(dest) if os.path.isfile(dest + os.sep + f)] | |
for el in file_names: | |
print 'UNKOWN FILE: ' + el | |
dir_names = [f for f in os.listdir(dest) if os.path.isdir(dest + os.sep + f)] | |
repo_names = [r.name for r in repos] | |
for el in repo_names: | |
if el not in dir_names: | |
try: | |
print 'cloning ' + el | |
path = 'git@github.com:' + group + os.sep + el + '.git' | |
to = dest + os.sep + el | |
subprocess.call(['git', 'clone', path, to], stdout=subprocess.PIPE) | |
except OSError: | |
print 'OSError!' | |
for el in repo_names: | |
try: | |
os.chdir(dest + os.sep + el) | |
print 'updating ' + group + os.sep + el | |
subprocess.call(['git', 'pull'], stdout=subprocess.PIPE) | |
except OSError: | |
print 'OSError!' | |
os.chdir(dest) | |
for el in dir_names: | |
if el not in repo_names: | |
print 'UNKOWN DIRECTORY: ' + el | |
if __name__ == '__main__': | |
sync() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment