-
Notifications
You must be signed in to change notification settings - Fork 23
/
update-contributors.py
executable file
·110 lines (96 loc) · 3.77 KB
/
update-contributors.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/python3
# Authors:
# Martin Kosek <[email protected]>
#
# Copyright (C) 2011 Red Hat
# see file 'COPYING' for use and warranty information
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import subprocess
import re
import os
import shutil
from contextlib import contextmanager
import tempfile
import icu # sudo dnf install pyicu
@contextmanager
def chdir(path):
old_dir = os.getcwd()
os.chdir(path)
try:
yield
finally:
os.chdir(old_dir)
def contributors_from_git(git_directory, branch):
git_authors = []
with chdir(git_directory):
cmd = ['git', 'log', '--format=format:%aN', '--use-mailmap']
git_authors = subprocess.check_output(cmd, stderr=subprocess.STDOUT, encoding='UTF-8').splitlines()
return set(git_authors)
def contributors_from_po(git_directory, branch):
po_authors = []
with chdir(git_directory):
cmd = ['git', 'grep', '-h', '#zanata', 'po/']
po_attrs = subprocess.check_output(cmd, stderr=subprocess.STDOUT, encoding='UTF-8').splitlines()
for author in po_attrs:
po_authors.append(" ".join([x for x in author.split() if x[0] != '#' and x[0] != '<' and not x[0].isdigit()]))
return set(po_authors)
def update_changelog(git_directory, branch):
git_authors = contributors_from_git(git_directory, branch)
po_authors = contributors_from_po(git_directory, branch)
contributors_file_path = os.path.join(git_directory, 'Contributors.txt')
with open(contributors_file_path, 'r') as f:
lines = f.readlines()
new_file = []
in_developers = False
in_translators = False
file_authors = set()
for line in lines:
if line.startswith("Developers:"):
in_developers = True
new_file.append(line)
continue
if line.startswith("Translators:"):
in_translators = True
new_file.append(line)
continue
if (in_developers or in_translators) and not line.strip():
# last developer
if in_developers:
in_developers = False
authors = list(git_authors | file_authors)
if in_translators:
in_translators = False
authors = list(po_authors | file_authors)
collator = icu.Collator.createInstance(icu.Locale('en_US.UTF-8'))
authors.sort(key=collator.getSortKey)
for author in authors:
new_file.append(u"\t%s\n" % author)
file_authors = set()
if in_developers or in_translators:
author = line.strip()
file_authors.add(author)
else:
new_file.append(line)
(update_fd, update_file) = tempfile.mkstemp()
update_file_content = u"".join(new_file).encode("UTF-8")
os.write(update_fd, update_file_content)
os.close(update_fd)
shutil.move(update_file, contributors_file_path)
def main():
# Update changelog in current working directory master branch
path = os.getcwd()
update_changelog(path, "HEAD")
if __name__ == "__main__":
main()