summaryrefslogtreecommitdiff
path: root/bumper
blob: 4bbb25ad2da48a2c482bc2a843ceb89fead0a0eb (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
#!/usr/bin/env python2
# Copyright 2016  Lars Wirzenius
#
# 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 3 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, see <http://www.gnu.org/licenses/>.
#
# =*= License: GPL-3+ =*=


import re
import time

import cliapp

import bumperlib


class Bumper(cliapp.Application):

    def process_args(self, args):
        project_name = self.get_project_name()
        version = self.get_version(args)
        print 'Releasing {} version {}'.format(project_name, version)

        current_version = self.get_current_version()
        if not self.version_is_newer(version, current_version):
            raise cliapp.AppException(
                'New version {} is older than current version {}'.format(
                    version, current_version))

        version_py = self.find_version_py()
        print '... {}'.format(version_py)
        self.write_version_py(version_py, version, '')

        print '... debian/changelog'
        self.update_debian_changelog(version, '')
        self.release_debian_changelog()

        print '... NEWS'
        self.update_NEWS_for_release(version)

        print '... git commit'
        self.commit(version, 'Prepare to release version {}'.format(version))

        print '... git tag'
        self.make_release_tag(version)

        gitversion = version + '+git'
        print 'Updating in-development version to', gitversion

        print '... {}'.format(version_py)
        self.write_version_py(version_py, version, '+git')

        print '... debian/changelog'
        self.update_debian_changelog(version, 'New upstream version.')
        self.update_debian_changelog(gitversion, '')

        print '... NEWS'
        self.update_NEWS_for_git(gitversion)

        print '... git commit'
        self.commit(
            version,
            'Bump version number post-release to {}'.format(gitversion))

    def get_version(self, args):
        if len(args) != 1:
            raise cliapp.AppException('Need exactly one argument, the version')
        return args[0]

    def find_version_py(self):
        output = cliapp.runcmd(['git', 'ls-files'])
        filenames = [x.strip() for x in output.splitlines()]
        version_pys = [x for x in filenames if x.endswith('/version.py')]
        if len(version_pys) == 0:
            raise cliapp.AppException('No version.py in project')
        elif len(version_pys) > 1:
            raise cliapp.AppException('Too many version.py in project')
        return version_pys[0]

    def get_current_version(self):
        return cliapp.runcmd(['python', 'setup.py', '--version']).strip()

    def version_is_newer(self, v1, v2):
        '''Is v1 newer than v2?'''
        vi1 = self.parse_version_info(v1, None)
        vi2 = self.parse_version_info(v2, None)
        returncode, out, err = cliapp.runcmd_unchecked(
            ['dpkg', '--compare-versions', v1, 'gt', v2])
        return returncode == 0

    def update_debian_changelog(self, version, msg):
        debian_version = '{}-1'.format(version)
        cliapp.runcmd(['dch', '-v', debian_version, msg])

    def release_debian_changelog(self):
        cliapp.runcmd(['dch', '-r', ''])

    def update_NEWS_for_release(self, version):
        with open('NEWS') as f:
            text = f.read()
        date = time.strftime('%Y-%m-%d')
        pattern = r'^Version \d+(\.\d+)*(\+git)?, not yet released$'
        replacement = 'Version {}, released {}'.format(version, date)
        updated = re.sub(pattern, replacement, text, count=1, flags=re.M)
        with open('NEWS', 'w') as f:
            f.write(updated)

    def update_NEWS_for_git(self, version):
        with open('NEWS') as f:
            text = f.read()
        pattern = r'^Version \d+(\.\d+)*, released \d\d\d\d-\d\d-\d\d$'
        replacement = 'Version {}, not yet released'.format(version)
        match = re.search(pattern, text, flags=re.M)
        if not match:
            raise cliapp.AppException('No place to insert new entry in NEWS')
        before, after = text[:match.start()], text[match.start():]
        with open('NEWS', 'w') as f:
            f.write(before)
            f.write(replacement + '\n')
            f.write('-' * len(replacement))
            f.write('\n\n\n')
            f.write(after)

    def commit(self, version, msg):
        cliapp.runcmd(['git', 'commit', '-am', msg])

    def make_release_tag(self, version):
        name = self.get_project_name()
        tag_name = '{}-{}'.format(name, version)
        msg = 'Release version {}'.format(version)
        cliapp.runcmd(['git', 'tag', '-sam', msg, tag_name])

    def get_project_name(self):
        output = cliapp.runcmd(['python', 'setup.py', '--name'])
        return output.strip()

    def write_version_py(self, filename, version, suffix):
        version_info = self.parse_version_info(version, suffix)
        with open(filename, 'w') as f:
            f.write('__version__ = "{}"\n'.format(version + suffix))
            f.write('__version_info__ = {!r}\n'.format(version_info))

    def parse_version_info(self, version, suffix):
        parts = version.split('.')
        result = []
        for part in parts:
            try:
                result.append(int(part))
            except ValueError:
                result.append(part)
        if suffix:
            result.append(suffix)
        return tuple(result)


Bumper(version=bumperlib.__version__).run()