forked from lbt/useful-scripts
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchanges-from-deb
executable file
·67 lines (56 loc) · 2.16 KB
/
changes-from-deb
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
#!/usr/bin/python
from debian.changelog import Changelog, ChangelogParseError
import re
import sys
def gen_changelog(infile, outfile):
date_re = re.compile(r'(?P<dayname>\w\w\w), (?P<day>\d\d)'
r' (?P<monthname>\w\w\w) (?P<year>\d+)')
try:
chlog = Changelog(infile)
except ChangelogParseError, exc:
sys.stderr.write("%s: %s\n" % (infile.name, str(exc)))
sys.exit(1)
for entry in chlog:
match = date_re.match(entry.date)
date = u'%(dayname)s %(monthname)s %(day)s %(year)s' % match.groupdict()
changes = []
author = u""
# Some versions of debian.changelog.Changelog return str objects,
# other (later) versions return unicode objects.
for line in entry.changes():
if not line.strip():
continue
if isinstance(line, str):
line = line.decode('utf-8')
if line.startswith(u" * "):
changes.append(u"- %s%s" % (author, line[4:].strip()))
author = u""
elif line.startswith(u" [" ):
author = u"%s " % line.strip()
else:
changes.append(u" " + line.strip())
if isinstance(entry.author, str):
entry.author = entry.author.decode('utf-8')
rpm_entry = u"* %s %s - %s\n%s\n\n" \
% (date, entry.author, entry.version, u'\n'.join(changes))
outfile.write(rpm_entry.encode('utf-8'))
def gen_changelog_file(input_filename, output_filename):
try:
infile = open(input_filename)
except IOError, exc:
sys.stderr.write("Could not open %s: %s\n" % (input_filename, exc))
sys.exit(1)
try:
outfile = open(output_filename, 'w')
except IOError, exc:
sys.stderr.write("Could not open %s for writing: %s\n"
% (output_filename, exc))
sys.exit(1)
with infile:
with outfile:
gen_changelog(infile, outfile)
if __name__ == "__main__":
if len(sys.argv) != 3:
sys.stderr.write("Usage: %s INFILE OUTFILE\n" % sys.argv[0])
sys.exit(2)
gen_changelog_file(sys.argv[1], sys.argv[2])