blob: 3bd2690ab694748241535b46e188194341aac787 [file] [log] [blame]
vidorteg2b675b02019-11-25 17:51:281#!/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
7import argparse
8import os.path as path
9import re
10import subprocess
11import sys
12
13scripts_path = path.dirname(path.dirname(path.abspath(__file__)))
14sys.path.append(scripts_path)
15import devtools_paths
16
17devtools_path = devtools_paths.devtools_root_path()
18
19
20def parse_options(cli_args):
21 parser = argparse.ArgumentParser(description='Process localization check arguments.')
vidorteg75c025e2019-11-25 17:52:4322 parser.add_argument('--all', '-a', action='store_true', dest='all_files', help='If present, check all devtools frontend .js files')
vidorteg2b675b02019-11-25 17:51:2823 parser.add_argument('--files', nargs='+', help='List of .js files with absolute paths separated by a space')
Christy Chen1ab87e02020-01-31 00:32:1624 parser.add_argument('--file-list', dest='file_list', help='Specifies a file with a list of files (one per line)')
vidorteg2b675b02019-11-25 17:51:2825 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
vidorteg75c025e2019-11-25 17:52:4329 if len(cli_args) == 0:
Christy Chen1ab87e02020-01-31 00:32:1630 print('No argument provided. Assuming --all to check all files.')
31 args.all_files = True
vidorteg75c025e2019-11-25 17:52:4332
33 if args.all_files and args.files:
vidorteg2b675b02019-11-25 17:51:2834 parser.error(
vidorteg75c025e2019-11-25 17:52:4335 "Please provide only one option for scanning files: --all for all files or --files <FILE_LIST> for specific files.")
vidorteg2b675b02019-11-25 17:51:2836 return args
37
38
39def popen(arguments, cwd=None):
40 return subprocess.Popen(arguments, cwd=cwd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
41
42
43def 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
60def 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
68def 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
74def 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
80def 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 Chen1ab87e02020-01-31 00:32:1689 elif parsed_args.file_list:
90 check_devtools_localizability_args = ['--file-list', parsed_args.file_list]
vidorteg2b675b02019-11-25 17:51:2891
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
104if __name__ == '__main__':
105 main()