-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
add-metadata.py
executable file
·87 lines (62 loc) · 2.14 KB
/
add-metadata.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Preparing to run it:
# brew install pipenv # or other installation method
# pipenv install
# generate a personal access token at https://github.com/settings/tokens
# Running it:
# GITHUB_TOKEN=xxxxxxx pipenv run python add-metadata.py < template.md > README.md
import fileinput
import os
import re
import sys
from datetime import datetime, timedelta
from github import Github
g = Github(os.environ['GITHUB_TOKEN'])
gh_repo_regex = re.compile('\[([\w\._-]+\/[\w\._-]+)\]\(@ghRepo\)')
def github_table_row(repo):
name = repo.name
if repo.stargazers_count >= 500:
name = f"**{name}**"
project_link = f"[{name}]({repo.html_url})"
stars_shield = f"![GitHub stars](https://img.shields.io/github/stars/{repo.full_name})"
commit_shield = f"![GitHub commit activity](https://img.shields.io/github/commit-activity/y/{repo.full_name})"
return f"{project_link} | {repo.description} | {stars_shield} {commit_shield}"
def warn(msg):
print(f"Warn: {msg}", file=sys.stderr)
def retrieve_repo(name):
try:
repo = g.get_repo(name)
except Exception:
warn(f"Error occured while getting {name} repo")
raise
print('.', file=sys.stderr, end='', flush=True)
if is_stale(repo):
raise Exception("Repo is too old or inactive")
return repo
def is_stale(repo):
if repo.archived:
warn(f"Repo {repo.full_name} is archived")
return True
elif repo.pushed_at < datetime.utcnow() - timedelta(days=365):
warn(f"Repo {repo.full_name} has not been pushed to since {repo.pushed_at}")
return True
return False
def parse(line):
m = gh_repo_regex.search(line)
if m:
[repo_name] = m.groups()
try:
row = github_table_row(retrieve_repo(repo_name))
except Exception:
row = "skip"
return row
else:
return line.rstrip()
def run():
print('<!--- This file is automatically generated. Do not edit directly. -->')
for line in fileinput.input():
parsed = parse(line)
if parsed != "skip":
print(parsed)
run()