File: release_notes.py

package info (click to toggle)
vistrails 3.0~git%2B9dc22bd-2
  • links: PTS
  • area: main
  • in suites: bullseye
  • size: 62,860 kB
  • sloc: python: 314,054; xml: 42,697; sql: 4,113; php: 731; sh: 469; makefile: 253
file content (331 lines) | stat: -rwxr-xr-x 11,735 bytes parent folder | download | duplicates (2)
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#!/usr/bin/python3
###############################################################################
##
## Copyright (C) 2014-2016, New York University.
## Copyright (C) 2011-2014, NYU-Poly.
## Copyright (C) 2006-2011, University of Utah.
## All rights reserved.
## Contact: contact@vistrails.org
##
## This file is part of VisTrails.
##
## "Redistribution and use in source and binary forms, with or without
## modification, are permitted provided that the following conditions are met:
##
##  - Redistributions of source code must retain the above copyright notice,
##    this list of conditions and the following disclaimer.
##  - Redistributions in binary form must reproduce the above copyright
##    notice, this list of conditions and the following disclaimer in the
##    documentation and/or other materials provided with the distribution.
##  - Neither the name of the New York University nor the names of its
##    contributors may be used to endorse or promote products derived from
##    this software without specific prior written permission.
##
## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
## AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
## THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
## PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
## CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
## EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
## PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
## OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
## WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
## OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
## ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
##
###############################################################################
"""This will write text of a release notes file based on commit messages and trac
tickets. Use the configuration section below to tune the start and end commits.
The text will be written to the standard output.
"""

import xmlrpc.client
import git
import getpass
import os
import sys
import re
import tempfile
import subprocess
import shutil

#### configuration ####
commit_start = "269e4808eca3" # hash of version used on last release notes
commit_end = "HEAD" # current hash
branch = "v2.1" # git branch to be used
release_name = "2.1.4"
clonepath = None    # set this to the complete path of a vistrails clone to be
                    # used
                    # if None, the remote repository will be cloned to a
                    # temporary folder and removed at the end of the script
#clonepath = '/Users/tommy/git/vistrails'
cloneremote = 'git://www.vistrails.org/vistrails.git'
#### end configuration #####

## The script will ask for your Trac user and password
## so no need to change this now
username = None
password = None
need_cleanup = False

################################################################################

def userpass(realm, u, may_save):
    global username
    global password
    if not username:
        print("Username:", end=' ')
        sys.stdout.flush()
        username = input()
        password = getpass.getpass()
    return True, username, password, False

################################################################################

def rstrip(s):
    return '\n'.join(l.rstrip() for l in s.splitlines())

################################################################################

def clone_vistrails_git_repository(path_to):
    global cloneremote
    cmdlist = ['git', 'clone', cloneremote,
               path_to]
    print("Cloning vistrails from:")
    print("  %s to"%cloneremote)
    print("  %s"%path_to)
    print("Be patient. This may take a while.")
    process = subprocess.Popen(cmdlist, shell=False,
                               stdin=subprocess.PIPE,
                               stdout=subprocess.PIPE,
                               stderr=subprocess.STDOUT)
    process.wait()
    if process.returncode == 0:
        print("repository is cloned.")
    return process.returncode

################################################################################

def init_repo():
    global clonepath, need_cleanup, branch
    ok = False
    if clonepath is None:
        clonepath = tempfile.mkdtemp(prefix="vtrel")
        try:
            if clone_vistrails_git_repository(clonepath) == 0:
                ok = True
                init_branch(clonepath,branch)
            need_cleanup = True
        except Exception as e:
            print("ERROR: Could not clone vistrails repository!")
            print(str(e))
            shutil.rmtree(clonepath)
            sys.exit(1)
    else:
        init_branch(clonepath,branch)
        pull_changes(clonepath)
        ok = True
    if ok:
        repo = git.Repo(clonepath)
        return repo
    else:
        print("ERROR: git clone failed.")
        sys.exit(1)

################################################################################

def cleanup_repo():
    global clonepath, need_cleanup
    if need_cleanup:
        shutil.rmtree(clonepath)

################################################################################

def init_branch(path_to, branch):
    cmdlist = ['git', 'checkout', "%s"%branch]
    print("Checking out %s branch..."%branch)
    current_dir = os.getcwd()
    os.chdir(path_to)
    process = subprocess.Popen(cmdlist, shell=False,
                               stdin=subprocess.PIPE,
                               stdout=subprocess.PIPE,
                               stderr=subprocess.STDOUT)
    process.wait()
    lines = process.stdout.readlines()
    for line in lines:
        print("   ", line)
    if process.returncode == 0:
        print("Branch %s was checked out."%branch)
    os.chdir(current_dir)
    return process.returncode

################################################################################

def pull_changes(path_to):
    cmdlist = ['git', 'pull']
    print("Pulling changes into the branch...")
    current_dir = os.getcwd()
    os.chdir(path_to)
    process = subprocess.Popen(cmdlist, shell=False,
                               stdin=subprocess.PIPE,
                               stdout=subprocess.PIPE,
                               stderr=subprocess.STDOUT)
    process.wait()
    lines = process.stdout.readlines()
    for line in lines:
        print("   ", line)
    if process.returncode == 0:
        print("Changes were pulled.")
    os.chdir(current_dir)
    return process.returncode

################################################################################

def checkout_branch(repo, branch):
    repobranch = getattr(repo.heads, branch)
    repobranch.checkout()

##############################################################################

def build_release_notes(repo, branch):
    global username
    global password
    global commit_start, commit_end

    def check_inside_skip(skip_list, message):
        found = False
        for s in skip_list:
            if s.find(message) != -1:
                found = True
                break
        return found

    re_ticket_old = re.compile(r'<ticket>(.*?)</ticket>', re.M | re.S)
    re_ticket = re.compile(r'^Ticket: (.*?)$', re.M | re.S)
    re_ticket2 = re.compile(r'^Fixes: (.*?)$', re.M | re.S)
    re_bugfix_old = re.compile(r'<bugfix>(.*?)</bugfix>', re.M | re.S)
    re_bugfix = re.compile(r'^Bugfix: (.*?)$', re.M | re.S)
    re_feature_old = re.compile(r'<feature>(.*?)</feature>', re.M | re.S)
    re_feature = re.compile(r'^Feature:(.*?)$', re.M | re.S)
    re_skip = re.compile(r'<skip>(.*?)</skip>', re.M | re.S)

    #build list and dictionary with commits
    logs = []
    log_map_time = {}
    checkout_branch(repo,branch)
    for c in repo.iter_commits("%s..%s"%(commit_start,commit_end)):
        logs.append(c)
        log_map_time[c.hexsha] = c.committed_date

    #populate dictionaries
    bugfixes = {}
    tickets = {}
    features = {}
    changes = {}
    ticket_info = {}

    for log in logs:
        ls = re_skip.findall(log.message)
        lf = re_feature.findall(log.message)
        lf.extend(re_feature_old.findall(log.message))
        lt = re_ticket.findall(log.message)
        lt.extend(re_ticket2.findall(log.message))
        lt.extend(re_ticket_old.findall(log.message))
        lb = re_bugfix.findall(log.message)
        lb.extend(re_bugfix_old.findall(log.message))
        for s in ls:
            changes[s.strip()] = log.hexsha
        for f in lf:
            features[f.strip()] = log.hexsha
        for t in lt:
            # handle tickets with # (should not be used)
            t = t.strip()
            if t.startswith('#'):
                t = t[1:]
            try:
                tickets[int(t)] = log.hexsha
            except ValueError:
                pass
        for b in lb:
            bugfixes[b.strip()] = log.hexsha
        if len(ls) == 0 and len(lf) == 0 and len(lt) == 0 and len(lb) == 0:
            changes[log.message] = log.hexsha


    #get ticket summaries from xmlrpc plugin installed on vistrails trac
    print("Will connect to VisTrails Trac with authentication...")
    if not username:
        print("Username:", end=' ')
        sys.stdout.flush()
        username = input()
        password = getpass.getpass()

    url = "https://%s:%s@www.vistrails.org/login/xmlrpc"%(username,
                                                               password)
    server = xmlrpc.client.ServerProxy(url)
    print("downloading tickets.", end=' ')
    for (tid,r) in list(tickets.items()):
        print(".", end=' ')
        sys.stdout.flush()
        try:
            ticket_info[tid] = server.ticket.get(tid)
        except Exception as e:
            del tickets[tid]
            print("commit %s: Could not get info for ticket %s"%(r,tid))
    print("done.")

    #place tickets on bugfixes or enhancements
    for (tid,r) in tickets.items():
        txt = "Ticket %s: %s"%(tid,ticket_info[tid][3]['summary'])
        if ticket_info[tid][3]['type'] == 'enhancement':
            features[txt] = r
        elif ticket_info[tid][3]['type'] in ['defect', 'defect+question']:
            bugfixes[txt] = r
        else:
            #put the rest as changes
            changes[txt] = r
    if commit_end == "HEAD" and len(logs) > 0:
        commit_end = logs[0].hexsha

    print()
    print()
    print("Release Name: v%s build %s from %s branch" % (release_name,
                                                         commit_end[0:12],
                                                         branch))
    print()
    print("Enhancements:")
    times = []
    for t, r in features.items():
        times.append((log_map_time[r], t))
    revisions = sorted(times)
    revisions.reverse()
    for (t,text) in revisions:
        r = features[text]
        print(" - %s (%s)" %(rstrip(text),r[0:12]))

    print()
    print("Bug fixes:")
    times = []
    for t, r in bugfixes.items():
        times.append((log_map_time[r], t))
    revisions = sorted(times)
    revisions.reverse()
    for (t,text) in revisions:
        r = bugfixes[text]
        print(" - %s (%s)" %(rstrip(text),r[0:12]))

    print()
    print("Other changes:")
    times = []
    for t, r in changes.items():
        times.append((log_map_time[r], t))
    revisions = sorted(times)
    revisions.reverse()
    for (t,text) in revisions:
        r = changes[text]
        print(" - %s (%s)" %(text.split('\n')[0][0:100].rstrip(),r[0:12]))

if __name__ == "__main__":
    repo = init_repo()
    build_release_notes(repo, branch)
    cleanup_repo()