comparison 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
comparison
equal deleted inserted replaced
6:a0d9679c4f4a 7:7f479502a8ab
1 #!bin/python 1 #!bin/python
2 2
3 import os, subprocess, urllib2, json, logging, traceback, time, re 3 import os, subprocess, json, logging, traceback, time, re
4 from pathlib import Path
4 from github import Github, GithubException 5 from github import Github, GithubException
5 logging.basicConfig(level=logging.INFO) 6 logging.basicConfig(level=logging.INFO)
6 log = logging.getLogger() 7 log = logging.getLogger()
7 8
8 class Project(object): 9 class Project(object):
9 def __init__(self, config, gh, name): 10 def __init__(self, config, gh, projRoot: Path):
10 self.config = config 11 self.config = config
11 self.gh = gh 12 self.gh = gh
12 self.name = name 13 self.projRoot = projRoot
14 self.name = projRoot.name
13 15
14 def darcsTime(self): 16 def darcsTime(self):
15 j = os.path.join 17 j = os.path.join
16 darcsPriv = j(self.config['darcsDir'], self.name, '_darcs') 18 darcsPriv = j(self.config['darcsDir'], self.name, '_darcs')
17 for n in ['inventory', 'hashed_inventory']: 19 for n in ['inventory', 'hashed_inventory']:
18 if os.path.exists(j(darcsPriv, n)): 20 if os.path.exists(j(darcsPriv, n)):
19 return os.path.getmtime(j(darcsPriv, n)) 21 return os.path.getmtime(j(darcsPriv, n))
20 raise ValueError("can't find a darcs time") 22 raise ValueError("can't find a darcs time")
21 23
22 def gitDir(self): 24 def gitDir(self):
23 gitDir = os.path.join(self.config['gitSyncDir'], self.name) 25 gitDir = os.path.join(self.config['gitSyncDir'], self.name)
24 try: 26 try:
25 os.mkdir(gitDir) 27 os.mkdir(gitDir)
26 except OSError: pass 28 except OSError: pass
27 return gitDir 29 return gitDir
28 30
29 def syncToLocalGit(self): 31 def syncToLocalGit(self):
30 darcsDir = os.path.join(self.config['darcsDir'], self.name) 32 darcsDir = os.path.join(self.config['darcsDir'], self.name)
31 try: 33 try:
32 os.rmdir(os.path.join(darcsDir, 'darcs_testing_for_nfs')) 34 os.rmdir(os.path.join(darcsDir, 'darcs_testing_for_nfs'))
33 except OSError: pass 35 except OSError: pass
34 self.runGitCommand([self.config['darcsToGitCmd'], '--verbose', darcsDir]) 36 self.runGitCommand([self.config['darcsToGitCmd'], '--verbose', darcsDir])
35 37
36 def runGitCommand(self, args): 38 def runGitCommand(self, args, callKw={}):
37 try: 39 try:
38 subprocess.check_call(args, cwd=self.gitDir(), 40 subprocess.check_call(args, cwd=self.gitDir(),
39 env={'SSH_AUTH_SOCK': self.config['SSH_AUTH_SOCK'], 41 env={'SSH_AUTH_SOCK': self.config['SSH_AUTH_SOCK'],
40 'HOME': os.environ['HOME'], # darcs-to-git uses this 42 'HOME': os.environ['HOME'], # darcs-to-git uses this
41 }) 43 }, **callKw)
42 except: 44 except:
43 log.error("in %s" % self.gitDir()) 45 log.error("in %s" % self.gitDir())
44 raise 46 raise
45 47
46 def makeGitHubRepo(self): 48 def makeGitHubRepo(self):
47 try: 49 try:
48 self.gh.create_repo(self.name) 50 self.gh.create_repo(self.name)
49 except GithubException, e: 51 except GithubException as e:
52 print('exists')
50 assert e.data['errors'][0]['message'].startswith('name already exists'), (e, self.name) 53 assert e.data['errors'][0]['message'].startswith('name already exists'), (e, self.name)
51 return 54 return
52 self.runGitCommand(['git', 'remote', 'add', 'origin', 55 self.runGitCommand(['git', 'remote', 'add', 'origin',
53 'git@github.com:%s/%s.git' % (self.gh.login, 56 'git@github.com:%s/%s.git' % (self.gh.login,
54 self.name)]) 57 self.name)])
55 58
56 def pushToGitHub(self): 59 def pushToGitHub(self):
57 self.runGitCommand(['git', 'push', 'origin', 'master']) 60 self.runGitCommand(['git', 'push', 'origin', 'master'])
58 61
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
59 def getSshAuthSock(): 71 def getSshAuthSock():
60 keychain = subprocess.check_output([ 72 keychain = subprocess.check_output([
61 "keychain", "--noask", "--quiet", "--eval", "id_rsa"]) 73 "keychain", "--noask", "--quiet", "--eval", "id_rsa"]).decode('ascii')
62 m = re.search(r'SSH_AUTH_SOCK=([^; \n]+)', keychain) 74 m = re.search(r'SSH_AUTH_SOCK=([^; \n]+)', keychain)
63 if m is not None: 75 if m is None:
64 return m.group(1)
65 else:
66 raise ValueError("couldn't find SSH_AUTH_SOCK in output " 76 raise ValueError("couldn't find SSH_AUTH_SOCK in output "
67 "from keychain: %r" % keychain) 77 "from keychain: %r" % keychain)
68 78 return m.group(1)
69 config = json.loads(open("config.json").read())
70 config['SSH_AUTH_SOCK'] = getSshAuthSock()
71 79
72 # to get this token: 80 if __name__ == '__main__':
73 # curl -u drewp https://api.github.com/authorizations -d '{"scopes":["repo"]}' 81 config = json.loads(open("config.json").read())
74 # from http://developer.github.com/v3/oauth/#oauth-authorizations-api 82 config['SSH_AUTH_SOCK'] = getSshAuthSock()
75 gh = Github(config['gitHubToken']).get_user()
76 83
77 for proj in os.listdir(config['darcsDir']): 84 # to get this token:
78 if not os.path.isdir(os.path.join(config['darcsDir'], proj)): 85 # curl -u drewp https://api.github.com/authorizations -d '{"scopes":["repo"]}'
79 continue 86 # --new:
80 try: 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
81 p = Project(config, gh, proj)
82 88
83 if p.darcsTime() < time.time() - 86400*config['tooOldDays']: 89 gh = Github(config['gitHubToken']).get_user()
90
91 for proj in os.listdir(config['darcsDir']):
92 if not os.path.isdir(os.path.join(config['darcsDir'], proj)):
84 continue 93 continue
85 94 try:
86 log.info("syncing %s" % proj) 95 p = Project(config, gh, proj)
87 p.syncToLocalGit() 96
88 p.makeGitHubRepo() 97 if p.darcsTime() < time.time() - 86400*config['tooOldDays']:
89 p.pushToGitHub() 98 continue
90 except Exception, e: 99
91 traceback.print_exc() 100 log.info("syncing %s", proj)
92 101 p.syncToLocalGit()
102 p.makeGitHubRepo()
103 p.pushToGitHub()
104 except Exception as e:
105 traceback.print_exc()