forked from fikovnik/ShiftIt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fabfile.py
322 lines (251 loc) · 10.3 KB
/
fabfile.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
from fabric.api import local, execute, abort, task, lcd, puts
from fabric.contrib.console import confirm
from xml.etree import ElementTree
import os
import pystache
import github3
import tempfile
import base64
import datetime
################################################################################
## Configuration
################################################################################
proj_name = 'ShiftIt'
proj_info_plist = 'ShiftIt-Info.plist'
proj_src_dir = 'ShiftIt'
proj_private_key = '/Users/krikava/Dropbox/Personal/Keys/ShiftIt/dsa_priv.pem'
release_notes_template = '''
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<body>
<h1>{{proj_name}} version {{proj_version}}</h1>
{{#devel}}
<b>This is a development release that is intended for testing purposes only!</b>
{{/devel}}
{{#has_issues}}
<h2>Issues closed</h2>
<ul>
{{#issues}}
<li><a href="{{html_url}}"><b>#{{number}}</b></a> - {{title}}</li>
{{/issues}}
</ul>
{{/has_issues}}
More information about this release can be found on the <a href="{{milestone_url}}">here</a>.
<br/><br/>
If you find any bugs please report them on <a href="http://github.com/fikovnik/ShiftIt/issues">github</a>.
</body>
</html>
'''.strip()
appcast_template = '''
<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:sparkle="http://www.andymatuschak.org/xml-namespaces/sparkle" xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
<title>{{proj_name}} Changelog</title>
<link>{{proj_appcast_url}}</link>
<language>en</language>
<item>
<title>{{proj_name}} version {{proj_version}}</title>
<sparkle:releaseNotesLink>
{{proj_release_notes_url}}
</sparkle:releaseNotesLink>
<pubDate>{{date}}</pubDate>
<enclosure url="{{download_url}}" sparkle:version="{{proj_version}}" length="{{download_size}}" type="application/octet-stream" sparkle:dsaSignature="{{download_signature}}" />
</item>
</channel>
</rss>
'''.strip()
################################################################################
## Prerequisites
################################################################################
# if not local('git diff-index --quiet HEAD --').return_code:
# abort('There are pending changes in the repository. Run git status')
################################################################################
## Code
################################################################################
def _find(f, seq):
"""Return first item in sequence where f(item) == True."""
for item in seq:
if f(item):
return item
def _get_bundle_version(info_plist):
version = local('defaults read %s CFBundleVersion' % info_plist, capture=True)
return version.strip()
def _get_git_branch():
branch = local('git symbolic-ref HEAD', capture=True)
return branch[len('refs/heads/'):].strip()
def _gen_release_notes():
def _convert(i):
return { \
'number': i.number, \
'html_url': i.html_url, \
'title': i.title, \
}
github = _github()
shiftit = github.repository('fikovnik','ShiftIt')
milestone = _find(lambda m: proj_version.startswith(m.title), shiftit.iter_milestones())
if not milestone:
raise Exception('Unable to find milestone: %s' % proj_version)
open_issues = list(shiftit.iter_issues(milestone=milestone.number, state='open'))
if len(open_issues) > 0 and not proj_is_dev:
puts('Warning: there are still open issues')
for i in open_issues:
print '\t * #%s: %s' % (i.number, i.title)
closed_issues = list(shiftit.iter_issues(milestone=milestone.number, state='closed'))
closed_issues.sort(key=lambda i: i.closed_at)
release_notes = dict( \
has_issues = len(closed_issues) > 0, \
issues = closed_issues, \
proj_name=proj_name, \
proj_version=proj_version, \
devel=proj_is_dev, \
milestone_url='https://github.com/fikovnik/ShiftIt/issues?milestone=%d' % milestone.number, \
)
puts('Release notes properties:')
for (k,v) in release_notes.items():
print "\t%s: %s" % (k,v)
return pystache.render(release_notes_template, release_notes)
def _github():
return github3.login(_keychain_get_username('github.com'),
_keychain_get_password('github.com'))
def _keychain_get_username(account):
username = local("security find-internet-password -l %s | grep 'acct' | " \
"cut -d '\"' -f 4" % account, capture=True)
return username
def _keychain_get_password(account):
password = local("security 2>&1 > /dev/null find-internet-password -g -l" \
" %s | cut -d '\"' -f 2" % account, capture=True)
return password
################################################################################
## Project settings
################################################################################
proj_branch = _get_git_branch()
proj_is_dev = not proj_branch.startswith('release')
proj_src_dir = os.path.join(os.getcwd(), proj_src_dir)
proj_build_dir = os.path.join(os.getcwd(), 'build')
proj_app_dir = os.path.join(proj_src_dir,'build','Release',proj_name+'.app')
proj_public_key = os.path.join(proj_src_dir,'dsa_pub.pem')
proj_info_plist = os.path.join(proj_src_dir, proj_info_plist)
proj_version = _get_bundle_version(proj_info_plist)
proj_archive_tag = '-develop' if proj_is_dev else ''
proj_archive_name = proj_name + proj_archive_tag + '-' + proj_version + '.zip'
proj_archive_path = os.path.join(proj_build_dir, proj_archive_name)
proj_download_url = 'https://github.com/downloads/fikovnik/ShiftIt/'+proj_archive_name
proj_release_notes_url = 'http://htmlpreview.github.com/?https://raw.github.com/fikovnik/ShiftIt/'+proj_branch+'/release/release-notes-'+proj_version+'.html'
proj_release_notes_file = os.path.join(os.getcwd(),'release','release-notes-'+proj_version+'.html')
proj_appcast_url = 'https://raw.github.com/fikovnik/ShiftIt/'+proj_branch+'/release/appcast.xml'
proj_appcast_file = os.path.join(os.getcwd(),'release','appcast.xml')
################################################################################
## Tasks
################################################################################
@task
def info():
'''
Output all the build properties
'''
print 'Build info:'
for (k,v) in [(k,v) for (k,v) in globals().items() if k.startswith('proj_')]:
print "\t%s: %s" % (k[len('proj_'):],v)
@task
def build():
'''
Makes a build by executing xcodebuild
'''
with lcd(proj_src_dir):
local('xcodebuild -target %s -configuration Release' % proj_name)
@task
def archive():
'''
Archives build
'''
# dependencies
execute(build)
local('ditto -ck --keepParent %s %s' % (proj_app_dir, proj_archive_path))
@task
def prepare_release():
'''
Prepare the release: sign the build, generate appcast, generate release notes, commit and push.
'''
# prerequisites
puts('Verify that the update URL matches')
tree = ElementTree.parse(proj_info_plist)
root = tree.getroot().find('dict')
elem = list(root.findall('*'))
plist_appcast_url = _find(lambda (k,v): k.text == 'SUFeedURL', zip(*[iter(elem)]*2))[1].text.strip()
if plist_appcast_url != proj_appcast_url:
abort('Appcasts are different! Expected: `%s`, got: `%s`' % (proj_appcast_url, plist_appcast_url))
# dependencies
execute(archive)
puts('Sign the build')
sign_file = tempfile.mktemp()
local('openssl dgst -sha1 -binary < %s | openssl dgst -dss1 -sign %s > %s'
% (proj_archive_path, proj_private_key, sign_file))
local('openssl dgst -sha1 -binary < %s | openssl dgst -dss1 -verify %s -signature %s'
% (proj_archive_path, proj_public_key, sign_file))
signature = None
with open(sign_file) as f:
signature = base64.b64encode(f.read())
os.remove(sign_file)
# appcast properties
appcast = dict( \
proj_name=proj_name, \
proj_appcast_url=proj_appcast_url, \
proj_version=proj_version, \
proj_release_notes_url=proj_release_notes_url, \
date=datetime.datetime.now().strftime('%a, %d %b %G %T %z'), \
download_url=proj_download_url, \
download_size=os.path.getsize(proj_archive_path), \
download_signature=signature, \
)
puts('Appcast properties:')
for (k,v) in appcast.items():
print "\t%s: %s" % (k,v)
appcast_str = pystache.render(appcast_template, appcast)
release_notes_str = _gen_release_notes()
puts('Following will update appcast and release-notes, COMMIT and PUSH TO ORIGIN!')
if not confirm('Proceed with release (make sure you know what are you doing!)?'):
return
with open(proj_appcast_file,"w") as f:
f.write(appcast_str)
with open(proj_release_notes_file,"w") as f:
f.write(release_notes_str)
local('git add %s' % proj_appcast_file)
local('git add %s' % proj_release_notes_file)
local('git commit -m "Added appcast and release notes for the %s release"' % proj_archive_name)
local('git push origin %s' % proj_branch)
@task
def upload_release():
'''
Uploads the release to github
'''
# dependencies
execute(archive)
if not confirm('Proceed with upload?'):
return
github = _github()
shiftit = github.repository('fikovnik','ShiftIt')
download = _find(lambda d: d.name == proj_archive_name, shiftit.iter_downloads())
if download:
if not confirm('Download %s (id: %s, size: %s bytes) already exists. Override?' % (download.name, download.id, download.size)):
return
else:
puts('Deleting download: %s (%s)' % (download.name, download.id))
download.delete()
download = shiftit.create_download(proj_archive_name, proj_archive_path)
if not download:
raise Exception('Unable to upload')
puts('Uploaded: %s (id: %s, size: %s bytes): %s' % (download.name, download.id, download.size, download.html_url))
proj_download_url=download.html_url
@task
def release():
'''
Makes the complete release (same as prepare_release, upload_release)
'''
# dependencies
execute(prepare_release)
execute(upload_release)
@task
def print_release_notes():
'''
Prints release notes
'''
puts(_gen_release_notes())