|
| 1 | +import glob |
| 2 | +import os |
| 3 | +import sys |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +from blurb._cli import require_ok, subcommand |
| 7 | +from blurb._template import ( |
| 8 | + next_filename_unsanitize_sections, sanitize_section, |
| 9 | + sanitize_section_legacy, sections, |
| 10 | +) |
| 11 | +from blurb._versions import glob_versions, printable_version |
| 12 | +from blurb.blurb import Blurbs, textwrap_body |
| 13 | + |
| 14 | +original_dir: str = os.getcwd() |
| 15 | + |
| 16 | + |
| 17 | +@subcommand |
| 18 | +def merge(output: str | None = None, *, forced: bool = False) -> None: |
| 19 | + """Merge all blurbs together into a single Misc/NEWS file. |
| 20 | +
|
| 21 | + Optional output argument specifies where to write to. |
| 22 | + Default is <cpython-root>/Misc/NEWS. |
| 23 | +
|
| 24 | + If overwriting, blurb merge will prompt you to make sure it's okay. |
| 25 | + To force it to overwrite, use -f. |
| 26 | + """ |
| 27 | + if output: |
| 28 | + output = os.path.join(original_dir, output) |
| 29 | + else: |
| 30 | + output = 'Misc/NEWS' |
| 31 | + |
| 32 | + versions = glob_versions() |
| 33 | + if not versions: |
| 34 | + sys.exit("You literally don't have ANY blurbs to merge together!") |
| 35 | + |
| 36 | + if os.path.exists(output) and not forced: |
| 37 | + print(f'You already have a {output!r} file.') |
| 38 | + require_ok('Type ok to overwrite') |
| 39 | + |
| 40 | + write_news(output, versions=versions) |
| 41 | + |
| 42 | + |
| 43 | +def write_news(output: str, *, versions: list[str]) -> None: |
| 44 | + buff = [] |
| 45 | + |
| 46 | + def prnt(msg: str = '', /): |
| 47 | + buff.append(msg) |
| 48 | + |
| 49 | + prnt(""" |
| 50 | ++++++++++++ |
| 51 | +Python News |
| 52 | ++++++++++++ |
| 53 | +
|
| 54 | +""".strip()) |
| 55 | + |
| 56 | + for version in versions: |
| 57 | + filenames = glob_blurbs(version) |
| 58 | + |
| 59 | + blurbs = Blurbs() |
| 60 | + if version == 'next': |
| 61 | + for filename in filenames: |
| 62 | + if os.path.basename(filename) == 'README.rst': |
| 63 | + continue |
| 64 | + blurbs.load_next(filename) |
| 65 | + if not blurbs: |
| 66 | + continue |
| 67 | + metadata = blurbs[0][0] |
| 68 | + metadata['release date'] = 'XXXX-XX-XX' |
| 69 | + else: |
| 70 | + assert len(filenames) == 1 |
| 71 | + blurbs.load(filenames[0]) |
| 72 | + |
| 73 | + header = f"What's New in Python {printable_version(version)}?" |
| 74 | + prnt() |
| 75 | + prnt(header) |
| 76 | + prnt('=' * len(header)) |
| 77 | + prnt() |
| 78 | + |
| 79 | + metadata, body = blurbs[0] |
| 80 | + release_date = metadata['release date'] |
| 81 | + |
| 82 | + prnt(f'*Release date: {release_date}*') |
| 83 | + prnt() |
| 84 | + |
| 85 | + if 'no changes' in metadata: |
| 86 | + prnt(body) |
| 87 | + prnt() |
| 88 | + continue |
| 89 | + |
| 90 | + last_section = None |
| 91 | + for metadata, body in blurbs: |
| 92 | + section = metadata['section'] |
| 93 | + if last_section != section: |
| 94 | + last_section = section |
| 95 | + prnt(section) |
| 96 | + prnt('-' * len(section)) |
| 97 | + prnt() |
| 98 | + if metadata.get('gh-issue'): |
| 99 | + issue_number = metadata['gh-issue'] |
| 100 | + if int(issue_number): |
| 101 | + body = f'gh-{issue_number}: {body}' |
| 102 | + elif metadata.get('bpo'): |
| 103 | + issue_number = metadata['bpo'] |
| 104 | + if int(issue_number): |
| 105 | + body = f'bpo-{issue_number}: {body}' |
| 106 | + |
| 107 | + body = f'- {body}' |
| 108 | + text = textwrap_body(body, subsequent_indent=' ') |
| 109 | + prnt(text) |
| 110 | + prnt() |
| 111 | + prnt('**(For information about older versions, consult the HISTORY file.)**') |
| 112 | + |
| 113 | + new_contents = '\n'.join(buff) |
| 114 | + |
| 115 | + # Only write in `output` if the contents are different |
| 116 | + # This speeds up subsequent Sphinx builds |
| 117 | + try: |
| 118 | + previous_contents = Path(output).read_text(encoding='utf-8') |
| 119 | + except (FileNotFoundError, UnicodeError): |
| 120 | + previous_contents = None |
| 121 | + if new_contents != previous_contents: |
| 122 | + Path(output).write_text(new_contents, encoding='utf-8') |
| 123 | + else: |
| 124 | + print(output, 'is already up to date') |
| 125 | + |
| 126 | + |
| 127 | +def glob_blurbs(version: str) -> list[str]: |
| 128 | + filenames = [] |
| 129 | + base = os.path.join('Misc', 'NEWS.d', version) |
| 130 | + if version != 'next': |
| 131 | + wildcard = f'{base}.rst' |
| 132 | + filenames.extend(glob.glob(wildcard)) |
| 133 | + else: |
| 134 | + sanitized_sections = ( |
| 135 | + {sanitize_section(section) for section in sections} | |
| 136 | + {sanitize_section_legacy(section) for section in sections} |
| 137 | + ) |
| 138 | + for section in sanitized_sections: |
| 139 | + wildcard = os.path.join(base, section, '*.rst') |
| 140 | + entries = glob.glob(wildcard) |
| 141 | + deletables = [x for x in entries if x.endswith('/README.rst')] |
| 142 | + for filename in deletables: |
| 143 | + entries.remove(filename) |
| 144 | + filenames.extend(entries) |
| 145 | + filenames.sort(reverse=True, key=next_filename_unsanitize_sections) |
| 146 | + return filenames |
0 commit comments