| 1 |
From: Ben Rowland <[email protected]> |
| 2 |
Date: Fri, 8 Nov 2024 16:10:57 +0000 |
| 3 |
Subject: fix: make compatible with towncrier>=24.7 |
| 4 |
|
| 5 |
towncrier 24.7 changed the way that its find_fragments() function works |
| 6 |
to accept a Config dataclass instead of specific components of the |
| 7 |
config. This commit adds a new version of lookup_towncrier_fragments() |
| 8 |
to use the new API and chooses which one to use based on the version |
| 9 |
of towncrier obtained from importlib.metadata. It also slightly |
| 10 |
changes the way that towncrier config can be obtained: the previous |
| 11 |
get_towncrier_config() function (which returned a dict) is now renamed |
| 12 |
as get_towncrier_config_as_dict() and get_towncrier_config() returns |
| 13 |
the new Config dataclass object for towncrier versions above 22.12.0rc1 |
| 14 |
and raises NotImplementedError for older versions. This API change |
| 15 |
should be ok as get_towncrier_config() was only called in one place |
| 16 |
before. |
| 17 |
|
| 18 |
Origin: other, https://github.com/sphinx-contrib/sphinxcontrib-towncrier/pull/96 |
| 19 |
Bug: https://github.com/sphinx-contrib/sphinxcontrib-towncrier/issues/92 |
| 20 |
Bug-Debian: https://bugs.debian.org/1088147 |
| 21 |
Last-Update: 2024-11-24 |
| 22 |
--- |
| 23 |
src/sphinxcontrib/towncrier/_fragment_discovery.py | 50 +++++++++++++++++++++- |
| 24 |
src/sphinxcontrib/towncrier/_towncrier.py | 16 +++++++ |
| 25 |
2 files changed, 65 insertions(+), 1 deletion(-) |
| 26 |
|
| 27 |
diff --git a/src/sphinxcontrib/towncrier/_fragment_discovery.py b/src/sphinxcontrib/towncrier/_fragment_discovery.py |
| 28 |
index e4836d5..fdcb30c 100644 |
| 29 |
--- a/src/sphinxcontrib/towncrier/_fragment_discovery.py |
| 30 |
+++ b/src/sphinxcontrib/towncrier/_fragment_discovery.py |
| 31 |
@@ -2,11 +2,14 @@ |
| 32 |
|
| 33 |
|
| 34 |
from functools import lru_cache |
| 35 |
+from importlib.metadata import version |
| 36 |
from pathlib import Path |
| 37 |
from typing import Optional, Set |
| 38 |
|
| 39 |
from sphinx.util import logging |
| 40 |
|
| 41 |
+from packaging.version import Version |
| 42 |
+ |
| 43 |
|
| 44 |
try: |
| 45 |
# pylint: disable=no-name-in-module |
| 46 |
@@ -17,7 +20,9 @@ except ImportError: |
| 47 |
find_fragments, |
| 48 |
) |
| 49 |
|
| 50 |
-from ._towncrier import get_towncrier_config # noqa: WPS436 |
| 51 |
+from ._towncrier import ( # noqa: WPS436 |
| 52 |
+ get_towncrier_config, get_towncrier_config_as_dict, |
| 53 |
+) |
| 54 |
|
| 55 |
|
| 56 |
logger = logging.getLogger(__name__) |
| 57 |
@@ -47,6 +52,17 @@ def lookup_towncrier_fragments( # noqa: WPS210 |
| 58 |
config_path: Optional[str] = None, |
| 59 |
) -> Set[Path]: |
| 60 |
"""Emit RST-formatted Towncrier changelog fragment paths.""" |
| 61 |
+ tc_version = Version(version('towncrier')) |
| 62 |
+ if tc_version >= Version('24.7.0'): |
| 63 |
+ return _lookup_towncrier_fragments_post24_7(working_dir, config_path) |
| 64 |
+ else: |
| 65 |
+ return _lookup_towncrier_fragments_pre24_7(working_dir, config_path) |
| 66 |
+ |
| 67 |
+ |
| 68 |
+# used for towncrier version 24.7 and above |
| 69 |
+def _lookup_towncrier_fragments_post24_7( |
| 70 |
+ working_dir: Optional[str] = None, config_path: Optional[str] = None, |
| 71 |
+) -> Set[Path]: |
| 72 |
project_path = Path.cwd() if working_dir is None else Path(working_dir) |
| 73 |
|
| 74 |
final_config_path = ( |
| 75 |
@@ -66,6 +82,38 @@ def lookup_towncrier_fragments( # noqa: WPS210 |
| 76 |
) |
| 77 |
return set() |
| 78 |
|
| 79 |
+ fragment_base_directory = project_path / (towncrier_config.directory or 'newsfragments') |
| 80 |
+ |
| 81 |
+ _fragments, fragment_filenames = find_fragments( |
| 82 |
+ str(fragment_base_directory), towncrier_config, strict=False, |
| 83 |
+ ) |
| 84 |
+ |
| 85 |
+ return {f[0] for f in fragment_filenames} |
| 86 |
+ |
| 87 |
+ |
| 88 |
+# used for versions of towncrier before 24.7 |
| 89 |
+def _lookup_towncrier_fragments_pre24_7( |
| 90 |
+ working_dir: Optional[str] = None, config_path: Optional[str] = None, |
| 91 |
+) -> Set[Path]: |
| 92 |
+ project_path = Path.cwd() if working_dir is None else Path(working_dir) |
| 93 |
+ |
| 94 |
+ final_config_path = ( |
| 95 |
+ _resolve_spec_config(project_path, config_path) |
| 96 |
+ or _find_config_file(project_path) |
| 97 |
+ ) |
| 98 |
+ |
| 99 |
+ try: |
| 100 |
+ towncrier_config = get_towncrier_config_as_dict( |
| 101 |
+ project_path, |
| 102 |
+ final_config_path, |
| 103 |
+ ) |
| 104 |
+ except KeyError as key_err: |
| 105 |
+ # NOTE: The error is missing key 'towncrier' or similar |
| 106 |
+ logger.warning( |
| 107 |
+ f'Missing key {key_err!s} in file {final_config_path!s}', |
| 108 |
+ ) |
| 109 |
+ return set() |
| 110 |
+ |
| 111 |
fragment_directory: Optional[str] = 'newsfragments' |
| 112 |
try: |
| 113 |
fragment_base_directory = project_path / towncrier_config['directory'] |
| 114 |
diff --git a/src/sphinxcontrib/towncrier/_towncrier.py b/src/sphinxcontrib/towncrier/_towncrier.py |
| 115 |
index ecba95e..498976d 100644 |
| 116 |
--- a/src/sphinxcontrib/towncrier/_towncrier.py |
| 117 |
+++ b/src/sphinxcontrib/towncrier/_towncrier.py |
| 118 |
@@ -1,9 +1,12 @@ |
| 119 |
"""Towncrier related shims.""" |
| 120 |
|
| 121 |
from contextlib import suppress as _suppress_exception |
| 122 |
+from importlib.metadata import version |
| 123 |
from pathlib import Path |
| 124 |
from typing import Any, Dict, Union |
| 125 |
|
| 126 |
+from packaging.version import Version |
| 127 |
+ |
| 128 |
|
| 129 |
with _suppress_exception(ImportError): |
| 130 |
# NOTE: This will not raise an exception under Python >= 3.7, and is only |
| 131 |
@@ -25,6 +28,19 @@ except ImportError: |
| 132 |
|
| 133 |
|
| 134 |
def get_towncrier_config( |
| 135 |
+ project_path: Path, |
| 136 |
+ final_config_path: Union[Path, None], |
| 137 |
+): |
| 138 |
+ tc_version = Version(version('towncrier')) |
| 139 |
+ if tc_version >= Version('22.12.0rc1'): |
| 140 |
+ return load_config_from_file(str(project_path), str(final_config_path)) |
| 141 |
+ raise NotImplementedError( |
| 142 |
+ 'Towncrier Config is not available before version 22.12.0rc1, consider ' |
| 143 |
+ 'using "get_towncrier_config_as_dict()" instead', |
| 144 |
+ ) |
| 145 |
+ |
| 146 |
+ |
| 147 |
+def get_towncrier_config_as_dict( |
| 148 |
project_path: Path, |
| 149 |
final_config_path: Union[Path, None], |
| 150 |
) -> Dict[str, Any]: # FIXME: add a better type # pylint: disable=fixme |