Mercurial > code > home > repos > reposync
annotate sync.py @ 7:7f479502a8ab
wip sync_to_github (from hg)
author | drewp@bigasterisk.com |
---|---|
date | Fri, 16 Jul 2021 00:05:25 -0700 |
parents | 4077903a9520 |
children | cc3321b8adc1 |
rev | line source |
---|---|
0 | 1 #!bin/python |
2 | |
7 | 3 import os, subprocess, json, logging, traceback, time, re |
4 from pathlib import Path | |
1
1da34eecdfd8
port to PyGithub so repo creates work again
drewp@bigasterisk.com
parents:
0
diff
changeset
|
5 from github import Github, GithubException |
0 | 6 logging.basicConfig(level=logging.INFO) |
7 log = logging.getLogger() | |
8 | |
9 class Project(object): | |
7 | 10 def __init__(self, config, gh, projRoot: Path): |
0 | 11 self.config = config |
12 self.gh = gh | |
7 | 13 self.projRoot = projRoot |
14 self.name = projRoot.name | |
2
22ccc05756de
don't waste time scanning repos that appear to have no recent activity
drewp@bigasterisk.com
parents:
1
diff
changeset
|
15 |
22ccc05756de
don't waste time scanning repos that appear to have no recent activity
drewp@bigasterisk.com
parents:
1
diff
changeset
|
16 def darcsTime(self): |
22ccc05756de
don't waste time scanning repos that appear to have no recent activity
drewp@bigasterisk.com
parents:
1
diff
changeset
|
17 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
|
18 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
|
19 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
|
20 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
|
21 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
|
22 raise ValueError("can't find a darcs time") |
7 | 23 |
0 | 24 def gitDir(self): |
25 gitDir = os.path.join(self.config['gitSyncDir'], self.name) | |
26 try: | |
27 os.mkdir(gitDir) | |
28 except OSError: pass | |
29 return gitDir | |
7 | 30 |
0 | 31 def syncToLocalGit(self): |
32 darcsDir = os.path.join(self.config['darcsDir'], self.name) | |
33 try: | |
34 os.rmdir(os.path.join(darcsDir, 'darcs_testing_for_nfs')) | |
35 except OSError: pass | |
3
4077903a9520
upgrade PyGithub. use keychain for ssh sock path
drewp@bigasterisk.com
parents:
2
diff
changeset
|
36 self.runGitCommand([self.config['darcsToGitCmd'], '--verbose', darcsDir]) |
0 | 37 |
7 | 38 def runGitCommand(self, args, callKw={}): |
1
1da34eecdfd8
port to PyGithub so repo creates work again
drewp@bigasterisk.com
parents:
0
diff
changeset
|
39 try: |
1da34eecdfd8
port to PyGithub so repo creates work again
drewp@bigasterisk.com
parents:
0
diff
changeset
|
40 subprocess.check_call(args, cwd=self.gitDir(), |
1da34eecdfd8
port to PyGithub so repo creates work again
drewp@bigasterisk.com
parents:
0
diff
changeset
|
41 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
|
42 'HOME': os.environ['HOME'], # darcs-to-git uses this |
7 | 43 }, **callKw) |
1
1da34eecdfd8
port to PyGithub so repo creates work again
drewp@bigasterisk.com
parents:
0
diff
changeset
|
44 except: |
1da34eecdfd8
port to PyGithub so repo creates work again
drewp@bigasterisk.com
parents:
0
diff
changeset
|
45 log.error("in %s" % self.gitDir()) |
1da34eecdfd8
port to PyGithub so repo creates work again
drewp@bigasterisk.com
parents:
0
diff
changeset
|
46 raise |
0 | 47 |
48 def makeGitHubRepo(self): | |
49 try: | |
1
1da34eecdfd8
port to PyGithub so repo creates work again
drewp@bigasterisk.com
parents:
0
diff
changeset
|
50 self.gh.create_repo(self.name) |
7 | 51 except GithubException as e: |
52 print('exists') | |
3
4077903a9520
upgrade PyGithub. use keychain for ssh sock path
drewp@bigasterisk.com
parents:
2
diff
changeset
|
53 assert e.data['errors'][0]['message'].startswith('name already exists'), (e, self.name) |
0 | 54 return |
55 self.runGitCommand(['git', 'remote', 'add', 'origin', | |
1
1da34eecdfd8
port to PyGithub so repo creates work again
drewp@bigasterisk.com
parents:
0
diff
changeset
|
56 'git@github.com:%s/%s.git' % (self.gh.login, |
0 | 57 self.name)]) |
58 | |
59 def pushToGitHub(self): | |
60 self.runGitCommand(['git', 'push', 'origin', 'master']) | |
61 | |
7 | 62 def hgToGitHub(self): |
63 subprocess.check_call(['hg', 'bookmark', '-r', 'default', 'main'], | |
64 cwd=self.projRoot) | |
65 subprocess.check_call(['hg', 'push', | |
66 f'git+ssh://git@github.com/{self.gh.login}/{self.name}' | |
67 ], | |
68 cwd=self.projRoot, | |
69 env={'SSH_AUTH_SOCK': self.config['SSH_AUTH_SOCK']}) | |
70 | |
3
4077903a9520
upgrade PyGithub. use keychain for ssh sock path
drewp@bigasterisk.com
parents:
2
diff
changeset
|
71 def getSshAuthSock(): |
4077903a9520
upgrade PyGithub. use keychain for ssh sock path
drewp@bigasterisk.com
parents:
2
diff
changeset
|
72 keychain = subprocess.check_output([ |
7 | 73 "keychain", "--noask", "--quiet", "--eval", "id_rsa"]).decode('ascii') |
3
4077903a9520
upgrade PyGithub. use keychain for ssh sock path
drewp@bigasterisk.com
parents:
2
diff
changeset
|
74 m = re.search(r'SSH_AUTH_SOCK=([^; \n]+)', keychain) |
7 | 75 if m is None: |
3
4077903a9520
upgrade PyGithub. use keychain for ssh sock path
drewp@bigasterisk.com
parents:
2
diff
changeset
|
76 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
|
77 "from keychain: %r" % keychain) |
7 | 78 return m.group(1) |
79 | |
80 if __name__ == '__main__': | |
81 config = json.loads(open("config.json").read()) | |
82 config['SSH_AUTH_SOCK'] = getSshAuthSock() | |
1
1da34eecdfd8
port to PyGithub so repo creates work again
drewp@bigasterisk.com
parents:
0
diff
changeset
|
83 |
7 | 84 # to get this token: |
85 # curl -u drewp https://api.github.com/authorizations -d '{"scopes":["repo"]}' | |
86 # --new: | |
87 # from https://docs.github.com/en/developers/apps/building-oauth-apps/authorizing-oauth-apps#non-web-application-flow -> https://github.com/settings/tokens to make one | |
88 | |
89 gh = Github(config['gitHubToken']).get_user() | |
0 | 90 |
7 | 91 for proj in os.listdir(config['darcsDir']): |
92 if not os.path.isdir(os.path.join(config['darcsDir'], proj)): | |
2
22ccc05756de
don't waste time scanning repos that appear to have no recent activity
drewp@bigasterisk.com
parents:
1
diff
changeset
|
93 continue |
7 | 94 try: |
95 p = Project(config, gh, proj) | |
96 | |
97 if p.darcsTime() < time.time() - 86400*config['tooOldDays']: | |
98 continue | |
99 | |
100 log.info("syncing %s", proj) | |
101 p.syncToLocalGit() | |
102 p.makeGitHubRepo() | |
103 p.pushToGitHub() | |
104 except Exception as e: | |
105 traceback.print_exc() |