Mercurial > code > home > repos > reposync
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 |
rev | line source |
---|---|
0 | 1 #!bin/python |
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 | 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): | |
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 | 37 |
38 def makeGitHubRepo(self): | |
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 | 43 return |
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 | 46 self.name)]) |
47 | |
48 def pushToGitHub(self): | |
49 self.runGitCommand(['git', 'push', 'origin', 'master']) | |
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 | 57 |
58 for proj in os.listdir(config['darcsDir']): | |
59 if 'repo' not in proj: | |
60 continue | |
61 if not os.path.isdir(os.path.join(config['darcsDir'], proj)): | |
62 continue | |
63 try: | |
64 p = Project(config, gh, proj) | |
65 log.info("syncing %s" % proj) | |
66 p.syncToLocalGit() | |
67 p.makeGitHubRepo() | |
68 p.pushToGitHub() | |
69 except Exception, e: | |
70 traceback.print_exc() | |
71 |