From 43a0a2f8478f613ceb203d6a28f2696abf459c3b Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Sun, 5 Mar 2023 22:36:08 +0100 Subject: [PATCH 1/4] Support PEP 621 and package.json. --- .../fragments/100-more-other-projects.yml | 5 +++++ src/antsibull_changelog/cli.py | 20 +++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 changelogs/fragments/100-more-other-projects.yml diff --git a/changelogs/fragments/100-more-other-projects.yml b/changelogs/fragments/100-more-other-projects.yml new file mode 100644 index 00000000..6fc52ad3 --- /dev/null +++ b/changelogs/fragments/100-more-other-projects.yml @@ -0,0 +1,5 @@ +minor_changes: + - Allow to extract other project versions for Python projects from PEP 621 conformant ``pyproject.toml`` + (https://github.com/ansible-community/antsibull-changelog/pull/100). + - Allow to extract other project versions for JavaScript / TypeScript projects form ``package.json`` + (https://github.com/ansible-community/antsibull-changelog/pull/100). diff --git a/src/antsibull_changelog/cli.py b/src/antsibull_changelog/cli.py index f9389964..279fa283 100644 --- a/src/antsibull_changelog/cli.py +++ b/src/antsibull_changelog/cli.py @@ -12,6 +12,7 @@ import argparse import datetime +import json import os import sys import traceback @@ -448,8 +449,13 @@ def _get_pyproject_toml_version(project_toml_path: str) -> str | None: with open(project_toml_path, 'r', encoding='utf-8') as f: data = (toml if HAS_TOML else tomli).loads(f.read()) + # PEP 621 project metadata (https://peps.python.org/pep-0621/#version) + if data.get('version'): + return data.get('version') + tool_config = data.get('tool') or {} + # Python Poetry (https://python-poetry.org/docs/pyproject/#version) if 'poetry' in tool_config: poetry_config = tool_config['poetry'] return poetry_config.get('version') @@ -457,6 +463,16 @@ def _get_pyproject_toml_version(project_toml_path: str) -> str | None: return None +def _get_package_json_version(package_json_path: str) -> str | None: + ''' + Try to extract version from package.json. + ''' + with open(package_json_path, 'rb') as f: + data = json.load(f) + + return data.get('version') + + def _get_project_version(paths: PathsConfig) -> str | None: ''' Try to extract version for other projects. @@ -465,6 +481,10 @@ def _get_project_version(paths: PathsConfig) -> str | None: if os.path.isfile(project_toml_path): return _get_pyproject_toml_version(project_toml_path) + package_json_path = os.path.join(paths.base_dir, 'package.json') + if os.path.isfile(package_json_path): + return _get_package_json_version(package_json_path) + return None From 88f13ee427a795bda89adf9d0412b396fed17122 Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Mon, 6 Mar 2023 06:21:16 +0100 Subject: [PATCH 2/4] Use correct source for PEP 621. --- src/antsibull_changelog/cli.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/antsibull_changelog/cli.py b/src/antsibull_changelog/cli.py index 279fa283..daf98497 100644 --- a/src/antsibull_changelog/cli.py +++ b/src/antsibull_changelog/cli.py @@ -450,8 +450,9 @@ def _get_pyproject_toml_version(project_toml_path: str) -> str | None: data = (toml if HAS_TOML else tomli).loads(f.read()) # PEP 621 project metadata (https://peps.python.org/pep-0621/#version) - if data.get('version'): - return data.get('version') + project_data = data.get('project') + if isinstance(project_data, dict) and project_data.get('version'): + return project_data.get('version') tool_config = data.get('tool') or {} From e8054089d2fa2845f11f8b77b0003c69ecd3da55 Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Mon, 6 Mar 2023 06:23:15 +0100 Subject: [PATCH 3/4] Use UTF-8 encoding or JSON to read as text. --- src/antsibull_changelog/cli.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/antsibull_changelog/cli.py b/src/antsibull_changelog/cli.py index daf98497..9b40da7d 100644 --- a/src/antsibull_changelog/cli.py +++ b/src/antsibull_changelog/cli.py @@ -468,7 +468,7 @@ def _get_package_json_version(package_json_path: str) -> str | None: ''' Try to extract version from package.json. ''' - with open(package_json_path, 'rb') as f: + with open(package_json_path, 'r', encoding='utf-8') as f: data = json.load(f) return data.get('version') From e1a6c248a16f7408f0bb1fc4ad69c16889621b6e Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Mon, 6 Mar 2023 06:23:39 +0100 Subject: [PATCH 4/4] Fix typo. --- changelogs/fragments/100-more-other-projects.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelogs/fragments/100-more-other-projects.yml b/changelogs/fragments/100-more-other-projects.yml index 6fc52ad3..7e7adc02 100644 --- a/changelogs/fragments/100-more-other-projects.yml +++ b/changelogs/fragments/100-more-other-projects.yml @@ -1,5 +1,5 @@ minor_changes: - Allow to extract other project versions for Python projects from PEP 621 conformant ``pyproject.toml`` (https://github.com/ansible-community/antsibull-changelog/pull/100). - - Allow to extract other project versions for JavaScript / TypeScript projects form ``package.json`` + - Allow to extract other project versions for JavaScript / TypeScript projects from ``package.json`` (https://github.com/ansible-community/antsibull-changelog/pull/100).