1# Copyright 2022 the V8 project authors. All rights reserved. 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4 5import json 6 7# This line is 'magic' in that git-cl looks for it to decide whether to 8# use Python3 instead of Python2 when running the code in this file. 9USE_PYTHON3 = True 10 11 12def _CheckTrialsConfig(input_api, output_api): 13 def FilterFile(affected_file): 14 return input_api.FilterSourceFile( 15 affected_file, 16 files_to_check=(r'.+clusterfuzz_trials_config\.json',)) 17 18 results = [] 19 for f in input_api.AffectedFiles( 20 file_filter=FilterFile, include_deletes=False): 21 with open(f.AbsoluteLocalPath()) as j: 22 try: 23 trials = json.load(j) 24 for trial in trials: 25 if not all( 26 k in trial for k in ('app_args', 'app_name', 'probability')): 27 results.append('trial {} is not configured correctly'.format(trial)) 28 if trial['app_name'] != 'd8': 29 results.append('trial {} has an incorrect app_name'.format(trial)) 30 if not isinstance(trial['probability'], float): 31 results.append('trial {} probability is not a float'.format(trial)) 32 if not (0 <= trial['probability'] <= 1): 33 results.append( 34 'trial {} has invalid probability value'.format(trial)) 35 if not isinstance(trial['app_args'], str) or not trial['app_args']: 36 results.append( 37 'trial {} should have a non-empty string for app_args'.format( 38 trial)) 39 except Exception as e: 40 results.append( 41 'JSON validation failed for %s. Error:\n%s' % (f.LocalPath(), e)) 42 43 return [output_api.PresubmitError(r) for r in results] 44 45def _CommonChecks(input_api, output_api): 46 """Checks common to both upload and commit.""" 47 checks = [ 48 _CheckTrialsConfig, 49 ] 50 51 return sum([check(input_api, output_api) for check in checks], []) 52 53def CheckChangeOnCommit(input_api, output_api): 54 return _CommonChecks(input_api, output_api) 55 56def CheckChangeOnUpload(input_api, output_api): 57 return _CommonChecks(input_api, output_api) 58