This repository has been archived by the owner on May 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget_changelog.py
87 lines (66 loc) · 2.29 KB
/
get_changelog.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
# -*- coding=UTF-8 -*-
"""Get changelog for sepecific version. """
from __future__ import (absolute_import, division, print_function,
unicode_literals)
import argparse
import codecs
import os
import re
import sys
from collections import namedtuple
__dirname__ = os.path.abspath(os.path.dirname(__file__))
CHANGELOG_DIR = os.path.join(__dirname__, 'source', 'changelogs')
def _changelogs():
return
def get_file(version):
"""Get changelog file for the version. """
assert isinstance(version, Version), type(version)
files = os.listdir(CHANGELOG_DIR)
pattern = re.compile(
r'\d+_{0.major}\.{0.minor}\.md'.format(version), flags=re.I)
try:
return next(os.path.join(CHANGELOG_DIR, i)
for i in files if pattern.match(i))
except StopIteration:
raise ValueError('Can not found changelog file.')
def get_changelog(filename, version):
"""Get changelog from file for the version. """
assert isinstance(version, Version), type(version)
groups = []
with codecs.open(filename, encoding='utf-8') as f:
is_start = False
while True:
line = f.readline()
if not line:
break
elif re.match('^#+', line):
is_start = True
group = []
groups.append(group)
if is_start:
group.append(line)
return [group for group in groups
if re.match(
r'^#+ {0.major}\.{0.minor}(?:\.{0.patch})?$'.format(version),
group[0].replace('\r\n', '\n'))][-1]
class Version(namedtuple('Version', ('major', 'minor', 'patch'))):
"""Semantic version."""
@classmethod
def parse(cls, text):
"""Get version from text. """
parts = re.split(r'\.|-', text)[:3]
if len(parts) == 2:
parts.append('0')
return cls(*parts)
def main():
parser = argparse.ArgumentParser()
parser.add_argument('version')
parser.add_argument('destination')
args = parser.parse_args()
version = Version.parse(args.version)
filename = get_file(version)
result = get_changelog(filename, version)
with codecs.open(args.destination, 'w', 'utf-8') as f:
f.writelines(result)
if __name__ == '__main__':
main()