Skip to content

Instantly share code, notes, and snippets.

@faithandbrave
Created November 6, 2023 07:10
Show Gist options
  • Save faithandbrave/0371d1c04bc34ceab68880cdf44f80ba to your computer and use it in GitHub Desktop.
Save faithandbrave/0371d1c04bc34ceab68880cdf44f80ba to your computer and use it in GitHub Desktop.
# TODO : cpprefjp以外にも対応する
import glob
import re
import os
import sys
from subprocess import Popen
point_dict = {
"cpprefjp/typo": 1,
"cpprefjp/link": 2,
"cpprefjp/addref": 20,
"cpprefjp/addlang": 20,
"cpprefjp/addpage": 20,
"cpprefjp/fixs": 2,
"cpprefjp/fixm": 5,
"cpprefjp/fixl": 10,
"cpprefjp/compiler": 2,
"boostjp/typo": 1,
"boostjp/releases": 2,
"boostjp/releasem": 5,
"boostjp/releasem": 10,
"boostjp/releasel": 20,
"boostjp/addrefs": 10,
"boostjp/addrefm": 20,
"boostjp/boosts": 5,
"tool/fixbug": 30,
"tool/improves": 10,
"tool/improvem": 30,
"tool/improvem": 50,
"tool/updatelib": 20,
"tool/updatelang": 10,
"tool/updatelang": 30,
"tool/updatelang": 50,
"tool/adds": 30,
"tool/addm": 50,
"tool/addl": 100,
}
def stats_contribution(text: str, filename: str) -> set[str]:
user_name: str | None = None
user_point = 0
commit_set = set()
users = dict()
for line in text.split("\n"):
m = re.fullmatch(r'## (.*?)', line)
if m:
user_name = m.group(1)
user_point = 0
continue
if not user_name:
continue
if line.startswith("| ["):
cols = line.split("|")
commits = cols[1].split(",")
for commit in commits:
m = re.fullmatch(r'\[(.*?)\]\((.*?)/commit/(.*?)\)', commit.strip())
full_commit_id = m.group(3)
commit_set.add(full_commit_id)
points = cols[2].split(",")
for point in points:
point_values = point.split(":")
point_name = point_values[0].strip()
point_quantity = int(point_values[1].strip())
point_value = point_dict.get(point_name)
if not point_value:
raise KeyError("{}: invalid point tag `{}`".format(filename, point_name))
user_point += point_value * point_quantity
users[user_name] = user_point
for name, point in sorted(users.items(), key=lambda item: item[1], reverse=True):
print("{}: {}".format(name, point))
return commit_set
def check_commit_set(commit_set: set[str]) -> None:
command = "git log --after \'2023-01-01\' --pretty=oneline --no-merges".split(" ")
proc = Popen(command, stdin=-1,stdout=-1,stderr=-1)
out, err = proc.communicate()
if len(err) > 0:
print(err)
return
commit_log_set = set()
for line in out.decode().split("\n"):
if len(line) <= 0:
continue
cols = line.split(" ")
full_commit_id = cols[0]
commit_log_set.add(full_commit_id)
diff = commit_log_set - commit_set
if len(diff) > 0:
print("unstats commits: {}".format(diff))
if __name__ == '__main__':
commit_set = set()
for p in sorted(list(glob.glob("start_editing/*.md", recursive=True))):
filename = os.path.basename(p)
if not filename.startswith("contribution_stats_"):
continue
with open(p) as f:
text = f.read()
commit_set = commit_set.union(stats_contribution(text, p))
check_commit_set(commit_set)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment