release.py 8.06 KB
Newer Older
1 2 3 4 5 6 7
#!/usr/bin/env python

import os
import re
import urllib
import csv
import datetime
8
import time
9
from string import Template
10
from optparse import OptionParser
11

12
last_tag_pattern = 'EMPATHY_2_32*'
13 14
upload_server = 'master.gnome.org'
template = '''\
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
$name $version is now available for download from:
$download

$md5sums

What is it?
===========
$about

You can visit the project web site:
$website

What's New?
===========
$news

31
$footer'''
32

33 34
class Bug:
	number = ''
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
	author = ''

class Project:
	def __init__(self):
		f = open('config.h', 'r')
		s = f.read()
		f.close()

		key = {}
		key['package'] = '#define PACKAGE_NAME "'
		key['version'] = '#define PACKAGE_VERSION "'
		key['bugreport'] = '#define PACKAGE_BUGREPORT "'

		for line in s.splitlines(1):
			if line.startswith(key['package']):
				p1 = len(key['package'])
				p2 = line.rfind('"')
				self.package_name = line[p1:p2] 		
			elif line.startswith(key['version']):
				p1 = len(key['version'])
				p2 = line.rfind('"')
				self.package_version = line[p1:p2] 		
			elif line.startswith(key['bugreport']):
				p2 = line.rfind('"')
				p1 = line.rfind('=') + 1
				self.package_module = line[p1:p2] 		

		first = self.package_version.find('.')
		second = self.package_version.find('.', first + 1)
		if first == -1 or second == -1 or first == second:
			version_dir = self.package_version
		else:
			version_dir = self.package_version[:second]
		self.package_dl_url = 'http://download.gnome.org/sources/%s/%s/' % (self.package_name.lower(), 
										    version_dir)
70
		tags_str = self.exec_cmd('git tag -l %s' % (last_tag_pattern))
Xavier Claessens's avatar
Xavier Claessens committed
71 72 73
		tags = tags_str.splitlines()
		self.last_tag = tags[len(tags)-1]

74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
	def exec_cmd(self,cmd):
		return os.popen(cmd).read()

	def get_news(self):
		f = open ('NEWS', 'r')
		s = f.read()
		f.close()
		start = s.find ('NEW in '+ self.package_version)
		if start != -1:
			start = s.find ('\n', start) + 1
			start = s.find ('\n', start) + 1
	        	end = s.find ('NEW in', start) - 1
		        return s[start:end].strip()

	def get_md5sums(self):
		md5sums = ''

		cmd = 'md5sum %s-%s.tar.gz' % (self.package_name.lower(), self.package_version)
		md5sums += self.exec_cmd(cmd)

		cmd = 'md5sum %s-%s.tar.bz2' % (self.package_name.lower(), self.package_version)
		md5sums += self.exec_cmd(cmd).strip()

		return md5sums
98 99 100 101 102 103 104 105 106 107 108 109

	def get_bugzilla_info(self):
		query = 'http://bugzilla.gnome.org/browse.cgi?product=%s' % (self.package_module)
		f = urllib.urlopen(query)
		s = f.read()
		f.close()

		s1 = '<p><i>'
		i = s.find(s1)
		start = i + len(s1)
		s2 = '</i></p>'
		end = s.find(s2, i + 1)
110
		description = s[start:end]
111

112
		s1 = "homepage"
113 114
		i = s.find(s1)
		s1 = "href"
115
		i = s.rfind(s1, 0, i)
116 117 118
		start = i + 6
		s2 = '">'
		end = s.find(s2, start)
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
		project_url = s[start:end]

		return (description, project_url)

	def get_release_notes(self):
		name = self.package_name
		version = self.package_version
		download = self.package_dl_url
		md5sums = self.get_md5sums()
		(about, website) = self.get_bugzilla_info()
		news = self.get_news()
		footer = '%s\n%s team' % (datetime.date.today().strftime('%d %B %Y'),\
					  self.package_name)

		t = Template(template)
		return t.substitute(locals())
135
	
136 137 138
	def get_translations(self, cmd, format):
		translations = ''
		files_str = self.exec_cmd(cmd)
Xavier Claessens's avatar
Xavier Claessens committed
139
		files = files_str.splitlines()
140 141 142
		for line in files:
			f = line[line.rfind(' '):]
			lang = f[f.rfind('/')+1:f.rfind('.')]
Xavier Claessens's avatar
Xavier Claessens committed
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
			commit_str = self.exec_cmd("git log %s..  %s" % (self.last_tag, f))
			if commit_str == '':
				continue

			authors = ''
			for line in commit_str.splitlines():
				if line.startswith('Author:'):
	        			p1 = line.find(' ')
        				p2 = line.find('<')
        				author = line[p1:p2].strip()

					if authors.find(author) != -1:
						continue
        				if authors != '':
        					authors += ", "
        				authors += author

160 161
			translations += format % (lang, authors)
		return translations
Xavier Claessens's avatar
Xavier Claessens committed
162

163
	def get_bug_author(self, bug_number):
164
		cmd = 'git log %s.. | grep -B 20 -E "(bug %s|#%s)"' \
165
		      ' | tac | grep ^Author: | head -1' \
166
		      % (self.last_tag, bug_number, bug_number)
167 168 169 170 171 172
		line = self.exec_cmd (cmd)
		p1 = line.find(" ")
		p2 = line.find("<")

		return line[p1:p2].strip()

Xavier Claessens's avatar
Xavier Claessens committed
173
	def get_bugs(self):
174 175 176 177 178 179 180 181
		commit_str = self.exec_cmd('git show %s' % (self.last_tag))
		for line in commit_str.splitlines():
			if line.startswith('Date:'):
				time_str = line[5:line.rfind('+')].strip()
				t = time.strptime(time_str)
				last_tag_date = time.strftime('%Y-%m-%d', t)
				break

Xavier Claessens's avatar
Xavier Claessens committed
182 183 184
		query = 'http://bugzilla.gnome.org/buglist.cgi?' \
			'ctype=csv&product=empathy&' \
			'bug_status=RESOLVED,CLOSED,VERIFIED&resolution=FIXED&' \
185
			'chfieldfrom=%s&chfieldto=Now' % (last_tag_date)
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
		f = urllib.urlopen(query)
		s = f.read()
		f.close()

		col_bug_id = -1
		col_description = -1

		reader = csv.reader(s.splitlines(1))
		header = reader.next()
		i = 0

		for col in header:
			if col == 'bug_id':
				col_bug_id = i
			if col == 'short_short_desc':
				col_description = i
			i = i + 1

204
		bugs = ''
205 206 207
		for row in reader:
			bug_number = row[col_bug_id]
			description = row[col_description]
208 209 210 211 212
			author = self.get_bug_author(bug_number)
			bugs += ' - Fixed #%s, %s' % (bug_number, description)
			if author != '':
				bugs += ' (%s)' % (author)
			bugs += '\n'
213
		return bugs
214

215
	def generate_news(self):
216 217 218 219
		translations = self.get_translations("ls -l po/*.po", \
			" - Updated %s Translation (%s)\n")
		help_translations = self.get_translations("ls -l help/*/*.po", \
			" - Updated %s Documentation translation (%s)\n")
220 221
		bugs = self.get_bugs()

222 223
		news = 'NEW in '+ self.package_version
		line = '=' * len(news)
224 225
		today = datetime.date.today()
		news += ' (%s)\n%s\n' % (today.strftime('%d/%m/%Y'),line)
226 227 228 229 230 231 232
		if bugs != '':
			news += 'Bugs fixed:\n' + bugs + '\n'
		if translations != '':
			news += 'Translations:\n' + translations + '\n'
		if help_translations != '':
			news += 'Documentation translations:\n' + \
			        help_translations + '\n'
233

234 235 236 237 238
		return news

	def write_news(self):
		news = self.generate_news()

239 240 241 242 243 244 245
		f = open ('/tmp/NEWS', 'w')
		s = f.write(news)
		f.close()

		self.exec_cmd('cat NEWS >> /tmp/NEWS')
		self.exec_cmd('mv /tmp/NEWS .')

246 247 248 249 250
	def make_tag(self):
		new_tag = self.package_name.upper() + '_' +\
			  self.package_version.replace('.', '_')
		self.exec_cmd('git tag -m "Tagged for release %s." %s' % ( self.package_version, new_tag))

251 252 253 254 255 256 257 258
	def _get_username(self):
		username = os.environ.get('GNOME_ACCOUNT_NAME')
		if username is not None:
			return username
			
		return os.getlogin()
		

259
	def upload_tarball(self):
260
		username = self._get_username()
261
		tarball = '%s-%s.tar.gz' % (self.package_name.lower(), self.package_version)
262

263 264
		cmd = 'scp %s %s@%s:' % (tarball, username, upload_server)
		self.exec_cmd(cmd)
265

266
		cmd = 'ssh %s@%s install-module -u %s' % (username, upload_server, tarball)
267
		self.exec_cmd(cmd)
268

269 270
	def send_email(self):
		notes = self.get_release_notes()
271
                print notes
272 273
		cmd = 'xdg-email ' \
		      ' --cc telepathy@lists.freedesktop.org' \
274 275
		      ' --subject "ANNOUNCE: Empathy %s"' \
		      ' --body "%s"' \
276 277
		      ' gnome-announce-list@gnome.org' % (self.package_version,
		      					  notes.replace('"', '\\"'))
278 279
		self.exec_cmd(cmd)

280 281 282
	def release(self):
		self.make_tag()
		self.upload_tarball()
283
		self.send_email()
284

285 286 287 288 289 290 291 292 293 294 295
if __name__ == '__main__':
	p = Project()
	parser = OptionParser()
	parser.add_option("-n", "--print-news", action="store_true",\
		dest="print_news", help="Generate and print news")
	parser.add_option("-p", "--print-notes", action="store_true",\
		dest="print_notes", help="Generate and print the release notes")
	parser.add_option("-w", "--write-news", action="store_true",\
		dest="write_news", help="Generate and write news into the NEWS file")
	parser.add_option("-r", "--release", action="store_true",\
		dest="release", help="Release the tarball")
296 297
	parser.add_option("-e", "--email", action="store_true",\
		dest="email", help="Send the release announce email")
298 299 300 301 302 303 304 305 306 307

	(options, args) = parser.parse_args ()
	if (options.print_news):
		print p.generate_news ()
	if (options.print_notes):
		print p.get_release_notes ()
	if (options.write_news):
		p.write_news ()
	if (options.release):
		p.release ()
308 309
	if (options.email):
		p.send_email ()
310