From 86e5e17faffa2dce3261d83a515c45c0e2d3f199 Mon Sep 17 00:00:00 2001 From: Joel Lefkowitz Date: Sat, 23 Nov 2024 21:13:29 +0000 Subject: [PATCH] Allow missing SCONS_FLAGS keys when registering builds --- package.json | 5 ++++- pyproject.toml | 2 +- src/build.py | 5 ++++- src/containers.py | 16 +++++++++++++--- test/test_containers.py | 7 ++++++- 5 files changed, 28 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index 68a1f9d..ec0afd6 100644 --- a/package.json +++ b/package.json @@ -1,11 +1,14 @@ { "packageManager": "yarn@4.1.1", "scripts": { - "postinstall": "husky" + "postinstall": "husky", + "prepack": "pinst --disable", + "postpack": "pinst --enable" }, "devDependencies": { "cspell": "^8.0.0", "husky": "^9.0.0", + "pinst": "^3.0.0", "prettier": "^3.0.0" } } diff --git a/pyproject.toml b/pyproject.toml index 1cad3b6..74f39e8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -85,7 +85,7 @@ src = "miniscons" lint = [ "npx cspell . --dot --gitignore", "pylint src --disable=C0114,C0115,C0116,C0411,C3001", - "mypy -m src --ignore-missing-import", + "mypy src --ignore-missing-import", "trufflehog3", ] diff --git a/src/build.py b/src/build.py index e8b4253..6d9b4a9 100644 --- a/src/build.py +++ b/src/build.py @@ -31,7 +31,10 @@ def path(self, file: str) -> str: return f"{root.replace('.', '-')}-[{self.name}]" def merge(self, env: Environment) -> dict[str, list[str]]: - merged = {k: unique(env[k] + self.packages.get(k, [])) for k in SCONS_FLAGS} + merged = { + k: unique(env.get(k, []) + self.packages.get(k, [])) for k in SCONS_FLAGS + } + merged["CXXFLAGS"] += self.flags return merged diff --git a/src/containers.py b/src/containers.py index 6ebab0b..2c77d8f 100644 --- a/src/containers.py +++ b/src/containers.py @@ -1,10 +1,20 @@ from functools import reduce +from typing import TypeVar +T = TypeVar("T") +U = TypeVar("U") -def unique(lst: list[str]) -> list[str]: - empty = [] # type: list[str] + +def unique(lst: list[T]) -> list[T]: + empty = [] # type: list[T] return list(reduce(lambda acc, x: acc if x in acc else [*acc, x], lst, empty)) -def flatten(ignore: list[str | list[str]]) -> list[str]: +def flatten(ignore: list[T | list[T]]) -> list[T]: return list(sum(map(lambda x: x if isinstance(x, list) else [x], ignore), [])) + + +def merge_maps( + x: dict[T, list[U]], y: dict[T, list[U]], keys: list[T] +) -> dict[T, list[U]]: + return {k: unique(x.get(k, []) + y.get(k, [])) for k in keys} diff --git a/test/test_containers.py b/test/test_containers.py index 33e6436..a128ba5 100644 --- a/test/test_containers.py +++ b/test/test_containers.py @@ -1,4 +1,4 @@ -from src.containers import flatten, unique +from src.containers import flatten, merge_maps, unique def test_unique(): @@ -7,3 +7,8 @@ def test_unique(): def test_flatten(): assert flatten(["a", ["b", "c"]]) == ["a", "b", "c"] + + +def test_merge_maps(): + assert merge_maps({}, {}, ["a"]) == {"a": []} + assert merge_maps({"a": [1]}, {"a": [2, 3]}, ["a"]) == {"a": [1, 2, 3]}