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