Left: | ||
Right: |
OLD | NEW |
---|---|
1 # This Source Code Form is subject to the terms of the Mozilla Public | 1 # This Source Code Form is subject to the terms of the Mozilla Public |
2 # License, v. 2.0. If a copy of the MPL was not distributed with this | 2 # License, v. 2.0. If a copy of the MPL was not distributed with this |
3 # file, You can obtain one at http://mozilla.org/MPL/2.0/. | 3 # file, You can obtain one at http://mozilla.org/MPL/2.0/. |
4 | 4 |
5 import argparse | 5 import argparse |
6 import logging | 6 import logging |
7 import io | |
8 import json | |
7 import os | 9 import os |
8 import re | 10 import re |
9 import shutil | 11 import shutil |
10 import subprocess | 12 import subprocess |
11 import sys | 13 import sys |
14 from urllib import urlencode | |
15 import urllib2 | |
12 from functools import partial | 16 from functools import partial |
13 from StringIO import StringIO | 17 from StringIO import StringIO |
14 from zipfile import ZipFile | 18 from zipfile import ZipFile |
15 from buildtools.localeTools import read_locale_config | 19 from buildtools.localeTools import read_locale_config |
16 | 20 |
17 KNOWN_PLATFORMS = {'chrome', 'gecko', 'edge', 'generic'} | 21 KNOWN_PLATFORMS = {'chrome', 'gecko', 'edge', 'generic'} |
18 | 22 |
19 MAIN_PARSER = argparse.ArgumentParser( | 23 MAIN_PARSER = argparse.ArgumentParser( |
20 description=__doc__, | 24 description=__doc__, |
21 formatter_class=argparse.RawDescriptionHelpFormatter) | 25 formatter_class=argparse.RawDescriptionHelpFormatter) |
22 | 26 |
23 SUB_PARSERS = MAIN_PARSER.add_subparsers(title='Commands', dest='action', | 27 SUB_PARSERS = MAIN_PARSER.add_subparsers(title='Commands', dest='action', |
24 metavar='[command]') | 28 metavar='[command]') |
25 | 29 |
26 ALL_COMMANDS = [] | 30 ALL_COMMANDS = [] |
27 | 31 |
28 | 32 |
29 def make_argument(*args, **kwargs): | 33 def make_argument(*args, **kwargs): |
30 def _make_argument(*args, **kwargs): | 34 def _make_argument(*args, **kwargs): |
31 parser = kwargs.pop('parser') | 35 parser = kwargs.pop('parser') |
32 parser.add_argument(*args, **kwargs) | 36 parser.add_argument(*args, **kwargs) |
33 | 37 |
34 return partial(_make_argument, *args, **kwargs) | 38 return partial(_make_argument, *args, **kwargs) |
35 | 39 |
36 | 40 |
37 def argparse_command(valid_platforms=None, multi_platform=False, arguments=()): | 41 def argparse_command(valid_platforms=None, multi_platform=False, |
42 no_platform=False, arguments=()): | |
38 def wrapper(func): | 43 def wrapper(func): |
39 def func_wrapper(*args, **kwargs): | 44 def func_wrapper(*args, **kwargs): |
40 return func(*args, **kwargs) | 45 return func(*args, **kwargs) |
41 | 46 |
42 short_desc, long_desc = func.__doc__.split('\n\n', 1) | 47 short_desc, long_desc = func.__doc__.split('\n\n', 1) |
43 | 48 |
44 ALL_COMMANDS.append({ | 49 ALL_COMMANDS.append({ |
45 'name': func.__name__, | 50 'name': func.__name__, |
46 'description': long_desc, | 51 'description': long_desc, |
47 'help_text': short_desc, | 52 'help_text': short_desc, |
48 'valid_platforms': valid_platforms or KNOWN_PLATFORMS, | 53 'valid_platforms': valid_platforms or KNOWN_PLATFORMS, |
49 'multi_platform': multi_platform, | 54 'multi_platform': multi_platform, |
50 'function': func, | 55 'function': func, |
51 'arguments': arguments, | 56 'arguments': arguments, |
57 'no_platform': no_platform, | |
52 }) | 58 }) |
53 return func_wrapper | 59 return func_wrapper |
54 return wrapper | 60 return wrapper |
55 | 61 |
56 | 62 |
57 def make_subcommand(name, description, help_text, function, arguments): | 63 def make_subcommand(name, description, help_text, function, arguments): |
58 new_parser = SUB_PARSERS.add_parser( | 64 new_parser = SUB_PARSERS.add_parser( |
59 name, description=description, help=help_text, | 65 name.replace('_', '-'), description=description, help=help_text, |
60 formatter_class=argparse.RawDescriptionHelpFormatter, | 66 formatter_class=argparse.RawDescriptionHelpFormatter, |
61 ) | 67 ) |
62 | 68 |
63 for argument in arguments: | 69 for argument in arguments: |
64 argument(parser=new_parser) | 70 argument(parser=new_parser) |
65 | 71 |
66 new_parser.set_defaults(function=function) | 72 new_parser.set_defaults(function=function) |
67 return new_parser | 73 return new_parser |
68 | 74 |
69 | 75 |
(...skipping 18 matching lines...) Expand all Loading... | |
88 | 94 |
89 if len(types) == 0: | 95 if len(types) == 0: |
90 logging.error('No metadata file found in this repository. Expecting ' | 96 logging.error('No metadata file found in this repository. Expecting ' |
91 'one or more of {} to be present.'.format( | 97 'one or more of {} to be present.'.format( |
92 ', '.join('metadata.' + p for p in KNOWN_PLATFORMS))) | 98 ', '.join('metadata.' + p for p in KNOWN_PLATFORMS))) |
93 build_available_subcommands._result = False | 99 build_available_subcommands._result = False |
94 return False | 100 return False |
95 | 101 |
96 for command_params in ALL_COMMANDS: | 102 for command_params in ALL_COMMANDS: |
97 multi_platform = command_params.pop('multi_platform') | 103 multi_platform = command_params.pop('multi_platform') |
104 no_platform = command_params.pop('no_platform') | |
98 platforms = types.intersection(command_params.pop('valid_platforms')) | 105 platforms = types.intersection(command_params.pop('valid_platforms')) |
99 if len(platforms) > 1: | 106 if len(platforms) > 1: |
100 if multi_platform: | 107 if multi_platform: |
101 help_text = ('Multiple types may be specifed (each preceded ' | 108 help_text = ('Multiple types may be specifed (each preceded ' |
102 'by -t/--type)') | 109 'by -t/--type)') |
103 action = 'append' | 110 action = 'append' |
104 else: | 111 else: |
105 help_text = None | 112 help_text = None |
106 action = 'store' | 113 action = 'store' |
107 | 114 if not no_platform: |
108 command_params['arguments'] += ( | 115 command_params['arguments'] += ( |
109 make_argument('-t', '--type', dest='platform', required=True, | 116 make_argument('-t', '--type', dest='platform', |
110 choices=platforms, action=action, | 117 required=True, choices=platforms, |
111 help=help_text), | 118 action=action, help=help_text), |
112 ) | 119 ) |
113 make_subcommand(**command_params) | 120 make_subcommand(**command_params) |
114 elif len(platforms) == 1: | 121 elif len(platforms) == 1: |
115 sub_parser = make_subcommand(**command_params) | 122 sub_parser = make_subcommand(**command_params) |
116 sub_parser.set_defaults(platform=platforms.pop()) | 123 sub_parser.set_defaults(platform=platforms.pop()) |
117 | 124 |
118 build_available_subcommands._result = True | 125 build_available_subcommands._result = True |
119 return True | 126 return True |
120 | 127 |
121 | 128 |
122 build_available_subcommands._result = None | 129 build_available_subcommands._result = None |
(...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
368 def updatepsl(base_dir, **kwargs): | 375 def updatepsl(base_dir, **kwargs): |
369 """Update Public Suffix List. | 376 """Update Public Suffix List. |
370 | 377 |
371 Downloads Public Suffix List (see http://publicsuffix.org/) and generates | 378 Downloads Public Suffix List (see http://publicsuffix.org/) and generates |
372 lib/publicSuffixList.js from it. | 379 lib/publicSuffixList.js from it. |
373 """ | 380 """ |
374 import buildtools.publicSuffixListUpdater as publicSuffixListUpdater | 381 import buildtools.publicSuffixListUpdater as publicSuffixListUpdater |
375 publicSuffixListUpdater.updatePSL(base_dir) | 382 publicSuffixListUpdater.updatePSL(base_dir) |
376 | 383 |
377 | 384 |
385 @argparse_command(no_platform=True) | |
386 def lint_gitlab_ci(base_dir, **kwargs): | |
387 """Lint the .gitlab-ci.yaml file. | |
388 | |
389 Test the .gitlab-ci.yaml file for validity. (Note: You need to have PyYAML | |
390 installed.) | |
391 """ | |
392 import yaml | |
393 filename = '.gitlab-ci.yml' | |
394 try: | |
395 with io.open(os.path.join(base_dir, filename), 'rt') as fp: | |
396 yaml_data = yaml.load(fp.read()) | |
397 | |
398 post_data = {'content': json.dumps(yaml_data)} | |
399 request = urllib2.Request('https://gitlab.com/api/v4/ci/lint/', | |
400 data=urlencode(post_data)) | |
401 print urllib2.urlopen(request).read() | |
Vasily Kuznetsov
2018/07/09 17:59:48
Could we maybe set the return code based on the re
tlucas
2018/07/10 09:41:35
It's not hard to interpret at all ( on success it
| |
402 except IOError: | |
403 print 'No valid {} found.'.format(filename) | |
404 | |
405 | |
378 def process_args(base_dir, *args): | 406 def process_args(base_dir, *args): |
379 if build_available_subcommands(base_dir): | 407 if build_available_subcommands(base_dir): |
380 MAIN_PARSER.set_defaults(base_dir=base_dir) | 408 MAIN_PARSER.set_defaults(base_dir=base_dir) |
381 | 409 |
382 # If no args are provided, this module is run directly from the command | 410 # If no args are provided, this module is run directly from the command |
383 # line. argparse will take care of consuming sys.argv. | 411 # line. argparse will take care of consuming sys.argv. |
384 arguments = MAIN_PARSER.parse_args(args if len(args) > 0 else None) | 412 arguments = MAIN_PARSER.parse_args(args if len(args) > 0 else None) |
385 | 413 |
386 function = arguments.function | 414 function = arguments.function |
387 del arguments.function | 415 del arguments.function |
388 function(**vars(arguments)) | 416 function(**vars(arguments)) |
OLD | NEW |