annotate sync.py @ 1:1da34eecdfd8

port to PyGithub so repo creates work again Ignore-this: 5e00f0483c54df98dddda680b00d1318
author drewp@bigasterisk.com
date Tue, 08 Jan 2013 23:40:24 -0800
parents 90405940c263
children 22ccc05756de
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
1
1da34eecdfd8 port to PyGithub so repo creates work again
drewp@bigasterisk.com
parents: 0
diff changeset
3 import os, subprocess, urllib2, json, logging, traceback, time
1da34eecdfd8 port to PyGithub so repo creates work again
drewp@bigasterisk.com
parents: 0
diff changeset
4 from github import Github, GithubException
0
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):
1
1da34eecdfd8 port to PyGithub so repo creates work again
drewp@bigasterisk.com
parents: 0
diff changeset
29 try:
1da34eecdfd8 port to PyGithub so repo creates work again
drewp@bigasterisk.com
parents: 0
diff changeset
30 subprocess.check_call(args, cwd=self.gitDir(),
1da34eecdfd8 port to PyGithub so repo creates work again
drewp@bigasterisk.com
parents: 0
diff changeset
31 env={'SSH_AUTH_SOCK': self.config['SSH_AUTH_SOCK'],
1da34eecdfd8 port to PyGithub so repo creates work again
drewp@bigasterisk.com
parents: 0
diff changeset
32 'HOME': os.environ['HOME'], # darcs-to-git uses this
1da34eecdfd8 port to PyGithub so repo creates work again
drewp@bigasterisk.com
parents: 0
diff changeset
33 })
1da34eecdfd8 port to PyGithub so repo creates work again
drewp@bigasterisk.com
parents: 0
diff changeset
34 except:
1da34eecdfd8 port to PyGithub so repo creates work again
drewp@bigasterisk.com
parents: 0
diff changeset
35 log.error("in %s" % self.gitDir())
1da34eecdfd8 port to PyGithub so repo creates work again
drewp@bigasterisk.com
parents: 0
diff changeset
36 raise
0
drewp@bigasterisk.com
parents:
diff changeset
37
drewp@bigasterisk.com
parents:
diff changeset
38 def makeGitHubRepo(self):
drewp@bigasterisk.com
parents:
diff changeset
39 try:
1
1da34eecdfd8 port to PyGithub so repo creates work again
drewp@bigasterisk.com
parents: 0
diff changeset
40 self.gh.create_repo(self.name)
1da34eecdfd8 port to PyGithub so repo creates work again
drewp@bigasterisk.com
parents: 0
diff changeset
41 except GithubException, e:
1da34eecdfd8 port to PyGithub so repo creates work again
drewp@bigasterisk.com
parents: 0
diff changeset
42 assert e.data['errors'][0]['message'].startswith('name already exists'), e
0
drewp@bigasterisk.com
parents:
diff changeset
43 return
drewp@bigasterisk.com
parents:
diff changeset
44 self.runGitCommand(['git', 'remote', 'add', 'origin',
1
1da34eecdfd8 port to PyGithub so repo creates work again
drewp@bigasterisk.com
parents: 0
diff changeset
45 'git@github.com:%s/%s.git' % (self.gh.login,
0
drewp@bigasterisk.com
parents:
diff changeset
46 self.name)])
drewp@bigasterisk.com
parents:
diff changeset
47
drewp@bigasterisk.com
parents:
diff changeset
48 def pushToGitHub(self):
drewp@bigasterisk.com
parents:
diff changeset
49 self.runGitCommand(['git', 'push', 'origin', 'master'])
drewp@bigasterisk.com
parents:
diff changeset
50
1
1da34eecdfd8 port to PyGithub so repo creates work again
drewp@bigasterisk.com
parents: 0
diff changeset
51 config = json.loads(open("config.json").read())
1da34eecdfd8 port to PyGithub so repo creates work again
drewp@bigasterisk.com
parents: 0
diff changeset
52
1da34eecdfd8 port to PyGithub so repo creates work again
drewp@bigasterisk.com
parents: 0
diff changeset
53 # to get this token:
1da34eecdfd8 port to PyGithub so repo creates work again
drewp@bigasterisk.com
parents: 0
diff changeset
54 # curl -u drewp https://api.github.com/authorizations -d '{"scopes":["repo"]}'
1da34eecdfd8 port to PyGithub so repo creates work again
drewp@bigasterisk.com
parents: 0
diff changeset
55 # from http://developer.github.com/v3/oauth/#oauth-authorizations-api
1da34eecdfd8 port to PyGithub so repo creates work again
drewp@bigasterisk.com
parents: 0
diff changeset
56 gh = Github(config['gitHubToken']).get_user()
0
drewp@bigasterisk.com
parents:
diff changeset
57
drewp@bigasterisk.com
parents:
diff changeset
58 for proj in os.listdir(config['darcsDir']):
drewp@bigasterisk.com
parents:
diff changeset
59 if 'repo' not in proj:
drewp@bigasterisk.com
parents:
diff changeset
60 continue
drewp@bigasterisk.com
parents:
diff changeset
61 if not os.path.isdir(os.path.join(config['darcsDir'], proj)):
drewp@bigasterisk.com
parents:
diff changeset
62 continue
drewp@bigasterisk.com
parents:
diff changeset
63 try:
drewp@bigasterisk.com
parents:
diff changeset
64 p = Project(config, gh, proj)
drewp@bigasterisk.com
parents:
diff changeset
65 log.info("syncing %s" % proj)
drewp@bigasterisk.com
parents:
diff changeset
66 p.syncToLocalGit()
drewp@bigasterisk.com
parents:
diff changeset
67 p.makeGitHubRepo()
drewp@bigasterisk.com
parents:
diff changeset
68 p.pushToGitHub()
drewp@bigasterisk.com
parents:
diff changeset
69 except Exception, e:
drewp@bigasterisk.com
parents:
diff changeset
70 traceback.print_exc()
drewp@bigasterisk.com
parents:
diff changeset
71