| # Copyright 2014 The Chromium Authors | |
| # Use of this source code is governed by a BSD-style license that can be | |
| # found in the LICENSE file. | |
| """ | |
| DevTools presubmit script | |
| See http://goo.gle/devtools-testing-guide#Presubmit-checks for more how to | |
| run presubmit checks in DevTools. | |
| See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts | |
| for more details about the presubmit API built into gcl. | |
| `git cl presubmit -v -v` to debug presubmit checks. | |
| """ | |
| import json | |
| import re | |
| import sys | |
| import six | |
| import time | |
| from pathlib import Path | |
| USE_PYTHON3 = True | |
| PRESUBMIT_VERSION = '2.0.0' | |
| _EXCLUDED_PATHS = [ | |
| r'^extensions[\\/]cxx_debugging[\\/]e2e[\\/]runner\.py$', # Lines too long | |
| r'^front_end[\\/]core[\\/]common[\\/]Color\.ts$', # Apple copyright | |
| r'^front_end[\\/]core[\\/]common[\\/]Object\.ts$', # Apple copyright | |
| r'^front_end[\\/]core[\\/]common[\\/]ResourceType\.ts$', # Apple copyright | |
| # Apple copyright | |
| r'^front_end[\\/]ui[\\/]dom_extension[\\/]DOMExtension\.ts$', | |
| r'^front_end[\\/]core[\\/]platform[\\/]UIString\.ts$', # Apple copyright | |
| r'^front_end[\\/]core[\\/]sdk[\\/]Resource\.ts$', # Apple copyright | |
| r'^front_end[\\/]core[\\/]sdk[\\/]Script\.ts$', # Apple copyright | |
| # Apple copyright | |
| r'^front_end[\\/]panels[\\/]application[\\/]resourcesPanel\.css$', | |
| r'^front_end[\\/]panels[\\/]profiler[\\/]profilesPanel\.css$', | |
| r'^front_end[\\/]panels[\\/]sources[\\/]navigatorTree\.css$', | |
| r'^front_end[\\/]panels[\\/]sources[\\/]sourcesPanel\.css$', | |
| r'^front_end[\\/]panels[\\/]network[\\/]networkPanel\.css$', | |
| r'^front_end[\\/]panels[\\/]profiler[\\/]heapProfiler\.css$', | |
| r'^front_end[\\/]panels[\\/]timeline[\\/]timelinePanel\.css$', | |
| r'^front_end[\\/]third_party[\\/].*', # 3rd party code | |
| # Apple copyright | |
| r'^front_end[\\/]ui[\\/]legacy[\\/]components[\\/]data_grid[\\/]DataGrid\.ts$', | |
| r'^front_end[\\/]ui[\\/]legacy[\\/]tabbedPane\.css$', # Apple copyright | |
| r'^node_modules[\\/].*', # 3rd party code | |
| r'^scripts[\\/]build[\\/]code_generator_frontend\.py$', | |
| r'^scripts[\\/]deps[\\/]manage_node_deps\.py$', # Lines too long | |
| # Auto-generated files | |
| r'front_end[\\/]generated[\\/]InspectorBackendCommands\.ts$' | |
| ] | |
| def _ExecuteSubProcess(input_api, | |
| output_api, | |
| script_path, | |
| script_arguments=None, | |
| message=None): | |
| if isinstance(script_path, six.string_types): | |
| script_path = [input_api.python3_executable, script_path] | |
| start_time = time.time() | |
| process = input_api.subprocess.Popen(script_path + | |
| (script_arguments or []), | |
| stdout=input_api.subprocess.PIPE, | |
| stderr=input_api.subprocess.STDOUT) | |
| out, _ = process.communicate() | |
| end_time = time.time() | |
| time_difference = end_time - start_time | |
| return [ | |
| output_api.PresubmitError( | |
| "%s (%.1fs): %s" % | |
| (message if message is not None else script_path, time_difference, | |
| out.decode('utf-8').strip())) | |
| ] if process.returncode != 0 else [] | |
| def _IsEnvCog(input_api): | |
| old_sys_path = sys.path[:] | |
| devtools_root = input_api.PresubmitLocalPath() | |
| depot_tools = input_api.os_path.join(devtools_root, 'third_party', | |
| 'depot_tools') | |
| try: | |
| sys.path.append(depot_tools) | |
| from gclient_utils import IsEnvCog | |
| if IsEnvCog(): | |
| return True | |
| finally: | |
| sys.path = old_sys_path | |
| return False | |
| def _GetAffectedFiles(input_api, parent_directories, excluded_actions, | |
| accepted_endings): | |
| """Return absolute file paths of affected files (not due to an excluded action) | |
| under a parent directory with an accepted file ending. | |
| """ | |
| local_paths = [ | |
| f.AbsoluteLocalPath() for f in input_api.AffectedFiles() | |
| if all(f.Action() != action for action in excluded_actions) | |
| ] | |
| affected_files = [ | |
| file_name for file_name in local_paths | |
| if any(parent_directory in file_name | |
| for parent_directory in parent_directories) and ( | |
| len(accepted_endings) == 0 or any( | |
| file_name.endswith(accepted_ending) | |
| for accepted_ending in accepted_endings)) | |
| ] | |
| return affected_files | |
| def _CheckWithNodeScript(input_api, | |
| output_api, | |
| script_path, | |
| script_arguments=None, | |
| message=None): | |
| original_sys_path = sys.path | |
| try: | |
| sys.path = sys.path + [ | |
| input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts') | |
| ] | |
| import devtools_paths | |
| finally: | |
| sys.path = original_sys_path | |
| process = [devtools_paths.node_path(), script_path] | |
| return _ExecuteSubProcess(input_api, | |
| output_api, | |
| process, | |
| script_arguments=script_arguments, | |
| message=message) | |
| def _GetFilesToLint(input_api, lint_config_files, accepted_endings): | |
| run_full_check = False | |
| files_to_lint = [] | |
| # We are changing the lint configuration; run the full check. | |
| if len(lint_config_files) != 0: | |
| run_full_check = True | |
| else: | |
| root_dir = input_api.os_path.join(input_api.PresubmitLocalPath()) | |
| # Only run the linter on files that are relevant, to save PRESUBMIT time. | |
| files_to_lint = _GetAffectedFiles(input_api, root_dir, ['D'], | |
| accepted_endings) | |
| should_bail_out = len(files_to_lint) == 0 and not run_full_check | |
| return should_bail_out, files_to_lint | |
| def _CheckFormat(input_api, output_api): | |
| if _IsEnvCog(input_api): | |
| return [ | |
| output_api.PresubmitPromptWarning( | |
| 'Non-git environment detected, skipping _CheckFormat.') | |
| ] | |
| return _ExecuteSubProcess(input_api, | |
| output_api, ['git', 'cl', 'format', '--js'], | |
| message='Format') | |
| def CheckBugAssociationOnCommit(input_api, output_api): | |
| results = [] | |
| bugs = input_api.change.BugsFromDescription() | |
| message = ( | |
| "Each CL should be associated with a bug, use \'Bug:\' or \'Fixed:\' lines in\n" | |
| "the footer of the commit description. If you explicitly don\'t want to\n" | |
| "set a bug, use \'Bug: none\' in the footer of the commit description.\n\n" | |
| "Note: The footer of the commit description is the last block of lines in\n" | |
| "the commit description that doesn't contain empty lines. This means that\n" | |
| "any \'Bug:\' or \'Fixed:\' lines that are eventually followed by an empty\n" | |
| "line are not detected by this presubmit check.") | |
| if not bugs: | |
| results.append(output_api.PresubmitError(message)) | |
| return results | |
| def CheckExperimentTelemetry(input_api, output_api): | |
| experiment_telemetry_files = [ | |
| input_api.os_path.join(input_api.PresubmitLocalPath(), 'front_end', | |
| 'entrypoints', 'main', 'MainImpl.ts'), | |
| input_api.os_path.join(input_api.PresubmitLocalPath(), 'front_end', | |
| 'core', 'host', 'UserMetrics.ts') | |
| ] | |
| affected_main_files = _GetAffectedFiles(input_api, | |
| experiment_telemetry_files, [], | |
| ['.ts']) | |
| if len(affected_main_files) == 0: | |
| return [] | |
| script_path = input_api.os_path.join(input_api.PresubmitLocalPath(), | |
| 'scripts', 'check_experiments.js') | |
| return _CheckWithNodeScript(input_api, | |
| output_api, | |
| script_path, | |
| message='Experiment telemetry') | |
| def CheckESBuildVersion(input_api, output_api): | |
| script_path = input_api.os_path.join(input_api.PresubmitLocalPath(), | |
| 'scripts', | |
| 'check_esbuild_versions.js') | |
| return _CheckWithNodeScript(input_api, | |
| output_api, | |
| script_path, | |
| message='ESBuild version') | |
| def CheckPackageJsonScriptsDontCallNpm(input_api, output_api): | |
| """`scripts/tools/dt.py` runs the scripts in package.json without npm, so | |
| they must not call it either.""" | |
| package_json = input_api.os_path.join(input_api.PresubmitLocalPath(), | |
| 'package.json') | |
| if not _GetAffectedFiles(input_api, [package_json], ['D'], []): | |
| return [] | |
| with open(package_json, encoding='utf-8') as f: | |
| scripts = json.load(f).get('scripts', {}) | |
| # `npm`/`npx` not part of a longer name (`npm_test.js`, `npm-run-all`, | |
| # `npm.skia.org`) or directory (`scripts/npm/`). | |
| return [ | |
| output_api.PresubmitError( | |
| 'package.json script "%s" runs npm, which scripts/tools/dt.py ' | |
| 'does not use. Inline the command instead: %s' % (name, command)) | |
| for name, command in scripts.items() | |
| if re.search(r'(?<![\w.-])(npm|npx)(?![\w./-])', command) | |
| ] | |
| def CheckBuildGN(input_api, output_api): | |
| devtools_root = input_api.PresubmitLocalPath() | |
| devtools_front_end = input_api.os_path.join(devtools_root, 'front_end') | |
| verifier_dir = input_api.os_path.join(devtools_root, 'scripts', | |
| 'gn_deps_verifier') | |
| script_path = input_api.os_path.join(verifier_dir, 'cli.ts') | |
| verifier_files = _GetAffectedFiles(input_api, [verifier_dir], ['D'], []) | |
| affected_front_end_files = _GetAffectedFiles( | |
| input_api, [devtools_front_end], ['D'], | |
| ['.ts', '.js', '.css', 'BUILD.gn']) | |
| excluded_front_end_dirs = [ | |
| input_api.os_path.join(devtools_front_end, 'third_party'), | |
| input_api.os_path.join(devtools_front_end, 'legacy_test_runner'), | |
| ] | |
| affected_front_end_files = [ | |
| f for f in affected_front_end_files | |
| if not any(excluded_dir in f | |
| for excluded_dir in excluded_front_end_dirs) | |
| ] | |
| if len(verifier_files) == 0 and len(affected_front_end_files) == 0: | |
| return [] | |
| if len(verifier_files) > 0 or len(affected_front_end_files) > 50: | |
| script_arguments = ['--dry-run', '--all'] | |
| else: | |
| script_arguments = ['--dry-run'] + affected_front_end_files | |
| return _CheckWithNodeScript(input_api, | |
| output_api, | |
| script_path, | |
| script_arguments=script_arguments, | |
| message='BUILD.gn dependency check') | |
| def CheckDevToolsLint(input_api, output_api): | |
| lint_path = input_api.os_path.join(input_api.PresubmitLocalPath(), | |
| 'scripts', 'lint', 'run_lint_check.mjs') | |
| lint_related_paths = [ | |
| input_api.os_path.join(input_api.PresubmitLocalPath(), | |
| 'eslint.config.mjs'), | |
| input_api.os_path.join(input_api.PresubmitLocalPath(), | |
| '.stylelintrc.json'), | |
| input_api.os_path.join(input_api.PresubmitLocalPath(), | |
| '.stylelintignore'), | |
| # This file includes the LitAnalyzer rules | |
| input_api.os_path.join(input_api.PresubmitLocalPath(), 'front_end', | |
| 'tsconfig.json'), | |
| input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts', | |
| 'lint'), | |
| input_api.os_path.join(input_api.PresubmitLocalPath(), 'node_modules'), | |
| ] | |
| lint_config_files = _GetAffectedFiles(input_api, lint_related_paths, [], | |
| []) | |
| should_bail_out, files_to_lint = _GetFilesToLint( | |
| input_api, lint_config_files, ['.css', '.mjs', '.js', '.ts']) | |
| if should_bail_out: | |
| # Run the formatter on all non-js like files | |
| results = [] | |
| results.extend(_CheckFormat(input_api, output_api)) | |
| return results | |
| # If there are more than 50 files to check, don't bother and check | |
| # everything, so as to not run into command line length limits on Windows. | |
| if len(files_to_lint) > 50: | |
| files_to_lint = [] | |
| results = [] | |
| results.extend( | |
| _CheckWithNodeScript(input_api, | |
| output_api, | |
| lint_path, | |
| script_arguments=files_to_lint, | |
| message="Lint")) | |
| results.extend(_CheckFormat(input_api, output_api)) | |
| return results | |
| def CheckGeneratedFiles(input_api, output_api): | |
| v8_directory_path = input_api.os_path.join(input_api.PresubmitLocalPath(), | |
| 'v8') | |
| blink_directory_path = input_api.os_path.join( | |
| input_api.PresubmitLocalPath(), 'third_party', 'blink') | |
| scripts_build_path = input_api.os_path.join(input_api.PresubmitLocalPath(), | |
| 'scripts', 'build') | |
| scripts_generated_output_path = input_api.os_path.join( | |
| input_api.PresubmitLocalPath(), 'front_end', 'generated') | |
| generated_aria_path = input_api.os_path.join(scripts_build_path, | |
| 'generate_aria.py') | |
| generated_supported_css_path = input_api.os_path.join( | |
| scripts_build_path, 'generate_supported_css.py') | |
| generated_deprecation_path = input_api.os_path.join( | |
| scripts_build_path, 'generate_deprecations.py') | |
| generated_protocol_path = input_api.os_path.join( | |
| scripts_build_path, 'code_generator_frontend.py') | |
| generated_protocol_typescript_path = input_api.os_path.join( | |
| input_api.PresubmitLocalPath(), 'scripts', 'protocol_typescript') | |
| concatenate_protocols_path = input_api.os_path.join( | |
| input_api.PresubmitLocalPath(), 'third_party', 'inspector_protocol', | |
| 'concatenate_protocols.py') | |
| affected_files = _GetAffectedFiles(input_api, [ | |
| v8_directory_path, | |
| blink_directory_path, | |
| input_api.os_path.join(input_api.PresubmitLocalPath(), 'third_party', | |
| 'pyjson5'), | |
| generated_aria_path, | |
| generated_supported_css_path, | |
| generated_deprecation_path, | |
| concatenate_protocols_path, | |
| generated_protocol_path, | |
| scripts_generated_output_path, | |
| generated_protocol_typescript_path, | |
| ], [], ['.pdl', '.json5', '.py', '.js', '.ts']) | |
| if len(affected_files) == 0: | |
| return [] | |
| generate_protocol_resources_path = input_api.os_path.join( | |
| input_api.PresubmitLocalPath(), 'scripts', 'deps', | |
| 'generate_protocol_resources.py') | |
| return _ExecuteSubProcess(input_api, | |
| output_api, | |
| generate_protocol_resources_path, | |
| message='Generated files') | |
| def CheckL10nStrings(input_api, output_api): | |
| devtools_root = input_api.PresubmitLocalPath() | |
| devtools_front_end = input_api.os_path.join(devtools_root, 'front_end') | |
| script_path = input_api.os_path.join(devtools_root, 'third_party', 'i18n', | |
| 'check-strings.js') | |
| affected_front_end_files = [ | |
| file_name for file_name in | |
| _GetAffectedFiles(input_api, [devtools_front_end, script_path], ['D'], | |
| ['.js', '.ts']) if file_name == script_path | |
| or 'UIStrings' in input_api.ReadFile(file_name) | |
| ] | |
| if len(affected_front_end_files) == 0: | |
| return [] | |
| return _CheckWithNodeScript(input_api, | |
| output_api, | |
| script_path, [devtools_front_end], | |
| message='l10n strings') | |
| def CheckForTooLargeFiles(input_api, output_api): | |
| """Avoid large files, especially binary files, in the repository since | |
| git doesn't scale well for those. They will be in everyone's repo | |
| clones forever, forever making Chromium slower to clone and work | |
| with.""" | |
| # Uploading files to cloud storage is not trivial so we don't want | |
| # to set the limit too low, but the upper limit for "normal" large | |
| # files seems to be 1-2 MB, with a handful around 5-8 MB, so | |
| # anything over 20 MB is exceptional. | |
| TOO_LARGE_FILE_SIZE_LIMIT = 20 * 1024 * 1024 # 10 MB | |
| too_large_files = [] | |
| for f in input_api.AffectedFiles(): | |
| # Check both added and modified files (but not deleted files). | |
| if f.Action() in ('A', 'M'): | |
| size = input_api.os_path.getsize(f.AbsoluteLocalPath()) | |
| if size > TOO_LARGE_FILE_SIZE_LIMIT: | |
| too_large_files.append("%s: %d bytes" % (f.LocalPath(), size)) | |
| if too_large_files: | |
| message = ( | |
| 'Do not commit large files to git since git scales badly for those.\n' | |
| + | |
| 'Instead put the large files in cloud storage and use DEPS to\n' + | |
| 'fetch them.\n' + '\n'.join(too_large_files)) | |
| return [ | |
| output_api.PresubmitError('Too large files found in commit', | |
| long_text=message + '\n') | |
| ] | |
| return [] | |
| def CheckObsoleteScreenshotGoldens(input_api, output_api): | |
| devtools_root = input_api.PresubmitLocalPath() | |
| script_path = input_api.os_path.join(devtools_root, 'scripts', 'test', | |
| 'check_obsolete_goldens.js') | |
| # Only unit tests, goldens, and the script itself decide which goldens are | |
| # obsolete. Deleted files count, because a deleted test can leave goldens | |
| # behind. | |
| # LINT.IfChange(ObsoleteGoldensPaths) | |
| affected_unit_tests = _GetAffectedFiles( | |
| input_api, [input_api.os_path.join(devtools_root, 'front_end')], [], | |
| ['.test.ts']) | |
| affected_goldens = _GetAffectedFiles(input_api, [ | |
| input_api.os_path.join(devtools_root, 'test', 'goldens'), script_path | |
| ], [], []) | |
| # LINT.ThenChange(/scripts/test/check_obsolete_goldens.js:ObsoleteGoldensPaths) | |
| if len(affected_unit_tests) == 0 and len(affected_goldens) == 0: | |
| return [] | |
| return _CheckWithNodeScript(input_api, | |
| output_api, | |
| script_path, | |
| script_arguments=[], | |
| message='Obsolete screenshot images') | |
| def CheckTestExpectations(input_api, output_api): | |
| script_path = input_api.os_path.join(input_api.PresubmitLocalPath(), | |
| 'scripts', 'test', | |
| 'check_test_expectations.js') | |
| expectations_path = input_api.os_path.join(input_api.PresubmitLocalPath(), | |
| 'test', 'TestExpectations') | |
| return _CheckWithNodeScript( | |
| input_api, | |
| output_api, | |
| script_path, | |
| script_arguments=['--expectations-file', expectations_path], | |
| message='Test expectations') | |
| _SKIP_NODE_MODULES_CHECK_FOOTER = 'Skip-Node-Modules-Check' | |
| def _ImportManageNodeDeps(input_api): | |
| original_sys_path = sys.path | |
| try: | |
| sys.path = sys.path + [ | |
| input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts', | |
| 'deps') | |
| ] | |
| import manage_node_deps | |
| return manage_node_deps | |
| finally: | |
| sys.path = original_sys_path | |
| def CheckNodeModules(input_api, output_api): | |
| """node_modules/ is the archive pinned in DEPS, extracted by gclient sync. | |
| Checks that it is present, that the DEPS object is content-addressed and | |
| that the files the archive is built from change together with DEPS. | |
| """ | |
| if input_api.is_committing: | |
| report = output_api.PresubmitError | |
| else: | |
| report = output_api.PresubmitPromptWarning | |
| node_modules = input_api.os_path.join(input_api.PresubmitLocalPath(), | |
| 'node_modules') | |
| if not input_api.os_path.isdir(node_modules): | |
| return [ | |
| report('node_modules/ is missing. Run `gclient sync` to ' | |
| 'download it.') | |
| ] | |
| manage_node_deps = _ImportManageNodeDeps(input_api) | |
| affected = { | |
| f.LocalPath().replace(input_api.os_path.sep, '/'): f | |
| for f in input_api.AffectedFiles() | |
| } | |
| package_json = affected.get('package.json') | |
| inputs = manage_node_deps.archive_inputs_changed( | |
| affected.keys(), | |
| '\n'.join(package_json.OldContents()) if package_json else None, | |
| '\n'.join(package_json.NewContents()) if package_json else None) | |
| results = [] | |
| archive_changed = False | |
| deps = affected.get('DEPS') | |
| if deps: | |
| old_deps = '\n'.join(deps.OldContents()) | |
| new_deps = '\n'.join(deps.NewContents()) | |
| results.extend( | |
| output_api.PresubmitError(problem) | |
| for problem in manage_node_deps.check_deps_entry(new_deps)) | |
| archive_changed = manage_node_deps.deps_entry_changed( | |
| old_deps, new_deps) | |
| if input_api.change.GitFootersFromDescription().get( | |
| _SKIP_NODE_MODULES_CHECK_FOOTER): | |
| return results | |
| if inputs and not archive_changed: | |
| results.append( | |
| report('%s changed but the node_modules archive in DEPS did ' | |
| 'not. Run `./dt install-deps --upload`, or add a ' | |
| '"%s: <reason>" footer if no new archive is needed.' % | |
| (', '.join(inputs), _SKIP_NODE_MODULES_CHECK_FOOTER))) | |
| elif (archive_changed and not inputs | |
| and 'scripts/deps/manage_node_deps.py' not in affected): | |
| results.append( | |
| output_api.PresubmitPromptWarning( | |
| 'The node_modules archive in DEPS changed, but none of ' | |
| 'package.json, package-lock.json, npm_exclude.txt or the ' | |
| 'patches did. Was this intentional?')) | |
| return results | |
| def CheckNoUncheckedFiles(input_api, output_api): | |
| if _IsEnvCog(input_api): | |
| return [] | |
| process = input_api.subprocess.Popen(['git', 'diff', '--exit-code'], | |
| stdout=input_api.subprocess.PIPE, | |
| stderr=input_api.subprocess.STDOUT) | |
| out, _ = process.communicate() | |
| if process.returncode != 0: | |
| files_changed_process = input_api.subprocess.Popen( | |
| ['git', 'diff'], | |
| stdout=input_api.subprocess.PIPE, | |
| stderr=input_api.subprocess.STDOUT) | |
| files_changed, _ = files_changed_process.communicate() | |
| return [ | |
| output_api.PresubmitError( | |
| 'You have changed files that need to be committed:\n%s' % | |
| (files_changed.decode('utf-8').strip())) | |
| ] | |
| return [] | |
| def CheckKnownContextValues(input_api, output_api): | |
| """Ensure all additions to `KnownContextValues.ts` following the naming convention. | |
| This check ensures that all new cases added to the enum in `KnownContextValues.ts` | |
| follow the extended Kebab Case naming convention. Specifically it doesn't look at | |
| unchanged lines, because there are various existing values that cannot be changed | |
| (easily). | |
| """ | |
| # This regexp matches the one we use in `StringUtilities.isExtendedKebabCase()`. | |
| kebab_case_re = re.compile( | |
| r"^-?([a-z0-9]+(?:-[a-z0-9]+)*\.)*[a-z0-9]+(?:-[a-z0-9]+)*$") | |
| local_path = input_api.os_path.join('front_end', 'ui', 'visual_logging', | |
| 'KnownContextValues.ts') | |
| invalid_contexts = [] | |
| for f in filter( | |
| lambda x: (x.LocalPath() == local_path and x.Action() == 'M'), | |
| input_api.AffectedFiles()): | |
| # Loop only through the changed lines of the affected file. | |
| for _, line in f.ChangedContents(): | |
| match = re.search(r"\s+'(.+)',", line) | |
| if match: | |
| context = match.group(1) | |
| if not kebab_case_re.match(context): | |
| invalid_contexts.append(context) | |
| continue | |
| if not invalid_contexts: | |
| return [] | |
| return [ | |
| output_api.PresubmitError( | |
| message=f"Invalid jslog context(s): {', '.join(invalid_contexts)}", | |
| long_text= | |
| ("""The jslog contexts must follow the extended Kebab Case naming convention, where | |
| words are separated with either a dash (`-`) or a dot (`.`), and all characters | |
| must be lower-case alphanumeric. | |
| """), | |
| locations=[ | |
| output_api.PresubmitResultLocation(file_path=local_path) | |
| ], | |
| ) | |
| ] | |
| def CheckVisualLoggingNoContextValueRemoval(input_api, output_api): | |
| """Ensure no values are removed from `KnownContextValues.ts`. | |
| Visual logging context values must never be removed as it breaks log analysis | |
| for historical data. | |
| """ | |
| local_path = input_api.os_path.join('front_end', 'ui', 'visual_logging', | |
| 'KnownContextValues.ts') | |
| removed_values = [] | |
| for f in filter( | |
| lambda x: (x.LocalPath() == local_path and x.Action() == 'M'), | |
| input_api.AffectedFiles()): | |
| diff = f.GenerateScmDiff() | |
| for line in diff.splitlines(): | |
| if line.startswith('-'): | |
| # Line without the leading '-' | |
| line_content = line[1:] | |
| # Match values: optional whitespace, quote, value, quote, optional comma | |
| match = input_api.re.search(r"^\s*['\"]([^'\"]+)['\"],?", | |
| line_content) | |
| if match: | |
| removed_values.append(match.group(1)) | |
| if not removed_values: | |
| return [] | |
| return [ | |
| output_api.PresubmitError( | |
| message= | |
| f"Removed visual logging context(s): {', '.join(removed_values)}", | |
| long_text= | |
| ("Removing visual logging context values breaks log analysis for historical data.\n" | |
| "Do NOT remove values. New values should be added to the end of the list.\n" | |
| "See crbug.com/504758084 for details.\n"), | |
| locations=[ | |
| output_api.PresubmitResultLocation(file_path=local_path) | |
| ], | |
| ) | |
| ] | |
| _EXEMPT_THIRD_PARTY_LICENSE_PACKAGES = { | |
| 'vscode.web-custom-data', | |
| } | |
| def CheckThirdPartyAndSvgAssets(input_api, output_api): | |
| """Verify front_end/third_party/ package metadata and front_end/Images/src/ SVG icon hygiene.""" | |
| devtools_root = input_api.PresubmitLocalPath() | |
| third_party_prefix = input_api.os_path.join( | |
| 'front_end', 'third_party') + input_api.os_path.sep | |
| images_src_prefix = input_api.os_path.join('front_end', 'Images', | |
| 'src') + input_api.os_path.sep | |
| checked_packages = set() | |
| errors = [] | |
| for f in input_api.AffectedFiles(): | |
| # Check both added and modified files (but not deleted files). | |
| if f.Action() not in ('A', 'M'): | |
| continue | |
| local_path = f.LocalPath() | |
| if local_path.startswith(third_party_prefix): | |
| rel_path = local_path[len(third_party_prefix):] | |
| parts = rel_path.split(input_api.os_path.sep) | |
| if len(parts) < 2: | |
| continue | |
| pkg = parts[0] | |
| if pkg in checked_packages: | |
| continue | |
| checked_packages.add(pkg) | |
| pkg_dir = input_api.os_path.join(devtools_root, 'front_end', | |
| 'third_party', pkg) | |
| if not input_api.os_path.isdir(pkg_dir): | |
| continue | |
| readme_path = input_api.os_path.join(pkg_dir, 'README.chromium') | |
| if not input_api.os_path.isfile(readme_path): | |
| errors.append( | |
| f'front_end/third_party/{pkg}/README.chromium is missing.') | |
| else: | |
| readme_text = Path(readme_path).read_text(encoding='utf-8') | |
| if 'Update Mechanism:' not in readme_text: | |
| errors.append( | |
| f'front_end/third_party/{pkg}/README.chromium must include an "Update Mechanism:" field.' | |
| ) | |
| if pkg not in _EXEMPT_THIRD_PARTY_LICENSE_PACKAGES: | |
| license_path = input_api.os_path.join(pkg_dir, 'LICENSE') | |
| pkg_license_path = input_api.os_path.join( | |
| pkg_dir, 'package', 'LICENSE') | |
| license_md_path = input_api.os_path.join(pkg_dir, 'LICENSE.md') | |
| has_license = input_api.os_path.isfile( | |
| license_path) or input_api.os_path.isfile(pkg_license_path) | |
| if input_api.os_path.isfile(license_md_path): | |
| errors.append( | |
| f'front_end/third_party/{pkg}/ uses LICENSE.md; name the license file "LICENSE" instead.' | |
| ) | |
| elif not has_license: | |
| errors.append( | |
| f'front_end/third_party/{pkg}/ is missing a LICENSE file.' | |
| ) | |
| elif local_path.startswith(images_src_prefix) and local_path.endswith( | |
| '.svg'): | |
| svg_text = '\n'.join(f.NewContents()) | |
| svg_tag_match = input_api.re.search(r'<svg\b[^>]*>', svg_text) | |
| if not svg_tag_match: | |
| continue | |
| svg_tag = svg_tag_match.group(0) | |
| def _CanonicalizeFill(fill): | |
| fill = fill.strip().lower() | |
| return '#000' if fill in ('#000', '#000000', 'black') else fill | |
| fills = [ | |
| _CanonicalizeFill(fill) for fill in input_api.re.findall( | |
| r'''\bfill=["']([^"']+)["']''', svg_text) | |
| if fill.strip().lower() != 'none' | |
| ] | |
| has_gradient_or_defs = bool( | |
| input_api.re.search( | |
| r'<(linearGradient|radialGradient|style)\b|url\(#', | |
| svg_text)) | |
| distinct_fills = set(fills) | |
| width_match = input_api.re.search( | |
| r'''\bwidth=["'](\d+)(?:px)?["']''', svg_tag) | |
| height_match = input_api.re.search( | |
| r'''\bheight=["'](\d+)(?:px)?["']''', svg_tag) | |
| viewbox_match = input_api.re.search( | |
| r'''\bviewBox=["']([^"']+)["']''', svg_tag) | |
| # Skip multi-color illustrations, gradient illustrations, or large non-icon SVGs. | |
| if has_gradient_or_defs or len(distinct_fills) > 1: | |
| continue | |
| if width_match and height_match: | |
| w = int(width_match.group(1)) | |
| h = int(height_match.group(1)) | |
| if w != h or w > 24: | |
| continue | |
| if not viewbox_match: | |
| errors.append( | |
| f'{local_path}: SVG icon is missing a viewBox attribute (expected viewBox="0 0 20 20").' | |
| ) | |
| else: | |
| normalized_viewbox = ' '.join( | |
| input_api.re.split(r'[\s,]+', | |
| viewbox_match.group(1).strip())) | |
| if normalized_viewbox not in ('0 0 20 20', '0 0 16 16', | |
| '0 0 24 24', '0 -960 960 960'): | |
| errors.append( | |
| f'{local_path}: SVG icon has viewBox="{viewbox_match.group(1)}" (expected "0 0 20 20").' | |
| ) | |
| invalid_fills = [fill for fill in distinct_fills if fill != '#000'] | |
| if invalid_fills: | |
| errors.append( | |
| f'{local_path}: Monochromatic SVG icon uses non-black fill {invalid_fills[0]} (expected #000 or black).' | |
| ) | |
| if not errors: | |
| return [] | |
| return [ | |
| output_api.PresubmitError( | |
| 'Third-party package or SVG asset hygiene check failed:', | |
| long_text='\n'.join(errors) + '\n', | |
| ) | |
| ] | |
| # Canned check wrappers below. | |
| def _TextFilesOnlyFilter(file): | |
| """Filter that yields only text files. | |
| Filters files based on prefixes and extensions that should not be treated as | |
| text files for the canned checks below. | |
| """ | |
| excluded_prefixes = [ | |
| 'node_modules', | |
| 'third_party', | |
| 'front_end/third_party', | |
| 'extensions/cxx_debugging/third_party', | |
| ] | |
| excluded_extensions = [ | |
| '.png', '.webm', '.svg', '.avif', '.rawresponse', '.gz' | |
| ] | |
| if any(file.LocalPath().startswith(prefix) | |
| for prefix in excluded_prefixes): | |
| return False | |
| if any(file.LocalPath().endswith(ext) for ext in excluded_extensions): | |
| return False | |
| return True | |
| def CheckChangeHasNoCrAndHasOnlyOneEol(input_api, output_api): | |
| return input_api.canned_checks.CheckChangeHasNoCrAndHasOnlyOneEol( | |
| input_api, output_api, source_file_filter=_TextFilesOnlyFilter) | |
| def CheckGenderNeutral(input_api, output_api): | |
| return input_api.canned_checks.CheckGenderNeutral(input_api, output_api) | |
| def CheckAuthorizedAuthor(input_api, output_api): | |
| return input_api.canned_checks.CheckAuthorizedAuthor( | |
| input_api, | |
| output_api, | |
| bot_allowlist=[ | |
| 'chrome-cherry-picker@chops-service-accounts.iam.gserviceaccount.com', | |
| 'devtools-ci-autoroll-builder@chops-service-accounts.iam.gserviceaccount.com', | |
| ]) | |
| def CheckPanProjectChecksOnCommit(input_api, output_api): | |
| return input_api.canned_checks.PanProjectChecks( | |
| input_api, output_api, excluded_paths=_EXCLUDED_PATHS, maxlen=120) | |
| def CheckAyeAye(input_api, output_api): | |
| return input_api.canned_checks.CheckAyeAye(input_api, output_api) | |
| def CheckDirMetadataFormat(input_api, output_api): | |
| return input_api.RunTests( | |
| input_api.canned_checks.CheckDirMetadataFormat(input_api, output_api)) |