Merge branch 'sasl'
[empathy.git] / release.py
1 #!/usr/bin/env python
2
3 import os
4 import re
5 import urllib
6 import csv
7 import datetime
8 import time
9 from string import Template
10 from optparse import OptionParser
11
12 last_tag_pattern = 'EMPATHY_2_91*'
13 upload_server = 'master.gnome.org'
14 template = '''\
15 $name $version is now available for download from:
16 $download
17
18 $md5sums
19
20 What is it?
21 ===========
22 $about
23
24 You can visit the project web site:
25 $website
26
27 What's New?
28 ===========
29 $news
30
31 $footer'''
32
33 class Bug:
34         number = ''
35         author = ''
36
37 class Project:
38         def __init__(self):
39                 f = open('config.h', 'r')
40                 s = f.read()
41                 f.close()
42
43                 key = {}
44                 key['package'] = '#define PACKAGE_NAME "'
45                 key['version'] = '#define PACKAGE_VERSION "'
46                 key['bugreport'] = '#define PACKAGE_BUGREPORT "'
47
48                 for line in s.splitlines(1):
49                         if line.startswith(key['package']):
50                                 p1 = len(key['package'])
51                                 p2 = line.rfind('"')
52                                 self.package_name = line[p1:p2]                 
53                         elif line.startswith(key['version']):
54                                 p1 = len(key['version'])
55                                 p2 = line.rfind('"')
56                                 self.package_version = line[p1:p2]              
57                         elif line.startswith(key['bugreport']):
58                                 p2 = line.rfind('"')
59                                 p1 = line.rfind('=') + 1
60                                 self.package_module = line[p1:p2]               
61
62                 first = self.package_version.find('.')
63                 second = self.package_version.find('.', first + 1)
64                 if first == -1 or second == -1 or first == second:
65                         version_dir = self.package_version
66                 else:
67                         version_dir = self.package_version[:second]
68                 self.package_dl_url = 'http://download.gnome.org/sources/%s/%s/' % (self.package_name.lower(), 
69                                                                                     version_dir)
70                 tags_str = self.exec_cmd('git tag -l %s' % (last_tag_pattern))
71                 tags = tags_str.splitlines()
72                 self.last_tag = tags[len(tags)-1]
73
74         def exec_cmd(self,cmd):
75                 return os.popen(cmd).read()
76
77         def get_news(self):
78                 f = open ('NEWS', 'r')
79                 s = f.read()
80                 f.close()
81                 start = s.find ('NEW in '+ self.package_version)
82                 if start != -1:
83                         start = s.find ('\n', start) + 1
84                         start = s.find ('\n', start) + 1
85                         end = s.find ('NEW in', start) - 1
86                         return s[start:end].strip()
87
88         def get_md5sums(self):
89                 md5sums = ''
90
91                 cmd = 'md5sum %s-%s.tar.gz' % (self.package_name.lower(), self.package_version)
92                 md5sums += self.exec_cmd(cmd)
93
94                 cmd = 'md5sum %s-%s.tar.bz2' % (self.package_name.lower(), self.package_version)
95                 md5sums += self.exec_cmd(cmd).strip()
96
97                 return md5sums
98
99         def get_bugzilla_info(self):
100                 query = 'http://bugzilla.gnome.org/browse.cgi?product=%s' % (self.package_module)
101                 f = urllib.urlopen(query)
102                 s = f.read()
103                 f.close()
104
105                 s1 = '<p><i>'
106                 i = s.find(s1)
107                 start = i + len(s1)
108                 s2 = '</i></p>'
109                 end = s.find(s2, i + 1)
110                 description = s[start:end]
111
112                 s1 = "homepage"
113                 i = s.find(s1)
114                 s1 = "href"
115                 i = s.rfind(s1, 0, i)
116                 start = i + 6
117                 s2 = '">'
118                 end = s.find(s2, start)
119                 project_url = s[start:end]
120
121                 return (description, project_url)
122
123         def get_release_notes(self):
124                 name = self.package_name
125                 version = self.package_version
126                 download = self.package_dl_url
127                 md5sums = self.get_md5sums()
128                 (about, website) = self.get_bugzilla_info()
129                 news = self.get_news()
130                 footer = '%s\n%s team' % (datetime.date.today().strftime('%d %B %Y'),\
131                                           self.package_name)
132
133                 t = Template(template)
134                 return t.substitute(locals())
135         
136         def get_translations(self, cmd, format):
137                 translations = ''
138                 files_str = self.exec_cmd(cmd)
139                 files = files_str.splitlines()
140                 for line in files:
141                         f = line[line.rfind(' '):]
142                         lang = f[f.rfind('/')+1:f.rfind('.')]
143                         commit_str = self.exec_cmd("git log %s..  %s" % (self.last_tag, f))
144                         if commit_str == '':
145                                 continue
146
147                         authors = ''
148                         for line in commit_str.splitlines():
149                                 if line.startswith('Author:'):
150                                         p1 = line.find(' ')
151                                         p2 = line.find('<')
152                                         author = line[p1:p2].strip()
153
154                                         if authors.find(author) != -1:
155                                                 continue
156                                         if authors != '':
157                                                 authors += ", "
158                                         authors += author
159
160                         translations += format % (lang, authors)
161                 return translations
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                 line = self.exec_cmd (cmd)
168                 p1 = line.find(" ")
169                 p2 = line.find("<")
170
171                 return line[p1:p2].strip()
172
173         def get_bugs(self):
174                 commit_str = self.exec_cmd('git show %s' % (self.last_tag))
175                 for line in commit_str.splitlines():
176                         if line.startswith('Date:'):
177                                 time_str = line[5:line.rfind('+')].strip()
178                                 t = time.strptime(time_str)
179                                 last_tag_date = time.strftime('%Y-%m-%d', t)
180                                 break
181
182                 query = 'http://bugzilla.gnome.org/buglist.cgi?' \
183                         'ctype=csv&product=empathy&' \
184                         'bug_status=RESOLVED,CLOSED,VERIFIED&resolution=FIXED&' \
185                         'chfieldfrom=%s&chfieldto=Now' % (last_tag_date)
186                 f = urllib.urlopen(query)
187                 s = f.read()
188                 f.close()
189
190                 col_bug_id = -1
191                 col_description = -1
192
193                 reader = csv.reader(s.splitlines(1))
194                 header = reader.next()
195                 i = 0
196
197                 for col in header:
198                         if col == 'bug_id':
199                                 col_bug_id = i
200                         if col == 'short_short_desc':
201                                 col_description = i
202                         i = i + 1
203
204                 bugs = ''
205                 for row in reader:
206                         bug_number = row[col_bug_id]
207                         description = row[col_description]
208                         author = self.get_bug_author(bug_number)
209                         bugs += ' - Fixed #%s, %s' % (bug_number, description)
210                         if author != '':
211                                 bugs += ' (%s)' % (author)
212                         bugs += '\n'
213                 return bugs
214
215         def generate_news(self):
216                 translations = self.get_translations("ls -l po/*.po", \
217                         " - Updated %s Translation (%s)\n")
218                 help_translations = self.get_translations("ls -l help/*/*.po", \
219                         " - Updated %s Documentation translation (%s)\n")
220                 bugs = self.get_bugs()
221
222                 news = 'NEW in '+ self.package_version
223                 line = '=' * len(news)
224                 today = datetime.date.today()
225                 news += ' (%s)\n%s\n' % (today.strftime('%d/%m/%Y'),line)
226                 if bugs != '':
227                         news += 'Bugs fixed:\n' + bugs + '\n'
228                 if translations != '':
229                         news += 'Translations:\n' + translations + '\n'
230                 if help_translations != '':
231                         news += 'Documentation translations:\n' + \
232                                 help_translations + '\n'
233
234                 return news
235
236         def write_news(self):
237                 news = self.generate_news()
238
239                 f = open ('/tmp/NEWS', 'w')
240                 s = f.write(news)
241                 f.close()
242
243                 self.exec_cmd('cat NEWS >> /tmp/NEWS')
244                 self.exec_cmd('mv /tmp/NEWS .')
245
246         def make_tag(self):
247                 new_tag = self.package_name.upper() + '_' +\
248                           self.package_version.replace('.', '_')
249                 self.exec_cmd('git tag -m "Tagged for release %s." %s' % ( self.package_version, new_tag))
250
251         def _get_username(self):
252                 username = os.environ.get('GNOME_ACCOUNT_NAME')
253                 if username is not None:
254                         return username
255                         
256                 return os.getlogin()
257                 
258
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                 cmd = 'scp %s %s@%s:' % (tarball, username, upload_server)
264                 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         def send_email(self):
270                 notes = self.get_release_notes()
271                 cmd = 'xdg-email ' \
272                       ' --cc telepathy@lists.freedesktop.org' \
273                       ' --subject "ANNOUNCE: Empathy %s"' \
274                       ' --body "%s"' \
275                       ' gnome-announce-list@gnome.org' % (self.package_version,
276                                                           notes.replace('"', '\\"'))
277                 self.exec_cmd(cmd)
278
279         def release(self):
280                 self.make_tag()
281                 self.upload_tarball()
282                 self.send_email()
283
284 if __name__ == '__main__':
285         p = Project()
286         parser = OptionParser()
287         parser.add_option("-n", "--print-news", action="store_true",\
288                 dest="print_news", help="Generate and print news")
289         parser.add_option("-p", "--print-notes", action="store_true",\
290                 dest="print_notes", help="Generate and print the release notes")
291         parser.add_option("-w", "--write-news", action="store_true",\
292                 dest="write_news", help="Generate and write news into the NEWS file")
293         parser.add_option("-r", "--release", action="store_true",\
294                 dest="release", help="Release the tarball")
295         parser.add_option("-e", "--email", action="store_true",\
296                 dest="email", help="Send the release announce email")
297
298         (options, args) = parser.parse_args ()
299         if (options.print_news):
300                 print p.generate_news ()
301         if (options.print_notes):
302                 print p.get_release_notes ()
303         if (options.write_news):
304                 p.write_news ()
305         if (options.release):
306                 p.release ()
307         if (options.email):
308                 p.send_email ()
309