|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +# Copyright 2021 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 | +"""Edit KEPs en-masse by round-tripping them through ruamel.yaml |
| 18 | +
|
| 19 | +This is not intended for general usage, because: |
| 20 | +- many keps have different formatting, and we're not at a point where |
| 21 | + we can enforce formatting standards, so this is almost guaranteed |
| 22 | + to introduce formatting change noise |
| 23 | +- the idea is to manually edit this file with the specific edit to be |
| 24 | + done, rather that developing a general purpose language to do this |
| 25 | +""" |
| 26 | + |
| 27 | +import argparse |
| 28 | +import glob |
| 29 | + |
| 30 | +from os import path |
| 31 | + |
| 32 | +import ruamel.yaml |
| 33 | + |
| 34 | +# Files that will be ignored |
| 35 | +EXCLUDED_FILES = [] |
| 36 | +# A hilariously large line length to ensure we never line-wrap |
| 37 | +MAX_WIDTH = 2000000000 |
| 38 | + |
| 39 | +def setup_yaml(): |
| 40 | + # Setup the ruamel.yaml parser |
| 41 | + yaml = ruamel.yaml.YAML(typ='rt') |
| 42 | + yaml.preserve_quotes = True |
| 43 | + # This is what's used in the template, currently ~36 KEPs have drifted |
| 44 | + yaml.indent(mapping=2, sequence=4, offset=2) |
| 45 | + yaml.width = MAX_WIDTH |
| 46 | + return yaml |
| 47 | + |
| 48 | +def edit_kep(yaml, file_name, force_rewrite=False): |
| 49 | + with open(file_name, "r") as fp: |
| 50 | + kep = yaml.load(fp) |
| 51 | + |
| 52 | + rewrite = force_rewrite |
| 53 | + |
| 54 | + stage = kep.get("stage", "unknown") |
| 55 | + status = kep.get("status", "unknown") |
| 56 | + latest_milestone = kep.get("latest-milestone", "unknown") |
| 57 | + last_updated = kep.get("last-updated", "unknown") |
| 58 | + milestone = kep.get("milestone", {}) |
| 59 | + |
| 60 | + if status == "implemented": |
| 61 | + if latest_milestone == "unknown": |
| 62 | + print(f'status: {status} stage: {stage} last-updated: {last_updated} file: {file_name}') |
| 63 | + kep["latest-milestone"] = "0.0" |
| 64 | + rewrite = True |
| 65 | + if stage == "unknown": |
| 66 | + if latest_milestone == "unknown": |
| 67 | + kep["stage"] = "stable" |
| 68 | + else: |
| 69 | + kep["stage"] = [s for s,v in milestone.items() if v == latest_milestone][0] |
| 70 | + rewrite = True |
| 71 | + |
| 72 | + # Dump KEP to file_name |
| 73 | + if rewrite: |
| 74 | + print(f' writing {file_name}') |
| 75 | + with open(file_name, "w") as fp: |
| 76 | + yaml.dump(kep, fp) |
| 77 | + fp.truncate() |
| 78 | + |
| 79 | +def main(keps_dir, force_rewrite): |
| 80 | + yaml = setup_yaml() |
| 81 | + for f in glob.glob(f'{keps_dir}/**/kep.yaml', recursive=True): |
| 82 | + if path.basename(f) not in EXCLUDED_FILES: |
| 83 | + try: |
| 84 | + print(f'processing file: {f}') |
| 85 | + edit_kep(yaml, f, force_rewrite) |
| 86 | + except Exception as e: # pylint: disable=broad-except |
| 87 | + print(f'ERROR: could not edit {f}: {e}') |
| 88 | + |
| 89 | +if __name__ == '__main__': |
| 90 | + PARSER = argparse.ArgumentParser( |
| 91 | + description='Does things to KEPs') |
| 92 | + PARSER.add_argument( |
| 93 | + '--keps-dir', |
| 94 | + default='../keps', |
| 95 | + help='Path to KEPs directoryProw Job Directory') |
| 96 | + PARSER.add_argument( |
| 97 | + '--force', |
| 98 | + default=False, |
| 99 | + help='Force rewrite of all KEPs') |
| 100 | + ARGS = PARSER.parse_args() |
| 101 | + |
| 102 | + main(ARGS.keps_dir, ARGS.force) |
| 103 | + |
0 commit comments