aboutsummaryrefslogtreecommitdiffstats
path: root/release.py
blob: d064b11ec08d17ef1187f219918701bec9f95aad (plain) (blame)
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
#!/usr/bin/python
#
# Copyright (C) 2009 W. Trevor King <wking@drexel.edu>
#
# 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 Street, Fifth Floor, Boston, MA 02110-1301 USA.

import os
import os.path
import shutil
import string
from subprocess import Popen
import sys

from update_copyright import Pipe

def validate_tag(tag):
    """
    >>> validate_tag('1.0.0')
    >>> validate_tag('A.B.C-r7')
    >>> validate_tag('A.B.C r7')
    Traceback (most recent call last):
      ...
    Exception: Invalid character ' ' in tag 'A.B.C r7'
    >>> validate_tag('"')
    Traceback (most recent call last):
      ...
    Exception: Invalid character '"' in tag '"'
    >>> validate_tag("'")
    Traceback (most recent call last):
      ...
    Exception: Invalid character ''' in tag '''
    """
    for char in tag:
        if char in string.digits:
            continue
        elif char in string.letters:
            continue
        elif char in ['.','-']:
            continue
        raise Exception("Invalid character '%s' in tag '%s'" % (char, tag))

def bzr_pending_changes():
    """Use `bzr diff`s exit status to detect change:
    1 - changed
    2 - unrepresentable changes
    3 - error
    0 - no change
    """
    p = Pipe([['bzr', 'diff']])
    if p.status == 0:
        return False
    elif p.status in [1,2]:
        return True
    raise Exception("Error in bzr diff %d\n%s" % (p.status, p.stderrs[-1]))

def set_release_version(tag):
    print "set libbe.version._VERSION = '%s'" % tag
    p = Pipe([['sed', '-i', "s/^# *_VERSION *=.*/_VERSION = '%s'/" % tag,
               os.path.join('libbe', 'version.py')]])
    assert p.status == 0, p.statuses

def bzr_commit(commit_message):
    print 'commit current status:', commit_message
    p = Pipe([['bzr', 'commit', '-m', commit_message]])
    assert p.status == 0, p.statuses

def bzr_tag(tag):
    print 'tag current revision', tag
    p = Pipe([['bzr', 'tag', tag]])
    assert p.status == 0, p.statuses

def bzr_export(target_dir):
    print 'export current revision to', target_dir
    p = Pipe([['bzr', 'export', target_dir]])
    assert p.status == 0, p.statuses

def make_version():
    print 'generate libbe/_version.py'
    p = Pipe([['make', os.path.join('libbe', '_version.py')]])
    assert p.status == 0, p.statuses

def make_changelog(filename, tag):
    print 'generate ChangeLog file', filename, 'up to tag', tag
    p = Popen(['bzr', 'log', '--gnu-changelog', '-n1', '-r',
               '..tag:%s' % tag], stdout=file(filename, 'w'))
    status = p.wait()
    assert status == 0, status

def set_vcs_name(filename, vcs_name='None'):
    """Exported directory is not a bzr repository, so set vcs_name to
    something that will work.
      vcs_name: new_vcs_name
    """
    print 'set_vcs_name in', filename, 'to', vcs_name
    p = Pipe([['sed', '-i', "s/^vcs_name:.*/vcs_name: %s/" % vcs_name,
               filename]])
    assert p.status == 0, p.statuses

def create_tarball(tag):
    release_name='be-%s' % tag
    export_dir = release_name
    bzr_export(export_dir)
    make_version()
    print 'copy libbe/_version.py to %s/libbe/_version.py' % export_dir
    shutil.copy(os.path.join('libbe', '_version.py'),
                os.path.join(export_dir, 'libbe', '_version.py'))
    make_changelog(os.path.join(export_dir, 'ChangeLog'), tag)
    set_vcs_name(os.path.join(export_dir, '.be', 'settings'))
    tarball_file = '%s.tar.gz' % release_name
    print 'create tarball', tarball_file
    p = Pipe([['tar', '-czf', tarball_file, export_dir]])
    assert p.status == 0, p.statuses
    print 'remove', export_dir
    shutil.rmtree(export_dir)

def test():
    import doctest
    doctest.testmod() 

if __name__ == '__main__':
    import optparse
    usage = """%prog [options] TAG

Create a bzr tag and a release tarball from the current revision.
For example
  %prog 1.0.0
"""
    p = optparse.OptionParser(usage)
    p.add_option('--test', dest='test', default=False,
                 action='store_true', help='Run internal tests and exit')
    options,args = p.parse_args()

    if options.test == True:
        test()
        sys.exit(0)

    assert len(args) == 1, '%d (!= 1) arguments: %s' % (len(args), args)
    tag = args[0]
    validate_tag(tag)

    if bzr_pending_changes() == True:
        print "Handle pending changes before releasing."
        sys.exit(1)
    set_release_version(tag)
    bzr_commit("Bumped to version %s" % tag)
    bzr_tag(tag)
    create_tarball(tag)