#!/usr/bin/env python3 import json import logging import os from argparse import ArgumentParser, FileType from urllib.parse import urlparse from github import Github def process_plugins(file, token): data = json.load(file) file.close() github = Github(token) for plugin in data: if plugin.get('archived'): continue parsed = urlparse(plugin['url']) name = parsed.path.lstrip('/') try: repo = github.get_repo(name) except Exception as e: logging.error('Could not find repo %s: %s', name, e) else: if repo.full_name != name: logging.info('Repo %s has moved to %s', repo, repo.full_name) print(repo.html_url) plugin['url'] = repo.html_url if repo.archived: plugin['archived'] = repo.archived else: try: commit = repo.get_commits()[0] except IndexError: pass else: plugin['last_commit'] = commit.commit.committer.date.isoformat() with open(file.name, 'w', encoding='UTF-8') as f: json.dump(data, f) def main(): parser = ArgumentParser(description='Set Github repository topics based on repository content') parser.add_argument('--file', '-f', help='Filename with plugins', type=FileType('r', encoding='UTF-8')) parser.add_argument('--verbose', '-v', help='Show debug output', action='store_true') args = parser.parse_args() log_level = logging.DEBUG if args.verbose else logging.INFO logging.basicConfig(level=log_level) try: token = os.environ['GITHUB_TOKEN'] except KeyError: raise SystemExit('Set the GITHUB_TOKEN environment variable to a valid Github token') try: process_plugins(args.file, token) except KeyboardInterrupt: pass if __name__ == '__main__': main()