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 4558013 commit f6750b8
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 28 deletions.
12 changes: 6 additions & 6 deletions cibuildwheel/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,15 +153,15 @@ def main() -> None:
archs_config_str = options("archs") if args.archs is None else args.archs

environment_config = options("environment")
before_all = options("before-all")
before_build = options("before-build")
repair_command = options("repair-wheel-command")
before_all = options("before-all", sep=" && ")
before_build = options("before-build", sep=" && ")
repair_command = options("repair-wheel-command", sep=" && ")

dependency_versions = options("dependency-versions")
test_command = options("test-command")
before_test = options("before-test")
test_command = options("test-command", sep=" && ")
before_test = options("before-test", sep=" && ")
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
13 changes: 8 additions & 5 deletions cibuildwheel/resources/defaults.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ build = "*"
skip = ""
test-skip = ""

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

before-all = ""
Expand All @@ -14,8 +14,8 @@ repair-wheel-command = ""

test-command = ""
before-test = ""
test-requires = ""
test-extras = ""
test-requires = []
test-extras = []


[tool.cibuildwheel.manylinux]
Expand All @@ -33,6 +33,9 @@ pypy_aarch64-image = "manylinux2014"
repair-wheel-command = "auditwheel repair -w {dest_dir} {wheel}"

[tool.cibuildwheel.macos]
repair-wheel-command = "delocate-listdeps {wheel} && delocate-wheel --require-archs {delocate_archs} -w {dest_dir} {wheel}"
repair-wheel-command = [
"delocate-listdeps {wheel}",
"delocate-wheel --require-archs {delocate_archs} -w {dest_dir} {wheel}",
]

[tool.cibuildwheel.windows]
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 f6750b8

Please sign in to comment.