12
12
from typing import TYPE_CHECKING
13
13
from typing import Union
14
14
15
+ from ._integration .pyproject_reading import (
16
+ get_args_for_pyproject as _get_args_for_pyproject ,
17
+ )
18
+ from ._integration .pyproject_reading import read_pyproject as _read_pyproject
15
19
from ._version_cls import NonNormalizedVersion
16
20
from ._version_cls import Version
17
21
from .utils import trace
24
28
DEFAULT_TAG_REGEX = r"^(?:[\w-]+-)?(?P<version>[vV]?\d+(?:\.\d+){0,2}[^\+]*)(?:\+.*)?$"
25
29
DEFAULT_VERSION_SCHEME = "guess-next-dev"
26
30
DEFAULT_LOCAL_SCHEME = "node-and-date"
27
- _ROOT = "root"
28
31
29
32
30
33
def _check_tag_regex (value : str | Pattern [str ] | None ) -> Pattern [str ]:
@@ -47,7 +50,8 @@ def _check_absolute_root(root: _t.PathT, relative_to: _t.PathT | None) -> str:
47
50
if relative_to :
48
51
if (
49
52
os .path .isabs (root )
50
- and not os .path .commonpath ([root , relative_to ]) == relative_to
53
+ and os .path .isabs (relative_to )
54
+ and not os .path .commonpath ([root , relative_to ]) == root
51
55
):
52
56
warnings .warn (
53
57
"absolute root path '%s' overrides relative_to '%s'"
@@ -67,12 +71,6 @@ def _check_absolute_root(root: _t.PathT, relative_to: _t.PathT | None) -> str:
67
71
return os .path .abspath (root )
68
72
69
73
70
- def _lazy_tomli_load (data : str ) -> dict [str , Any ]:
71
- from tomli import loads
72
-
73
- return loads (data )
74
-
75
-
76
74
_VersionT = Union [Version , NonNormalizedVersion ]
77
75
78
76
@@ -202,7 +200,7 @@ def from_file(
202
200
cls ,
203
201
name : str = "pyproject.toml" ,
204
202
dist_name : str | None = None ,
205
- _load_toml : Callable [[str ], dict [str , Any ]] = _lazy_tomli_load ,
203
+ _load_toml : Callable [[str ], dict [str , Any ]] | None = None ,
206
204
** kwargs : Any ,
207
205
) -> Configuration :
208
206
"""
@@ -212,61 +210,7 @@ def from_file(
212
210
not contain the [tool.setuptools_scm] section.
213
211
"""
214
212
215
- with open (name , encoding = "UTF-8" ) as strm :
216
- data = strm . read ( )
213
+ pyproject_data = _read_pyproject (name , _load_toml = _load_toml )
214
+ args = _get_args_for_pyproject ( pyproject_data , dist_name , kwargs )
217
215
218
- defn = _load_toml (data )
219
- try :
220
- section = defn .get ("tool" , {})["setuptools_scm" ]
221
- except LookupError as e :
222
- raise LookupError (
223
- f"{ name } does not contain a tool.setuptools_scm section"
224
- ) from e
225
-
226
- project = defn .get ("project" , {})
227
- dist_name = cls ._cleanup_from_file_args_data (
228
- project , dist_name , kwargs , section
229
- )
230
- return cls (dist_name = dist_name , relative_to = name , ** section , ** kwargs )
231
-
232
- @staticmethod
233
- def _cleanup_from_file_args_data (
234
- project : dict [str , Any ],
235
- dist_name : str | None ,
236
- kwargs : dict [str , Any ],
237
- section : dict [str , Any ],
238
- ) -> str | None :
239
- """drops problematic details and figures the distribution name"""
240
- if "dist_name" in section :
241
- if dist_name is None :
242
- dist_name = section .pop ("dist_name" )
243
- else :
244
- assert dist_name == section ["dist_name" ]
245
- del section ["dist_name" ]
246
- if dist_name is None :
247
- # minimal pep 621 support for figuring the pretend keys
248
- dist_name = project .get ("name" )
249
- if dist_name is None :
250
- dist_name = _read_dist_name_from_setup_cfg ()
251
- if _ROOT in kwargs :
252
- if kwargs [_ROOT ] is None :
253
- kwargs .pop (_ROOT , None )
254
- elif _ROOT in section :
255
- if section [_ROOT ] != kwargs [_ROOT ]:
256
- warnings .warn (
257
- f"root { section [_ROOT ]} is overridden"
258
- f" by the cli arg { kwargs [_ROOT ]} "
259
- )
260
- section .pop ("root" , None )
261
- return dist_name
262
-
263
-
264
- def _read_dist_name_from_setup_cfg () -> str | None :
265
-
266
- # minimal effort to read dist_name off setup.cfg metadata
267
- import configparser
268
-
269
- parser = configparser .ConfigParser ()
270
- parser .read (["setup.cfg" ])
271
- dist_name = parser .get ("metadata" , "name" , fallback = None )
272
- return dist_name
216
+ return cls (relative_to = name , ** args )
0 commit comments