vidorteg | 2b675b0 | 2019-11-25 17:51:28 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright 2019 The Chromium Authors. All rights reserved. |
| 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | |
| 7 | import argparse |
| 8 | import os.path as path |
| 9 | import re |
| 10 | import subprocess |
| 11 | import sys |
| 12 | |
| 13 | scripts_path = path.dirname(path.dirname(path.abspath(__file__))) |
| 14 | sys.path.append(scripts_path) |
| 15 | import devtools_paths |
| 16 | |
| 17 | devtools_path = devtools_paths.devtools_root_path() |
| 18 | |
| 19 | |
| 20 | def parse_options(cli_args): |
| 21 | parser = argparse.ArgumentParser(description='Process localization check arguments.') |
vidorteg | 75c025e | 2019-11-25 17:52:43 | [diff] [blame] | 22 | parser.add_argument('--all', '-a', action='store_true', dest='all_files', help='If present, check all devtools frontend .js files') |
vidorteg | 2b675b0 | 2019-11-25 17:51:28 | [diff] [blame] | 23 | parser.add_argument('--files', nargs='+', help='List of .js files with absolute paths separated by a space') |
Christy Chen | 1ab87e0 | 2020-01-31 00:32:16 | [diff] [blame] | 24 | parser.add_argument('--file-list', dest='file_list', help='Specifies a file with a list of files (one per line)') |
vidorteg | 2b675b0 | 2019-11-25 17:51:28 | [diff] [blame] | 25 | parser.add_argument( |
| 26 | '--autofix', action='store_true', help='If present, errors in localizable resources will be fixed automatically') |
| 27 | args = parser.parse_args(cli_args) |
| 28 | |
vidorteg | 75c025e | 2019-11-25 17:52:43 | [diff] [blame] | 29 | if len(cli_args) == 0: |
Christy Chen | 1ab87e0 | 2020-01-31 00:32:16 | [diff] [blame] | 30 | print('No argument provided. Assuming --all to check all files.') |
| 31 | args.all_files = True |
vidorteg | 75c025e | 2019-11-25 17:52:43 | [diff] [blame] | 32 | |
| 33 | if args.all_files and args.files: |
vidorteg | 2b675b0 | 2019-11-25 17:51:28 | [diff] [blame] | 34 | parser.error( |
vidorteg | 75c025e | 2019-11-25 17:52:43 | [diff] [blame] | 35 | "Please provide only one option for scanning files: --all for all files or --files <FILE_LIST> for specific files.") |
vidorteg | 2b675b0 | 2019-11-25 17:51:28 | [diff] [blame] | 36 | return args |
| 37 | |
| 38 | |
| 39 | def popen(arguments, cwd=None): |
| 40 | return subprocess.Popen(arguments, cwd=cwd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
| 41 | |
| 42 | |
| 43 | def check_with_node_script(script_path, script_description, args): |
| 44 | print(script_description + "...") |
| 45 | script_proc_errors_found = False |
| 46 | exec_command = [ |
| 47 | devtools_paths.node_path(), |
| 48 | script_path, |
| 49 | ] + args |
| 50 | |
| 51 | script_proc = popen(exec_command) |
| 52 | (script_proc_out, _) = script_proc.communicate() |
| 53 | if script_proc.returncode != 0: |
| 54 | script_proc_errors_found = True |
| 55 | |
| 56 | print(script_proc_out) |
| 57 | return script_proc_errors_found |
| 58 | |
| 59 | |
| 60 | def show_result(errors, checkIndex): |
| 61 | if errors: |
| 62 | print('Check {0} failed (see above)'.format(checkIndex)) |
| 63 | sys.exit(1) |
| 64 | else: |
| 65 | print('Check {0} succeeded'.format(checkIndex)) |
| 66 | |
| 67 | |
| 68 | def check_devtools_localizability(index, check_devtools_localizability_args): |
| 69 | script_path = devtools_paths.check_localized_strings_path() |
| 70 | script_description = 'Check {0}: Verifying that all resources are localizable'.format(index) |
| 71 | return check_with_node_script(script_path, script_description, check_devtools_localizability_args) |
| 72 | |
| 73 | |
| 74 | def check_devtools_localizable_resources(index, check_devtools_localizable_resources_args): |
| 75 | script_path = devtools_paths.check_localizable_resources_path() |
| 76 | script_description = 'Check {0}: Verifying the structure of localization resource files (grd)'.format(index) |
| 77 | return check_with_node_script(script_path, script_description, check_devtools_localizable_resources_args) |
| 78 | |
| 79 | |
| 80 | def main(): |
| 81 | check_devtools_localizable_resources_args = [] |
| 82 | check_devtools_localizability_args = [] |
| 83 | |
| 84 | parsed_args = parse_options(sys.argv[1:]) |
| 85 | if parsed_args.all_files: |
| 86 | check_devtools_localizability_args = ['-a'] |
| 87 | elif parsed_args.files: |
| 88 | check_devtools_localizability_args = parsed_args.files |
Christy Chen | 1ab87e0 | 2020-01-31 00:32:16 | [diff] [blame] | 89 | elif parsed_args.file_list: |
| 90 | check_devtools_localizability_args = ['--file-list', parsed_args.file_list] |
vidorteg | 2b675b0 | 2019-11-25 17:51:28 | [diff] [blame] | 91 | |
| 92 | if parsed_args.autofix: |
| 93 | check_devtools_localizable_resources_args = ['--autofix'] |
| 94 | |
| 95 | resource_index = 1 |
| 96 | localizability_index = 2 |
| 97 | resources_errors_found_localizable = check_devtools_localizable_resources(resource_index, |
| 98 | check_devtools_localizable_resources_args) |
| 99 | localizability_errors_found = check_devtools_localizability(localizability_index, check_devtools_localizability_args) |
| 100 | show_result(resources_errors_found_localizable, resource_index) |
| 101 | show_result(localizability_errors_found, localizability_index) |
| 102 | |
| 103 | |
| 104 | if __name__ == '__main__': |
| 105 | main() |