Skip to content

Instantly share code, notes, and snippets.

@edwardgeorge
Created January 8, 2010 21:53
Show Gist options
  • Save edwardgeorge/272460 to your computer and use it in GitHub Desktop.
Save edwardgeorge/272460 to your computer and use it in GitHub Desktop.
sync gists to local hg repos
#!/usr/bin/env python
import os
import subprocess
import sys
import urlparse
from lxml import html
BASE_URL = 'http://gist.github.com/%s'
def get_gists_for_user(username):
url = BASE_URL % username
doc = html.parse(url)
doc = doc.getroot()
doc.make_links_absolute(url)
gists = doc.cssselect('#files .file .meta .info span a')
for gist in gists:
yield gist.attrib['href']
def hg_pull(repodir, remoteurl):
p = subprocess.Popen(['hg', '-R', repodir, 'pull', '-u', remoteurl],
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
stdout, stderr = p.communicate()
if p.wait() != 0:
print >> sys.stderr, 'error updating %s from %s' % (repodir, remoteurl)
print >> sys.stderr, stdout
sys.exit(1)
def hg_clone(targetdir, remoteurl):
p = subprocess.Popen(['hg', 'clone', remoteurl, targetdir],
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
stdout, stderr = p.communicate()
if p.wait() != 0:
print >> sys.stderr, 'error cloning %s to %s' % (remoteurl, targetdir)
print >> sys.stderr, stdout
sys.exit(1)
def sync_gist_to_dir(targetdir, gisturl):
gistnum = urlparse.urlparse(gisturl).path.lstrip('/')
gistdir = os.path.join(targetdir, gistnum)
doc = html.parse(gisturl)
doc = doc.getroot()
giturl = doc.xpath('//a[@rel="#git-clone"]/@href')[0]
hg_pull(gistdir, giturl) if os.path.exists(gistdir)\
else hg_clone(gistdir, giturl)
if __name__ == '__main__':
try:
username = sys.argv[1]
targetdir = sys.argv[2]
except IndexError, e:
print >> sys.stderr, 'Usage: %s username repo_target_dir' % sys.argv[0]
sys.exit(1)
targetdir = os.path.abspath(targetdir)
for gist in get_gists_for_user(username):
sync_gist_to_dir(targetdir, gist)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment