5b132406432a746b21c897922b94fae85ea78a70
[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_3_10*'
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.xz' % (self.package_name.lower(), self.package_version)
92                 md5sums += self.exec_cmd(cmd)
93
94                 return md5sums
95
96         def get_bugzilla_info(self):
97                 query = 'http://bugzilla.gnome.org/browse.cgi?product=%s' % (self.package_module)
98                 f = urllib.urlopen(query)
99                 s = f.read()
100                 f.close()
101
102                 s1 = '<p><i>'
103                 i = s.find(s1)
104                 start = i + len(s1)
105                 s2 = '</i></p>'
106                 end = s.find(s2, i + 1)
107                 description = s[start:end]
108
109                 s1 = "homepage"
110                 i = s.find(s1)
111                 s1 = "href"
112                 i = s.rfind(s1, 0, i)
113                 start = i + 6
114                 s2 = '">'
115                 end = s.find(s2, start)
116                 project_url = s[start:end]
117
118                 return (description, project_url)
119
120         def get_release_notes(self):
121                 name = self.package_name
122                 version = self.package_version
123                 download = self.package_dl_url
124                 md5sums = self.get_md5sums()
125                 (about, website) = self.get_bugzilla_info()
126                 news = self.get_news()
127                 footer = '%s\n%s team' % (datetime.date.today().strftime('%d %B %Y'),\
128                                           self.package_name)
129
130                 t = Template(template)
131                 return t.substitute(locals())
132         
133         def get_translations(self, cmd, format):
134                 translations = ''
135                 files_str = self.exec_cmd(cmd)
136                 files = files_str.splitlines()
137                 for line in files:
138                         f = line[line.rfind(' '):]
139                         lang = f[f.rfind('/')+1:f.rfind('.')]
140                         commit_str = self.exec_cmd("git log %s..  %s" % (self.last_tag, f))
141                         if commit_str == '':
142                                 continue
143
144                         authors = ''
145                         for line in commit_str.splitlines():
146                                 if line.startswith('Author:'):
147                                         p1 = line.find(' ')
148                                         p2 = line.find('<')
149                                         author = line[p1:p2].strip()
150
151                                         if authors.find(author) != -1:
152                                                 continue
153                                         if authors != '':
154                                                 authors += ", "
155                                         authors += author
156
157                         translations += format % (lang, authors)
158                 return translations
159
160         def get_bug_author(self, bug_number):
161                 cmd = 'git log %s.. | grep -B 20 -E \
162                       "(bug %s|#%s)|bugzilla.gnome.org/show_bug.cgi\?id=%s"' \
163                       ' | tac | grep ^Author: | head -1' \
164                       % (self.last_tag, bug_number, bug_number, bug_number)
165                 line = self.exec_cmd (cmd)
166                 p1 = line.find(" ")
167                 p2 = line.find("<")
168
169                 return line[p1:p2].strip()
170
171         def get_bugs(self):
172                 commit_str = self.exec_cmd('git show %s' % (self.last_tag))
173                 for line in commit_str.splitlines():
174                         if line.startswith('Date:'):
175                                 time_str = line[5:line.rfind('+')].strip()
176                                 t = time.strptime(time_str)
177                                 last_tag_date = time.strftime('%Y-%m-%d', t)
178                                 break
179
180                 query = 'http://bugzilla.gnome.org/buglist.cgi?' \
181                         'ctype=csv&product=empathy&' \
182                         'bug_status=RESOLVED,CLOSED,VERIFIED&resolution=FIXED&' \
183                         'chfieldfrom=%s&chfieldto=Now' % (last_tag_date)
184                 f = urllib.urlopen(query)
185                 s = f.read()
186                 f.close()
187
188                 col_bug_id = -1
189                 col_description = -1
190
191                 reader = csv.reader(s.splitlines(1))
192                 header = reader.next()
193                 i = 0
194
195                 for col in header:
196                         if col == 'bug_id':
197                                 col_bug_id = i
198                         if col == 'short_short_desc':
199                                 col_description = i
200                         i = i + 1
201
202                 bugs = ''
203                 for row in reader:
204                         bug_number = row[col_bug_id]
205                         description = row[col_description]
206                         author = self.get_bug_author(bug_number)
207                         bugs += ' - Fixed #%s, %s' % (bug_number, description)
208                         if author != '':
209                                 bugs += ' (%s)' % (author)
210                         bugs += '\n'
211                 return bugs
212
213         def generate_news(self):
214                 translations = self.get_translations("ls -l po/*.po", \
215                         " - Updated %s Translation (%s)\n")
216                 help_translations = self.get_translations("ls -l help/*/*.po", \
217                         " - Updated %s Documentation translation (%s)\n")
218                 bugs = self.get_bugs()
219
220                 news = 'NEW in '+ self.package_version
221                 line = '=' * len(news)
222                 today = datetime.date.today()
223                 news += ' (%s)\n%s\n' % (today.strftime('%d/%m/%Y'),line)
224                 if bugs != '':
225                         news += 'Bugs fixed:\n' + bugs + '\n'
226                 if translations != '':
227                         news += 'Translations:\n' + translations + '\n'
228                 if help_translations != '':
229                         news += 'Documentation translations:\n' + \
230                                 help_translations + '\n'
231
232                 return news
233
234         def write_news(self):
235                 news = self.generate_news()
236
237                 f = open ('/tmp/NEWS', 'w')
238                 s = f.write(news)
239                 f.close()
240
241                 self.exec_cmd('cat NEWS >> /tmp/NEWS')
242                 self.exec_cmd('mv /tmp/NEWS .')
243
244         def make_tag(self):
245                 new_tag = self.package_name.upper() + '_' +\
246                           self.package_version.replace('.', '_')
247                 self.exec_cmd('git tag -m "Tagged for release %s." %s' % ( self.package_version, new_tag))
248
249         def _get_username(self):
250                 username = os.environ.get('GNOME_ACCOUNT_NAME')
251                 if username is not None:
252                         return username
253                         
254                 return os.getlogin()
255                 
256
257         def upload_tarball(self):
258                 username = self._get_username()
259                 tarball = '%s-%s.tar.xz' % (self.package_name.lower(), self.package_version)
260
261                 cmd = 'scp %s %s@%s:' % (tarball, username, upload_server)
262                 self.exec_cmd(cmd)
263
264                 cmd = 'ssh %s@%s install-module -u %s' % (username, upload_server, tarball)
265                 self.exec_cmd(cmd)
266
267         def send_email(self):
268                 notes = self.get_release_notes()
269                 print notes
270                 cmd = 'xdg-email ' \
271                       ' --cc telepathy@lists.freedesktop.org' \
272                       ' --subject "ANNOUNCE: Empathy %s"' \
273                       ' --body "%s"' \
274                       ' gnome-announce-list@gnome.org' % (self.package_version,
275                                                           notes.replace('"', '\\"'))
276                 self.exec_cmd(cmd)
277
278         def release(self):
279                 self.make_tag()
280                 self.upload_tarball()
281                 self.send_email()
282
283 if __name__ == '__main__':
284         p = Project()
285         parser = OptionParser()
286         parser.add_option("-n", "--print-news", action="store_true",\
287                 dest="print_news", help="Generate and print news")
288         parser.add_option("-p", "--print-notes", action="store_true",\
289                 dest="print_notes", help="Generate and print the release notes")
290         parser.add_option("-w", "--write-news", action="store_true",\
291                 dest="write_news", help="Generate and write news into the NEWS file")
292         parser.add_option("-r", "--release", action="store_true",\
293                 dest="release", help="Release the tarball")
294         parser.add_option("-e", "--email", action="store_true",\
295                 dest="email", help="Send the release announce email")
296
297         (options, args) = parser.parse_args ()
298         if (options.print_news):
299                 print p.generate_news ()
300         if (options.print_notes):
301                 print p.get_release_notes ()
302         if (options.write_news):
303                 p.write_news ()
304         if (options.release):
305                 p.release ()
306         if (options.email):
307                 p.send_email ()
308