blob: f064cacaf8a0215b23e97dbcddc1814873ab69bb [file] [log] [blame]
Jeremy Leconte1ab4e872022-08-29 21:14:15 +02001#!/usr/bin/env vpython3
2
Jeremy Leconte0c4059c2022-04-04 07:58:40 +02003# 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
Mirko Bonadei5c8993c2022-04-04 18:43:54 +000011import os
Byoungchan Lee7970f872022-12-06 21:34:35 +090012import shlex
Mirko Bonadei5c8993c2022-04-04 18:43:54 +000013
Jeremy Leconte1ab4e872022-08-29 21:14:15 +020014# Runs PRESUBMIT.py in py3 mode by git cl presubmit.
15USE_PYTHON3 = True
16
Jeremy Leconte0c4059c2022-04-04 07:58:40 +020017
18def _HasLocalChanges(input_api):
19 ret = input_api.subprocess.call(['git', 'diff', '--quiet'])
20 return ret != 0
21
22
23def CheckPatchFormatted(input_api, output_api):
24 results = []
25 file_filter = lambda x: x.LocalPath().endswith('.pyl')
26 affected_files = input_api.AffectedFiles(include_deletes=False,
27 file_filter=file_filter)
28
29 for f in affected_files:
30 cmd = ['yapf', '-i', f.AbsoluteLocalPath()]
31 if input_api.subprocess.call(cmd):
Byoungchan Lee7970f872022-12-06 21:34:35 +090032 results.append(
33 output_api.PresubmitError('Error calling "' + shlex.join(cmd) + '"'))
Jeremy Leconte0c4059c2022-04-04 07:58:40 +020034
35 if _HasLocalChanges(input_api):
36 msg = ('Diff found after running "yapf -i" on modified .pyl files.\n'
37 'Please commit or discard the new changes.')
38 results.append(output_api.PresubmitError(msg))
39
40 return results
41
42
Mirko Bonadei5c8993c2022-04-04 18:43:54 +000043def CheckSourceSideSpecs(input_api, output_api):
44 d = os.path.dirname
Jeremy Leconte1ab4e872022-08-29 21:14:15 +020045 webrtc_root = d(d(input_api.PresubmitLocalPath()))
46 gen_script = os.path.join(webrtc_root, 'testing', 'buildbot',
Mirko Bonadei5c8993c2022-04-04 18:43:54 +000047 'generate_buildbot_json.py')
48
49 commands = [
50 input_api.Command(name='generate_buildbot_json',
51 cmd=[
Jeremy Leconte1ab4e872022-08-29 21:14:15 +020052 input_api.python3_executable, gen_script, '--check',
Mirko Bonadei5c8993c2022-04-04 18:43:54 +000053 '--verbose', '--pyl-files-dir',
54 input_api.PresubmitLocalPath()
55 ],
56 kwargs={},
57 message=output_api.PresubmitError),
58 ]
59 return input_api.RunTests(commands)
60
61
Jeremy Leconte0c4059c2022-04-04 07:58:40 +020062def CheckChangeOnUpload(input_api, output_api):
63 results = []
64 results.extend(CheckPatchFormatted(input_api, output_api))
Mirko Bonadei5c8993c2022-04-04 18:43:54 +000065 results.extend(CheckSourceSideSpecs(input_api, output_api))
Jeremy Leconte0c4059c2022-04-04 07:58:40 +020066 return results
67
68
69def CheckChangeOnCommit(input_api, output_api):
70 results = []
71 results.extend(CheckPatchFormatted(input_api, output_api))
Mirko Bonadei5c8993c2022-04-04 18:43:54 +000072 results.extend(CheckSourceSideSpecs(input_api, output_api))
Jeremy Leconte0c4059c2022-04-04 07:58:40 +020073 return results