3 # Copyright (C) 2009-2010 W. Trevor King <wking@drexel.edu>
5 # This file is part of Bugs Everywhere.
7 # Bugs Everywhere is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by the
9 # Free Software Foundation, either version 2 of the License, or (at your
10 # option) any later version.
12 # Bugs Everywhere is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Bugs Everywhere. If not, see <http://www.gnu.org/licenses/>.
26 from libbe.subproc import Pipe, invoke
27 from update_copyright import update_authors, update_files
29 def validate_tag(tag):
31 >>> validate_tag('1.0.0')
32 >>> validate_tag('A.B.C-r7')
33 >>> validate_tag('A.B.C r7')
34 Traceback (most recent call last):
36 Exception: Invalid character ' ' in tag 'A.B.C r7'
38 Traceback (most recent call last):
40 Exception: Invalid character '"' in tag '"'
42 Traceback (most recent call last):
44 Exception: Invalid character ''' in tag '''
47 if char in string.digits:
49 elif char in string.letters:
51 elif char in ['.','-']:
53 raise Exception("Invalid character '%s' in tag '%s'" % (char, tag))
55 def bzr_pending_changes():
56 """Use `bzr diff`s exit status to detect change:
58 2 - unrepresentable changes
62 p = Pipe([['bzr', 'diff']])
65 elif p.status in [1,2]:
67 raise Exception("Error in bzr diff %d\n%s" % (p.status, p.stderrs[-1]))
69 def set_release_version(tag):
70 print "set libbe.version._VERSION = '%s'" % tag
71 p = Pipe([['sed', '-i', "s/^# *_VERSION *=.*/_VERSION = '%s'/" % tag,
72 os.path.join('libbe', 'version.py')]])
73 assert p.status == 0, p.statuses
75 def bzr_commit(commit_message):
76 print 'commit current status:', commit_message
77 p = Pipe([['bzr', 'commit', '-m', commit_message]])
78 assert p.status == 0, p.statuses
81 print 'tag current revision', tag
82 p = Pipe([['bzr', 'tag', tag]])
83 assert p.status == 0, p.statuses
85 def bzr_export(target_dir):
86 print 'export current revision to', target_dir
87 p = Pipe([['bzr', 'export', target_dir]])
88 assert p.status == 0, p.statuses
91 print 'generate libbe/_version.py'
92 p = Pipe([['make', os.path.join('libbe', '_version.py')]])
93 assert p.status == 0, p.statuses
95 def make_changelog(filename, tag):
96 print 'generate ChangeLog file', filename, 'up to tag', tag
97 p = invoke(['bzr', 'log', '--gnu-changelog', '-n1', '-r',
98 '..tag:%s' % tag], stdout=file(filename, 'w'))
100 assert status == 0, status
102 def set_vcs_name(filename, vcs_name='None'):
103 """Exported directory is not a bzr repository, so set vcs_name to
104 something that will work.
105 vcs_name: new_vcs_name
107 print 'set vcs_name in', filename, 'to', vcs_name
108 p = Pipe([['sed', '-i', "s/^vcs_name:.*/vcs_name: %s/" % vcs_name,
110 assert p.status == 0, p.statuses
112 def create_tarball(tag):
113 release_name='be-%s' % tag
114 export_dir = release_name
115 bzr_export(export_dir)
117 print 'copy libbe/_version.py to %s/libbe/_version.py' % export_dir
118 shutil.copy(os.path.join('libbe', '_version.py'),
119 os.path.join(export_dir, 'libbe', '_version.py'))
120 make_changelog(os.path.join(export_dir, 'ChangeLog'), tag)
121 set_vcs_name(os.path.join(export_dir, '.be', 'settings'))
122 tarball_file = '%s.tar.gz' % release_name
123 print 'create tarball', tarball_file
124 p = Pipe([['tar', '-czf', tarball_file, export_dir]])
125 assert p.status == 0, p.statuses
126 print 'remove', export_dir
127 shutil.rmtree(export_dir)
133 if __name__ == '__main__':
135 usage = """%prog [options] TAG
137 Create a bzr tag and a release tarball from the current revision.
141 p = optparse.OptionParser(usage)
142 p.add_option('--test', dest='test', default=False,
143 action='store_true', help='Run internal tests and exit')
144 options,args = p.parse_args()
146 if options.test == True:
150 assert len(args) == 1, '%d (!= 1) arguments: %s' % (len(args), args)
154 if bzr_pending_changes() == True:
155 print "Handle pending changes before releasing."
157 set_release_version(tag)
160 bzr_commit("Bumped to version %s" % tag)