1"""
2PEP-517 compliant buildsystem API
3"""
4import logging
5
6from typing import Any
7from typing import Dict
8from typing import List
9from typing import Optional
10
11from poetry.core.factory import Factory
12from poetry.core.utils._compat import Path
13from poetry.core.utils._compat import unicode
14
15from .builders.sdist import SdistBuilder
16from .builders.wheel import WheelBuilder
17
18
19log = logging.getLogger(__name__)
20
21
22def get_requires_for_build_wheel(
23    config_settings=None,
24):  # type: (Optional[Dict[str, Any]]) -> List[str]
25    """
26    Returns an additional list of requirements for building, as PEP508 strings,
27    above and beyond those specified in the pyproject.toml file.
28
29    This implementation is optional. At the moment it only returns an empty list, which would be the same as if
30    not define. So this is just for completeness for future implementation.
31    """
32
33    return []
34
35
36# For now, we require all dependencies to build either a wheel or an sdist.
37get_requires_for_build_sdist = get_requires_for_build_wheel
38
39
40def prepare_metadata_for_build_wheel(
41    metadata_directory, config_settings=None
42):  # type: (str, Optional[Dict[str, Any]]) -> str
43    poetry = Factory().create_poetry(Path(".").resolve(), with_dev=False)
44    builder = WheelBuilder(poetry)
45
46    dist_info = Path(metadata_directory, builder.dist_info)
47    dist_info.mkdir(parents=True, exist_ok=True)
48
49    if "scripts" in poetry.local_config or "plugins" in poetry.local_config:
50        with (dist_info / "entry_points.txt").open("w", encoding="utf-8") as f:
51            builder._write_entry_points(f)
52
53    with (dist_info / "WHEEL").open("w", encoding="utf-8") as f:
54        builder._write_wheel_file(f)
55
56    with (dist_info / "METADATA").open("w", encoding="utf-8") as f:
57        builder._write_metadata_file(f)
58
59    return dist_info.name
60
61
62def build_wheel(
63    wheel_directory, config_settings=None, metadata_directory=None
64):  # type: (str, Optional[Dict[str, Any]], Optional[str]) -> str
65    """Builds a wheel, places it in wheel_directory"""
66    poetry = Factory().create_poetry(Path(".").resolve(), with_dev=False)
67
68    return unicode(WheelBuilder.make_in(poetry, Path(wheel_directory)))
69
70
71def build_sdist(
72    sdist_directory, config_settings=None
73):  # type: (str, Optional[Dict[str, Any]]) -> str
74    """Builds an sdist, places it in sdist_directory"""
75    poetry = Factory().create_poetry(Path(".").resolve(), with_dev=False)
76
77    path = SdistBuilder(poetry).build(Path(sdist_directory))
78
79    return unicode(path.name)
80