Debian Codegraph @

src/sphinxcontrib/towncrier/_fragment_discovery.py

raw ◀ ▶
1 Debian patch touch this file
1 """Changelog fragment discovery helpers."""
2
3
4 from functools import lru_cache
•5 from importlib.metadata import version
6 from pathlib import Path
7 from typing import Optional, Set
8
9 from sphinx.util import logging
10
•11 from packaging.version import Version
12
13
14 try:
15 # pylint: disable=no-name-in-module
16 from towncrier.build import find_fragments # noqa: WPS433
17 except ImportError:
18 # pylint: disable=import-self,no-name-in-module
19 from towncrier import ( # type: ignore[attr-defined] # noqa: WPS433,WPS440
20 find_fragments,
21 )
22
•23 from ._towncrier import ( # noqa: WPS436
24 get_towncrier_config, get_towncrier_config_as_dict,
25 )
26
27
28 logger = logging.getLogger(__name__)
29
30
31 def _resolve_spec_config(
32 base: Path, spec_name: Optional[str] = None,
33 ) -> Optional[Path]:
34 return base / spec_name if spec_name is not None else None
35
36
37 # pylint: disable=fixme
38 # FIXME: consider consolidating this logic upstream in towncrier
39 def _find_config_file(base: Path) -> Path:
40 """Find the best config file."""
41 candidate_names = 'towncrier.toml', 'pyproject.toml'
42 candidates = list(map(base.joinpath, candidate_names))
43 extant = filter(Path.is_file, candidates)
44 return next(extant, candidates[-1])
45
46
47 # pylint: disable=fixme
48 # FIXME: refactor `lookup_towncrier_fragments` to drop noqas
49 @lru_cache(maxsize=1, typed=True) # noqa: WPS210
50 def lookup_towncrier_fragments( # noqa: WPS210
51 working_dir: Optional[str] = None,
52 config_path: Optional[str] = None,
53 ) -> Set[Path]:
54 """Emit RST-formatted Towncrier changelog fragment paths."""
•55 tc_version = Version(version('towncrier'))
56 if tc_version >= Version('24.7.0'):
57 return _lookup_towncrier_fragments_post24_7(working_dir, config_path)
58 else:
59 return _lookup_towncrier_fragments_pre24_7(working_dir, config_path)
60
61
62 # used for towncrier version 24.7 and above
63 def _lookup_towncrier_fragments_post24_7(
64 working_dir: Optional[str] = None, config_path: Optional[str] = None,
65 ) -> Set[Path]:
66 project_path = Path.cwd() if working_dir is None else Path(working_dir)
67
68 final_config_path = (
69 _resolve_spec_config(project_path, config_path)
70 or _find_config_file(project_path)
71 )
72
73 try:
74 towncrier_config = get_towncrier_config(
75 project_path,
76 final_config_path,
77 )
78 except KeyError as key_err:
79 # NOTE: The error is missing key 'towncrier' or similar
80 logger.warning(
81 f'Missing key {key_err!s} in file {final_config_path!s}',
82 )
83 return set()
84
•85 fragment_base_directory = project_path / (towncrier_config.directory or 'newsfragments')
86
87 _fragments, fragment_filenames = find_fragments(
88 str(fragment_base_directory), towncrier_config, strict=False,
89 )
90
91 return {f[0] for f in fragment_filenames}
92
93
94 # used for versions of towncrier before 24.7
95 def _lookup_towncrier_fragments_pre24_7(
96 working_dir: Optional[str] = None, config_path: Optional[str] = None,
97 ) -> Set[Path]:
98 project_path = Path.cwd() if working_dir is None else Path(working_dir)
99
100 final_config_path = (
101 _resolve_spec_config(project_path, config_path)
102 or _find_config_file(project_path)
103 )
104
105 try:
106 towncrier_config = get_towncrier_config_as_dict(
107 project_path,
108 final_config_path,
109 )
110 except KeyError as key_err:
111 # NOTE: The error is missing key 'towncrier' or similar
112 logger.warning(
113 f'Missing key {key_err!s} in file {final_config_path!s}',
114 )
115 return set()
116
117 fragment_directory: Optional[str] = 'newsfragments'
118 try:
119 fragment_base_directory = project_path / towncrier_config['directory']
120 except KeyError:
121 assert fragment_directory is not None
122 fragment_base_directory = project_path / fragment_directory
123 else:
124 fragment_directory = None
125
126 _fragments, fragment_filenames = find_fragments(
127 str(fragment_base_directory),
128 towncrier_config['sections'],
129 fragment_directory,
130 towncrier_config['types'],
131 )
132 return set(fragment_filenames)