Skip to content

Commit

Permalink
fix: support tables and arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
henryiii committed May 27, 2021
1 parent 706ba88 commit f8003be
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 20 deletions.
2 changes: 1 addition & 1 deletion cibuildwheel/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def main() -> None:
test_command = options("test-command")
before_test = options("before-test")
test_requires = options("test-requires").split()
test_extras = options("test-extras")
test_extras = options("test-extras", sep=",")
build_verbosity_str = options("build-verbosity")

package_files = {"setup.py", "setup.cfg", "pyproject.toml"}
Expand Down
28 changes: 19 additions & 9 deletions cibuildwheel/options.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
import os
from pathlib import Path
from typing import Any, Dict, Mapping, Tuple
from typing import Any, Dict, List, Mapping, Tuple, Union

import toml

from .typing import PLATFORMS

DIR = Path(__file__).parent.resolve()

Setting = str

# These keys are allowed to merge; setting one will not go away if another is
# set in a overriding file. tool.cibuildwheel.manylinux.X will not remove
# tool.cibuildwheel.manylinux.Y from defaults, for example.
ALLOWED_TO_MERGE = {"manylinux"}


Setting = Union[Dict[str, str], List[str], str]


class ConfigOptionError(KeyError):
pass


def _dig_first(*pairs: Tuple[Mapping[str, Setting], str]) -> Setting:
def _dig_first(*pairs: Tuple[Mapping[str, Any], str]) -> Setting:
"""
Return the first dict item that matches from pairs of dicts and keys.
Final result is will throw a KeyError if missing.
Expand Down Expand Up @@ -120,9 +121,11 @@ def _load_file(self, filename: Path, *, update: bool) -> None:

self._update(self.config, tool_cibuildwheel, update=update)

def __call__(self, name: str, *, env_plat: bool = True) -> Setting:
def __call__(self, name: str, *, env_plat: bool = True, sep: str = " ") -> str:
"""
Get and return envvar for name or the override or the default.
Get and return envvar for name or the override or the default. If env_plat is False,
then don't accept platform versions of the environment variable. If this is an array
or a dict, it will be merged with sep before returning.
"""
config = self.config

Expand All @@ -138,17 +141,24 @@ def __call__(self, name: str, *, env_plat: bool = True) -> Setting:

# Environment variable form
envvar = f"CIBW_{name.upper().replace('-', '_').replace('.', '_')}"
plat_envvar = f"{envvar}_{self.platform.upper()}"

# Let environment variable override setting in config
if env_plat:
plat_envvar = f"{envvar}_{self.platform.upper()}"
return _dig_first(
result = _dig_first(
(os.environ, plat_envvar),
(os.environ, envvar),
(config, key),
)
else:
return _dig_first(
result = _dig_first(
(os.environ, envvar),
(config, key),
)

if isinstance(result, dict):
return sep.join(f'{k}="{v}"' for k, v in result.items())
elif isinstance(result, list):
return sep.join(result)
else:
return result
4 changes: 2 additions & 2 deletions cibuildwheel/resources/defaults.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ test-skip = ""

archs = "auto"
dependency-versions = "pinned"
environment = ""
environment = {}
build-verbosity = ""

before-all = ""
Expand All @@ -15,7 +15,7 @@ repair-wheel-command = ""
test-command = ""
before-test = ""
test-requires = ""
test-extras = ""
test-extras = []


[tool.cibuildwheel.manylinux]
Expand Down
16 changes: 8 additions & 8 deletions unit_test/options_toml_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
PYPROJECT_1 = """
[tool.cibuildwheel]
build = "cp39*"
environment = {THING = "OTHER", FOO="BAR"}
test-command = "pyproject"
test-requires = "something"
test-extras = ["one", "two"]
[tool.cibuildwheel.manylinux]
x86_64-image = "manylinux1"
Expand All @@ -16,7 +18,7 @@
test-requires = "else"
[tool.cibuildwheel.linux]
test-requires = "other"
test-requires = ["other", "many"]
"""


Expand All @@ -37,9 +39,12 @@ def test_simple_settings(tmp_path, platform):
assert options("archs") == "auto"
assert (
options("test-requires")
== {"windows": "something", "macos": "else", "linux": "other"}[platform]
== {"windows": "something", "macos": "else", "linux": "other many"}[platform]
)

assert options("environment") == 'THING="OTHER" FOO="BAR"'
assert options("test-extras", sep=",") == "one,two"

assert options("manylinux.x86_64-image") == "manylinux1"
assert options("manylinux.i686-image") == "manylinux2010"

Expand All @@ -58,12 +63,7 @@ def test_envvar_override(tmp_path, platform, monkeypatch):

assert options("archs") == "auto"

assert (
options(
"build",
)
== "cp38*"
)
assert options("build") == "cp38*"
assert options("manylinux.x86_64-image") == "manylinux2014"
assert options("manylinux.i686-image") == "manylinux2010"

Expand Down

0 comments on commit f8003be

Please sign in to comment.