diff --git a/metal/setup_mac.py b/metal/setup_mac.py index f263809a..575aa893 100644 --- a/metal/setup_mac.py +++ b/metal/setup_mac.py @@ -498,6 +498,9 @@ def main(argv=None) -> int: ap.add_argument("--models-dir") ap.add_argument("--gguf-dir") ap.add_argument("--yes", action="store_true") + ap.add_argument("--source", choices=S.SOURCES, default=None, + help="where model files come from: auto (huggingface), huggingface, modelscope; " + "same as STRATA_SOURCE") ap.add_argument("--setup", action="store_true") ap.add_argument("--no-start", action="store_true") ap.add_argument("--check", action="store_true") @@ -508,6 +511,11 @@ def main(argv=None) -> int: a, rest = ap.parse_known_args(argv) if rest: S.warn("not used on a Mac: " + " ".join(rest)) + # setup.py hands a Mac over before its own parser runs, so --source has to be honoured here too: + # source_hint() recommends it on every platform, and a Mac that dropped it failed the download + # with the same unreachable host (#2363). + if a.source: + os.environ["STRATA_SOURCE"] = a.source S.say("Strata on a Mac (Apple Silicon, Metal)") data, elsewhere = S.data_folder(a.data_dir) roots = [data, *elsewhere] diff --git a/tools/test_setup_mac.py b/tools/test_setup_mac.py index 9f887eb8..5a1e6c15 100644 --- a/tools/test_setup_mac.py +++ b/tools/test_setup_mac.py @@ -6,8 +6,10 @@ PC: sysctl / sw_vers / xcode-select are mocked. No GPU, no network. """ from __future__ import annotations +import os import subprocess import sys +import tempfile import unittest from pathlib import Path from unittest import mock @@ -249,5 +251,27 @@ class RepackGuard(unittest.TestCase): self.assertNotIn("mmap", src) # no dirty mapping of the output +class Source(unittest.TestCase): + """setup.py hands a Mac over before its own parser runs, so the Mac parser has to know --source: + source_hint() recommends it on every platform (#2363).""" + + def test_the_source_flag_reaches_strata_source(self): + env = mock.patch.dict(os.environ, {}, clear=False) + env.start() + self.addCleanup(env.stop) + os.environ.pop("STRATA_SOURCE", None) + warnings = [] + with tempfile.TemporaryDirectory() as tmp, \ + mock.patch("subprocess.run", fake_run(M5)), \ + mock.patch.object(setup, "warn", lambda *a, **k: warnings.append(" ".join(str(x) for x in a))), \ + mock.patch.object(setup, "say", lambda *a, **k: None), \ + mock.patch.object(setup, "ok", lambda *a, **k: None), \ + mock.patch.object(setup, "step", lambda *a, **k: None): + M.main(["--source", "modelscope", "--yes", "--check", "--data-dir", tmp]) + self.assertEqual(os.environ.get("STRATA_SOURCE"), "modelscope") + self.assertFalse([w for w in warnings if "--source" in w], + f"--source was not used: {warnings}") + + if __name__ == "__main__": unittest.main()