Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/release
blob: 2f40ad1e7710f1ddf5ae1f272b8ac7a29b20b718 (plain)
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
#!/usr/bin/env python
# Copyright (C) 2008, Red Hat, Inc.
# Copyright (C) 2010 One Laptop per Child
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

import gtk
import optparse
import os
import re
import subprocess
import sys
import StringIO

upload_host = 'shell.sugarlabs.org'
upload_root = '/upload/sources/sucrose'
download_uri = 'http://download.sugarlabs.org/sources/sucrose/'
announce_to = 'sugar-devel@lists.sugarlabs.org'

class ReleaseReport(object):
    def __init__(self, version):
        self.version = version
        self.commits = ''
        self.testcases = []

    def generate(self):
        p = subprocess.Popen(['git', 'tag'], stdout=subprocess.PIPE)
        tags = p.stdout.read().split('\n')
        tags = [ tag for tag in tags if tag.startswith('v') ]

        release_tag = 'v' + self.version
        try:
            i = tags.index(release_tag)
        except ValueError:
            print 'The tag you provided does not exist.'
            return

        if i > 0:
            previous = tags[i - 1]
            interval = previous + '..' + release_tag
        else:
            interval = release_tag

        p = subprocess.Popen(['git', 'log', interval, '--no-merges',
                              '--pretty=format:%s (%an)'],
                             stdout=subprocess.PIPE)

        lines = p.stdout.readlines()
        for line in lines:
            self.commits += '* ' + line

class Release(object):
    def __init__(self):
        self.name = None
        self.version = None

        user, err_ = subprocess.Popen(['git', 'config', 'user.name'],
                                      stdout=subprocess.PIPE).communicate()
        email, err_ = subprocess.Popen(['git', 'config', 'user.email'],
                                       stdout=subprocess.PIPE).communicate()

        self.email = '%s <%s>' % (user.strip('\n'), email.strip('\n'))

    def read_config(self):
        config = open(self.config_path).read()

        m = re.search(self.name_regexp, config)
        self.name = m.group(1)

        m = re.search(self.version_regexp, config)
        self.version = m.group(1)

    def check_version(self):
        p = subprocess.Popen(['git', 'tag'], stdout=subprocess.PIPE)
        tags = p.stdout.read().split('\n')
        tags = [tag for tag in tags if tag != '' ]
        tags.sort(key=lambda x: int(str.replace(x, 'v', '')))
        latest_tag = tags[-1]
        if latest_tag.startswith('v'):
            latest_tag = latest_tag[1:]
        if latest_tag != self.version:
            print 'Warning: Tag (%s) does not match version number (%s)' \
                % (latest_tag, self.version)
            print 'You can use the --version option to force a specific version'
            sys.exit(1)

    def next_version(self, current):
        splitted = current.split('.')
        new_minor = int(splitted[-1]) + 1
        splitted[-1] = str(new_minor)

        return '.'.join(splitted)

    def bump_version(self, version):
        config = open(self.config_path).read()

        m = re.search(self.version_regexp, config)
        if version is None:
            version = self.next_version(m.group(1))
        config = config[:m.start(1)] + version + config[m.end(1):]

        open(self.config_path, "w").write(config)

        self.version = version

    def undo_version(self):
        subprocess.check_call(['git', 'checkout', self.config_path])

    def tag(self):
        message = 'Release %s' % self.version

        subprocess.check_call(['git', 'commit', '-a', '-m' , '%s' % message])
        subprocess.check_call(['git', 'tag', 'v%s' % self.version])

    def push(self):
        subprocess.check_call(['git', 'push', 'origin', 'master'])
        subprocess.check_call(['git', 'push', '--tags'])

    def build_tarball(self):
        ret = subprocess.call(self.tarball_command)
        return ret == 0

    def get_tarball_name(self):
        return '%s-%s.tar.bz2' % (self.name, self.version)

    def get_tarball_path(self):
        return self.get_tarball_name()

    def upload(self):
        upload_path = os.path.join(upload_root, self.path_differentiator, 
                                   self.name)
        upload_dest = upload_host + ':' + upload_path

        subprocess.check_call(['ssh', upload_host, 'mkdir', '-m', '775',
                               '-p', upload_path])
        subprocess.check_call(['scp', self.get_tarball_path(), upload_dest])

    def announce(self):
        out = StringIO.StringIO()

        out.write('== Source ==\n\n')
        path = os.path.join(self.path_differentiator, self.name, 
                            self.get_tarball_name())

        out.write(download_uri + path + '\n')

        report = ReleaseReport(self.version)
        report.generate()

        if report.commits:
            out.write('\n== News ==\n\n')
            out.write(report.commits)

        announce = out.getvalue()
        out.close()

        dialog = gtk.Dialog()
        dialog.set_default_size(400, 400)
        dialog.add_buttons(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
                           'Announce', gtk.RESPONSE_OK)

        scrolledwindow = gtk.ScrolledWindow()
        scrolledwindow.set_policy(gtk.POLICY_AUTOMATIC,
                                  gtk.POLICY_AUTOMATIC)

        text_view = gtk.TextView()
        text_view.get_buffer().set_text(announce)
        scrolledwindow.add_with_viewport(text_view)
        text_view.show()

        dialog.vbox.pack_start(scrolledwindow)
        scrolledwindow.show()

        if dialog.run() == gtk.RESPONSE_OK:
            buf = text_view.get_buffer()
            text = buf.get_text(buf.get_start_iter(), buf.get_end_iter())
            subject = '[RELEASE] %s-%s' % (self.name, self.version)

            announce_filename = '.sugar-announce'
            f = open(announce_filename, 'w')
            f.write('From: %s\nTo: %s\nSubject: %s\n%s' % \
                    (self.email, announce_to, subject, text))
            f.close()

            subprocess.check_call(['scp', announce_filename,
                                   upload_host + ':~'])
            subprocess.check_call(['ssh', upload_host,
                                   '/usr/sbin/sendmail', '-t', '<', 
                                   announce_filename])
            subprocess.check_call(['ssh', upload_host,
                                   'rm', announce_filename])

            os.unlink(announce_filename)

class ActivityRelease(Release):
    def __init__(self):
        Release.__init__(self)

        self.config_path = os.path.join('activity', 'activity.info')
        self.name_regexp = 'name\s*=\s*(.*)'
        self.version_regexp = 'activity_version\s*=\s*(.*)'
        self.tarball_command = ['./setup.py', 'dist_source']
        self.path_differentiator = 'fructose'

    def get_tarball_path(self):
        return os.path.join('dist', self.get_tarball_name())

class AutomakeRelease(Release):
    def __init__(self):
        Release.__init__(self)

        self.config_path = 'configure.ac'
        self.name_regexp = 'AC_INIT\(\[.*?\],\[.*?\],\[.*?\],\[(.*?)\]'
        self.version_regexp = 'AC_INIT\(\[.*?\],\[(.*?)\]'
        self.tarball_command = ['make', 'distcheck']
        self.path_differentiator = 'glucose'

def main():
    parser = optparse.OptionParser()
    parser.add_option('-v', '--version', dest='version',
                      help='Release version')
    (options, args_) = parser.parse_args()

    if os.path.exists('configure.ac'):
        release = AutomakeRelease()
    elif os.path.exists('setup.py'):
        release = ActivityRelease()
    else:
        print 'Unknown module type.'

    release.read_config()
    if options.version is None:
        release.check_version()

    print 'Bump version number...'
    release.bump_version(options.version)

    print 'Build source tarball...'
    if not release.build_tarball():
        print 'Failed to build source tarball.'
        release.undo_version()
        sys.exit(1)

    print 'Tag the release in git...'
    release.tag()

    print 'Push the changes to remote git...'
    release.push()

    print 'Upload the source tarball...'
    release.upload()

    print 'Announce the release...'
    release.announce()

main()