changeset 0:90405940c263

start Ignore-this: dff7ea315a05d30553e01e9a903aff4e
author drewp@bigasterisk.com
date Mon, 04 Apr 2011 01:24:59 -0700
parents
children 1da34eecdfd8
files readme sync.py
diffstat 2 files changed, 74 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/readme	Mon Apr 04 01:24:59 2011 -0700
@@ -0,0 +1,14 @@
+Mirrors a directory of darcs repos onto github.
+
+Config description:
+{
+  "user"          : "<username on github>",
+  "SSH_AUTH_SOCK" : "path to ssh-agent socket file, like /tmp/ssh-abcde12345/agent.12345",
+  "gitHubToken"   : "<from https://github.com/account/admin>",
+  "gitsyncDir"    : "<dir to dump the local git mirrors>",
+  "darcsDir"      : "<dir whose subdirs are the darcs repos you want to mirror>",
+  "darcsToGitCmd" : "/usr/bin/darcs-to-git" 
+}
+
+See also:
+http://www.silverwareconsulting.com/index.cfm/2009/10/30/Placing-Config-Files-Under-Version-Control-with-Git-and-GitHub
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sync.py	Mon Apr 04 01:24:59 2011 -0700
@@ -0,0 +1,60 @@
+#!bin/python
+
+import os, subprocess, urllib2, jsonlib, logging, traceback
+from github import github
+logging.basicConfig(level=logging.INFO)
+log = logging.getLogger()
+
+class Project(object):
+    def __init__(self, config, gh, name):
+        self.config = config
+        self.gh = gh
+        self.name = name
+        
+    def gitDir(self):
+        gitDir = os.path.join(self.config['gitSyncDir'], self.name)
+        try:
+            os.mkdir(gitDir)
+        except OSError: pass
+        return gitDir
+    
+    def syncToLocalGit(self):
+        darcsDir = os.path.join(self.config['darcsDir'], self.name)
+        try:
+            os.rmdir(os.path.join(darcsDir, 'darcs_testing_for_nfs'))
+        except OSError: pass
+        self.runGitCommand([self.config['darcsToGitCmd'], '--no-verbose', darcsDir])
+
+    def runGitCommand(self, args):
+        subprocess.check_call(args, cwd=self.gitDir(),
+                          env={'SSH_AUTH_SOCK': self.config['SSH_AUTH_SOCK']})
+
+    def makeGitHubRepo(self):
+        try:
+            self.gh.repos.create(self.name)
+        except urllib2.HTTPError:
+            return
+        self.runGitCommand(['git', 'remote', 'add', 'origin',
+                            'git@github.com:%s/%s.git' % (self.gh.user,
+                                                          self.name)])
+
+    def pushToGitHub(self):
+        self.runGitCommand(['git', 'push', 'origin', 'master'])
+
+config = jsonlib.read(open("config.json").read())
+gh = github.GitHub(config['user'], config['gitHubToken'])
+
+for proj in os.listdir(config['darcsDir']):
+    if 'repo' not in proj:
+        continue
+    if not os.path.isdir(os.path.join(config['darcsDir'], proj)):
+        continue
+    try:
+        p = Project(config, gh, proj)
+        log.info("syncing %s" % proj)
+        p.syncToLocalGit()
+        p.makeGitHubRepo()
+        p.pushToGitHub()
+    except Exception, e:
+        traceback.print_exc()
+