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