12
12
import warnings
13
13
14
14
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
16
16
17
17
import pep517 .wrappers
18
18
import toml
@@ -44,6 +44,13 @@ class BuildBackendException(Exception):
44
44
Exception raised when the backend fails
45
45
"""
46
46
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
+
47
54
48
55
class TypoWarning (Warning ):
49
56
"""
@@ -230,8 +237,8 @@ def get_dependencies(self, distribution, config_settings=None): # type: (str, O
230
237
return set (get_requires (config_settings ))
231
238
except pep517 .wrappers .BackendUnavailable :
232
239
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 )
235
242
236
243
def check_dependencies (self , distribution , config_settings = None ):
237
244
# type: (str, Optional[ConfigSettings]) -> Set[Tuple[str, ...]]
@@ -258,25 +265,12 @@ def prepare(self, distribution, output_directory, config_settings=None):
258
265
:returns: The full path to the prepared metadata directory
259
266
"""
260
267
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
-
269
268
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
280
274
281
275
def build (self , distribution , output_directory , config_settings = None , metadata_directory = None ):
282
276
# type: (str, str, Optional[ConfigSettings], Optional[str]) -> str
@@ -291,27 +285,27 @@ def build(self, distribution, output_directory, config_settings=None, metadata_d
291
285
:returns: The full path to the built distribution
292
286
"""
293
287
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 )
295
290
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 )
301
294
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 ))
304
298
else :
305
- kwargs = {}
299
+ os . mkdir ( outdir )
306
300
307
301
try :
308
302
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 )
311
305
except pep517 .wrappers .BackendUnavailable :
312
306
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 )
315
309
316
310
317
311
__all__ = (
0 commit comments