-
Notifications
You must be signed in to change notification settings - Fork 0
/
changelog-transform.py
executable file
·247 lines (227 loc) · 6.85 KB
/
changelog-transform.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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#!/usr/bin/env python3
#
# CLI frontend to changelog transformer
#
# (c) Kurt Garloff <[email protected]>, 1/2018
# License: CC-BY-SA 3.0
import sys
import os
import changelog
from six import print_
quiet = False
verbose = False
infmt = None
outfmt = None
tolerant = False
joinln = False
initver = '?-0'
dist = 'stable'
pkgnm = ''
maxent = 0
emails = {}
emaildb = False
guessmail= False
def helpout(rc=1):
print_("Usage: changelog-transform.py [options] in out", file=sys.stderr)
print_(" Options:", file=sys.stderr)
print_(" -h, --help: Output this help", file=sys.stderr)
# print_(" -v, --verbose: Increase verbosity (not implemented)", file=sys.stderr)
# print_(" -q, --quiet: Be quiet (not implemented)", file=sys.stderr)
print_(" -r, --rewrap: Rewrap changelog entries to fill width", file=sys.stderr)
print_(" -t, --tolerant: Tolerate non-std formatting", file=sys.stderr)
print_(" -i, --infmt rpm/deb: Override input file detection", file=sys.stderr)
print_(" -o, --outfmt rpm/deb: Override output file detection", file=sys.stderr)
print_(" -m, --maxent no: Set max number of entries to process (def=all)", file=sys.stderr)
print_(" Options to fill in info for RPM->DEB conversions:", file=sys.stderr)
print_(" -V, --version x.y-r: Set initial version (def: ?-0)", file=sys.stderr)
print_(" -a, --emails LIST: provide list of mails \"NAME <adr> [, NAME <adr> [..]]]\"", file=sys.stderr)
print_(" -e, --emaildb: use .emaildb and for names", file=sys.stderr)
print_(" -E, --emaildbguess: use .emaildb and .guessmaildb for names", file=sys.stderr)
print_(" -d, --distro distname: Override distro name (def=stable)", file=sys.stderr)
print_(" -n, --pkgname pkgnm: Set package name (def=autodetect)", file=sys.stderr)
sys.exit(rc)
def parsemailaddr(addr):
"Return tuple email, name from Name <addr> string"
idx = addr.find('<')
idx2 = addr.find('>')
if idx < 0 or idx2 < 0:
raise ValueError("Invalid Mail Address \"%s\"" % addr)
nm = addr[:idx].lstrip(' ').rstrip(' ')
return (addr[idx+1:idx2].lower(), nm)
def parse_args(argv):
"Parse command line args"
import getopt
global quiet, verbose, infmt, outfmt, tolerant, joinln
global initver, dist, pkgnm, maxent, emails, emaildb, guessmail
# options
try:
optlist, args = getopt.gnu_getopt(argv, 'vqhi:o:trV:a:d:n:m:eE', ('help', 'quiet', 'verbose', 'tolerant', 'rewrap', 'infmt=', 'outfmt=', 'version=', 'distro=', 'pkgname=', 'maxent=', 'emails=', 'emaildb', 'emaildbguess'))
except getopt.GetoptError as exc:
print_(exc)
helpout(1)
for (opt, arg) in optlist:
if opt == '-q' or opt == '--quiet':
quiet = True
continue
if opt == '-v' or opt == '--verbose':
verbose = True
continue
if opt == '-t' or opt == '--tolerant':
tolerant = True
continue
if opt == '-r' or opt == '--rewrap':
joinln = True
continue
if opt == '-i' or opt == '--infmt':
infmt = arg
continue
if opt == '-o' or opt == '--outfmt':
outfmt = arg
continue
if opt == '-m' or opt == '--maxent':
maxent = int(arg)
continue
# for RPM -> DEB
if opt == '-V' or opt == '--version':
initver = arg
continue
if opt == '-a' or opt == '--emails':
for addr in arg.split(','):
email, name = parsemailaddr(addr)
emails[email] = name
continue
if opt == '-e' or opt == '--emaildb':
emaildb = True
continue
if opt == '-E' or opt == '--emaildbguess':
emaildb = True
guessmail = True
continue
if opt == '-d' or opt == '--distro':
dist = arg
continue
if opt == '-n' or opt == '--pkgname':
pkgnm = arg
continue
if opt == '-h' or opt == '--help':
helpout(0)
if len(args) != 3:
helpout(1)
return args[1:]
EMAILDB = 'emaildb'
GMAILDB = 'guessmaildb'
class emailsdb:
def readdb(self, nm):
fullnm = self.pref+nm
emails = {}
if not os.access(os.path.dirname(fullnm), os.X_OK):
os.mkdir(os.path.dirname(fullnm), mode=0o750)
if not os.access(fullnm, os.R_OK):
open(fullnm, 'x').write('#EMail Address Database %s for changlog-transform.py\n' % nm)
return emails
fd = open(fullnm, 'r')
for ln in fd:
ln = ln.rstrip('\n')
if not ln or ln[0] == '#':
continue
email, name = parsemailaddr(ln)
emails[email] = name
return emails
def __init__(self, pref=os.environ['HOME']+'/.changelog-transform/', guess=True):
self.pref = pref
self.emaildb = self.readdb(EMAILDB)
self.guess = guess
if guess:
self.guessmaildb = self.readdb(GMAILDB)
def addrappend(self, nm, addrs):
fullnm = self.pref+nm
fd = open(fullnm, 'a')
db = self.guessmaildb if nm == GMAILDB else self.emaildb
for it in addrs.keys():
val = db.get(it)
if val:
if val == addrs[it]:
continue
else:
raise ValueError("Will not overwrite %s <%s> with %s" % (val, it, addrs[it]))
print_("%s <%s>" % (addrs[it], it), file=fd)
db[it] = addrs[it]
return self
def __getitem__(self, srch):
srch = srch.lower()
try:
nm = self.emaildb[srch]
except KeyError:
nm = changelog.guessnm(srch)
if self.guess:
try:
nm = self.guessmaildb[srch]
except:
#nm = changelog.guessnm(srch)
print_("WARN: Add %s <%s> to guessmaildb" % (nm, srch), file=sys.stderr)
self.addrappend(GMAILDB, {srch: nm})
else:
raise ValueError('No such email %s <%s>' % (nm, srch))
return nm
def main(argv):
global infmt, outfmt, pkgnm, emails
innm, outnm = parse_args(argv)
if not infmt:
if innm[-8:] == ".changes":
infmt = "rpm"
elif innm[-10:] == ".changelog":
infmt = "deb"
else:
print_("ERROR: Can not determine input format", file=sys.stderr)
sys.exit(2)
if not outfmt:
if outnm[-8:] == ".changes":
outfmt = "rpm"
elif outnm[-10:] == ".changelog":
outfmt = "deb"
else:
print_("ERROR: Can not determine output format", file=sys.stderr)
sys.exit(2)
if not pkgnm:
idx = innm.rfind('.')
if idx > 0:
pkgnm = os.path.basename(innm[0:idx])
else:
idx = outnm.rfind('.')
if idx > 0:
pkgnm = os.path.basename(outnm[0:idx])
elif infmt == 'rpm':
print_("WARN: Can not determine package name format", file=sys.stderr)
if innm == '-':
infd = sys.stdin
else:
infd = open(innm, 'r')
if emaildb:
if emails:
emails = emailsdb(guess = guessmail).addrappend(EMAILDB, emails)
else:
emails = emailsdb(guess = guessmail)
chglog = changelog.changelog(pkgnm = pkgnm, distover = dist, initver = initver, emaildb = emails)
if infmt == 'rpm':
chglog.rpmparse(infd, joinln, tolerant, maxent)
elif infmt == 'deb':
chglog.debparse(infd, joinln, tolerant, maxent)
else:
print_("ERROR: Input format %s unknown" % infmt)
sys.exit(3)
infd.close()
if outnm == '-':
outfd = sys.stdout
else:
outfd = open(outnm, 'w')
if outfmt == 'rpm':
print_(chglog.rpmout(), file=outfd, end='')
elif outfmt == 'deb':
print_(chglog.debout(), file=outfd, end='')
else:
print_("ERROR: Output format %s unknown" % outfmt)
sys.exit(4)
outfd.close()
return 0
if __name__ == "__main__":
sys.exit(main(sys.argv))