-
Notifications
You must be signed in to change notification settings - Fork 32
/
releaseAutomation.py
299 lines (235 loc) · 10.1 KB
/
releaseAutomation.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
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from __future__ import print_function
import os
import operator
import re
import codecs
import logging
import subprocess
import sys
import tarfile
import json
from packager import readMetadata, getDefaultFileName, get_extension
from localeTools import read_locale_config
SOURCE_ARCHIVE = 'adblockplus-{}-source.tgz'
def get_dependencies(prefix, repos):
from ensure_dependencies import read_deps, safe_join
repo = repos[prefix]
deps = read_deps(repo)
if deps:
for subpath in deps:
if subpath.startswith('_'):
continue
depprefix = prefix + subpath + '/'
deppath = safe_join(repo, subpath)
repos[depprefix] = deppath
get_dependencies(depprefix, repos)
def create_sourcearchive(repo, output):
with tarfile.open(output, mode='w:gz') as archive:
repos = {'': repo}
get_dependencies('', repos)
for prefix, path in repos.iteritems():
process = subprocess.Popen(['hg', 'archive', '-R', path, '-t', 'tar', '-S', '-'], stdout=subprocess.PIPE)
try:
with tarfile.open(fileobj=process.stdout, mode='r|') as repoarchive:
for fileinfo in repoarchive:
if os.path.basename(fileinfo.name) in ('.hgtags', '.hgignore'):
continue
filedata = repoarchive.extractfile(fileinfo)
fileinfo.name = re.sub(r'^[^/]+/', prefix, fileinfo.name)
archive.addfile(fileinfo, filedata)
finally:
process.stdout.close()
process.wait()
def repo_has_uncommitted():
"""Checks if the given repository is clean"""
buff = subprocess.check_output(['hg', 'status'])
if len(buff):
print('Dirty / uncommitted changes in repository!')
return True
return False
def repo_has_outgoing():
"""Checks whether there would be outgoing changesets to the given path"""
try:
subprocess.check_output(['hg', 'outgoing'])
print('Detected outgoing changesets!')
return True
except subprocess.CalledProcessError as e:
if e.returncode == 1:
return False
raise
def repo_has_incoming(*repo_paths):
"""Checks whether the local repositories are up-to-date"""
incoming = False
for repo_path in repo_paths:
try:
subprocess.check_output(['hg', 'incoming', '-R', repo_path])
print('Detected incoming changesets in "{}"'.format(repo_path))
incoming = True
except subprocess.CalledProcessError as e:
if e.returncode != 1:
raise
return incoming
def continue_with_outgoing():
"""Asks the user if they want to continue despite facing warnings"""
print('If you proceed with the release, they will be included in the '
'release and pushed.')
print('Are you sure about continuing the release process?')
while True:
choice = raw_input('Please choose (yes / no): ').lower().strip()
if choice == 'yes':
return True
if choice == 'no':
return False
def can_safely_release(*repo_paths):
"""Run repository-checks in order to bail out early if necessary"""
if repo_has_uncommitted():
return False
if repo_has_incoming(*repo_paths):
return False
if repo_has_outgoing():
return continue_with_outgoing()
return True
def compare_versions(a, b):
"""Compare two version numbers."""
a_digits = [int(v) for v in a.split('.')]
b_digits = [int(v) for v in b.split('.')]
def safe_get(items, index):
return items[index] if index < len(items) else 0
for i in range(max(len(a_digits), len(b_digits))):
result = safe_get(a_digits, i) - safe_get(b_digits, i)
if result != 0:
return result
return 0
def release_combination_is_possible(version, platforms, base_dir):
"""Determine whether a release for the given parameters is possible.
Examine existing tags in order to find either higher or matching versions.
The release is impossible if a) a higher version for a requested platform
exists, or if b) a matching version exists and the requested set of
platforms differs from what was already released.
"""
def higher_tag_version(tag, version, platforms):
return (compare_versions(tag[0], version) > 0 and
set(tag[1:]).intersection(platforms))
def incomplete_platforms_for_version(tag, version, platforms):
intersection = set(tag[1:]).intersection(platforms)
return (compare_versions(tag[0], version) == 0 and
intersection and set(platforms) != set(tag[1:]))
# only consider tags of the form "1.2[.x ...]-platform[-platform ...]
platform_tags = re.compile(r'^(\d+(?:(?:\.\d+)*)(?:-\w+)+)$', re.MULTILINE)
tags = [
c for c in [
t.split('-') for t in
platform_tags.findall(subprocess.check_output(
['hg', 'tags', '-R', base_dir, '-q']))
] if compare_versions(c[0], version) >= 0
]
for tag in tags:
if higher_tag_version(tag, version, platforms):
reason = ('The higher version {} has already been released for '
'the platforms {}.').format(tag[0], ', '.join(platforms))
return False, reason, None
if incomplete_platforms_for_version(tag, version, platforms):
reason = ('You have to re-release version {} for exactly all '
'of: {}').format(version, ', '.join(tag[1:]))
return False, reason, None
return (True, None,
any(compare_versions(tag[0], version) == 0 for tag in tags))
def update_metadata(metadata, version):
"""Replace version number in metadata file "manually".
The ConfigParser would mess up the order of lines.
"""
with open(metadata.option_source('general', 'version'), 'r+b') as fp:
rawMetadata = fp.read()
rawMetadata = re.sub(
r'^(\s*version\s*=\s*).*', r'\g<1>%s' % version,
rawMetadata, flags=re.I | re.M,
)
fp.seek(0)
fp.write(rawMetadata)
fp.truncate()
def create_build(platform, base_dir, target_path, version, key_file=None):
"""Create a build for the target platform and version."""
if platform == 'edge':
import buildtools.packagerEdge as packager
else:
import buildtools.packagerChrome as packager
metadata = readMetadata(base_dir, platform)
update_metadata(metadata, version)
build_path = os.path.join(
target_path,
getDefaultFileName(metadata, version,
get_extension(platform, key_file is not None)),
)
packager.createBuild(base_dir, type=platform, outFile=build_path,
releaseBuild=True, keyFile=key_file)
return build_path
def release_commit(base_dir, extension_name, version, platforms):
"""Create a release commit with a representative message."""
subprocess.check_output([
'hg', 'commit', '-R', base_dir, '-m',
'Noissue - Releasing {} {} for {}'.format(
extension_name, version,
', '.join([p.capitalize() for p in platforms]))],
stderr=subprocess.STDOUT)
def release_tag(base_dir, tag_name, extension_name):
"""Create a tag, along with a commit message for that tag."""
subprocess.check_call([
'hg', 'tag', '-R', base_dir, '-f', tag_name,
'-m', 'Noissue - Adding release tag for {} {}'.format(
extension_name, tag_name)])
def run(baseDir, platforms, version, keyFile, downloads_repo):
if not can_safely_release(baseDir, downloads_repo):
print('Aborting release.')
return 1
target_platforms = sorted(platforms)
release_identifier = '-'.join([version] + [p for p in target_platforms])
release_possible, reason, re_release = release_combination_is_possible(
version, platforms, baseDir)
if not release_possible:
logging.error(reason)
return 2
downloads = []
# Read extension name from first provided platform
locale_config = read_locale_config(
baseDir, target_platforms[0],
readMetadata(baseDir, target_platforms[0]))
default_locale_path = os.path.join(locale_config['base_path'],
locale_config['default_locale'],
'manifest.json')
with open(default_locale_path, 'r') as fp:
extension_name = json.load(fp)['name']['message']
for platform in target_platforms:
used_key_file = None
if platform == 'chrome':
# Currently, only chrome builds are provided by us as signed
# packages. Create an unsigned package in base_dir which should be
# uploaded to the Chrome Web Store
create_build(platform, baseDir, baseDir, version)
used_key_file = keyFile
downloads.append(
create_build(platform, baseDir, downloads_repo, version,
used_key_file),
)
# Only create one commit, one tag and one source archive for all
# platforms
archive_path = os.path.join(
downloads_repo,
'adblockplus-{}-source.tgz'.format(release_identifier),
)
try:
release_commit(baseDir, extension_name, version, target_platforms)
except subprocess.CalledProcessError as e:
if not (re_release and 'nothing changed' in e.output):
raise
create_sourcearchive(baseDir, archive_path)
downloads.append(archive_path)
release_tag(baseDir, release_identifier, extension_name)
# Now add the downloads and commit
subprocess.check_call(['hg', 'add', '-R', downloads_repo] + downloads)
release_commit(downloads_repo, extension_name, version, target_platforms)
# Push all changes
subprocess.check_call(['hg', 'push', '-R', baseDir])
subprocess.check_call(['hg', 'push', '-R', downloads_repo])