File: PRESUBMIT.py

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (294 lines) | stat: -rw-r--r-- 12,967 bytes parent folder | download | duplicates (4)
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
# Copyright 2013 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Top-level presubmit script for Blink.

See https://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
for more details about the presubmit API built into gcl.
"""

import importlib
import inspect
import os
import re

try:
    # pylint: disable=C0103
    module_name = 'audit_non_blink_usage'
    module_path = os.path.join(
        os.path.dirname(inspect.stack()[0][1]),
        'tools/blinkpy/presubmit/audit_non_blink_usage.py')
    audit_non_blink_usage = importlib.machinery.SourceFileLoader(
        module_name, module_path).load_module()
except IOError:
    # One of the presubmit upload tests tries to exec this script, which
    # doesn't interact so well with the import hack... just ignore the
    # exception here and hope for the best.
    pass

_EXCLUDED_PATHS = (
    # These are third-party dependencies that we don't directly control.
    r'^third_party[\\/]blink[\\/]tools[\\/]blinkpy[\\/]third_party[\\/]wpt[\\/]wpt[\\/].*',
    r'^third_party[\\/]blink[\\/]web_tests[\\/]external[\\/]wpt[\\/]resources[\\/]webidl2[\\/].*',
)


def _CheckForWrongMojomIncludes(input_api, output_api):
    # In blink the code should either use -blink.h or -shared.h mojom
    # headers, except in public where only -shared.h headers should be
    # used to avoid exporting Blink types outside Blink.
    def source_file_filter(path):
        return input_api.FilterSourceFile(
            path,
            files_to_skip=[
                r'.*_test.*\.(cc|h)$',
                r'.*_unittest.*\.(cc|h)$',
                r'third_party[\\/]blink[\\/]common[\\/]',
                r'third_party[\\/]blink[\\/]public[\\/]common[\\/]',
                r'third_party[\\/]blink[\\/]renderer[\\/]platform[\\/]loader[\\/]fetch[\\/]url_loader[\\/]',
                r'third_party[\\/]blink[\\/]renderer[\\/]core[\\/]frame[\\/]web_view_impl.*\.(cc|h)$',
                r'third_party[\\/]blink[\\/]renderer[\\/]core[\\/]frame[\\/]web.*frame.*\.(cc|h)$',
            ])

    pattern = input_api.re.compile(r'#include\s+[<"](.+)\.mojom(.*)\.h[>"]')
    public_folder = input_api.os_path.normpath('third_party/blink/public/')
    non_blink_mojom_errors = []
    public_blink_mojom_errors = []

    # Allow including specific non-blink interfaces that are used in the
    # public C++ API. Adding to these allowed interfaces should meet the
    # following conditions:
    # - Its pros/cons is discussed and have consensus on
    #   platform-architecture-dev@ or
    # - It uses POD types that will not import STL (or base string) types into
    #   blink (such as no strings or vectors).
    #
    # So far, non-blink interfaces are allowed only for loading / loader and
    # media interfaces so that we don't need type conversions to get through
    # the boundary between Blink and non-Blink.
    allowed_interfaces = (
        'services/network/public/mojom/cross_origin_embedder_policy',
        'services/network/public/mojom/document_isolation_policy',
        'services/network/public/mojom/early_hints',
        'services/network/public/mojom/fetch_api',
        'services/network/public/mojom/load_timing_info',
        'services/network/public/mojom/url_loader',
        'services/network/public/mojom/url_loader_factory',
        'services/network/public/mojom/url_response_head',
        'third_party/blink/public/mojom/blob/blob',
        'third_party/blink/public/mojom/blob/serialized_blob',
        'third_party/blink/public/mojom/browser_interface_broker',
        'third_party/blink/public/mojom/fetch/fetch_api_request',
        'third_party/blink/public/mojom/loader/code_cache',
        'third_party/blink/public/mojom/loader/fetch_later',
        'third_party/blink/public/mojom/loader/local_resource_loader_config',
        'third_party/blink/public/mojom/loader/resource_load_info',
        'third_party/blink/public/mojom/loader/resource_load_info_notifier',
        'third_party/blink/public/mojom/loader/transferrable_url_loader',
        'third_party/blink/public/mojom/navigation/renderer_content_settings',
        'third_party/blink/public/mojom/page/prerender_page_param',
        'third_party/blink/public/mojom/partitioned_popins/partitioned_popin_params',
        'third_party/blink/public/mojom/worker/subresource_loader_updater',
        'third_party/blink/public/mojom/worker/worklet_global_scope_creation_params',
        'media/mojo/mojom/interface_factory', 'media/mojo/mojom/audio_decoder',
        'media/mojo/mojom/audio_encoder', 'media/mojo/mojom/video_decoder',
        'media/mojo/mojom/media_metrics_provider')

    for f in input_api.AffectedFiles(file_filter=source_file_filter):
        for line_num, line in f.ChangedContents():
            error_list = None
            match = pattern.match(line)
            if (match and match.group(1) not in allowed_interfaces):
                if match.group(2) not in ('-shared', '-forward'):
                    if f.LocalPath().startswith(public_folder):
                        error_list = public_blink_mojom_errors
                    elif match.group(2) not in ('-blink', '-blink-forward',
                                                '-blink-test-utils'):
                        # Neither -shared.h, -blink.h, -blink-forward.h nor
                        # -blink-test-utils.h.
                        error_list = non_blink_mojom_errors

            if error_list is not None:
                error_list.append('    %s:%d %s' %
                                  (f.LocalPath(), line_num, line))

    results = []
    if non_blink_mojom_errors:
        results.append(
            output_api.PresubmitError(
                'Files that include non-Blink variant mojoms found. '
                'You must include .mojom-blink.h, .mojom-forward.h or '
                '.mojom-shared.h instead:', non_blink_mojom_errors))

    if public_blink_mojom_errors:
        results.append(
            output_api.PresubmitError(
                'Public blink headers using Blink variant mojoms found. '
                'You must include .mojom-forward.h or .mojom-shared.h '
                'instead:', public_blink_mojom_errors))

    return results


def _CommonChecks(input_api, output_api):
    """Checks common to both upload and commit."""
    # We should figure out what license checks we actually want to use.
    license_header = r'.*'

    results = []
    results.extend(
        input_api.canned_checks.PanProjectChecks(
            input_api,
            output_api,
            excluded_paths=_EXCLUDED_PATHS,
            owners_check=False,
            maxlen=800,
            license_header=license_header,
            global_checks=False))
    results.extend(_CheckForWrongMojomIncludes(input_api, output_api))
    return results


def FilterPaths(input_api):
    """Returns input files with certain paths removed."""
    files = []
    for f in input_api.AffectedFiles():
        file_path = f.AbsoluteLocalPath()
        # Filter out changes in web_tests/ so they are not linted. Some files
        # are intentionally malformed for testing. Also, external WPTs may have
        # non-Chromium authors.
        if 'web_tests' + input_api.os_path.sep in file_path:
            continue
        if input_api.os_path.sep + 'PRESUBMIT' in file_path:
            continue
        # Skip files that were generated by bison.
        if re.search(
                'third_party.blink.renderer.'
                'core.xml.xpath_grammar_generated\.(cc|h)$', file_path):
            continue
        files.append(file_path)
    return files


def _CheckStyle(input_api, output_api):
    files = FilterPaths(input_api)
    # Do not call check_blink_style.py with empty affected file list if all
    # input_api.AffectedFiles got filtered.
    if not files:
        return []

    style_checker_path = input_api.os_path.join(input_api.PresubmitLocalPath(),
                                                'tools',
                                                'check_blink_style.py')
    # When running git cl presubmit --all this presubmit may be asked to check
    # ~260 files, leading to a command line that is about 17,000 characters.
    # This goes past the Windows 8191 character cmd.exe limit and causes cryptic
    # failures. To avoid these we break the command up into smaller pieces.
    # Depending on how long the command is on Windows the error may be:
    #     The command line is too long.
    # Or it may be:
    #     OSError: Execution failed with error: [WinError 206] The filename or
    #     extension is too long.
    # The latter error comes from CreateProcess hitting its 32768 character
    # limit.
    files_per_command = 40 if input_api.is_windows else 1000
    results = []
    for i in range(0, len(files), files_per_command):
        args = [
            input_api.python3_executable, style_checker_path, '--diff-files'
        ]
        args += files[i:i + files_per_command]

        try:
            child = input_api.subprocess.Popen(
                args, stderr=input_api.subprocess.PIPE)
            _, stderrdata = child.communicate()
            if child.returncode != 0:
                results.append(
                    output_api.PresubmitError('check_blink_style.py failed',
                                              [stderrdata.decode('utf-8')]))
        except Exception as e:
            results.append(
                output_api.PresubmitNotifyResult(
                    'Could not run check_blink_style.py', [str(e)]))

    # By default, the pylint canned check lints all Python files together to
    # check for potential problems between dependencies. This is slow to run
    # across all of Blink (>2 min), so only lint affected files.
    affected_python_files = [
        input_api.os_path.relpath(file_path, input_api.PresubmitLocalPath())
        for file_path in files if input_api.fnmatch.fnmatch(file_path, '*.py')
    ]
    if affected_python_files:
        pylintrc = input_api.os_path.join('tools', 'blinkpy', 'pylintrc')
        results.extend(
            input_api.RunTests(
                input_api.canned_checks.GetPylint(
                    input_api,
                    output_api,
                    files_to_check=[
                        re.escape(path) for path in affected_python_files
                    ],
                    pylintrc=pylintrc)))
    return results


def _CheckForPrintfDebugging(input_api, output_api):
    """Generally speaking, we'd prefer not to land patches that printf
    debug output.
    """
    printf_re = input_api.re.compile(r'^\s*(printf\(|fprintf\(stderr,)')
    errors = input_api.canned_checks._FindNewViolationsOfRule(
        lambda _, x: not printf_re.search(x), input_api, None)
    errors = ['  * %s' % violation for violation in errors]
    if errors:
        return [
            output_api.PresubmitPromptOrNotify(
                'printf debugging is best debugging! That said, it might '
                'be a good idea to drop the following occurences from '
                'your patch before uploading:\n%s' % '\n'.join(errors))
        ]
    return []


def _CheckForForbiddenChromiumCode(input_api, output_api):
    """Checks that Blink uses Chromium classes and namespaces only in
    permitted code.
    """
    # TODO(dcheng): This is pretty similar to _FindNewViolationsOfRule.
    # Unfortunately, that can't be used directly because it doesn't give the
    # callable enough information (namely the path to the file), so we
    # duplicate the logic here...
    results = []
    for f in input_api.AffectedFiles():
        path = f.LocalPath()
        errors = audit_non_blink_usage.check(
            path, [(i + 1, l) for i, l in enumerate(f.NewContents())])
        if errors:
            errors = audit_non_blink_usage.check(path, f.ChangedContents())
            if errors:
                for error in errors:
                    msg = '%s:%d uses disallowed identifier %s' % (
                        path, error.line, error.identifier)
                    if error.advice:
                        msg += ". Advice: %s" % "\n".join(error.advice)
                    if error.warning:
                        results.append(output_api.PresubmitPromptWarning(msg))
                    else:
                        results.append(output_api.PresubmitError(msg))
    return results


def CheckChangeOnUpload(input_api, output_api):
    results = []
    results.extend(_CommonChecks(input_api, output_api))
    results.extend(_CheckStyle(input_api, output_api))
    results.extend(_CheckForPrintfDebugging(input_api, output_api))
    results.extend(_CheckForForbiddenChromiumCode(input_api, output_api))
    return results


def CheckChangeOnCommit(input_api, output_api):
    results = []
    results.extend(_CommonChecks(input_api, output_api))
    return results