This repository has been archived by the owner on Aug 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
genchangelog
executable file
·149 lines (127 loc) · 3.62 KB
/
genchangelog
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/env python
import codecs
import click
import dateutil.parser
import jinja2
import locale
import os
DEB_TEMPLATE = """
{% for version in changelog %}
{{ project }} ({{ version.version }}-1{{ version_suffix }}) stable; urgency=low
{% for change in version.changes %}{% if change|is_list %}{% for subchange in change %} - {{ subchange }}
{% endfor %}{% else %} * {{ change }}
{% endif %}{% endfor %}
-- {{ version.author }} {{ version.date|deb_date_format }}
{% endfor %}
""" # noqa
RST_TEMPLATE = """
=========
Changelog
=========
{{ project }}
{% for version in changelog %}
Version {{ version.version }}
===============
released {{ version.date}} by {{ version.author }}:
{% for change in version.changes %}{% if change|is_list %}{% for subchange in change %} - {{ subchange }}
{% endfor %}{% else %}
* {{ change }}
{% endif %}
{% endfor %}
{% endfor %}
""" # noqa
@click.command()
@click.argument(
'project',
nargs=1,
)
@click.argument(
'changelog',
nargs=1,
type=click.Path(
exists=True,
file_okay=True,
dir_okay=False,
writable=False,
readable=True,
),
)
@click.argument(
'deb_changelog',
nargs=1,
type=click.Path(
file_okay=True,
dir_okay=False,
writable=True,
readable=True,
),
)
@click.argument(
'rst_changelog',
nargs=1,
type=click.Path(
file_okay=True,
dir_okay=False,
writable=True,
readable=True,
),
)
def genlog(project, changelog, deb_changelog, rst_changelog):
"""Generate derived changelog files. At the moment debian/changelog and
CHANGELOG.rst
CHANGELOG Input changelog file
DEB_CHANGELOG Output debian changelog
RST_CHANGELOG Output CHANGELOG.rst
"""
env = {}
with open(changelog) as f:
code = compile(f.read(), changelog, 'exec')
exec(code, env)
changelog_data = env['CHANGELOG']
gen_deb_log(project, changelog_data, deb_changelog)
gen_rst_log(project, changelog_data, rst_changelog)
def gen_deb_log(project, changelog_data, outfile):
"""Generate the debian/changelog"""
env = jinja2.Environment(autoescape=False)
env.filters["is_list"] = is_list
env.filters["deb_date_format"] = deb_date_format
template = env.from_string(DEB_TEMPLATE)
adsy_version_suffix = os.environ.get("ADSY_VERSION_SUFFIX", "")
res = template.render(
project=project,
changelog=changelog_data,
version_suffix=adsy_version_suffix
).strip()
with codecs.open(outfile, "w") as f:
f.write(res)
f.write("\n")
def gen_rst_log(project, changelog_data, outfile):
"""Generate the CHANGELOG.rst"""
env = jinja2.Environment(autoescape=False)
env.filters["is_list"] = is_list
template = env.from_string(RST_TEMPLATE)
res = template.render(project=project, changelog=changelog_data).strip()
with codecs.open(outfile, "w") as f:
f.write(res)
f.write("\n")
def is_list(value):
"""Returns the type of an object"""
return isinstance(value, list)
def deb_date_format(value):
"""Formating the debian data format"""
# Formatting this completely stupid format
# Thu, 25 Nov 2004 21:16:04 +0100
try:
try:
locale.setlocale(locale.LC_TIME, ('en_US', 'UTF-8'))
except locale.Error:
print(
"Warning could not set locale, "
"the changelog might be misformatted"
)
dt = dateutil.parser.parse(value)
return dt.strftime("%a, %d %b %Y %H:%M:%S %z")
finally:
locale.resetlocale()
if __name__ == "__main__":
genlog()