File: resource_checker.py

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, 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 (74 lines) | stat: -rw-r--r-- 2,999 bytes parent folder | download | duplicates (2)
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
# 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.

"""
Presubmit for Chromium HTML/CSS/JS resources. See chrome/browser/PRESUBMIT.py.
"""

from . import regex_check


class ResourceChecker(object):
  def __init__(self, input_api, output_api, file_filter=None):
    self.input_api = input_api
    self.output_api = output_api
    self.file_filter = file_filter

  def DeprecatedMojoBindingsCheck(self, line_number, line):
    return regex_check.RegexCheck(self.input_api.re, line_number, line,
        r'(mojo_bindings\.js)', 'Please use mojo_bindings_lite.js in new code')

  def DisallowIncludeCheck(self, msg, line_number, line):
    return regex_check.RegexCheck(self.input_api.re, line_number, line,
        r'^\s*(?:\/[\*\/])?\s*(<include)\s*src=', msg)

  # This is intentionally not included in RunChecks(). It's an optional check
  # that can be used from a PRESUBMIT.py in a directory that does not wish to
  # use <include> (i.e. uses a different bundling mechanism, does not grit
  # process, etc.).
  def DisallowIncludes(self, msg):
    check = lambda *args: self.DisallowIncludeCheck(msg, *args)
    return self._RunCheckOnAffectedFiles(check, 'Found resource errors in %s',
                                         is_error=True)

  def SelfClosingIncludeCheck(self, line_number, line):
    return regex_check.RegexCheck(self.input_api.re, line_number, line,
        '(</include>|<include.*/>)', 'Closing <include> tags is unnecessary.')

  def RunChecks(self):
    msg = 'Found resources style issues in %s'
    # TODO(crbug.com/40613816): is_error for Mojo check when -lite is majority?
    return self._RunCheckOnAffectedFiles(self.DeprecatedMojoBindingsCheck,
        msg, only_changed_lines=True) + \
        self._RunCheckOnAffectedFiles(self.SelfClosingIncludeCheck, msg)

  def _RunCheckOnAffectedFiles(self, check, msg_template, is_error=False,
                               only_changed_lines=False):
    """Check for violations of the Chromium web development style guide. See
       https://chromium.googlesource.com/chromium/src/+/main/styleguide/web/web.md
    """
    results = []

    affected_files= self.input_api.AffectedFiles(file_filter=self.file_filter,
                                                 include_deletes=False)
    for f in affected_files:
      errors = []
      if only_changed_lines:
        contents = f.ChangedContents()
      else:
        contents = enumerate(f.NewContents(), start=1)
      for line_number, line in contents:
        error = check(line_number, line)
        if error:
          errors.append(error)

      if errors:
        abs_local_path = f.AbsoluteLocalPath()
        msg = msg_template % abs_local_path + '\n\n' + '\n'.join(errors) + '\n'
        if is_error:
          results.append(self.output_api.PresubmitError(msg))
        else:
          results.append(self.output_api.PresubmitPromptWarning(msg))

    return results