Mercurial > code > home > repos > reposync
annotate sync.py @ 6:a0d9679c4f4a
loginbar element moves out
author | drewp@bigasterisk.com |
---|---|
date | Fri, 24 Jul 2020 15:22:06 -0700 |
parents | 4077903a9520 |
children | 7f479502a8ab |
rev | line source |
---|---|
0 | 1 #!bin/python |
2 | |
3
4077903a9520
upgrade PyGithub. use keychain for ssh sock path
drewp@bigasterisk.com
parents:
2
diff
changeset
|
3 import os, subprocess, urllib2, json, logging, traceback, time, re |
1
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 | |
2
22ccc05756de
don't waste time scanning repos that appear to have no recent activity
drewp@bigasterisk.com
parents:
1
diff
changeset
|
13 |
22ccc05756de
don't waste time scanning repos that appear to have no recent activity
drewp@bigasterisk.com
parents:
1
diff
changeset
|
14 def darcsTime(self): |
22ccc05756de
don't waste time scanning repos that appear to have no recent activity
drewp@bigasterisk.com
parents:
1
diff
changeset
|
15 j = os.path.join |
22ccc05756de
don't waste time scanning repos that appear to have no recent activity
drewp@bigasterisk.com
parents:
1
diff
changeset
|
16 darcsPriv = j(self.config['darcsDir'], self.name, '_darcs') |
22ccc05756de
don't waste time scanning repos that appear to have no recent activity
drewp@bigasterisk.com
parents:
1
diff
changeset
|
17 for n in ['inventory', 'hashed_inventory']: |
22ccc05756de
don't waste time scanning repos that appear to have no recent activity
drewp@bigasterisk.com
parents:
1
diff
changeset
|
18 if os.path.exists(j(darcsPriv, n)): |
22ccc05756de
don't waste time scanning repos that appear to have no recent activity
drewp@bigasterisk.com
parents:
1
diff
changeset
|
19 return os.path.getmtime(j(darcsPriv, n)) |
22ccc05756de
don't waste time scanning repos that appear to have no recent activity
drewp@bigasterisk.com
parents:
1
diff
changeset
|
20 raise ValueError("can't find a darcs time") |
0 | 21 |
22 def gitDir(self): | |
23 gitDir = os.path.join(self.config['gitSyncDir'], self.name) | |
24 try: | |
25 os.mkdir(gitDir) | |
26 except OSError: pass | |
27 return gitDir | |
28 | |
29 def syncToLocalGit(self): | |
30 darcsDir = os.path.join(self.config['darcsDir'], self.name) | |
31 try: | |
32 os.rmdir(os.path.join(darcsDir, 'darcs_testing_for_nfs')) | |
33 except OSError: pass | |
3
4077903a9520
upgrade PyGithub. use keychain for ssh sock path
drewp@bigasterisk.com
parents:
2
diff
changeset
|
34 self.runGitCommand([self.config['darcsToGitCmd'], '--verbose', darcsDir]) |
0 | 35 |
36 def runGitCommand(self, args): | |
1
1da34eecdfd8
port to PyGithub so repo creates work again
drewp@bigasterisk.com
parents:
0
diff
changeset
|
37 try: |
1da34eecdfd8
port to PyGithub so repo creates work again
drewp@bigasterisk.com
parents:
0
diff
changeset
|
38 subprocess.check_call(args, cwd=self.gitDir(), |
1da34eecdfd8
port to PyGithub so repo creates work again
drewp@bigasterisk.com
parents:
0
diff
changeset
|
39 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
|
40 '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
|
41 }) |
1da34eecdfd8
port to PyGithub so repo creates work again
drewp@bigasterisk.com
parents:
0
diff
changeset
|
42 except: |
1da34eecdfd8
port to PyGithub so repo creates work again
drewp@bigasterisk.com
parents:
0
diff
changeset
|
43 log.error("in %s" % self.gitDir()) |
1da34eecdfd8
port to PyGithub so repo creates work again
drewp@bigasterisk.com
parents:
0
diff
changeset
|
44 raise |
0 | 45 |
46 def makeGitHubRepo(self): | |
47 try: | |
1
1da34eecdfd8
port to PyGithub so repo creates work again
drewp@bigasterisk.com
parents:
0
diff
changeset
|
48 self.gh.create_repo(self.name) |
1da34eecdfd8
port to PyGithub so repo creates work again
drewp@bigasterisk.com
parents:
0
diff
changeset
|
49 except GithubException, e: |
3
4077903a9520
upgrade PyGithub. use keychain for ssh sock path
drewp@bigasterisk.com
parents:
2
diff
changeset
|
50 assert e.data['errors'][0]['message'].startswith('name already exists'), (e, self.name) |
0 | 51 return |
52 self.runGitCommand(['git', 'remote', 'add', 'origin', | |
1
1da34eecdfd8
port to PyGithub so repo creates work again
drewp@bigasterisk.com
parents:
0
diff
changeset
|
53 'git@github.com:%s/%s.git' % (self.gh.login, |
0 | 54 self.name)]) |
55 | |
56 def pushToGitHub(self): | |
57 self.runGitCommand(['git', 'push', 'origin', 'master']) | |
58 | |
3
4077903a9520
upgrade PyGithub. use keychain for ssh sock path
drewp@bigasterisk.com
parents:
2
diff
changeset
|
59 def getSshAuthSock(): |
4077903a9520
upgrade PyGithub. use keychain for ssh sock path
drewp@bigasterisk.com
parents:
2
diff
changeset
|
60 keychain = subprocess.check_output([ |
4077903a9520
upgrade PyGithub. use keychain for ssh sock path
drewp@bigasterisk.com
parents:
2
diff
changeset
|
61 "keychain", "--noask", "--quiet", "--eval", "id_rsa"]) |
4077903a9520
upgrade PyGithub. use keychain for ssh sock path
drewp@bigasterisk.com
parents:
2
diff
changeset
|
62 m = re.search(r'SSH_AUTH_SOCK=([^; \n]+)', keychain) |
4077903a9520
upgrade PyGithub. use keychain for ssh sock path
drewp@bigasterisk.com
parents:
2
diff
changeset
|
63 if m is not None: |
4077903a9520
upgrade PyGithub. use keychain for ssh sock path
drewp@bigasterisk.com
parents:
2
diff
changeset
|
64 return m.group(1) |
4077903a9520
upgrade PyGithub. use keychain for ssh sock path
drewp@bigasterisk.com
parents:
2
diff
changeset
|
65 else: |
4077903a9520
upgrade PyGithub. use keychain for ssh sock path
drewp@bigasterisk.com
parents:
2
diff
changeset
|
66 raise ValueError("couldn't find SSH_AUTH_SOCK in output " |
4077903a9520
upgrade PyGithub. use keychain for ssh sock path
drewp@bigasterisk.com
parents:
2
diff
changeset
|
67 "from keychain: %r" % keychain) |
4077903a9520
upgrade PyGithub. use keychain for ssh sock path
drewp@bigasterisk.com
parents:
2
diff
changeset
|
68 |
1
1da34eecdfd8
port to PyGithub so repo creates work again
drewp@bigasterisk.com
parents:
0
diff
changeset
|
69 config = json.loads(open("config.json").read()) |
3
4077903a9520
upgrade PyGithub. use keychain for ssh sock path
drewp@bigasterisk.com
parents:
2
diff
changeset
|
70 config['SSH_AUTH_SOCK'] = getSshAuthSock() |
1
1da34eecdfd8
port to PyGithub so repo creates work again
drewp@bigasterisk.com
parents:
0
diff
changeset
|
71 |
1da34eecdfd8
port to PyGithub so repo creates work again
drewp@bigasterisk.com
parents:
0
diff
changeset
|
72 # to get this token: |
1da34eecdfd8
port to PyGithub so repo creates work again
drewp@bigasterisk.com
parents:
0
diff
changeset
|
73 # 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
|
74 # 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
|
75 gh = Github(config['gitHubToken']).get_user() |
0 | 76 |
77 for proj in os.listdir(config['darcsDir']): | |
78 if not os.path.isdir(os.path.join(config['darcsDir'], proj)): | |
79 continue | |
80 try: | |
81 p = Project(config, gh, proj) | |
2
22ccc05756de
don't waste time scanning repos that appear to have no recent activity
drewp@bigasterisk.com
parents:
1
diff
changeset
|
82 |
22ccc05756de
don't waste time scanning repos that appear to have no recent activity
drewp@bigasterisk.com
parents:
1
diff
changeset
|
83 if p.darcsTime() < time.time() - 86400*config['tooOldDays']: |
22ccc05756de
don't waste time scanning repos that appear to have no recent activity
drewp@bigasterisk.com
parents:
1
diff
changeset
|
84 continue |
22ccc05756de
don't waste time scanning repos that appear to have no recent activity
drewp@bigasterisk.com
parents:
1
diff
changeset
|
85 |
0 | 86 log.info("syncing %s" % proj) |
87 p.syncToLocalGit() | |
88 p.makeGitHubRepo() | |
89 p.pushToGitHub() | |
90 except Exception, e: | |
91 traceback.print_exc() | |
92 |