github.com/abayer/test-infra@v0.0.5/jobs/config_sort.py (about) 1 #!/usr/bin/env python 2 3 # Copyright 2017 The Kubernetes Authors. 4 # 5 # Licensed under the Apache License, Version 2.0 (the "License"); 6 # you may not use this file except in compliance with the License. 7 # You may obtain a copy of the License at 8 # 9 # http://www.apache.org/licenses/LICENSE-2.0 10 # 11 # Unless required by applicable law or agreed to in writing, software 12 # distributed under the License is distributed on an "AS IS" BASIS, 13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 # See the License for the specific language governing permissions and 15 # limitations under the License. 16 17 """Sort current config.json and prow/config.yaml alphabetically. """ 18 19 import argparse 20 import cStringIO 21 import json 22 import os 23 24 import ruamel.yaml as yaml 25 26 ORIG_CWD = os.getcwd() # Checkout changes cwd 27 28 def test_infra(*paths): 29 """Return path relative to root of test-infra repo.""" 30 return os.path.join(ORIG_CWD, os.path.dirname(__file__), '..', *paths) 31 32 33 def sorted_seq(jobs): 34 return yaml.comments.CommentedSeq( 35 sorted(jobs, key=lambda job: job['name'])) 36 37 def sorted_args(args): 38 return sorted(args, key=lambda arg: arg.split("=")[0]) 39 40 def sorted_map(jobs): 41 for key, value in jobs.items(): 42 jobs[key] = sorted_seq(value) 43 return jobs 44 45 46 def sorted_job_config(): 47 """Sort config.json alphabetically.""" 48 with open(test_infra('jobs/config.json'), 'r') as fp: 49 configs = json.loads(fp.read()) 50 for _, config in configs.items(): 51 # The execute scenario is a free-for-all, don't sort. 52 if config["scenario"] != "execute" and "args" in config: 53 config["args"] = sorted_args(config["args"]) 54 output = cStringIO.StringIO() 55 json.dump( 56 configs, output, sort_keys=True, indent=2, separators=(',', ': ')) 57 output.write('\n') 58 return output 59 60 def sort_job_config(): 61 output = sorted_job_config() 62 with open(test_infra('jobs/config.json'), 'w+') as fp: 63 fp.write(output.getvalue()) 64 output.close() 65 66 def sorted_boskos_config(): 67 """Get the sorted boskos configuration.""" 68 with open(test_infra('boskos/resources.yaml'), 'r') as fp: 69 configs = yaml.round_trip_load(fp, preserve_quotes=True) 70 for rtype in configs['resources']: 71 rtype["names"] = sorted(rtype["names"]) 72 output = cStringIO.StringIO() 73 yaml.round_trip_dump( 74 configs, output, default_flow_style=False, width=float("inf")) 75 return output 76 77 def sort_boskos_config(): 78 """Sort boskos/resources.yaml alphabetically.""" 79 output = sorted_boskos_config() 80 with open(test_infra('boskos/resources.yaml'), 'w+') as fp: 81 fp.write(output.getvalue()) 82 output.close() 83 84 85 def sorted_prow_config(prow_config_path=None): 86 """Get the sorted Prow configuration.""" 87 with open(prow_config_path, 'r') as fp: 88 configs = yaml.round_trip_load(fp, preserve_quotes=True) 89 configs['periodics'] = sorted_seq(configs['periodics']) 90 configs['presubmits'] = sorted_map(configs['presubmits']) 91 configs['postsubmits'] = sorted_map(configs['postsubmits']) 92 output = cStringIO.StringIO() 93 yaml.round_trip_dump( 94 configs, output, default_flow_style=False, width=float("inf")) 95 return output 96 97 98 def sort_prow_config(prow_config_path=None): 99 """Sort test jobs in Prow configuration alphabetically.""" 100 output = sorted_prow_config(prow_config_path) 101 with open(prow_config_path, 'w+') as fp: 102 fp.write(output.getvalue()) 103 output.close() 104 105 106 def main(): 107 parser = argparse.ArgumentParser( 108 description='Sort config.json and prow/config.yaml alphabetically') 109 parser.add_argument('--prow-config', default=None, help='path to prow config') 110 parser.add_argument('--only-prow', default=False, 111 help='only sort prow config', action='store_true') 112 args = parser.parse_args() 113 # default to known relative path 114 prow_config_path = args.prow_config 115 if args.prow_config is None: 116 prow_config_path = test_infra('prow/config.yaml') 117 # actually sort 118 sort_prow_config(prow_config_path) 119 if not args.only_prow: 120 sort_job_config() 121 sort_boskos_config() 122 123 if __name__ == '__main__': 124 main()