|
| 1 | +#!/usr/bin/python3 -u |
| 2 | + |
| 3 | +import yaml |
| 4 | +import argparse |
| 5 | +import os |
| 6 | +import glob |
| 7 | + |
| 8 | +# create the top-level parser |
| 9 | +parser = argparse.ArgumentParser() |
| 10 | + |
| 11 | +# create the parser for the "build" command |
| 12 | +parser.add_argument( |
| 13 | + '--config-dir', help='FCOS config directory', required=True) |
| 14 | +parser.add_argument( |
| 15 | + '--output-dir', help='output directory for release-notes.yaml', required=True) |
| 16 | +args = parser.parse_args() |
| 17 | + |
| 18 | +if not os.path.exists(args.config_dir): |
| 19 | + raise Exception( |
| 20 | + "config directory '{}' does not exist".format(args.config_dir)) |
| 21 | + |
| 22 | +if not os.path.exists(os.path.join(args.config_dir, 'release-notes.d')): |
| 23 | + raise Exception( |
| 24 | + "release-notes.d does not exist under {}".format(args.config_dir)) |
| 25 | + |
| 26 | + |
| 27 | +if not os.path.exists(args.output_dir): |
| 28 | + raise Exception( |
| 29 | + "output directory '{}' does not exist".format(args.output_dir)) |
| 30 | + |
| 31 | +if not os.path.isdir(args.output_dir): |
| 32 | + raise Exception( |
| 33 | + "output path '{}' is not a directory".format(args.output_dir)) |
| 34 | + |
| 35 | +snippet_yaml_list = glob.glob(os.path.join( |
| 36 | + args.config_dir, 'release-notes.d/*.yaml')) |
| 37 | +if len(snippet_yaml_list) == 0: |
| 38 | + print("release-notes.d/ does not contain any yaml snippets under '{}'".format(args.config_dir)) |
| 39 | + exit(0) |
| 40 | + |
| 41 | +snippet_dict = dict() |
| 42 | +for snippet_yaml in snippet_yaml_list: |
| 43 | + with open(snippet_yaml, 'r') as f: |
| 44 | + basename = os.path.basename(snippet_yaml) |
| 45 | + project_name = os.path.splitext(basename)[0] |
| 46 | + snippet = yaml.load(f, Loader=yaml.FullLoader) |
| 47 | + snippet_dict[project_name] = snippet |
| 48 | + |
| 49 | +for project in snippet_dict.copy(): |
| 50 | + # filter out empty yaml file content |
| 51 | + if snippet_dict[project] is None: |
| 52 | + snippet_dict.pop(project, None) |
| 53 | + continue |
| 54 | + # eliminate note items with empty subject |
| 55 | + snippet_dict[project] = [ |
| 56 | + note_item for note_item in snippet_dict[project] if note_item['subject']] |
| 57 | + # filter empty note items |
| 58 | + if len(snippet_dict[project]) == 0: |
| 59 | + snippet_dict.pop(project, None) |
| 60 | + |
| 61 | +# write release-notes.yaml to the output directory |
| 62 | +outfile = os.path.join(args.output_dir, 'release-notes.yaml') |
| 63 | +# store list of release note dictionaries |
| 64 | +release_notes = [] |
| 65 | + |
| 66 | +if os.path.exists(outfile): |
| 67 | + print("{} already exists, checking 'next-release' field".format(outfile)) |
| 68 | + with open(outfile, 'r') as f: |
| 69 | + release_notes = yaml.load(f, Loader=yaml.FullLoader) |
| 70 | + # checks if the release note already exists in the yaml file |
| 71 | + latest_note = release_notes[0] if len(release_notes) != 0 else dict() |
| 72 | + if snippet_dict == latest_note.get("next-release", dict()): |
| 73 | + print("'next-release' field already exists and no changes detected") |
| 74 | + exit(0) |
| 75 | + |
| 76 | +with open(outfile, 'w') as w: |
| 77 | + # check if 'next-release' item already exists |
| 78 | + if len(release_notes) > 0 and "next-release" in release_notes[0]: |
| 79 | + print("'next-release' field updated for {}".format(outfile)) |
| 80 | + release_notes[0]["next-release"] = snippet_dict |
| 81 | + else: |
| 82 | + # else insert the new release notes at the front of the list |
| 83 | + release_notes.insert(0, {"next-release": snippet_dict}) |
| 84 | + yaml.dump(release_notes, w) |
| 85 | + print(f"Successfully wrote release note file at {outfile}") |
0 commit comments