fix(tools): reuse the SDK's region instead of probing the network - #100
Open
shiliu-yang wants to merge 2 commits into
Open
shiliu-yang wants to merge 2 commits into
shiliu-yang wants to merge 2 commits into
Conversation
Picking a toolchain mirror asked http://www.ip-api.com/json which country the machine is in, which made `requests` an import-time dependency of the whole prepare path: tools/util.py is imported by platform_prepare.py and build_example.py before either does any work. When the SDK invoked us with an interpreter that did not have it -- on Windows a bare `python` resolves to uv's base interpreter rather than the SDK's .venv -- prepare died on `ModuleNotFoundError: No module named 'requests'` before running a line. The SDK already makes this decision and now hands the result down in OPEN_COUNTRY_CODE. Read it, and treat any non-empty value as authoritative: "Other" means overseas, not "unknown". Older SDKs don't set it, so fall back to the timezone check the SDK itself uses rather than to the web service -- choosing a download mirror should not depend on an external service being reachable, and a 5s timeout on every prepare was paid for information that is now free. toolchain_get.sh makes the same choice through tools/get_conutry.py, so that honours the hand-off too and the two paths cannot disagree. Drops the last use of `requests` outside t5_os/; the toolchain download itself has always used urllib.request.
The early return duplicated the print at the end of get_country_code(). Assign MORROR in both branches and let control reach the single print, which is also how the timezone path already worked.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Stop probing a web service to decide which toolchain mirror to download from. Read the region the SDK already detected, and fall back to a timezone check rather than the network.
This drops the last use of
requestsoutsidet5_os/.Why
tools/util.pyimportedrequestsat module scope for a single call:tools/util.pyis imported by bothplatform_prepare.pyandbuild_example.pybefore either does any work, so that one convenience turned a third-party package into a hard requirement of the entire prepare path. When the SDK invoked us with an interpreter that did not have it — on Windows a barepythonresolves to uv's base interpreter under.tools/python/<ver>/rather than the SDK's.venv— prepare died withModuleNotFoundError: No module named 'requests'before running a line of its own logic.The SDK side of that is fixed separately (tuya/TuyaOpen,
fix/t5-build-env), but the dependency should not have been on this path to begin with:OPEN_COUNTRY_CODE. We were re-deriving information we had been given.ip-api.comwas slow or blocked, theexceptswallowed it,COUNTRY_CODEstayed empty, and every user silently fell through to the overseas host — the exact case the CN mirror exists for.The toolchain download itself has always used
urllib.request, so nothing else neededrequests.Changes
tools/util.py—set_country_code()readsOPEN_COUNTRY_CODEfirst and treats any non-empty value as authoritative:"Other"means overseas, not "unknown" (an empty string is indistinguishable from unset, so the SDK exports a definite value). Older SDKs don't set it, so the fallback is the same timezone check the SDK uses.import requestsremoved.tools/get_conutry.py—toolchain_get.sh:49makes the same decision through this script, so it honours the hand-off too and the two paths cannot disagree. Its timezone fallback is unchanged.Verification
requestsis no longer reachable from the prepare path:Region resolution and the mirror it selects:
OPEN_COUNTRY_CODEget_country_code()get_conutry.pyChina(from timezone)1ChinaChina(from SDK)1images.tuyacn.comOtherOther(from SDK)2armkeil.blob.core.windows.netTimezone rows were produced in UTC+8;
download_toolchain.get_toolchain_package_info()compares against"China", so"Other"takes the overseas branch exactly as an empty string used to.Compatibility
Works unchanged against SDKs that do not set
OPEN_COUNTRY_CODE. No change to the public shape ofget_country_code()— the only difference visible to callers is that overseas now returns"Other"instead of"", and the sole comparison in this repo (== "China") is unaffected.