Skip to content

Commit

Permalink
DEV: Make make_release.py compatible with Windows environment (#2894)
Browse files Browse the repository at this point in the history
  • Loading branch information
pubpub-zz authored Oct 13, 2024
1 parent c8bfa5f commit c9dda9a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 26 deletions.
40 changes: 22 additions & 18 deletions make_release.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def get_changelog(changelog_path: str) -> str:
Returns:
Data of the CHANGELOG
"""
with open(changelog_path) as fh:
with open(changelog_path, encoding="utf-8") as fh:
changelog = fh.read()
return changelog

Expand All @@ -178,7 +178,7 @@ def write_changelog(new_changelog: str, changelog_path: str) -> None:
new_changelog: Contents of the new CHANGELOG
changelog_path: Path where the CHANGELOG file is
"""
with open(changelog_path, "w") as fh:
with open(changelog_path, "w", encoding="utf-8") as fh:
fh.write(new_changelog)


Expand Down Expand Up @@ -253,7 +253,9 @@ def get_formatted_changes(git_tag: str) -> Tuple[str, str]:
for prefix in grouped:
for commit in grouped[prefix]:
output += f"- {prefix}: {commit['msg']}\n"
output_with_user += f"- {prefix}: {commit['msg']} by @{commit['author']}\n"
output_with_user += (
f"- {prefix}: {commit['msg']} by @{commit['author']}\n"
)

return output, output_with_user

Expand All @@ -267,7 +269,7 @@ def get_most_recent_git_tag() -> str:
"""
git_tag = str(
subprocess.check_output(
["git", "describe", "--abbrev=0"], stderr=subprocess.STDOUT
["git", "describe", "--tag", "--abbrev=0"], stderr=subprocess.STDOUT
)
).strip("'b\\n")
return git_tag
Expand Down Expand Up @@ -309,16 +311,20 @@ def get_git_commits_since_tag(git_tag: str) -> List[Change]:
Returns:
List of all changes since git_tag.
"""
commits = subprocess.check_output(
[
"git",
"--no-pager",
"log",
f"{git_tag}..HEAD",
'--pretty=format:"%H:::%s:::%aN"',
],
stderr=subprocess.STDOUT,
).decode("UTF-8").strip()
commits = (
subprocess.check_output(
[
"git",
"--no-pager",
"log",
f"{git_tag}..HEAD",
'--pretty=format:"%H:::%s:::%aN"',
],
stderr=subprocess.STDOUT,
)
.decode("UTF-8")
.strip()
)
lines = commits.splitlines()
authors = get_author_mapping(len(lines))
return [parse_commit_line(line, authors) for line in lines if line != ""]
Expand All @@ -337,7 +343,7 @@ def parse_commit_line(line: str, authors: Dict[str, str]) -> Change:
Raises:
ValueError: The commit line is not well-structured
"""
parts = line.split(":::")
parts = line.strip().strip('"\\').split(":::")
if len(parts) != 3:
raise ValueError(f"Invalid commit line: '{line}'")
commit_hash, rest, author = parts
Expand All @@ -349,10 +355,8 @@ def parse_commit_line(line: str, authors: Dict[str, str]) -> Change:

# Standardize
message.strip()
commit_hash = commit_hash.strip('"')
commit_hash = commit_hash.strip()

if author.endswith('"'):
author = author[:-1]
author_login = authors[commit_hash]

prefix = prefix.strip()
Expand Down
20 changes: 12 additions & 8 deletions tests/scripts/test_make_release.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

DATA_PATH = Path(__file__).parent.resolve() / "data"


# line starting with \ and ending with " have been observed on windows
GIT_LOG__VERSION_4_0_1 = """
b7bfd0d7eddfd0865a94cc9e7027df6596242cf7:::BUG: Use NumberObject for /Border elements of annotations (#2451):::rsinger417
8cacb0fc8fee9920b0515d1289e6ee8191eb3f21:::DOC: Document easier way to update metadata (#2454):::Stefan
3fb63f7e3839ce39ac98978c996f3086ba230a20:::TST: Avoid catching not emitted warnings (#2429):::Stefan
61b73d49778e8f0fb172d5323e67677c9974e420:::DOC: Typo `Polyline` → `PolyLine` in adding-pdf-annotations.md (#2426):::CWKSC
\\61b73d49778e8f0fb172d5323e67677c9974e420:::DOC: Typo `Polyline` → `PolyLine` in adding-pdf-annotations.md (#2426):::CWKSC"
f851a532a5ec23b572d86bd7185b327a3fac6b58:::DEV: Bump codecov/codecov-action from 3 to 4 (#2430):::dependabot[bot]""".encode() # noqa: E501

COMMITS__VERSION_4_0_1 = DATA_PATH.joinpath("commits__version_4_0_1.json")
Expand Down Expand Up @@ -116,28 +116,31 @@ def test_get_formatted_changes__other():
prefix="",
message="Improve lossless compression example (#2488)",
author="j-t-1",
author_login="j-t-1"
author_login="j-t-1",
),
make_release.Change(
commit_hash="afbee382f8fd2b39588db6470b9b2b2c82905318",
prefix="ENH",
message="Add reattach_fields function (#2480)",
author="pubpub-zz",
author_login="pubpub-zz"
author_login="pubpub-zz",
),
make_release.Change(
commit_hash="cd705f959064d8125397ddf4f7bdd2ea296f889f",
prefix="FIX",
message="Broken test due to expired test file URL (#2468)",
author="pubpub-zz",
author_login="pubpub-zz"
author_login="pubpub-zz",
),
]
with mock.patch.object(make_release, "get_git_commits_since_tag", return_value=changes):
with mock.patch.object(
make_release, "get_git_commits_since_tag", return_value=changes
):
output, output_with_user = make_release.get_formatted_changes("dummy")

assert (
output == """
output
== """
### New Features (ENH)
- Add reattach_fields function (#2480)
Expand All @@ -148,7 +151,8 @@ def test_get_formatted_changes__other():
)

assert (
output_with_user == """
output_with_user
== """
### New Features (ENH)
- Add reattach_fields function (#2480) by @pubpub-zz
Expand Down

0 comments on commit c9dda9a

Please sign in to comment.