|
| 1 | +"""Fedora CoreOS Release Note Generator |
| 2 | +
|
| 3 | +This script allows the developer to generate a YAML / JSON format Fedora |
| 4 | +CoreOS release note file with specified build id. |
| 5 | +
|
| 6 | +This script updates existing `release-notes.yaml` if specified by the flag |
| 7 | +`--release-notes-file` or creates a new one if none is specified. |
| 8 | +
|
| 9 | +This script writes the output to STDOUT by default or to the `release-notes.yaml` |
| 10 | +or `release-notes.json` specified by the flags `--output-dir` and `--json`. |
| 11 | +
|
| 12 | +This file contains the following functions: |
| 13 | +
|
| 14 | + * parse_args - parse command line arguments |
| 15 | + * read_yaml_snippets - read yaml snippets from `release-notes.d` directory |
| 16 | + * write_yaml_snippets - write yaml snippets to STDOUT or `release-notes.yaml` or `release-notes.json` |
| 17 | + * main - the main function of the script |
| 18 | +""" |
| 19 | + |
| 20 | + |
| 21 | +import yaml |
| 22 | +import argparse |
| 23 | +import os |
| 24 | +import glob |
| 25 | +import json |
| 26 | +import sys |
| 27 | + |
| 28 | + |
| 29 | +def parse_args(): |
| 30 | + """ |
| 31 | + Returns: |
| 32 | + Namspace: Parsed command line attributes |
| 33 | + """ |
| 34 | + parser = argparse.ArgumentParser( |
| 35 | + description="Builds 'release-notes.yaml' from yaml snippets \ |
| 36 | + under 'release-notes.d' directory with specified build id. \ |
| 37 | + Outputs to STDOUT by default.") |
| 38 | + |
| 39 | + parser.add_argument( |
| 40 | + '--build-id', help='build id for release notes', required=True) |
| 41 | + parser.add_argument( |
| 42 | + '--config-dir', help="FCOS config directory where 'release-notes.d' lives", required=True) |
| 43 | + parser.add_argument( |
| 44 | + '--release-notes-file', help="input 'release-notes.yaml' for update, omit to generate a new one", required=False) |
| 45 | + parser.add_argument( |
| 46 | + '--output-dir', help="output directory for 'release-notes.yaml'", required=False) |
| 47 | + parser.add_argument('--json', action='store_true', |
| 48 | + help='output json instead of yaml', required=False) |
| 49 | + args = parser.parse_args() |
| 50 | + return args |
| 51 | + |
| 52 | + |
| 53 | +def read_yaml_snippets(args): |
| 54 | + """Reads and parses yaml snippets under `release-notes.d` directory |
| 55 | +
|
| 56 | + Args: |
| 57 | + args (Namespace): Parsed command line attributes |
| 58 | +
|
| 59 | + Returns: |
| 60 | + dictionary: A dictionary consists of a new release note item generated from |
| 61 | + yaml snippets under `release-notes.yaml`. As an example: |
| 62 | + {"ignition": [{subject: "", body: ""}]} |
| 63 | + """ |
| 64 | + if not os.path.exists(args.config_dir): |
| 65 | + raise Exception( |
| 66 | + "config directory '{}' does not exist".format(args.config_dir)) |
| 67 | + |
| 68 | + if not os.path.exists(os.path.join(args.config_dir, 'release-notes.d')): |
| 69 | + raise Exception( |
| 70 | + "release-notes.d does not exist under {}".format(args.config_dir)) |
| 71 | + |
| 72 | + snippet_yaml_list = glob.glob(os.path.join( |
| 73 | + args.config_dir, 'release-notes.d/*.yaml')) |
| 74 | + if len(snippet_yaml_list) == 0: |
| 75 | + print("release-notes.d/ does not contain any yaml snippets under '{}'".format(args.config_dir)) |
| 76 | + exit(0) |
| 77 | + |
| 78 | + snippet_dict = dict() |
| 79 | + for snippet_yaml in snippet_yaml_list: |
| 80 | + with open(snippet_yaml, 'r') as f: |
| 81 | + snippet = yaml.load(f, Loader=yaml.FullLoader) |
| 82 | + for item in snippet: |
| 83 | + note = {'subject': item.get( |
| 84 | + 'subject', ''), 'body': item.get('body', '')} |
| 85 | + # purposely avoid item.get('component', '') to error out if the component key does not exist |
| 86 | + project_name = item['component'] |
| 87 | + snippet_dict[project_name] = [*snippet_dict.get(project_name, []), note] |
| 88 | + |
| 89 | + # clean up empty fields |
| 90 | + for project in snippet_dict.copy(): |
| 91 | + # filter out empty note item that has empty component line |
| 92 | + if project == '': |
| 93 | + snippet_dict.pop(project, '') |
| 94 | + continue |
| 95 | + # filter out empty note item that has empty subject line |
| 96 | + snippet_dict[project] = list( |
| 97 | + filter(lambda item: len(item['subject']) > 0, snippet_dict[project])) |
| 98 | + # remove empty note body from note item |
| 99 | + for i, item in enumerate(snippet_dict.copy()[project]): |
| 100 | + if item.get('body', '') == '': |
| 101 | + item.pop('body', '') |
| 102 | + snippet_dict[project][i] = item |
| 103 | + return snippet_dict |
| 104 | + |
| 105 | + |
| 106 | +def write_yaml_snippets(args, snippet_dict): |
| 107 | + """Writes the generated release note item to STDOUT or file |
| 108 | +
|
| 109 | + Writes a new release note if `--release-notes-file` is not specified and |
| 110 | + writes to STDOUT if `--outptu-dir` is not specified. Default format is |
| 111 | + YAML unless `--json` is specified. |
| 112 | +
|
| 113 | + Args: |
| 114 | + args (Namespace): Parsed command line attributes |
| 115 | + snippet_dict (dictionary): The newly created release note item returned |
| 116 | + by `read_yaml_snippets` |
| 117 | + """ |
| 118 | + # output file name and format depending on the --json flag |
| 119 | + outfile = 'release-notes.json' if args.json else 'release-notes.yaml' |
| 120 | + |
| 121 | + # store list of release note dictionaries |
| 122 | + release_notes = [] |
| 123 | + if args.release_notes_file: |
| 124 | + if not os.path.exists(args.release_notes_file): |
| 125 | + raise Exception( |
| 126 | + "intput file '{}' does not exist".format(args.release_notes_file)) |
| 127 | + with open(args.release_notes_file, 'r') as f: |
| 128 | + release_notes = yaml.load(f, Loader=yaml.FullLoader) |
| 129 | + release_notes.insert(0, {args.build_id: snippet_dict}) |
| 130 | + |
| 131 | + if args.output_dir: |
| 132 | + if not os.path.exists(args.output_dir): |
| 133 | + raise Exception( |
| 134 | + "output directory '{}' does not exist".format(args.output_dir)) |
| 135 | + if not os.path.isdir(args.output_dir): |
| 136 | + raise Exception( |
| 137 | + "output path '{}' is not a directory".format(args.output_dir)) |
| 138 | + outfile = os.path.join(args.output_dir, outfile) |
| 139 | + if args.json: |
| 140 | + with open(outfile, 'w') as f: |
| 141 | + json.dump(release_notes, f, indent=2) |
| 142 | + else: |
| 143 | + print(yaml.dump(release_notes, default_flow_style=False), |
| 144 | + file=open(outfile, 'w')) |
| 145 | + print(f"successfully wrote release note file at {outfile}") |
| 146 | + else: |
| 147 | + if args.json: |
| 148 | + json.dump(release_notes, sys.stdout, indent=2) |
| 149 | + else: |
| 150 | + print(yaml.dump(release_notes, default_flow_style=False)) |
| 151 | + return |
| 152 | + |
| 153 | + |
| 154 | +def main(): |
| 155 | + """Main function of the script |
| 156 | +
|
| 157 | + Parses command line argument, then reads and parses yaml snippets under |
| 158 | + `release-notes.d/`, then writes the newly generated release note item to |
| 159 | + either STDOUT(default) or a file. |
| 160 | + """ |
| 161 | + args = parse_args() |
| 162 | + snippet_dict = read_yaml_snippets(args) |
| 163 | + write_yaml_snippets(args, snippet_dict) |
| 164 | + |
| 165 | + |
| 166 | +if __name__ == "__main__": |
| 167 | + main() |
0 commit comments