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