Skip to content

Commit

Permalink
Fix creation of files
Browse files Browse the repository at this point in the history
  • Loading branch information
kdeldycke committed Aug 25, 2024
1 parent e02d4a8 commit 55095bb
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 9 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
> This version is not released yet and is under active development.
- Fix over-escaping of `[!IMPORTANT]` admonition in changelog.
- Fix content writing into output files.

## [4.5.0 (2024-08-24)](https://github.com/kdeldycke/workflows/compare/v4.4.5...v4.5.0)

Expand Down
2 changes: 1 addition & 1 deletion gha_utils/changelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def update(self) -> str | None:
# Extract current version as defined by bump-my-version.
config_file = Path("./pyproject.toml").resolve()
logging.info(f"Open {config_file}")
config = tomllib.loads(config_file.read_text(encoding="utf-8"))
config = tomllib.loads(config_file.read_text(encoding="UTF-8"))
current_version = config["tool"]["bumpversion"]["current_version"]
logging.info(f"Current version: {current_version}")
assert current_version
Expand Down
15 changes: 10 additions & 5 deletions gha_utils/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,18 @@


def is_stdout(filepath: Path) -> bool:
"""Check if a file path is set to stdout.
Prevents the creation of a ``-`` file in the current directory.
"""
return str(filepath) == "-"


def handle_stdout(filepath: Path) -> Path | None:
def prep_path(filepath: Path) -> Path | None:
"""Prepare the output file parameter for Click's echo function."""
if is_stdout(filepath):
return None
return filepath
return filepath.open("w", encoding="UTF-8")


def generate_header(ctx: Context) -> str:
Expand Down Expand Up @@ -157,7 +162,7 @@ def metadata(ctx, format, overwrite, output_path):

dialect = Dialects(format)
content = metadata.dump(dialect=dialect)
echo(content, file=handle_stdout(output_path))
echo(content, file=prep_path(output_path))


@gha_utils.command(short_help="Maintain a Markdown-formatted changelog")
Expand Down Expand Up @@ -189,7 +194,7 @@ def changelog(ctx, source, changelog_path):
logging.info(f"Print updated results to {sys.stdout.name}")
else:
logging.info(f"Save updated results to {changelog_path}")
echo(content, file=handle_stdout(changelog_path))
echo(content, file=prep_path(changelog_path))


@gha_utils.command(short_help="Update Git's .mailmap file with missing contributors")
Expand Down Expand Up @@ -263,4 +268,4 @@ def mailmap_sync(ctx, source, create_if_missing, destination_mailmap):
logging.warning("Nothing to update, stop the sync process.")
ctx.exit()

echo(generate_header(ctx) + new_content, file=handle_stdout(destination_mailmap))
echo(generate_header(ctx) + new_content, file=prep_path(destination_mailmap))
2 changes: 1 addition & 1 deletion gha_utils/mailmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def git_contributors(self) -> set[str]:

git_cli = ("git", "log", "--pretty=format:%aN <%aE>%n%cN <%cE>")
logging.debug(f"Run: {' '.join(git_cli)}")
process = run(git_cli, capture_output=True, encoding="utf-8")
process = run(git_cli, capture_output=True, encoding="UTF-8")

# Parse git CLI output.
if process.returncode:
Expand Down
4 changes: 2 additions & 2 deletions gha_utils/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,7 @@ def pyproject(self) -> StandardMetadata | None:
``pyproject.toml`` exists and respects the standards. ``False`` otherwise.
"""
if self.pyproject_path.exists() and self.pyproject_path.is_file():
toml = tomllib.loads(self.pyproject_path.read_text(encoding="utf-8"))
toml = tomllib.loads(self.pyproject_path.read_text(encoding="UTF-8"))
try:
metadata = StandardMetadata.from_pyproject(toml)
self._is_python_project = True
Expand Down Expand Up @@ -1104,7 +1104,7 @@ def release_notes(self) -> str | None:
changes = ""
match = re.search(
rf"^##(?P<title>.+{escape(version)} .+?)\n(?P<changes>.*?)\n##",
Path("./changelog.md").read_text(encoding="utf-8"),
Path("./changelog.md").read_text(encoding="UTF-8"),
flags=re.MULTILINE | re.DOTALL,
)
if match:
Expand Down

0 comments on commit 55095bb

Please sign in to comment.