PRESUBMIT.py (2795B)
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 11 import difflib 12 import os 13 import shlex 14 15 # Runs PRESUBMIT.py in py3 mode by git cl presubmit. 16 USE_PYTHON3 = True 17 18 19 def CheckPatchFormatted(input_api, output_api): 20 results = [] 21 file_filter = lambda x: x.LocalPath().endswith('.pyl') 22 affected_files = input_api.AffectedFiles( 23 include_deletes=False, file_filter=file_filter 24 ) 25 26 diffs = [] 27 for f in affected_files: 28 # NewContents just reads the file. 29 prev_content = f.NewContents() 30 31 cmd = ['yapf', '-i', f.AbsoluteLocalPath()] 32 if input_api.subprocess.call(cmd): 33 results.append( 34 output_api.PresubmitError('Error calling "' + shlex.join(cmd) + '"') 35 ) 36 37 # Make sure NewContents reads the updated files from disk and not cache. 38 new_content = f.NewContents(flush_cache=True) 39 if new_content != prev_content: 40 path = f.LocalPath() 41 diff = difflib.unified_diff(prev_content, new_content, path, path, lineterm='') 42 diffs.append('\n'.join(diff)) 43 44 if diffs: 45 combined_diffs = '\n'.join(diffs) 46 msg = ( 47 'Diff found after running "yapf -i" on modified .pyl files:\n\n' 48 f'{combined_diffs}\n\n' 49 'Please commit or discard the new changes.' 50 ) 51 results.append(output_api.PresubmitError(msg)) 52 53 return results 54 55 56 def CheckSourceSideSpecs(input_api, output_api): 57 d = os.path.dirname 58 webrtc_root = d(d(input_api.PresubmitLocalPath())) 59 gen_script = os.path.join(webrtc_root, 'testing', 'buildbot', 60 'generate_buildbot_json.py') 61 62 commands = [ 63 input_api.Command(name='generate_buildbot_json', 64 cmd=[ 65 input_api.python3_executable, gen_script, '--check', 66 '--verbose', '--pyl-files-dir', 67 input_api.PresubmitLocalPath() 68 ], 69 kwargs={}, 70 message=output_api.PresubmitError), 71 ] 72 return input_api.RunTests(commands) 73 74 75 def CheckChangeOnUpload(input_api, output_api): 76 results = [] 77 results.extend(CheckPatchFormatted(input_api, output_api)) 78 results.extend(CheckSourceSideSpecs(input_api, output_api)) 79 return results 80 81 82 def CheckChangeOnCommit(input_api, output_api): 83 results = [] 84 results.extend(CheckPatchFormatted(input_api, output_api)) 85 results.extend(CheckSourceSideSpecs(input_api, output_api)) 86 return results