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