-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathadd-metadata.py
executable file
·85 lines (61 loc) · 2.46 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
#!/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\)')
gl_repo_regex = re.compile('\[([\w\._-]+\/[\w\._-]+)\]\(@glRepo\)')
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 if repo.description else ''} | {stars_shield} {commit_shield}"
def gitlab_table_row(line):
name = line.split("[")[1].split("]")[0]
project_link = f"[{name.split('/')[-1]}](https://gitlab.com/{name})"
stars_shield = f"![GitLab stars](https://img.shields.io/gitlab/stars/{name})"
return f"{project_link} | | {stars_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)
check_freshness(repo)
return repo
def check_freshness(repo):
if repo.archived:
warn(f"Repo {repo.full_name} is archived")
elif datetime.timestamp(repo.pushed_at) < datetime.timestamp(datetime.utcnow() - timedelta(days=180)):
warn(f"Repo {repo.full_name} has not been pushed to since {repo.pushed_at}")
def parse(line):
gh = gh_repo_regex.search(line)
gl = gl_repo_regex.search(line)
if gh:
[repo_name] = gh.groups()
return github_table_row(retrieve_repo(repo_name)) + line.rsplit("(@ghRepo)", 1)[-1].rstrip()
elif gl:
return gitlab_table_row(line) + line.rsplit("(@glRepo)", 1)[-1].rstrip()
else:
return line.rstrip()
def run():
print('<!--- This file is automatically generated. Do not edit directly. -->')
for line in fileinput.input():
print(parse(line))
run()