# HG changeset patch # User drewp@bigasterisk.com # Date 1626419125 25200 # Node ID 7f479502a8ab78f59ce173637328f85b656936a5 # Parent a0d9679c4f4a841a59cb348647360b38a23e435f wip sync_to_github (from hg) diff -r a0d9679c4f4a -r 7f479502a8ab readme --- a/readme Fri Jul 24 15:22:06 2020 -0700 +++ b/readme Fri Jul 16 00:05:25 2021 -0700 @@ -12,3 +12,8 @@ See also: http://www.silverwareconsulting.com/index.cfm/2009/10/30/Placing-Config-Files-Under-Version-Control-with-Git-and-GitHub + +----- + + hg bookmark -r default main + hg push git+ssh://git@github.com/drewp/${repo}.git --quiet diff -r a0d9679c4f4a -r 7f479502a8ab sync.py --- a/sync.py Fri Jul 24 15:22:06 2020 -0700 +++ b/sync.py Fri Jul 16 00:05:25 2021 -0700 @@ -1,15 +1,17 @@ #!bin/python -import os, subprocess, urllib2, json, logging, traceback, time, re +import os, subprocess, json, logging, traceback, time, re +from pathlib import Path from github import Github, GithubException logging.basicConfig(level=logging.INFO) log = logging.getLogger() class Project(object): - def __init__(self, config, gh, name): + def __init__(self, config, gh, projRoot: Path): self.config = config self.gh = gh - self.name = name + self.projRoot = projRoot + self.name = projRoot.name def darcsTime(self): j = os.path.join @@ -18,14 +20,14 @@ if os.path.exists(j(darcsPriv, n)): return os.path.getmtime(j(darcsPriv, n)) raise ValueError("can't find a darcs time") - + def gitDir(self): gitDir = os.path.join(self.config['gitSyncDir'], self.name) try: os.mkdir(gitDir) except OSError: pass return gitDir - + def syncToLocalGit(self): darcsDir = os.path.join(self.config['darcsDir'], self.name) try: @@ -33,12 +35,12 @@ except OSError: pass self.runGitCommand([self.config['darcsToGitCmd'], '--verbose', darcsDir]) - def runGitCommand(self, args): + def runGitCommand(self, args, callKw={}): try: subprocess.check_call(args, cwd=self.gitDir(), env={'SSH_AUTH_SOCK': self.config['SSH_AUTH_SOCK'], 'HOME': os.environ['HOME'], # darcs-to-git uses this - }) + }, **callKw) except: log.error("in %s" % self.gitDir()) raise @@ -46,7 +48,8 @@ def makeGitHubRepo(self): try: self.gh.create_repo(self.name) - except GithubException, e: + except GithubException as e: + print('exists') assert e.data['errors'][0]['message'].startswith('name already exists'), (e, self.name) return self.runGitCommand(['git', 'remote', 'add', 'origin', @@ -56,37 +59,47 @@ def pushToGitHub(self): self.runGitCommand(['git', 'push', 'origin', 'master']) + def hgToGitHub(self): + subprocess.check_call(['hg', 'bookmark', '-r', 'default', 'main'], + cwd=self.projRoot) + subprocess.check_call(['hg', 'push', + f'git+ssh://git@github.com/{self.gh.login}/{self.name}' + ], + cwd=self.projRoot, + env={'SSH_AUTH_SOCK': self.config['SSH_AUTH_SOCK']}) + def getSshAuthSock(): keychain = subprocess.check_output([ - "keychain", "--noask", "--quiet", "--eval", "id_rsa"]) + "keychain", "--noask", "--quiet", "--eval", "id_rsa"]).decode('ascii') m = re.search(r'SSH_AUTH_SOCK=([^; \n]+)', keychain) - if m is not None: - return m.group(1) - else: + if m is None: raise ValueError("couldn't find SSH_AUTH_SOCK in output " "from keychain: %r" % keychain) - -config = json.loads(open("config.json").read()) -config['SSH_AUTH_SOCK'] = getSshAuthSock() + return m.group(1) + +if __name__ == '__main__': + config = json.loads(open("config.json").read()) + config['SSH_AUTH_SOCK'] = getSshAuthSock() -# to get this token: -# curl -u drewp https://api.github.com/authorizations -d '{"scopes":["repo"]}' -# from http://developer.github.com/v3/oauth/#oauth-authorizations-api -gh = Github(config['gitHubToken']).get_user() + # to get this token: + # curl -u drewp https://api.github.com/authorizations -d '{"scopes":["repo"]}' + # --new: + # 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 + + gh = Github(config['gitHubToken']).get_user() -for proj in os.listdir(config['darcsDir']): - if not os.path.isdir(os.path.join(config['darcsDir'], proj)): - continue - try: - p = Project(config, gh, proj) - - if p.darcsTime() < time.time() - 86400*config['tooOldDays']: + for proj in os.listdir(config['darcsDir']): + if not os.path.isdir(os.path.join(config['darcsDir'], proj)): continue - - log.info("syncing %s" % proj) - p.syncToLocalGit() - p.makeGitHubRepo() - p.pushToGitHub() - except Exception, e: - traceback.print_exc() - + try: + p = Project(config, gh, proj) + + if p.darcsTime() < time.time() - 86400*config['tooOldDays']: + continue + + log.info("syncing %s", proj) + p.syncToLocalGit() + p.makeGitHubRepo() + p.pushToGitHub() + except Exception as e: + traceback.print_exc() diff -r a0d9679c4f4a -r 7f479502a8ab sync_to_github.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sync_to_github.py Fri Jul 16 00:05:25 2021 -0700 @@ -0,0 +1,18 @@ +#!/usr/bin/python3 +from pathlib import Path +import json + +from github import Github +from sync import Project, getSshAuthSock + +config = json.loads(open("config.json").read()) +config['SSH_AUTH_SOCK'] = getSshAuthSock() + +# to get this token: +# curl -u drewp https://api.github.com/authorizations -d '{"scopes":["repo"]}' +# from http://developer.github.com/v3/oauth/#oauth-authorizations-api +gh = Github(config['gitHubToken']).get_user() + +p = Project(config, gh, Path('.').absolute()) +p.makeGitHubRepo() +p.hgToGitHub()