Skip to content

Commit efd8808

Browse files
authored
make VFOX_PYTHON_MIRROR variable works (#3)
1 parent da8932b commit efd8808

2 files changed

Lines changed: 25 additions & 16 deletions

File tree

lib/util.lua

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,35 @@
11
local http = require("http")
22
local html = require("html")
33

4+
-- get mirror
45
local PYTHON_URL = "https://www.python.org/ftp/python/"
6+
local VFOX_PYTHON_MIRROR = os.getenv("VFOX_PYTHON_MIRROR")
7+
if VFOX_PYTHON_MIRROR then
8+
PYTHON_URL = VFOX_PYTHON_MIRROR
9+
os.setenv("PYTHON_BUILD_MIRROR_URL_SKIP_CHECKSUM", 1)
10+
os.setenv("PYTHON_BUILD_MIRROR_URL", PYTHON_URL)
11+
end
12+
13+
-- request headers
14+
local REQUEST_HEADERS = {
15+
["User-Agent"] = "vfox"
16+
}
517

18+
-- download source
619
local DOWNLOAD_SOURCE = {
720
--- TODO support zip or web-based installers
8-
WEB_BASED = "https://www.python.org/ftp/python/%s/python-%s%s-webinstall.exe",
9-
ZIP = "https://www.python.org/ftp/python/%s/python-%s-embed-%s.zip",
21+
WEB_BASED = PYTHON_URL .. "%s/python-%s%s-webinstall.exe",
22+
ZIP = PYTHON_URL .. "%s/python-%s-embed-%s.zip",
1023
MSI = "",
1124
--- Currently only exe installers are supported
12-
EXE = "https://www.python.org/ftp/python/%s/python-%s%s.exe",
13-
SOURCE = "https://www.python.org/ftp/python/%s/Python-%s.tar.xz"
25+
EXE = PYTHON_URL .. "%s/python-%s%s.exe",
26+
SOURCE = PYTHON_URL .. "%s/Python-%s.tar.xz"
1427
}
1528

16-
function getMirror()
17-
local mirror = os.getenv("VFOX_PYTHON_MIRROR")
18-
if mirror == nil then
19-
return "https://www.python.org/ftp/python/"
20-
end
21-
return mirror
22-
end
23-
2429
function checkIsReleaseVersion(version)
2530
local resp, err = http.head({
26-
url = DOWNLOAD_SOURCE.SOURCE:format(version, version)
31+
url = DOWNLOAD_SOURCE.SOURCE:format(version, version),
32+
headers = REQUEST_HEADERS
2733
})
2834
if err ~= nil or resp.status_code ~= 200 then
2935
return false
@@ -80,7 +86,8 @@ function checkAvailableReleaseForWindows(version)
8086
--- TODO support zip or web-based installers
8187
local url = DOWNLOAD_SOURCE.EXE:format(version, version, archType)
8288
local resp, err = http.head({
83-
url = url
89+
url = url,
90+
headers = REQUEST_HEADERS
8491
})
8592
if err ~= nil or resp.status_code ~= 200 then
8693
error("No available installer found for current version")
@@ -89,7 +96,8 @@ function checkAvailableReleaseForWindows(version)
8996
end
9097
function parseVersion()
9198
local resp, err = http.get({
92-
url = PYTHON_URL
99+
url = PYTHON_URL,
100+
headers = REQUEST_HEADERS
93101
})
94102
if err ~= nil or resp.status_code ~= 200 then
95103
error("paring release info failed." .. err)
@@ -144,4 +152,4 @@ function compare_versions(v1, v2)
144152
end
145153

146154
return 0
147-
end
155+
end

metadata.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ PLUGIN.minRuntimeVersion = "0.3.0"
2626
PLUGIN.notes = {
2727
"Mirror Setting:",
2828
"You can use VFOX_PYTHON_MIRROR environment variable to set mirror.",
29+
"eg: `export VFOX_PYTHON_MIRROR=https://mirrors.huaweicloud.com/python/`",
2930
" ",
3031
"Others:",
3132
"For Windows, only support >=3.5.0, but no restrictions for unix-like."

0 commit comments

Comments
 (0)