xref: /aosp_15_r20/external/webrtc/infra/specs/PRESUBMIT.py (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1#!/usr/bin/env vpython3
2
3# Copyright (c) 2022 The WebRTC project authors. All Rights Reserved.
4#
5# Use of this source code is governed by a BSD-style license
6# that can be found in the LICENSE file in the root of the source
7# tree. An additional intellectual property rights grant can be found
8# in the file PATENTS.  All contributing project authors may
9# be found in the AUTHORS file in the root of the source tree.
10
11import os
12
13# Runs PRESUBMIT.py in py3 mode by git cl presubmit.
14USE_PYTHON3 = True
15
16
17def _HasLocalChanges(input_api):
18  ret = input_api.subprocess.call(['git', 'diff', '--quiet'])
19  return ret != 0
20
21
22def CheckPatchFormatted(input_api, output_api):
23  results = []
24  file_filter = lambda x: x.LocalPath().endswith('.pyl')
25  affected_files = input_api.AffectedFiles(include_deletes=False,
26                                           file_filter=file_filter)
27
28  for f in affected_files:
29    cmd = ['yapf', '-i', f.AbsoluteLocalPath()]
30    if input_api.subprocess.call(cmd):
31      results.append(output_api.PresubmitError('Error calling "' + cmd + '"'))
32
33  if _HasLocalChanges(input_api):
34    msg = ('Diff found after running "yapf -i" on modified .pyl files.\n'
35           'Please commit or discard the new changes.')
36    results.append(output_api.PresubmitError(msg))
37
38  return results
39
40
41def CheckSourceSideSpecs(input_api, output_api):
42  d = os.path.dirname
43  webrtc_root = d(d(input_api.PresubmitLocalPath()))
44  gen_script = os.path.join(webrtc_root, 'testing', 'buildbot',
45                            'generate_buildbot_json.py')
46
47  commands = [
48      input_api.Command(name='generate_buildbot_json',
49                        cmd=[
50                            input_api.python3_executable, gen_script, '--check',
51                            '--verbose', '--pyl-files-dir',
52                            input_api.PresubmitLocalPath()
53                        ],
54                        kwargs={},
55                        message=output_api.PresubmitError),
56  ]
57  return input_api.RunTests(commands)
58
59
60def CheckChangeOnUpload(input_api, output_api):
61  results = []
62  results.extend(CheckPatchFormatted(input_api, output_api))
63  results.extend(CheckSourceSideSpecs(input_api, output_api))
64  return results
65
66
67def CheckChangeOnCommit(input_api, output_api):
68  results = []
69  results.extend(CheckPatchFormatted(input_api, output_api))
70  results.extend(CheckSourceSideSpecs(input_api, output_api))
71  return results
72