Skip to content

fix(tools): reuse the SDK's region instead of probing the network - #100

Open
shiliu-yang wants to merge 2 commits into
masterfrom
fix/reuse-sdk-country-code
Open

shiliu-yang wants to merge 2 commits into
masterfrom
fix/reuse-sdk-country-code

Conversation

@shiliu-yang

Copy link
Copy Markdown
Contributor

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 requests outside t5_os/.

Why

tools/util.py imported requests at module scope for a single call:

response = requests.get('http://www.ip-api.com/json', timeout=5)

tools/util.py is imported by both platform_prepare.py and build_example.py before 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 bare python resolves to uv's base interpreter under .tools/python/<ver>/ rather than the SDK's .venv — prepare died with ModuleNotFoundError: 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:

  • The SDK already makes exactly this decision, and now hands the result down in OPEN_COUNTRY_CODE. We were re-deriving information we had been given.
  • Choosing a download mirror should not itself depend on an external service being reachable. When ip-api.com was slow or blocked, the except swallowed it, COUNTRY_CODE stayed empty, and every user silently fell through to the overseas host — the exact case the CN mirror exists for.
  • Every prepare paid up to a 5s timeout for information that is now free.

The toolchain download itself has always used urllib.request, so nothing else needed requests.

Changes

tools/util.py — set_country_code() reads OPEN_COUNTRY_CODE first 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 requests removed.

tools/get_conutry.py — toolchain_get.sh:49 makes 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

requests is no longer reachable from the prepare path:

$ python3 -c "import sys, tools.util, tools.download_toolchain; print('requests' in sys.modules)"
False          # before this change: True

Region resolution and the mirror it selects:

OPEN_COUNTRY_CODE get_country_code() get_conutry.py toolchain host
unset China (from timezone) 1 —
China China (from SDK) 1 images.tuyacn.com
Other Other (from SDK) 2 armkeil.blob.core.windows.net

Timezone 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 of get_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.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant