github.com/shashidharatd/test-infra@v0.0.0-20171006011030-71304e1ca560/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.json'), 'r') as fp: 69 configs = json.loads(fp.read()) 70 for rtype in configs: 71 rtype["names"] = sorted(rtype["names"]) 72 output = cStringIO.StringIO() 73 json.dump( 74 configs, output, sort_keys=True, indent=2, separators=(',', ': ')) 75 output.write('\n') 76 return output 77 78 def sort_boskos_config(): 79 """Sort boskos/resources.json alphabetically.""" 80 output = sorted_boskos_config() 81 with open(test_infra('boskos/resources.json'), 'w+') as fp: 82 fp.write(output.getvalue()) 83 output.close() 84 85 86 def sorted_prow_config(): 87 """Get the sorted Prow configuration.""" 88 with open(test_infra('prow/config.yaml'), 'r') as fp: 89 configs = yaml.round_trip_load(fp, preserve_quotes=True) 90 configs['periodics'] = sorted_seq(configs['periodics']) 91 configs['presubmits'] = sorted_map(configs['presubmits']) 92 configs['postsubmits'] = sorted_map(configs['postsubmits']) 93 output = cStringIO.StringIO() 94 yaml.round_trip_dump( 95 configs, output, default_flow_style=False, width=float("inf")) 96 return output 97 98 99 def sort_prow_config(): 100 """Sort test jobs in Prow configuration alphabetically.""" 101 output = sorted_prow_config() 102 with open(test_infra('prow/config.yaml'), 'w+') as fp: 103 fp.write(output.getvalue()) 104 output.close() 105 106 107 if __name__ == '__main__': 108 PARSER = argparse.ArgumentParser( 109 description='Sort config.json and prow/config.yaml alphabetically') 110 ARGS = PARSER.parse_args() 111 112 sort_job_config() 113 sort_prow_config() 114 sort_boskos_config()