diff --git a/src/blurb/_cli.py b/src/blurb/_cli.py index b61c0a8..7e214a1 100644 --- a/src/blurb/_cli.py +++ b/src/blurb/_cli.py @@ -298,5 +298,6 @@ def test_first_line(filename, test): break - blurb.root = path + import blurb.blurb + blurb.blurb.root = path return path diff --git a/src/blurb/_release.py b/src/blurb/_release.py new file mode 100644 index 0000000..8128a40 --- /dev/null +++ b/src/blurb/_release.py @@ -0,0 +1,70 @@ +from __future__ import annotations + +import os +import time + +import blurb.blurb +from blurb._cli import error, subcommand +from blurb.blurb import (Blurbs, flush_git_add_files, flush_git_rm_files, + git_rm_files, git_add_files, glob_blurbs, nonceify) + + +@subcommand +def release(version: str) -> None: + """Move all new blurbs to a single blurb file for the release. + + This is used by the release manager when cutting a new release. + """ + if version == '.': + # harvest version number from dirname of repo + # I remind you, we're in the Misc subdir right now + version = os.path.basename(blurb.blurb.root) + + existing_filenames = glob_blurbs(version) + if existing_filenames: + error("Sorry, can't handle appending 'next' files to an existing version (yet).") + + output = f'Misc/NEWS.d/{version}.rst' + filenames = glob_blurbs('next') + blurbs = Blurbs() + date = current_date() + + if not filenames: + print(f'No blurbs found. Setting {version} as having no changes.') + body = f'There were no new changes in version {version}.\n' + metadata = {'no changes': 'True', 'gh-issue': '0', 'section': 'Library', 'date': date, 'nonce': nonceify(body)} + blurbs.append((metadata, body)) + else: + count = len(filenames) + print(f'Merging {count} blurbs to "{output}".') + + for filename in filenames: + if not filename.endswith('.rst'): + continue + blurbs.load_next(filename) + + metadata = blurbs[0][0] + + metadata['release date'] = date + print('Saving.') + + blurbs.save(output) + git_add_files.append(output) + flush_git_add_files() + + how_many = len(filenames) + print(f"Removing {how_many} 'next' files from git.") + git_rm_files.extend(filenames) + flush_git_rm_files() + + # sanity check: ensuring that saving/reloading the merged blurb file works. + blurbs2 = Blurbs() + blurbs2.load(output) + assert blurbs2 == blurbs, f"Reloading {output} isn't reproducible?!" + + print() + print('Ready for commit.') + + +def current_date() -> str: + return time.strftime('%Y-%m-%d', time.localtime()) diff --git a/src/blurb/blurb.py b/src/blurb/blurb.py index dbbdb64..803835d 100755 --- a/src/blurb/blurb.py +++ b/src/blurb/blurb.py @@ -39,7 +39,6 @@ # # automatic git adds and removes -import atexit import base64 import builtins import glob @@ -49,18 +48,16 @@ import os from pathlib import Path import re -import shlex import shutil import subprocess import sys -import tempfile import textwrap import time from blurb._cli import main, subcommand from blurb._template import ( next_filename_unsanitize_sections, sanitize_section, - sanitize_section_legacy, sections, template, unsanitize_section, + sanitize_section_legacy, sections, unsanitize_section, ) root = None # Set by chdir_to_repo_root() @@ -156,10 +153,6 @@ def textwrap_body(body, *, subsequent_indent=''): text += "\n" return text - -def current_date(): - return time.strftime("%Y-%m-%d", time.localtime()) - def sortable_datetime(): return time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime()) @@ -566,65 +559,6 @@ def _find_blurb_dir(): return None -@subcommand -def release(version): - """ -Move all new blurbs to a single blurb file for the release. - -This is used by the release manager when cutting a new release. - """ - if version == ".": - # harvest version number from dirname of repo - # I remind you, we're in the Misc subdir right now - version = os.path.basename(root) - - existing_filenames = glob_blurbs(version) - if existing_filenames: - error("Sorry, can't handle appending 'next' files to an existing version (yet).") - - output = f"Misc/NEWS.d/{version}.rst" - filenames = glob_blurbs("next") - blurbs = Blurbs() - date = current_date() - - if not filenames: - print(f"No blurbs found. Setting {version} as having no changes.") - body = f"There were no new changes in version {version}.\n" - metadata = {"no changes": "True", "gh-issue": "0", "section": "Library", "date": date, "nonce": nonceify(body)} - blurbs.append((metadata, body)) - else: - count = len(filenames) - print(f'Merging {count} blurbs to "{output}".') - - for filename in filenames: - if not filename.endswith(".rst"): - continue - blurbs.load_next(filename) - - metadata = blurbs[0][0] - - metadata['release date'] = date - print("Saving.") - - blurbs.save(output) - git_add_files.append(output) - flush_git_add_files() - - how_many = len(filenames) - print(f"Removing {how_many} 'next' files from git.") - git_rm_files.extend(filenames) - flush_git_rm_files() - - # sanity check: ensuring that saving/reloading the merged blurb file works. - blurbs2 = Blurbs() - blurbs2.load(output) - assert blurbs2 == blurbs, f"Reloading {output} isn't reproducible?!" - - print() - print("Ready for commit.") - - - @subcommand def merge(output=None, *, forced=False): """ diff --git a/tests/test_blurb.py b/tests/test_blurb.py index 2339366..2ade934 100644 --- a/tests/test_blurb.py +++ b/tests/test_blurb.py @@ -88,11 +88,6 @@ def test_textwrap_body(body, subsequent_indent, expected): assert blurb.textwrap_body(body, subsequent_indent=subsequent_indent) == expected -@time_machine.travel("2025-01-07") -def test_current_date(): - assert blurb.current_date() == "2025-01-07" - - @time_machine.travel("2025-01-07 16:28:41") def test_sortable_datetime(): assert blurb.sortable_datetime() == "2025-01-07-16-28-41" diff --git a/tests/test_release.py b/tests/test_release.py new file mode 100644 index 0000000..3b4d25b --- /dev/null +++ b/tests/test_release.py @@ -0,0 +1,8 @@ +import time_machine + +from blurb._release import current_date + + +@time_machine.travel("2025-01-07") +def test_current_date(): + assert current_date() == "2025-01-07"