-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_book.py
81 lines (67 loc) · 3.05 KB
/
generate_book.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
import re
from typing import Tuple
from jinja2 import Environment, FileSystemLoader
CSS_FILE = 'sample_css.css'
PAGES = [{
'name': 'name', 'repos': ['repo'], 'comments': [{'author': 'iiogmgo', 'message': 'comments for you!'}]
}]
def get_css(filename: str) -> str:
css = ''
with open(filename) as f:
css += f.read() + '\n'
return css
def get_commits_by_repo(repos: list, name: str) -> Tuple[dict, dict, dict]:
data = {}
all_first_commit = None
all_last_commit = None
changes = re.compile(r'(\d+) files? changed(?:, (\d+) insertions?[(][+][)])?(?:, (\d+) deletions?)?')
for repo in repos:
with open(f'{repo}_{name.replace(" ", "_")}_log.txt') as f:
r_commits = f.readlines()
commits = []
sum_changes, sum_inserts, sum_deletes = 0, 0, 0
repo_first_commit = None
repo_last_commit = None
for r_commit in r_commits:
splits = r_commit.split('::')
change, insert, delete = re.search(changes, splits[4]).groups()
commit = {
'hash': splits[0],
'title': splits[1],
'date': splits[2],
'author': splits[3],
'changed': change or '-',
'inserts': insert or '-',
'deletes': delete or '-',
'repo': repo,
}
commits.append(commit)
sum_changes += int(change or 0)
sum_inserts += int(insert or 0)
sum_deletes += int(delete or 0)
if not repo_first_commit or (repo_first_commit and repo_first_commit['date'] > commit['date']):
repo_first_commit = commit
if not repo_last_commit or (repo_last_commit and repo_last_commit['date'] < commit['date']):
repo_last_commit = commit
if not all_first_commit or (all_first_commit and all_first_commit['date'] > repo_first_commit['date']):
all_first_commit = repo_first_commit
if not all_last_commit or (all_last_commit and all_last_commit['date'] < repo_last_commit['date']):
all_last_commit = repo_last_commit
data[repo] = {
'commits': commits, 'total_commits': len(commits),
'total_changes': sum_changes, 'total_inserts': sum_inserts, 'total_deletes': sum_deletes,
'range': f'{repo_first_commit["date"]} ~ {repo_last_commit["date"]}'
}
return data, all_first_commit, all_last_commit
for page in PAGES:
ENV = Environment(loader=FileSystemLoader('.'))
template = ENV.get_template('base.html')
commits_by_repo, first_commit, last_commit = get_commits_by_repo(page['repos'], page['name'])
data = {'style': get_css(CSS_FILE),
'commits_by_repo': commits_by_repo,
'first_commit': first_commit,
'last_commit': last_commit,
'rolling_paper': page['comments'],
'name': page['name'], 'range': f'{first_commit["date"][:4]} ~ {last_commit["date"][:4]}'},
with open(f'commit_book_{page["name"]}.html', 'w') as f:
f.write(template.render(*data))