forked from adblockplus/buildtools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.py
364 lines (286 loc) · 11.8 KB
/
build.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# 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/.
import argparse
import logging
import io
import json
import os
import re
import shutil
from urllib import urlencode
import urllib2
from functools import partial
from StringIO import StringIO
from zipfile import ZipFile
from buildtools.localeTools import read_locale_config
KNOWN_PLATFORMS = {'chrome', 'gecko', 'edge', 'generic'}
MAIN_PARSER = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter)
SUB_PARSERS = MAIN_PARSER.add_subparsers(title='Commands', dest='action',
metavar='[command]')
ALL_COMMANDS = []
def make_argument(*args, **kwargs):
def _make_argument(*args, **kwargs):
parser = kwargs.pop('parser')
parser.add_argument(*args, **kwargs)
return partial(_make_argument, *args, **kwargs)
def argparse_command(valid_platforms=None, multi_platform=False,
no_platform=False, arguments=()):
def wrapper(func):
def func_wrapper(*args, **kwargs):
return func(*args, **kwargs)
short_desc, long_desc = func.__doc__.split('\n\n', 1)
ALL_COMMANDS.append({
'name': func.__name__,
'description': long_desc,
'help_text': short_desc,
'valid_platforms': valid_platforms or KNOWN_PLATFORMS,
'multi_platform': multi_platform,
'function': func,
'arguments': arguments,
'no_platform': no_platform,
})
return func_wrapper
return wrapper
def make_subcommand(name, description, help_text, function, arguments):
new_parser = SUB_PARSERS.add_parser(
name.replace('_', '-'), description=description, help=help_text,
formatter_class=argparse.RawDescriptionHelpFormatter,
)
for argument in arguments:
argument(parser=new_parser)
new_parser.set_defaults(function=function)
return new_parser
def build_available_subcommands(base_dir):
"""Build subcommands, which are available for the repository in base_dir.
Search 'base_dir' for existing metadata.<type> files and make <type> an
avaible choice for the subcommands, intersected with their respective valid
platforms.
If no valid platform is found for a subcommand, it get's omitted.
"""
if build_available_subcommands._result is not None:
# Tests might run this code multiple times, make sure the collection
# of platforms is only run once.
return build_available_subcommands._result
types = set()
for p in KNOWN_PLATFORMS:
if os.path.exists(os.path.join(base_dir, 'metadata.' + p)):
types.add(p)
if len(types) == 0:
logging.error('No metadata file found in this repository. Expecting '
'one or more of {} to be present.'.format(
', '.join('metadata.' + p for p in KNOWN_PLATFORMS)))
build_available_subcommands._result = False
return False
for command_params in ALL_COMMANDS:
multi_platform = command_params.pop('multi_platform')
no_platform = command_params.pop('no_platform')
platforms = types.intersection(command_params.pop('valid_platforms'))
if len(platforms) > 1:
if multi_platform:
help_text = ('Multiple types may be specifed (each preceded '
'by -t/--type)')
action = 'append'
else:
help_text = None
action = 'store'
if not no_platform:
command_params['arguments'] += (
make_argument('-t', '--type', dest='platform',
required=True, choices=platforms,
action=action, help=help_text),
)
make_subcommand(**command_params)
elif len(platforms) == 1:
sub_parser = make_subcommand(**command_params)
sub_parser.set_defaults(platform=platforms.pop())
build_available_subcommands._result = True
return True
build_available_subcommands._result = None
@argparse_command(
valid_platforms={'chrome', 'gecko', 'edge'},
arguments=(
make_argument(
'-b', '--build-num', dest='build_num',
help='Use given build number (if omitted the build number will '
'be retrieved from Mercurial)'),
make_argument(
'-k', '--key', dest='key_file',
help='File containing private key and certificates required to '
'sign the package'),
make_argument(
'-r', '--release', action='store_true',
help='Create a release build'),
make_argument('output_file', nargs='?'),
),
)
def build(base_dir, build_num, key_file, release, output_file, platform,
**kwargs):
"""
Create a build.
Creates an extension build with given file name. If output_file is missing
a default name will be chosen.
"""
kwargs = {}
if platform == 'edge':
import buildtools.packagerEdge as packager
else:
import buildtools.packagerChrome as packager
kwargs['keyFile'] = key_file
kwargs['outFile'] = output_file
kwargs['releaseBuild'] = release
kwargs['buildNum'] = build_num
packager.createBuild(base_dir, type=platform, **kwargs)
@argparse_command(
valid_platforms={'chrome', 'gecko', 'edge'},
)
def devenv(base_dir, platform, **kwargs):
"""
Set up a development environment.
Will set up or update the devenv folder as an unpacked extension folder '
for development.
"""
if platform == 'edge':
import buildtools.packagerEdge as packager
else:
import buildtools.packagerChrome as packager
file = StringIO()
packager.createBuild(base_dir, type=platform, outFile=file, devenv=True,
releaseBuild=True)
from buildtools.packager import getDevEnvPath
devenv_dir = getDevEnvPath(base_dir, platform)
shutil.rmtree(devenv_dir, ignore_errors=True)
file.seek(0)
with ZipFile(file, 'r') as zip_file:
zip_file.extractall(devenv_dir)
project_key_argument = make_argument(
'project_key', help='The crowdin project key.',
)
@argparse_command(
arguments=(project_key_argument,),
)
def setuptrans(base_dir, project_key, platform, **kwargs):
"""
Set up translation languages.
Set up translation languages for the project on crowdin.com.
"""
from buildtools.packager import readMetadata
metadata = readMetadata(base_dir, platform)
basename = metadata.get('general', 'basename')
locale_config = read_locale_config(base_dir, platform, metadata)
import buildtools.localeTools as localeTools
localeTools.setupTranslations(locale_config, basename, project_key)
@argparse_command(
arguments=(project_key_argument,),
)
def translate(base_dir, project_key, platform, **kwargs):
"""
Update translation master files.
Update the translation master files in the project on crowdin.com.
"""
from buildtools.packager import readMetadata
metadata = readMetadata(base_dir, platform)
basename = metadata.get('general', 'basename')
locale_config = read_locale_config(base_dir, platform, metadata)
default_locale_dir = os.path.join(locale_config['base_path'],
locale_config['default_locale'])
import buildtools.localeTools as localeTools
localeTools.updateTranslationMaster(locale_config, metadata,
default_locale_dir, basename,
project_key)
@argparse_command(
arguments=(project_key_argument,),
)
def uploadtrans(base_dir, project_key, platform, **kwargs):
"""
Upload existing translations.
Upload already existing translations to the project on crowdin.com.
"""
from buildtools.packager import readMetadata
metadata = readMetadata(base_dir, platform)
basename = metadata.get('general', 'basename')
locale_config = read_locale_config(base_dir, platform, metadata)
import buildtools.localeTools as localeTools
for locale, locale_dir in locale_config['locales'].iteritems():
if locale != locale_config['default_locale'].replace('_', '-'):
localeTools.uploadTranslations(locale_config, metadata, locale_dir,
locale, basename, project_key)
@argparse_command(
arguments=(project_key_argument,),
)
def gettranslations(base_dir, project_key, platform, **kwargs):
"""
Download translation updates.
Download updated translations from crowdin.com.
"""
from buildtools.packager import readMetadata
metadata = readMetadata(base_dir, platform)
basename = metadata.get('general', 'basename')
locale_config = read_locale_config(base_dir, platform, metadata)
import buildtools.localeTools as localeTools
localeTools.getTranslations(locale_config, basename, project_key)
def valid_version_format(value):
if re.search(r'[^\d\.]', value):
raise argparse.ArgumentTypeError('Wrong version number format')
return value
@argparse_command(
valid_platforms={'chrome', 'gecko', 'edge'}, multi_platform=True,
arguments=(
make_argument(
'-k', '--key', dest='key_file',
help='File containing private key and certificates required to '
'sign the release.'),
make_argument(
'-d', '--downloads-repository', dest='downloads_repository',
help='Directory containing downloads repository (if omitted '
'../downloads is assumed)'),
make_argument(
'version', help='Version number of the release',
type=valid_version_format),
),
)
def release(base_dir, downloads_repository, key_file, platform, version,
**kwargs):
"""
Run release automation.
Note: If you are not the project owner then you probably don't want to run
this!
Run release automation: create downloads for the new version, tag source
code repository as well as downloads and buildtools repository.
"""
if downloads_repository is None:
downloads_repository = os.path.join(base_dir, os.pardir, 'downloads')
if 'chrome' in platform and key_file is None:
logging.error('You must specify a key file for this release')
return
import buildtools.releaseAutomation as releaseAutomation
releaseAutomation.run(base_dir, platform, version, key_file,
downloads_repository)
@argparse_command(no_platform=True)
def lint_gitlab_ci(base_dir, **kwargs):
"""Lint the .gitlab-ci.yaml file.
Test the .gitlab-ci.yaml file for validity. (Note: You need to have PyYAML
installed.)
"""
import yaml
filename = '.gitlab-ci.yml'
try:
with io.open(os.path.join(base_dir, filename), 'rt') as fp:
yaml_data = yaml.load(fp.read())
post_data = {'content': json.dumps(yaml_data)}
request = urllib2.Request('https://gitlab.com/api/v4/ci/lint/',
data=urlencode(post_data))
print urllib2.urlopen(request).read()
except IOError:
print 'No valid {} found.'.format(filename)
def process_args(base_dir, *args):
if build_available_subcommands(base_dir):
MAIN_PARSER.set_defaults(base_dir=base_dir)
# If no args are provided, this module is run directly from the command
# line. argparse will take care of consuming sys.argv.
arguments = MAIN_PARSER.parse_args(args if len(args) > 0 else None)
function = arguments.function
del arguments.function
function(**vars(arguments))