From a52157963522fb643fe94c3b8f10e493b74ccec8 Mon Sep 17 00:00:00 2001 From: Jacob Faibussowitsch Date: Tue, 13 May 2025 18:46:21 -0400 Subject: [PATCH 1/2] Move select_arch_color.py to aedifix --- pyproject.toml | 3 ++ src/aedifix/scripts/select_arch_color.py | 56 ++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100755 src/aedifix/scripts/select_arch_color.py diff --git a/pyproject.toml b/pyproject.toml index 59de58e..f9417bf 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -34,6 +34,9 @@ test = [ [project.urls] homepage = "https://github.com/nv-legate/aedifix" +[project.scripts] +aedifix-select-arch-color = "aedifix.scripts.select_arch_color:main" + [tool.setuptools_scm] write_to = "src/aedifix/_version.py" diff --git a/src/aedifix/scripts/select_arch_color.py b/src/aedifix/scripts/select_arch_color.py new file mode 100755 index 0000000..17fa7fc --- /dev/null +++ b/src/aedifix/scripts/select_arch_color.py @@ -0,0 +1,56 @@ +#!/usr/bin/env python3 +# SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. +# All rights reserved. +# SPDX-License-Identifier: Apache-2.0 +from __future__ import annotations + + +def select_arch_color(arch_value: str) -> str: + r"""Chooses a random color based on the value of arch_value. This helps to + visually distinguish between different arches when recompiling. + + Parameters + ---------- + arch_value : str + The arch value, or any string really. + + Returns + ------- + str + The chosen color. + """ + from zlib import adler32 + + colors = ("red", "green", "yellow", "blue", "magenta", "cyan", "normal") + + # Use zlib.adler32 instead of builtin hash() because the builtin hash() is + # not reproducible. Running hash("the same string") on different + # invocations of Python will yield different results every time. We don't + # want that, because we want the same arch value to have the same color + # each time: + # + # $ python3 -c 'print(hash("foo"))' + # 673628788748047246 + # $ python3 -c 'print(hash("foo"))' + # -6413379324416539761 + # + # With adler32: + # $ python3 -c 'import zlib; print(zlib.adler32("foo".encode()))' + # 42074437 + # $ python3 -c 'import zlib; print(zlib.adler32("foo".encode()))' + # 42074437 + return colors[adler32(arch_value.encode()) % len(colors)] + + +def main() -> None: + import sys + + if len(sys.argv) < 2: # noqa: PLR2004 + print(f"usage: {sys.argv[0]} ARCH_NAME") # noqa: T201 + sys.exit(1) + + print(select_arch_color(sys.argv[1]), end="") # noqa: T201 + + +if __name__ == "__main__": + main() From e94e38cea5687ad3ea56af3c580e44262453d540 Mon Sep 17 00:00:00 2001 From: Jacob Faibussowitsch Date: Tue, 13 May 2025 18:48:24 -0400 Subject: [PATCH 2/2] fixup! Move select_arch_color.py to aedifix --- src/aedifix/scripts/select_arch_color.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/aedifix/scripts/select_arch_color.py b/src/aedifix/scripts/select_arch_color.py index 17fa7fc..11d5fd6 100755 --- a/src/aedifix/scripts/select_arch_color.py +++ b/src/aedifix/scripts/select_arch_color.py @@ -4,6 +4,8 @@ # SPDX-License-Identifier: Apache-2.0 from __future__ import annotations +import sys + def select_arch_color(arch_value: str) -> str: r"""Chooses a random color based on the value of arch_value. This helps to @@ -42,15 +44,15 @@ def select_arch_color(arch_value: str) -> str: return colors[adler32(arch_value.encode()) % len(colors)] -def main() -> None: - import sys - +def main() -> int: if len(sys.argv) < 2: # noqa: PLR2004 print(f"usage: {sys.argv[0]} ARCH_NAME") # noqa: T201 - sys.exit(1) + return 1 - print(select_arch_color(sys.argv[1]), end="") # noqa: T201 + color = select_arch_color(sys.argv[1]) + print(color, end="") # noqa: T201 + return 0 if __name__ == "__main__": - main() + sys.exit(main())