From 9f9bc35dc0115a73938b85e96ab322d906de4b62 Mon Sep 17 00:00:00 2001 From: Dylan Baker Date: Tue, 5 Jan 2021 14:23:32 -0800 Subject: [PATCH] bin/gen_calendar_entries: Add support for making a release Acked-by: Eric Engestrom Part-of: --- bin/gen_calendar_entries.py | 58 ++++++++++++++- bin/gen_calendar_entries_test.py | 122 +++++++++++++++++++++++++++++++ 2 files changed, 179 insertions(+), 1 deletion(-) diff --git a/bin/gen_calendar_entries.py b/bin/gen_calendar_entries.py index dcf3773db77..711702e28ac 100755 --- a/bin/gen_calendar_entries.py +++ b/bin/gen_calendar_entries.py @@ -41,6 +41,13 @@ if typing.TYPE_CHECKING: manager: str + class FinalArguments(Protocol): + """Typing information for release command arguments.""" + + series: str + manager: str + zero_released: bool + class ExtendArguments(Protocol): """Typing information for extend command arguments.""" @@ -89,7 +96,6 @@ def _calculate_release_start(major: str, minor: str) -> datetime.date: return quarter.replace(day=quarter.day + 14) - def release_candidate(args: RCArguments) -> None: """Add release candidate entries.""" with VERSION.open('r') as f: @@ -113,6 +119,50 @@ def release_candidate(args: RCArguments) -> None: commit(f'docs: Add calendar entries for {major}.{minor} release candidates.') +def _calculate_next_release_date(next_is_zero: bool) -> datetime.date: + """Calculate the date of the next release. + + If the next is .0, we have the release in seven days, if the next is .1, + then it's in 14 + """ + date = datetime.date.today() + day = date.isoweekday() + if day < 3: + delta = 3 - day + elif day > 3: + # this will walk back into the previous month, it's much simpler to + # duplicate the 14 than handle the calculations for the month and year + # changing. + delta = (3 - day) + else: + delta = 0 + delta += 7 + if not next_is_zero: + delta += 7 + return date + datetime.timedelta(days=delta) + + +def final_release(args: FinalArguments) -> None: + """Add final release entries.""" + data = read_calendar() + date = _calculate_next_release_date(not args.zero_released) + + with CALENDAR_CSV.open('w') as f: + writer = csv.writer(f) + writer.writerows(data) + + base = 1 if args.zero_released else 0 + + writer.writerow([args.series, date.isoformat(), f'{args.series}.{base}', args.manager]) + for row in range(base + 1, 3): + date = date + datetime.timedelta(days=14) + writer.writerow([None, date.isoformat(), f'{args.series}.{row}', args.manager]) + date = date + datetime.timedelta(days=14) + writer.writerow([None, date.isoformat(), f'{args.series}.3', args.manager, LAST_RELEASE.format(args.series)]) + + commit(f'docs: Add calendar entries for {args.series} release.') + + def extend(args: ExtendArguments) -> None: """Extend a release.""" @contextlib.contextmanager @@ -184,6 +234,12 @@ def main() -> None: rc.add_argument('manager', help="the name of the person managing the release.") rc.set_defaults(func=release_candidate) + fr = sub.add_parser('release', help='Generate calendar entries for a final release.') + fr.add_argument('manager', help="the name of the person managing the release.") + fr.add_argument('series', help='The series to extend, such as "29.3" or "30.0".') + fr.add_argument('--zero-released', action='store_true', help='The .0 release was today, the next release is .1') + fr.set_defaults(func=final_release) + ex = sub.add_parser('extend', help='Generate additional entries for a release.') ex.add_argument('series', help='The series to extend, such as "29.3" or "30.0".') ex.add_argument('count', type=int, help='The number of new entries to add.') diff --git a/bin/gen_calendar_entries_test.py b/bin/gen_calendar_entries_test.py index 70536ba0f0d..59c3645b39a 100644 --- a/bin/gen_calendar_entries_test.py +++ b/bin/gen_calendar_entries_test.py @@ -26,6 +26,7 @@ from unittest import mock import argparse import csv import contextlib +import datetime import tempfile import os import pathlib @@ -77,6 +78,73 @@ class TestReleaseStart: assert d.year == 2021 +class TestNextReleaseDate: + + @contextlib.contextmanager + def _patch_date(date: datetime.date) -> typing.Iterator[None]: + mdate = mock.Mock() + mdate.today = mock.Mock(return_value=date) + with mock.patch('bin.gen_calendar_entries.datetime.date', mdate): + yield + + class TestIsWeds: + + @pytest.fixture(scope='class', autouse=True) + def data(self) -> None: + date = datetime.date(2021, 1, 6) + with TestNextReleaseDate._patch_date(date): + yield + + @pytest.mark.parametrize( + 'is_zero, expected', + [ + (True, 13), + (False, 20), + ], + ) + def test(self, is_zero: bool, expected: int) -> None: + date = gen_calendar_entries._calculate_next_release_date(is_zero) + assert date.day == expected + + class TestBeforeWeds: + + @pytest.fixture(scope='class', autouse=True) + def data(self) -> None: + date = datetime.date(2021, 1, 5) + with TestNextReleaseDate._patch_date(date): + yield + + @pytest.mark.parametrize( + 'is_zero, expected', + [ + (True, 13), + (False, 20), + ], + ) + def test(self, is_zero: bool, expected: int) -> None: + date = gen_calendar_entries._calculate_next_release_date(is_zero) + assert date.day == expected + + class TestAfterWeds: + + @pytest.fixture(scope='class', autouse=True) + def data(self) -> None: + date = datetime.date(2021, 1, 8) + with TestNextReleaseDate._patch_date(date): + yield + + @pytest.mark.parametrize( + 'is_zero, expected', + [ + (True, 13), + (False, 20), + ], + ) + def test(self, is_zero: bool, expected: int) -> None: + date = gen_calendar_entries._calculate_next_release_date(is_zero) + assert date.day == expected + + class TestRC: ORIGINAL_DATA = [ @@ -195,3 +263,57 @@ class TestExtend: ]) assert actual == expected + + +class TestFinal: + + @pytest.fixture(autouse=True, scope='class') + def _patch_date(self) -> typing.Iterator[None]: + mdate = mock.Mock() + mdate.today = mock.Mock(return_value=datetime.date(2021, 1, 6)) + with mock.patch('bin.gen_calendar_entries.datetime.date', mdate): + yield + + ORIGINAL_DATA = [ + ('20.3', '2021-01-13', '20.3.3', 'Dylan Baker', ''), + ('', '2021-01-27', '20.3.4', 'Dylan Baker', 'Last planned release of the 20.3.x series'), + ] + + @pytest.fixture(autouse=True) + def csv(self) -> None: + """inject our test data..""" + with mock_csv(self.ORIGINAL_DATA): + yield + + def test_zero_released(self) -> None: + args: gen_calendar_entries.FinalArguments = argparse.Namespace() + args.manager = "Dylan Baker" + args.zero_released = True + args.series = '21.0' + gen_calendar_entries.final_release(args) + + expected = self.ORIGINAL_DATA.copy() + expected.append(('21.0', '2021-01-20', f'21.0.1', 'Dylan Baker')) + expected.append(( '', '2021-02-03', f'21.0.2', 'Dylan Baker')) + expected.append(( '', '2021-02-17', f'21.0.3', 'Dylan Baker', gen_calendar_entries.LAST_RELEASE.format(args.series))) + + actual = gen_calendar_entries.read_calendar() + + assert actual == expected + + def test_zero_not_released(self) -> None: + args: gen_calendar_entries.FinalArguments = argparse.Namespace() + args.manager = "Dylan Baker" + args.zero_released = False + args.series = '21.0' + gen_calendar_entries.final_release(args) + + expected = self.ORIGINAL_DATA.copy() + expected.append(('21.0', '2021-01-13', f'21.0.0', 'Dylan Baker')) + expected.append(( '', '2021-01-27', f'21.0.1', 'Dylan Baker')) + expected.append(( '', '2021-02-10', f'21.0.2', 'Dylan Baker')) + expected.append(( '', '2021-02-24', f'21.0.3', 'Dylan Baker', gen_calendar_entries.LAST_RELEASE.format(args.series))) + + actual = gen_calendar_entries.read_calendar() + + assert actual == expected