Mercurial > code > home > repos > reposync
annotate sync.py @ 15:d653e1b558ce
rm dead code; reformat
author | drewp@bigasterisk.com <drewp@bigasterisk.com> |
---|---|
date | Sat, 17 Jul 2021 00:17:56 -0700 |
parents | f83b7426b97c |
children |
rev | line source |
---|---|
15
d653e1b558ce
rm dead code; reformat
drewp@bigasterisk.com <drewp@bigasterisk.com>
parents:
14
diff
changeset
|
1 import json |
d653e1b558ce
rm dead code; reformat
drewp@bigasterisk.com <drewp@bigasterisk.com>
parents:
14
diff
changeset
|
2 import logging |
d653e1b558ce
rm dead code; reformat
drewp@bigasterisk.com <drewp@bigasterisk.com>
parents:
14
diff
changeset
|
3 import os |
d653e1b558ce
rm dead code; reformat
drewp@bigasterisk.com <drewp@bigasterisk.com>
parents:
14
diff
changeset
|
4 import re |
d653e1b558ce
rm dead code; reformat
drewp@bigasterisk.com <drewp@bigasterisk.com>
parents:
14
diff
changeset
|
5 import subprocess |
d653e1b558ce
rm dead code; reformat
drewp@bigasterisk.com <drewp@bigasterisk.com>
parents:
14
diff
changeset
|
6 from pathlib import Path |
0 | 7 |
1
1da34eecdfd8
port to PyGithub so repo creates work again
drewp@bigasterisk.com
parents:
0
diff
changeset
|
8 from github import Github, GithubException |
15
d653e1b558ce
rm dead code; reformat
drewp@bigasterisk.com <drewp@bigasterisk.com>
parents:
14
diff
changeset
|
9 |
0 | 10 logging.basicConfig(level=logging.INFO) |
11 log = logging.getLogger() | |
12 | |
9 | 13 |
14 class Project: | |
15
d653e1b558ce
rm dead code; reformat
drewp@bigasterisk.com <drewp@bigasterisk.com>
parents:
14
diff
changeset
|
15 |
9 | 16 def __init__(self, projRoot: Path): |
15
d653e1b558ce
rm dead code; reformat
drewp@bigasterisk.com <drewp@bigasterisk.com>
parents:
14
diff
changeset
|
17 # from https://docs.github.com/en/developers/apps/building-oauth-apps/authorizing-oauth-apps#non-web-application-flow |
d653e1b558ce
rm dead code; reformat
drewp@bigasterisk.com <drewp@bigasterisk.com>
parents:
14
diff
changeset
|
18 # -> https://github.com/settings/tokens to make one |
9 | 19 self.config = json.load(open(Path(__file__).parent / "config.json")) |
20 self.config['SSH_AUTH_SOCK'] = getSshAuthSock() | |
21 | |
14
f83b7426b97c
fix makeGithubRepo; use ssh_url for almost no benefit
drewp@bigasterisk.com <drewp@bigasterisk.com>
parents:
11
diff
changeset
|
22 self.gh = Github(self.config['githubToken']) |
7 | 23 self.projRoot = projRoot |
24 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
|
25 |
22ccc05756de
don't waste time scanning repos that appear to have no recent activity
drewp@bigasterisk.com
parents:
1
diff
changeset
|
26 def darcsTime(self): |
22ccc05756de
don't waste time scanning repos that appear to have no recent activity
drewp@bigasterisk.com
parents:
1
diff
changeset
|
27 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
|
28 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
|
29 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
|
30 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
|
31 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
|
32 raise ValueError("can't find a darcs time") |
7 | 33 |
0 | 34 def gitDir(self): |
35 gitDir = os.path.join(self.config['gitSyncDir'], self.name) | |
36 try: | |
37 os.mkdir(gitDir) | |
15
d653e1b558ce
rm dead code; reformat
drewp@bigasterisk.com <drewp@bigasterisk.com>
parents:
14
diff
changeset
|
38 except OSError: |
d653e1b558ce
rm dead code; reformat
drewp@bigasterisk.com <drewp@bigasterisk.com>
parents:
14
diff
changeset
|
39 pass |
0 | 40 return gitDir |
7 | 41 |
0 | 42 def syncToLocalGit(self): |
43 darcsDir = os.path.join(self.config['darcsDir'], self.name) | |
44 try: | |
45 os.rmdir(os.path.join(darcsDir, 'darcs_testing_for_nfs')) | |
15
d653e1b558ce
rm dead code; reformat
drewp@bigasterisk.com <drewp@bigasterisk.com>
parents:
14
diff
changeset
|
46 except OSError: |
d653e1b558ce
rm dead code; reformat
drewp@bigasterisk.com <drewp@bigasterisk.com>
parents:
14
diff
changeset
|
47 pass |
3
4077903a9520
upgrade PyGithub. use keychain for ssh sock path
drewp@bigasterisk.com
parents:
2
diff
changeset
|
48 self.runGitCommand([self.config['darcsToGitCmd'], '--verbose', darcsDir]) |
0 | 49 |
7 | 50 def runGitCommand(self, args, callKw={}): |
1
1da34eecdfd8
port to PyGithub so repo creates work again
drewp@bigasterisk.com
parents:
0
diff
changeset
|
51 try: |
15
d653e1b558ce
rm dead code; reformat
drewp@bigasterisk.com <drewp@bigasterisk.com>
parents:
14
diff
changeset
|
52 subprocess.check_call( |
d653e1b558ce
rm dead code; reformat
drewp@bigasterisk.com <drewp@bigasterisk.com>
parents:
14
diff
changeset
|
53 args, |
d653e1b558ce
rm dead code; reformat
drewp@bigasterisk.com <drewp@bigasterisk.com>
parents:
14
diff
changeset
|
54 cwd=self.gitDir(), |
d653e1b558ce
rm dead code; reformat
drewp@bigasterisk.com <drewp@bigasterisk.com>
parents:
14
diff
changeset
|
55 env={ |
d653e1b558ce
rm dead code; reformat
drewp@bigasterisk.com <drewp@bigasterisk.com>
parents:
14
diff
changeset
|
56 'SSH_AUTH_SOCK': self.config['SSH_AUTH_SOCK'], |
d653e1b558ce
rm dead code; reformat
drewp@bigasterisk.com <drewp@bigasterisk.com>
parents:
14
diff
changeset
|
57 'HOME': os.environ['HOME'], # darcs-to-git uses this |
d653e1b558ce
rm dead code; reformat
drewp@bigasterisk.com <drewp@bigasterisk.com>
parents:
14
diff
changeset
|
58 }, |
d653e1b558ce
rm dead code; reformat
drewp@bigasterisk.com <drewp@bigasterisk.com>
parents:
14
diff
changeset
|
59 **callKw) |
1
1da34eecdfd8
port to PyGithub so repo creates work again
drewp@bigasterisk.com
parents:
0
diff
changeset
|
60 except: |
1da34eecdfd8
port to PyGithub so repo creates work again
drewp@bigasterisk.com
parents:
0
diff
changeset
|
61 log.error("in %s" % self.gitDir()) |
1da34eecdfd8
port to PyGithub so repo creates work again
drewp@bigasterisk.com
parents:
0
diff
changeset
|
62 raise |
0 | 63 |
8 | 64 def makeGithubRepo(self): |
0 | 65 try: |
14
f83b7426b97c
fix makeGithubRepo; use ssh_url for almost no benefit
drewp@bigasterisk.com <drewp@bigasterisk.com>
parents:
11
diff
changeset
|
66 self.gh.get_user().create_repo(self.name) |
7 | 67 except GithubException as e: |
3
4077903a9520
upgrade PyGithub. use keychain for ssh sock path
drewp@bigasterisk.com
parents:
2
diff
changeset
|
68 assert e.data['errors'][0]['message'].startswith('name already exists'), (e, self.name) |
0 | 69 return |
15
d653e1b558ce
rm dead code; reformat
drewp@bigasterisk.com <drewp@bigasterisk.com>
parents:
14
diff
changeset
|
70 |
8 | 71 def pushToGithub(self): |
0 | 72 self.runGitCommand(['git', 'push', 'origin', 'master']) |
73 | |
8 | 74 def hgToGithub(self): |
15
d653e1b558ce
rm dead code; reformat
drewp@bigasterisk.com <drewp@bigasterisk.com>
parents:
14
diff
changeset
|
75 subprocess.check_call(['hg', 'bookmark', '-r', 'default', 'main'], cwd=self.projRoot) |
14
f83b7426b97c
fix makeGithubRepo; use ssh_url for almost no benefit
drewp@bigasterisk.com <drewp@bigasterisk.com>
parents:
11
diff
changeset
|
76 repo = self.gh.get_user().get_repo(self.name) |
15
d653e1b558ce
rm dead code; reformat
drewp@bigasterisk.com <drewp@bigasterisk.com>
parents:
14
diff
changeset
|
77 push = subprocess.run([ |
d653e1b558ce
rm dead code; reformat
drewp@bigasterisk.com <drewp@bigasterisk.com>
parents:
14
diff
changeset
|
78 'hg', |
d653e1b558ce
rm dead code; reformat
drewp@bigasterisk.com <drewp@bigasterisk.com>
parents:
14
diff
changeset
|
79 'push', |
d653e1b558ce
rm dead code; reformat
drewp@bigasterisk.com <drewp@bigasterisk.com>
parents:
14
diff
changeset
|
80 f'git+ssh://' + repo.ssh_url.replace(':', '/'), |
d653e1b558ce
rm dead code; reformat
drewp@bigasterisk.com <drewp@bigasterisk.com>
parents:
14
diff
changeset
|
81 ], |
10 | 82 check=False, |
83 capture_output=True, | |
7 | 84 cwd=self.projRoot, |
85 env={'SSH_AUTH_SOCK': self.config['SSH_AUTH_SOCK']}) | |
11 | 86 if push.returncode != 0 and not push.stdout.endswith(b'no changes found\n'): |
10 | 87 raise ValueError(f'hg push failed with {push.stdout!r}') |
7 | 88 |
15
d653e1b558ce
rm dead code; reformat
drewp@bigasterisk.com <drewp@bigasterisk.com>
parents:
14
diff
changeset
|
89 |
3
4077903a9520
upgrade PyGithub. use keychain for ssh sock path
drewp@bigasterisk.com
parents:
2
diff
changeset
|
90 def getSshAuthSock(): |
15
d653e1b558ce
rm dead code; reformat
drewp@bigasterisk.com <drewp@bigasterisk.com>
parents:
14
diff
changeset
|
91 keychain = subprocess.check_output(["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
|
92 m = re.search(r'SSH_AUTH_SOCK=([^; \n]+)', keychain) |
7 | 93 if m is None: |
15
d653e1b558ce
rm dead code; reformat
drewp@bigasterisk.com <drewp@bigasterisk.com>
parents:
14
diff
changeset
|
94 raise ValueError("couldn't find SSH_AUTH_SOCK in output " "from keychain: %r" % keychain) |
7 | 95 return m.group(1) |