Skip to content

Commit 3af943d

Browse files
authored
chore(librarian): add support for updating date in version files (#14847)
This change is needed to unblock googleapis/python-bigquery-dataframes#2233 to onboard `bigframes` to librarian. This package has a release date which is currently being updated by release-please in each release PR. See googleapis/python-bigquery-dataframes#2225. We need a similar feature in the librarian python container. ``` # {x-release-please-start-date} __release_date__ = "2025-11-03" # {x-release-please-end} ```
1 parent e7a1a1c commit 3af943d

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

.generator/cli.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import sys
2525
import tempfile
2626
import yaml
27-
from datetime import datetime
27+
from datetime import date, datetime
2828
from pathlib import Path
2929
from typing import Dict, List
3030
import build.util
@@ -1168,6 +1168,12 @@ def _process_version_file(content, version, version_path) -> str:
11681168
raise ValueError(
11691169
f"Could not find version string in {version_path}. File was not modified."
11701170
)
1171+
1172+
# Optionally update the `__release_date__` date string, if it exists, in the format YYYY-MM-DD
1173+
date_pattern = r"(__release_date__\s*=\s*[\"'])([^\"']+)([\"'].*)"
1174+
today_iso = date.today().isoformat() # Get today's date in YYYY-MM-DD format
1175+
date_replacement_string = f"\\g<1>{today_iso}\\g<3>"
1176+
new_content, _ = re.subn(date_pattern, date_replacement_string, new_content)
11711177
return new_content
11721178

11731179

.generator/test_cli.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import subprocess
2121
import yaml
2222
import unittest.mock
23-
from datetime import datetime
23+
from datetime import date, datetime
2424
from pathlib import Path
2525
from unittest.mock import MagicMock, mock_open
2626

@@ -1105,6 +1105,44 @@ def test_update_version_for_library_success_proto_only_setup_py(mocker):
11051105
)
11061106

11071107

1108+
def test_update_version_for_library_success_with_date_string(mocker):
1109+
m = mock_open()
1110+
1111+
mock_rglob = mocker.patch("pathlib.Path.rglob")
1112+
mock_rglob.side_effect = [
1113+
[],
1114+
[pathlib.Path("repo/setup.py")],
1115+
[pathlib.Path("repo/samples/snippet_metadata.json")],
1116+
]
1117+
mock_shutil_copy = mocker.patch("shutil.copy")
1118+
mock_content = 'version = "1.2.2"\n__release_date__ = "2025-11-03"'
1119+
mock_json_metadata = {"clientLibrary": {"version": "0.1.0"}}
1120+
today_iso = date.today().isoformat()
1121+
1122+
with unittest.mock.patch("cli.open", m):
1123+
mocker.patch("cli._read_text_file", return_value=mock_content)
1124+
mocker.patch("cli._read_json_file", return_value=mock_json_metadata)
1125+
_update_version_for_library(
1126+
"repo", "output", "packages/google-cloud-language", "1.2.3"
1127+
)
1128+
1129+
handle = m()
1130+
assert (
1131+
handle.write.call_args_list[0].args[0]
1132+
== f'version = "1.2.3"\n__release_date__ = "{today_iso}"'
1133+
)
1134+
# Get all the arguments passed to the mock's write method
1135+
# and join them into a single string.
1136+
written_content = "".join(
1137+
[call.args[0] for call in handle.write.call_args_list[1:]]
1138+
)
1139+
# Create the expected output string with the correct formatting.
1140+
assert (
1141+
written_content
1142+
== '{\n "clientLibrary": {\n "version": "1.2.3"\n }\n}\n'
1143+
)
1144+
1145+
11081146
def test_update_version_for_library_success_proto_only_pyproject_toml(mocker):
11091147
m = mock_open()
11101148

0 commit comments

Comments
 (0)