Mercurial > code > home > repos > reposync
comparison sync.py @ 9:2c4d383d464c
move config stuff into Project
author | drewp@bigasterisk.com |
---|---|
date | Fri, 16 Jul 2021 00:19:01 -0700 |
parents | cc3321b8adc1 |
children | 460a2cf8b22b |
comparison
equal
deleted
inserted
replaced
8:cc3321b8adc1 | 9:2c4d383d464c |
---|---|
4 from pathlib import Path | 4 from pathlib import Path |
5 from github import Github, GithubException | 5 from github import Github, GithubException |
6 logging.basicConfig(level=logging.INFO) | 6 logging.basicConfig(level=logging.INFO) |
7 log = logging.getLogger() | 7 log = logging.getLogger() |
8 | 8 |
9 class Project(object): | 9 |
10 def __init__(self, config, gh, projRoot: Path): | 10 class Project: |
11 self.config = config | 11 def __init__(self, projRoot: Path): |
12 self.gh = gh | 12 self.config = json.load(open(Path(__file__).parent / "config.json")) |
13 self.config['SSH_AUTH_SOCK'] = getSshAuthSock() | |
14 | |
15 self.gh = Github(self.config['githubToken']).get_user() | |
13 self.projRoot = projRoot | 16 self.projRoot = projRoot |
14 self.name = projRoot.name | 17 self.name = projRoot.name |
15 | 18 |
16 def darcsTime(self): | 19 def darcsTime(self): |
17 j = os.path.join | 20 j = os.path.join |
47 | 50 |
48 def makeGithubRepo(self): | 51 def makeGithubRepo(self): |
49 try: | 52 try: |
50 self.gh.create_repo(self.name) | 53 self.gh.create_repo(self.name) |
51 except GithubException as e: | 54 except GithubException as e: |
52 print('exists') | |
53 assert e.data['errors'][0]['message'].startswith('name already exists'), (e, self.name) | 55 assert e.data['errors'][0]['message'].startswith('name already exists'), (e, self.name) |
54 return | 56 return |
55 self.runGitCommand(['git', 'remote', 'add', 'origin', | 57 self.runGitCommand(['git', 'remote', 'add', 'origin', |
56 'git@github.com:%s/%s.git' % (self.gh.login, | 58 'git@github.com:%s/%s.git' % (self.gh.login, |
57 self.name)]) | 59 self.name)]) |
76 raise ValueError("couldn't find SSH_AUTH_SOCK in output " | 78 raise ValueError("couldn't find SSH_AUTH_SOCK in output " |
77 "from keychain: %r" % keychain) | 79 "from keychain: %r" % keychain) |
78 return m.group(1) | 80 return m.group(1) |
79 | 81 |
80 if __name__ == '__main__': | 82 if __name__ == '__main__': |
81 config = json.loads(open("config.json").read()) | |
82 config['SSH_AUTH_SOCK'] = getSshAuthSock() | |
83 | 83 |
84 # to get this token: | 84 # to get this token: |
85 # curl -u drewp https://api.github.com/authorizations -d '{"scopes":["repo"]}' | 85 # curl -u drewp https://api.github.com/authorizations -d '{"scopes":["repo"]}' |
86 # --new: | 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 | 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 | 88 |
89 gh = Github(config['gitHubToken']).get_user() | |
90 | |
91 for proj in os.listdir(config['darcsDir']): | 89 for proj in os.listdir(config['darcsDir']): |
92 if not os.path.isdir(os.path.join(config['darcsDir'], proj)): | 90 if not os.path.isdir(os.path.join(config['darcsDir'], proj)): |
93 continue | 91 continue |
94 try: | 92 try: |
95 p = Project(config, gh, proj) | 93 p = Project(proj) |
96 | 94 |
97 if p.darcsTime() < time.time() - 86400*config['tooOldDays']: | 95 if p.darcsTime() < time.time() - 86400*config['tooOldDays']: |
98 continue | 96 continue |
99 | 97 |
100 log.info("syncing %s", proj) | 98 log.info("syncing %s", proj) |