Skip to content

Commit ae1fa87

Browse files
committed
PR feedback: avoid some code repetition
Signed-off-by: Bernát Gábor <[email protected]>
1 parent 02bf104 commit ae1fa87

File tree

3 files changed

+31
-38
lines changed

3 files changed

+31
-38
lines changed

src/build/__init__.py

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import warnings
1313

1414
from collections import OrderedDict
15-
from typing import AbstractSet, Dict, Iterator, Mapping, Optional, Sequence, Set, Text, Tuple, Union
15+
from typing import AbstractSet, Any, Callable, Dict, Iterator, Mapping, Optional, Sequence, Set, Text, Tuple, Union
1616

1717
import pep517.wrappers
1818
import toml
@@ -44,6 +44,13 @@ class BuildBackendException(Exception):
4444
Exception raised when the backend fails
4545
"""
4646

47+
def __init__(self, exception): # type: (Exception) -> None
48+
super(BuildBackendException, self).__init__()
49+
self.exception = exception # type: Exception
50+
51+
def __repr__(self): # type: () -> str
52+
return 'Backend operation failed: {!r}'.format(self.exception)
53+
4754

4855
class TypoWarning(Warning):
4956
"""
@@ -230,8 +237,8 @@ def get_dependencies(self, distribution, config_settings=None): # type: (str, O
230237
return set(get_requires(config_settings))
231238
except pep517.wrappers.BackendUnavailable:
232239
raise BuildException("Backend '{}' is not available.".format(self._backend))
233-
except Exception as e: # noqa: E722
234-
raise BuildBackendException('Backend operation failed: {}'.format(e))
240+
except Exception as e:
241+
raise BuildBackendException(e)
235242

236243
def check_dependencies(self, distribution, config_settings=None):
237244
# type: (str, Optional[ConfigSettings]) -> Set[Tuple[str, ...]]
@@ -258,25 +265,12 @@ def prepare(self, distribution, output_directory, config_settings=None):
258265
:returns: The full path to the prepared metadata directory
259266
"""
260267
prepare = getattr(self._hook, 'prepare_metadata_for_build_{}'.format(distribution))
261-
outdir = os.path.abspath(output_directory)
262-
263-
if os.path.exists(output_directory):
264-
if not os.path.isdir(output_directory):
265-
raise BuildException("Build path '{}' exists and is not a directory".format(output_directory))
266-
else:
267-
os.mkdir(output_directory)
268-
269268
try:
270-
with _working_directory(self.srcdir):
271-
basename = prepare(outdir, config_settings, _allow_fallback=False) # type: str
272-
path = os.path.join(outdir, basename)
273-
except pep517.wrappers.BackendUnavailable:
274-
raise BuildException("Backend '{}' is not available.".format(self._backend))
275-
except pep517.wrappers.HookMissing:
276-
return None
277-
except Exception as e: # noqa: E722
278-
raise BuildBackendException('Backend operation failed: {!r}'.format(e))
279-
return path
269+
return self._call_backend(prepare, output_directory, config_settings, _allow_fallback=False)
270+
except BuildBackendException as exception:
271+
if isinstance(exception.exception, pep517.wrappers.HookMissing):
272+
return None
273+
raise
280274

281275
def build(self, distribution, output_directory, config_settings=None, metadata_directory=None):
282276
# type: (str, str, Optional[ConfigSettings], Optional[str]) -> str
@@ -291,27 +285,27 @@ def build(self, distribution, output_directory, config_settings=None, metadata_d
291285
:returns: The full path to the built distribution
292286
"""
293287
build = getattr(self._hook, 'build_{}'.format(distribution))
294-
output_directory = os.path.abspath(output_directory)
288+
kwargs = {} if metadata_directory is None else {'metadata_directory': metadata_directory}
289+
return self._call_backend(build, output_directory, config_settings, **kwargs)
295290

296-
if os.path.exists(output_directory):
297-
if not os.path.isdir(output_directory):
298-
raise BuildException("Build path '{}' exists and is not a directory".format(output_directory))
299-
else:
300-
os.mkdir(output_directory)
291+
def _call_backend(self, callback, outdir, config_settings=None, **kwargs):
292+
# type: (Callable[...,str], str, Optional[ConfigSettings], Any) -> str
293+
outdir = os.path.abspath(outdir)
301294

302-
if metadata_directory is not None:
303-
kwargs = {'metadata_directory': metadata_directory}
295+
if os.path.exists(outdir):
296+
if not os.path.isdir(outdir):
297+
raise BuildException("Build path '{}' exists and is not a directory".format(outdir))
304298
else:
305-
kwargs = {}
299+
os.mkdir(outdir)
306300

307301
try:
308302
with _working_directory(self.srcdir):
309-
basename = build(output_directory, config_settings=config_settings, **kwargs) # type: str
310-
return os.path.join(output_directory, basename)
303+
basename = callback(outdir, config_settings, **kwargs) # type: str
304+
return os.path.join(outdir, basename)
311305
except pep517.wrappers.BackendUnavailable:
312306
raise BuildException("Backend '{}' is not available.".format(self._backend))
313-
except Exception as e: # noqa: E722
314-
raise BuildBackendException('Backend operation failed: {!r}'.format(e))
307+
except Exception as exception:
308+
raise BuildBackendException(exception)
315309

316310

317311
__all__ = (

tests/test_main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ def test_build_raises_build_exception(mocker, test_flit_path):
163163
@pytest.mark.isolated
164164
def test_build_raises_build_backend_exception(mocker, test_flit_path):
165165
error = mocker.patch('build.__main__._error')
166-
mocker.patch('build.ProjectBuilder.get_dependencies', side_effect=build.BuildBackendException)
166+
mocker.patch('build.ProjectBuilder.get_dependencies', side_effect=build.BuildBackendException(Exception('a')))
167167
mocker.patch('build.env._IsolatedEnvVenvPip.install')
168168

169169
build.__main__.build_package(test_flit_path, '.', ['sdist'])

tests/test_projectbuilder.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -402,9 +402,8 @@ def test_prepare_no_hook(mocker, tmp_dir, test_flit_path):
402402
mocker.patch('pep517.wrappers.Pep517HookCaller', autospec=True)
403403

404404
builder = build.ProjectBuilder(test_flit_path)
405-
builder._hook.prepare_metadata_for_build_wheel.side_effect = pep517.wrappers.HookMissing(
406-
'prepare_metadata_for_build_wheel'
407-
)
405+
failure = pep517.wrappers.HookMissing('prepare_metadata_for_build_wheel')
406+
builder._hook.prepare_metadata_for_build_wheel.side_effect = failure
408407

409408
assert builder.prepare('wheel', tmp_dir) is None
410409

0 commit comments

Comments
 (0)