Mercurial > code > home > repos > reposync
comparison 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 |
comparison
equal
deleted
inserted
replaced
14:f83b7426b97c | 15:d653e1b558ce |
---|---|
1 #!bin/python | 1 import json |
2 import logging | |
3 import os | |
4 import re | |
5 import subprocess | |
6 from pathlib import Path | |
2 | 7 |
3 import os, subprocess, json, logging, traceback, time, re | |
4 from pathlib import Path | |
5 from github import Github, GithubException | 8 from github import Github, GithubException |
9 | |
6 logging.basicConfig(level=logging.INFO) | 10 logging.basicConfig(level=logging.INFO) |
7 log = logging.getLogger() | 11 log = logging.getLogger() |
8 | 12 |
9 | 13 |
10 class Project: | 14 class Project: |
15 | |
11 def __init__(self, projRoot: Path): | 16 def __init__(self, projRoot: Path): |
17 # from https://docs.github.com/en/developers/apps/building-oauth-apps/authorizing-oauth-apps#non-web-application-flow | |
18 # -> https://github.com/settings/tokens to make one | |
12 self.config = json.load(open(Path(__file__).parent / "config.json")) | 19 self.config = json.load(open(Path(__file__).parent / "config.json")) |
13 self.config['SSH_AUTH_SOCK'] = getSshAuthSock() | 20 self.config['SSH_AUTH_SOCK'] = getSshAuthSock() |
14 | 21 |
15 self.gh = Github(self.config['githubToken']) | 22 self.gh = Github(self.config['githubToken']) |
16 self.projRoot = projRoot | 23 self.projRoot = projRoot |
26 | 33 |
27 def gitDir(self): | 34 def gitDir(self): |
28 gitDir = os.path.join(self.config['gitSyncDir'], self.name) | 35 gitDir = os.path.join(self.config['gitSyncDir'], self.name) |
29 try: | 36 try: |
30 os.mkdir(gitDir) | 37 os.mkdir(gitDir) |
31 except OSError: pass | 38 except OSError: |
39 pass | |
32 return gitDir | 40 return gitDir |
33 | 41 |
34 def syncToLocalGit(self): | 42 def syncToLocalGit(self): |
35 darcsDir = os.path.join(self.config['darcsDir'], self.name) | 43 darcsDir = os.path.join(self.config['darcsDir'], self.name) |
36 try: | 44 try: |
37 os.rmdir(os.path.join(darcsDir, 'darcs_testing_for_nfs')) | 45 os.rmdir(os.path.join(darcsDir, 'darcs_testing_for_nfs')) |
38 except OSError: pass | 46 except OSError: |
47 pass | |
39 self.runGitCommand([self.config['darcsToGitCmd'], '--verbose', darcsDir]) | 48 self.runGitCommand([self.config['darcsToGitCmd'], '--verbose', darcsDir]) |
40 | 49 |
41 def runGitCommand(self, args, callKw={}): | 50 def runGitCommand(self, args, callKw={}): |
42 try: | 51 try: |
43 subprocess.check_call(args, cwd=self.gitDir(), | 52 subprocess.check_call( |
44 env={'SSH_AUTH_SOCK': self.config['SSH_AUTH_SOCK'], | 53 args, |
45 'HOME': os.environ['HOME'], # darcs-to-git uses this | 54 cwd=self.gitDir(), |
46 }, **callKw) | 55 env={ |
56 'SSH_AUTH_SOCK': self.config['SSH_AUTH_SOCK'], | |
57 'HOME': os.environ['HOME'], # darcs-to-git uses this | |
58 }, | |
59 **callKw) | |
47 except: | 60 except: |
48 log.error("in %s" % self.gitDir()) | 61 log.error("in %s" % self.gitDir()) |
49 raise | 62 raise |
50 | 63 |
51 def makeGithubRepo(self): | 64 def makeGithubRepo(self): |
52 try: | 65 try: |
53 self.gh.get_user().create_repo(self.name) | 66 self.gh.get_user().create_repo(self.name) |
54 except GithubException as e: | 67 except GithubException as e: |
55 assert e.data['errors'][0]['message'].startswith('name already exists'), (e, self.name) | 68 assert e.data['errors'][0]['message'].startswith('name already exists'), (e, self.name) |
56 return | 69 return |
57 | 70 |
58 def pushToGithub(self): | 71 def pushToGithub(self): |
59 self.runGitCommand(['git', 'push', 'origin', 'master']) | 72 self.runGitCommand(['git', 'push', 'origin', 'master']) |
60 | 73 |
61 def hgToGithub(self): | 74 def hgToGithub(self): |
62 subprocess.check_call(['hg', 'bookmark', '-r', 'default', 'main'], | 75 subprocess.check_call(['hg', 'bookmark', '-r', 'default', 'main'], cwd=self.projRoot) |
63 cwd=self.projRoot) | |
64 repo = self.gh.get_user().get_repo(self.name) | 76 repo = self.gh.get_user().get_repo(self.name) |
65 push = subprocess.run(['hg', 'push', | 77 push = subprocess.run([ |
66 f'git+ssh://'+repo.ssh_url.replace(':', '/'), | 78 'hg', |
67 ], | 79 'push', |
80 f'git+ssh://' + repo.ssh_url.replace(':', '/'), | |
81 ], | |
68 check=False, | 82 check=False, |
69 capture_output=True, | 83 capture_output=True, |
70 cwd=self.projRoot, | 84 cwd=self.projRoot, |
71 env={'SSH_AUTH_SOCK': self.config['SSH_AUTH_SOCK']}) | 85 env={'SSH_AUTH_SOCK': self.config['SSH_AUTH_SOCK']}) |
72 if push.returncode != 0 and not push.stdout.endswith(b'no changes found\n'): | 86 if push.returncode != 0 and not push.stdout.endswith(b'no changes found\n'): |
73 raise ValueError(f'hg push failed with {push.stdout!r}') | 87 raise ValueError(f'hg push failed with {push.stdout!r}') |
74 | 88 |
89 | |
75 def getSshAuthSock(): | 90 def getSshAuthSock(): |
76 keychain = subprocess.check_output([ | 91 keychain = subprocess.check_output(["keychain", "--noask", "--quiet", "--eval", "id_rsa"]).decode('ascii') |
77 "keychain", "--noask", "--quiet", "--eval", "id_rsa"]).decode('ascii') | |
78 m = re.search(r'SSH_AUTH_SOCK=([^; \n]+)', keychain) | 92 m = re.search(r'SSH_AUTH_SOCK=([^; \n]+)', keychain) |
79 if m is None: | 93 if m is None: |
80 raise ValueError("couldn't find SSH_AUTH_SOCK in output " | 94 raise ValueError("couldn't find SSH_AUTH_SOCK in output " "from keychain: %r" % keychain) |
81 "from keychain: %r" % keychain) | |
82 return m.group(1) | 95 return m.group(1) |
83 | |
84 if __name__ == '__main__': | |
85 | |
86 # to get this token: | |
87 # curl -u drewp https://api.github.com/authorizations -d '{"scopes":["repo"]}' | |
88 # --new: | |
89 # 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 | |
90 | |
91 for proj in os.listdir(config['darcsDir']): | |
92 if not os.path.isdir(os.path.join(config['darcsDir'], proj)): | |
93 continue | |
94 try: | |
95 p = Project(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() |