annotate sync.py @ 0:90405940c263

start Ignore-this: dff7ea315a05d30553e01e9a903aff4e
author drewp@bigasterisk.com
date Mon, 04 Apr 2011 01:24:59 -0700
parents
children 1da34eecdfd8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
drewp@bigasterisk.com
parents:
diff changeset
1 #!bin/python
drewp@bigasterisk.com
parents:
diff changeset
2
drewp@bigasterisk.com
parents:
diff changeset
3 import os, subprocess, urllib2, jsonlib, logging, traceback
drewp@bigasterisk.com
parents:
diff changeset
4 from github import github
drewp@bigasterisk.com
parents:
diff changeset
5 logging.basicConfig(level=logging.INFO)
drewp@bigasterisk.com
parents:
diff changeset
6 log = logging.getLogger()
drewp@bigasterisk.com
parents:
diff changeset
7
drewp@bigasterisk.com
parents:
diff changeset
8 class Project(object):
drewp@bigasterisk.com
parents:
diff changeset
9 def __init__(self, config, gh, name):
drewp@bigasterisk.com
parents:
diff changeset
10 self.config = config
drewp@bigasterisk.com
parents:
diff changeset
11 self.gh = gh
drewp@bigasterisk.com
parents:
diff changeset
12 self.name = name
drewp@bigasterisk.com
parents:
diff changeset
13
drewp@bigasterisk.com
parents:
diff changeset
14 def gitDir(self):
drewp@bigasterisk.com
parents:
diff changeset
15 gitDir = os.path.join(self.config['gitSyncDir'], self.name)
drewp@bigasterisk.com
parents:
diff changeset
16 try:
drewp@bigasterisk.com
parents:
diff changeset
17 os.mkdir(gitDir)
drewp@bigasterisk.com
parents:
diff changeset
18 except OSError: pass
drewp@bigasterisk.com
parents:
diff changeset
19 return gitDir
drewp@bigasterisk.com
parents:
diff changeset
20
drewp@bigasterisk.com
parents:
diff changeset
21 def syncToLocalGit(self):
drewp@bigasterisk.com
parents:
diff changeset
22 darcsDir = os.path.join(self.config['darcsDir'], self.name)
drewp@bigasterisk.com
parents:
diff changeset
23 try:
drewp@bigasterisk.com
parents:
diff changeset
24 os.rmdir(os.path.join(darcsDir, 'darcs_testing_for_nfs'))
drewp@bigasterisk.com
parents:
diff changeset
25 except OSError: pass
drewp@bigasterisk.com
parents:
diff changeset
26 self.runGitCommand([self.config['darcsToGitCmd'], '--no-verbose', darcsDir])
drewp@bigasterisk.com
parents:
diff changeset
27
drewp@bigasterisk.com
parents:
diff changeset
28 def runGitCommand(self, args):
drewp@bigasterisk.com
parents:
diff changeset
29 subprocess.check_call(args, cwd=self.gitDir(),
drewp@bigasterisk.com
parents:
diff changeset
30 env={'SSH_AUTH_SOCK': self.config['SSH_AUTH_SOCK']})
drewp@bigasterisk.com
parents:
diff changeset
31
drewp@bigasterisk.com
parents:
diff changeset
32 def makeGitHubRepo(self):
drewp@bigasterisk.com
parents:
diff changeset
33 try:
drewp@bigasterisk.com
parents:
diff changeset
34 self.gh.repos.create(self.name)
drewp@bigasterisk.com
parents:
diff changeset
35 except urllib2.HTTPError:
drewp@bigasterisk.com
parents:
diff changeset
36 return
drewp@bigasterisk.com
parents:
diff changeset
37 self.runGitCommand(['git', 'remote', 'add', 'origin',
drewp@bigasterisk.com
parents:
diff changeset
38 'git@github.com:%s/%s.git' % (self.gh.user,
drewp@bigasterisk.com
parents:
diff changeset
39 self.name)])
drewp@bigasterisk.com
parents:
diff changeset
40
drewp@bigasterisk.com
parents:
diff changeset
41 def pushToGitHub(self):
drewp@bigasterisk.com
parents:
diff changeset
42 self.runGitCommand(['git', 'push', 'origin', 'master'])
drewp@bigasterisk.com
parents:
diff changeset
43
drewp@bigasterisk.com
parents:
diff changeset
44 config = jsonlib.read(open("config.json").read())
drewp@bigasterisk.com
parents:
diff changeset
45 gh = github.GitHub(config['user'], config['gitHubToken'])
drewp@bigasterisk.com
parents:
diff changeset
46
drewp@bigasterisk.com
parents:
diff changeset
47 for proj in os.listdir(config['darcsDir']):
drewp@bigasterisk.com
parents:
diff changeset
48 if 'repo' not in proj:
drewp@bigasterisk.com
parents:
diff changeset
49 continue
drewp@bigasterisk.com
parents:
diff changeset
50 if not os.path.isdir(os.path.join(config['darcsDir'], proj)):
drewp@bigasterisk.com
parents:
diff changeset
51 continue
drewp@bigasterisk.com
parents:
diff changeset
52 try:
drewp@bigasterisk.com
parents:
diff changeset
53 p = Project(config, gh, proj)
drewp@bigasterisk.com
parents:
diff changeset
54 log.info("syncing %s" % proj)
drewp@bigasterisk.com
parents:
diff changeset
55 p.syncToLocalGit()
drewp@bigasterisk.com
parents:
diff changeset
56 p.makeGitHubRepo()
drewp@bigasterisk.com
parents:
diff changeset
57 p.pushToGitHub()
drewp@bigasterisk.com
parents:
diff changeset
58 except Exception, e:
drewp@bigasterisk.com
parents:
diff changeset
59 traceback.print_exc()
drewp@bigasterisk.com
parents:
diff changeset
60