File: PRESUBMIT.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 (201 lines) | stat: -rw-r--r-- 6,799 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
# Copyright 2012 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Presubmit script for changes affecting tools/perf/.

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

import os

PRESUBMIT_VERSION = '2.0.0'


def _CommonChecks(input_api, output_api, block_on_failure=False):
  """Performs common checks that vary between commit and upload.

    block_on_failure: For some failures, we would like to warn the
        user but still allow them to upload the change. However, we
        don't want them to commit code with those failures, so we
        need to block the change on commit.
  """
  results = []

  results.extend(
      _CheckPerfDataCurrentness(input_api, output_api, block_on_failure))
  results.extend(
      _CheckPerfJsonConfigs(input_api, output_api, block_on_failure))
  results.extend(_CheckShardMaps(input_api, output_api, block_on_failure))
  return results


def CheckPyLint(input_api, output_api):
  return input_api.RunTests(
      input_api.canned_checks.GetPylint(
          input_api,
          output_api,
          extra_paths_list=_GetPathsToPrepend(input_api),
          pylintrc='pylintrc',
          version='2.7'))


def _GetPathsToPrepend(input_api):
  perf_dir = input_api.PresubmitLocalPath()
  chromium_src_dir = input_api.os_path.join(perf_dir, '..', '..')
  telemetry_dir = input_api.os_path.join(
      chromium_src_dir, 'third_party', 'catapult', 'telemetry')
  typ_dir = input_api.os_path.join(
       chromium_src_dir, 'third_party', 'catapult', 'third_party', 'typ')
  experimental_dir = input_api.os_path.join(
      chromium_src_dir, 'third_party', 'catapult', 'experimental')
  tracing_dir = input_api.os_path.join(
      chromium_src_dir, 'third_party', 'catapult', 'tracing')
  py_utils_dir = input_api.os_path.join(
      chromium_src_dir, 'third_party', 'catapult', 'common', 'py_utils')
  android_pylib_dir = input_api.os_path.join(
      chromium_src_dir, 'build', 'android')
  testing_dir = input_api.os_path.join(chromium_src_dir, 'testing')
  return [
      telemetry_dir,
      typ_dir,
      experimental_dir,
      tracing_dir,
      py_utils_dir,
      android_pylib_dir,
      testing_dir,
  ]


def _RunArgs(args, input_api):
  p = input_api.subprocess.Popen(args, stdout=input_api.subprocess.PIPE,
                                 stderr=input_api.subprocess.STDOUT)
  out, _ = p.communicate()
  return (out, p.returncode)

def _RunValidationScript(
    input_api,
    output_api,
    script_path,
    extra_args = None,
    block_on_failure = None):
  results = []
  vpython = 'vpython3.bat' if input_api.is_windows else 'vpython3'
  perf_dir = input_api.PresubmitLocalPath()
  script_abs_path = input_api.os_path.join(perf_dir, script_path)
  extra_args = extra_args if extra_args else []
  # When running git cl presubmit --all this presubmit may be asked to check
  # ~500 files, leading to a command line that is over 43,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. The
  # non-Windows limit is chosen so that the code that splits up commands will
  # get some exercise on other platforms.
  # 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.
  # I suspect that the latter error comes from CreateProcess hitting its 32768
  # character limit.
  files_per_command = 50 if input_api.is_windows else 1000
  # Handle the case where extra_args is empty.
  for i in range(0, len(extra_args) if extra_args else 1, files_per_command):
    args = [vpython, script_abs_path] + extra_args[i:i + files_per_command]
    out, return_code = _RunArgs(args, input_api)
    if return_code:
      error_msg = 'Script ' + script_path + ' failed.'
      if block_on_failure is None or block_on_failure:
        results.append(output_api.PresubmitError(error_msg, long_text=out))
      else:
        results.append(
            output_api.PresubmitPromptWarning(error_msg, long_text=out))
  return results


def CheckExpectations(input_api, output_api):
  return _RunValidationScript(
      input_api,
      output_api,
      'validate_story_expectation_data',
  )

def _CheckPerfDataCurrentness(input_api, output_api, block_on_failure):
  return _RunValidationScript(
      input_api,
      output_api,
      'generate_perf_data',
      ['--validate-only'],
      block_on_failure
  )

def _CheckPerfJsonConfigs(input_api, output_api, block_on_failure):
  return _RunValidationScript(
      input_api,
      output_api,
      'validate_perf_json_config',
      ['--validate-only'],
      block_on_failure
  )


def CheckWprShaFiles(input_api, output_api):
  """Check whether the wpr sha files have matching URLs."""
  wpr_archive_shas = []
  for affected_file in input_api.AffectedFiles(include_deletes=False):
    filename = affected_file.AbsoluteLocalPath()
    if not filename.endswith('.sha1'):
      continue
    wpr_archive_shas.append(filename)
  return _RunValidationScript(
      input_api,
      output_api,
      'validate_wpr_archives',
      wpr_archive_shas
  )

def _CheckShardMaps(input_api, output_api, block_on_failure):
  return _RunValidationScript(
      input_api,
      output_api,
      'generate_perf_sharding.py',
      ['validate'],
      block_on_failure
  )


def CheckJson(input_api, output_api):
  """Checks whether JSON files in this change can be parsed."""
  for affected_file in input_api.AffectedFiles(include_deletes=False):
    filename = affected_file.AbsoluteLocalPath()
    if os.path.splitext(filename)[1] != '.json':
      continue
    if (os.path.basename(filename) == 'perf_results.json' and
        os.path.basename(os.path.dirname(filename)) == 'speedometer2-future'):
      # Intentionally invalid JSON file.
      continue
    try:
      input_api.json.load(open(filename, encoding='utf-8'))
    except ValueError:
      return [output_api.PresubmitError('Error parsing JSON in %s!' % filename)]
  return []


def CheckVersionsInSmokeTests(input_api, output_api):
  return _RunValidationScript(
      input_api,
      output_api,
      input_api.os_path.join(
          'benchmarks', 'system_health_load_tests_smoke_test.py'),
  )

def CheckChangeOnUpload(input_api, output_api):
  report = []
  report.extend(_CommonChecks(input_api, output_api))
  return report


def CheckChangeOnCommit(input_api, output_api):
  report = []
  report.extend(_CommonChecks(input_api, output_api, block_on_failure=True))
  return report