forked from version-fox/vfox-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.lua
More file actions
320 lines (287 loc) · 10.3 KB
/
Copy pathutil.lua
File metadata and controls
320 lines (287 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
local http = require("http")
local html = require("html")
-- get mirror
local PYTHON_URL = "https://www.python.org/ftp/python/"
local VFOX_PYTHON_MIRROR = os.getenv("VFOX_PYTHON_MIRROR")
if VFOX_PYTHON_MIRROR then
PYTHON_URL = VFOX_PYTHON_MIRROR
os.setenv("PYTHON_BUILD_MIRROR_URL_SKIP_CHECKSUM", 1)
os.setenv("PYTHON_BUILD_MIRROR_URL", PYTHON_URL)
end
-- request headers
local REQUEST_HEADERS = {
["User-Agent"] = "vfox"
}
-- download source
local DOWNLOAD_SOURCE = {
MSI = PYTHON_URL .. "%s/python-%s.%s.msi",
EXE = PYTHON_URL .. "%s/python-%s%s.exe",
SOURCE = PYTHON_URL .. "%s/Python-%s.tar",
}
function checkIsReleaseVersion(version)
local resp, err = http.head({
url = DOWNLOAD_SOURCE.SOURCE:format(version, version) .. '.xz',
headers = REQUEST_HEADERS
})
if err == nil and resp.status_code == 200 then
return true
end
local resp, err = http.head({
url = DOWNLOAD_SOURCE.SOURCE:format(version, version) .. '.bz2',
headers = REQUEST_HEADERS
})
if err == nil and resp.status_code == 200 then
return true
end
return false
end
function windowsInstall(ctx)
local sdkInfo = ctx.sdkInfo['python']
local path = sdkInfo.path
local version = sdkInfo.version
local url = getReleaseForWindows(version)
local filename = url:match("[^/\\]+$")
if string.sub(filename, -3) == "msi" then
windowsInstallMsi(path, url, version, filename)
elseif string.sub(filename, -3) == "exe" then
windowsInstallExe(path, url, version, filename)
end
end
function windowsInstallMsi(path, url, version, filename)
-- WARNNING:
-- The msi installer for python 2.x must be downloaded to another directory
-- it cannot be installed in the current directory.
local qInstallFile = RUNTIME.pluginDirPath .. "\\" .. filename
local qInstallPath = path
-- download
print("Downloading installer...")
print("from:\t" .. url)
print("to:\t" .. qInstallFile)
local err = http.download_file({
url = url,
headers = REQUEST_HEADERS
}, qInstallFile)
if err ~= nil then
error("Downloading installer failed")
end
-- Install msi
print("Installing python...")
local command = 'msiexec /quiet /a ' .. qInstallFile .. ' TargetDir=' .. qInstallPath
local exitCode = os.execute(command)
os.remove(qInstallFile)
if exitCode ~= 0 then
error("Install msi failed: " .. qInstallFile)
end
-- Install pip
local ensurepipPath = qInstallPath .. "\\Lib\\ensurepip\\__init__.py"
local file = io.open(ensurepipPath, "r")
if file then
io.close(file)
print("Installing pip...")
local command = qInstallPath .. '\\python -E -s -m ensurepip -U --default-pip > NUL'
local exitCode = os.execute(command)
if exitCode ~= 0 then
error("Install pip failed. exit " .. exitCode)
end
end
end
function windowsInstallExe(path, url, version, filename)
--- Attention system difference
local qInstallFile = path .. "\\" .. filename
local qInstallPath = path
local msiPath = path .. '\\AttachedContainer'
-- download
print("Downloading installer...")
print("from:\t" .. url)
print("to:\t" .. qInstallFile)
local err = http.download_file({
url = url,
headers = REQUEST_HEADERS
}, qInstallFile)
if err ~= nil then
error("Downloading installer failed")
end
-- Extract
print("Extracting installer...")
local wixBin = RUNTIME.pluginDirPath .. '\\bin\\WiX\\dark.exe'
local command = wixBin .. " -x " .. qInstallPath .. '\\ ' .. qInstallFile .. ' > NUL'
local exitCode = os.execute(command)
if exitCode ~= 0 then
error("Extract failed")
end
-- Cleaning up ...
print("Cleaning installer...")
os.remove(qInstallFile)
local files = {'appendpath.msi', 'launcher.msi', 'path.msi', 'pip.msi'}
for _, file in ipairs(files) do
os.remove(msiPath .. '\\' .. file)
end
-- Install msi
print("Installing python...")
local files = io.popen("dir /b " .. msiPath):lines()
for file in files do
if file:match("%.msi$") then
local command = "msiexec /quiet /a " .. msiPath .. '\\' .. file .. " TargetDir=" .. qInstallPath
local exitCode = os.execute(command)
if exitCode ~= 0 then
error("Install msi failed: " .. file)
end
os.remove(qInstallPath .. '\\' .. file)
end
end
-- Install pip
print("Installing pip...")
local ensurepipPath = qInstallPath .. "\\Lib\\ensurepip\\__init__.py"
local file = io.open(ensurepipPath, "r")
if file then
io.close(file)
local command = qInstallPath .. '\\python -E -s -m ensurepip -U --default-pip > NUL'
local exitCode = os.execute(command)
if exitCode ~= 0 then
error("Install pip failed. exit " .. exitCode)
end
end
-- Define paths for executables based on installation path
local pythonExePath = qInstallPath .. "\\python.exe"
local pythonwExePath = qInstallPath .. "\\pythonw.exe"
local venvlauncherExePath = qInstallPath .. "\\Lib\\venv\\scripts\\nt\\python.exe"
local pattern = "(%d+)%.(%d+)"
local major, minor = string.match(version, pattern)
local majorMinor = major .. minor
local majorDotMinor = major .. "." .. minor
-- Copy Python executables with versioned names
-- python.exe
local files = {qInstallPath .. "\\python" .. major .. ".exe", qInstallPath .. "\\python" .. majorMinor .. ".exe",
qInstallPath .. "\\python" .. majorDotMinor .. ".exe"}
for _, file in ipairs(files) do
local command = 'copy /y ' .. pythonExePath .. ' ' .. file .. ' > NUL'
os.execute(command)
end
-- pythonw.exe
local files = {qInstallPath .. "\\pythonw" .. major .. ".exe", qInstallPath .. "\\pythonw" .. majorMinor .. ".exe",
qInstallPath .. "\\pythonw" .. majorDotMinor .. ".exe"}
for _, file in ipairs(files) do
local command = 'copy /y ' .. pythonwExePath .. ' ' .. file .. ' > NUL'
os.execute(command)
end
-- Check if venvlauncher exists
local file = io.open(venvlauncherExePath, "r")
if file then
io.close(file)
-- python.exe
local files = {qInstallPath .. "\\Lib\\venv\\scripts\\nt\\python" .. major .. ".exe",
qInstallPath .. "\\Lib\\venv\\scripts\\nt\\python" .. majorMinor .. ".exe",
qInstallPath .. "\\Lib\\venv\\scripts\\nt\\python" .. majorDotMinor .. ".exe",
qInstallPath .. "\\Lib\\venv\\scripts\\nt\\pythonw" .. major .. ".exe",
qInstallPath .. "\\Lib\\venv\\scripts\\nt\\pythonw" .. majorMinor .. ".exe",
qInstallPath .. "\\Lib\\venv\\scripts\\nt\\pythonw" .. majorDotMinor .. ".exe"}
for _, file in ipairs(files) do
local command = 'copy /y ' .. venvlauncherExePath .. ' ' .. file .. ' > NUL'
os.execute(command)
end
end
end
function linuxCompile(ctx)
local sdkInfo = ctx.sdkInfo['python']
local path = sdkInfo.path
local version = sdkInfo.version
local pyenv_url = "https://github.com/pyenv/pyenv.git"
local dest_pyenv_path = ctx.rootPath .. "/pyenv"
local status = os.execute("git clone --depth 1 " .. pyenv_url .. " " .. dest_pyenv_path)
if status ~= 0 then
error("git clone failed")
end
local pyenv_build_path = dest_pyenv_path .. "/plugins/python-build/bin/python-build"
print("Building python ...")
status = os.execute(pyenv_build_path .. " " .. version .. " " .. path)
if status ~= 0 then
error("python build failed")
end
print("Build python success!")
print("Cleaning up ...")
status = os.execute("rm -rf " .. dest_pyenv_path)
if status ~= 0 then
error("remove build tool failed")
end
end
function getReleaseForWindows(version)
local archType = RUNTIME.archType
if archType == "386" then
archType = ""
end
-- try get exe file
local url = DOWNLOAD_SOURCE.EXE:format(version, version, '-' .. archType)
local resp, err = http.head({
url = url,
headers = REQUEST_HEADERS
})
if err == nil and resp.status_code == 200 then
return url
end
-- try get msi file
local url = DOWNLOAD_SOURCE.MSI:format(version, version, archType)
local resp, err = http.head({
url = url,
headers = REQUEST_HEADERS
})
if err == nil and resp.status_code == 200 then
return url
end
print("url:\t" .. url)
error("No available installer found for current version")
end
function parseVersion()
local resp, err = http.get({
url = PYTHON_URL,
headers = REQUEST_HEADERS
})
if err ~= nil or resp.status_code ~= 200 then
error("paring release info failed." .. err)
end
local result = {}
html.parse(resp.body):find("a"):each(function(i, selection)
local href = selection:attr("href")
local sn = string.match(href, "^%d")
local es = string.match(href, "/$")
if sn and es then
local vn = string.sub(href, 1, -2)
if RUNTIME.osType == "windows" then
if compare_versions(vn, "2.5.0") >= 0 then
table.insert(result, {
version = string.sub(href, 1, -2),
note = ""
})
end
else
table.insert(result, {
version = vn,
note = ""
})
end
end
end)
table.sort(result, function(a, b)
return compare_versions(a.version, b.version) > 0
end)
return result
end
function compare_versions(v1, v2)
local v1_parts = {}
for part in string.gmatch(v1, "[^.]+") do
table.insert(v1_parts, tonumber(part))
end
local v2_parts = {}
for part in string.gmatch(v2, "[^.]+") do
table.insert(v2_parts, tonumber(part))
end
for i = 1, math.max(#v1_parts, #v2_parts) do
local v1_part = v1_parts[i] or 0
local v2_part = v2_parts[i] or 0
if v1_part > v2_part then
return 1
elseif v1_part < v2_part then
return -1
end
end
return 0
end