forked from lauris/awesome-scala
-
Notifications
You must be signed in to change notification settings - Fork 0
/
add-metadata.py
executable file
·69 lines (48 loc) · 1.78 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
#!/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(str):
print(f"Warn: {str}", file=sys.stderr)
def retrieve_repo(name):
repo = g.get_repo(name)
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 repo.pushed_at < datetime.utcnow() - timedelta(days=180):
warn(f"Repo {repo.full_name} has not been pushed to since {repo.pushed_at}")
def parse(line):
m = gh_repo_regex.search(line)
if m:
[repo_name] = m.groups()
return github_table_row(retrieve_repo(repo_name))
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()