Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: get versions from pyenv #22

Merged
merged 2 commits into from
Jan 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# vfox-python
Python plugin for [vfox](https://vfox.lhan.me/).

Python plugin for [vfox](https://vfox.lhan.me/).

## Install

Expand All @@ -10,7 +10,9 @@ After installing [vfox](https://github.com/version-fox/vfox), install the plugin
vfox add python
```

if you want install the free-threaded mode of python, you can select the version ends with `t`, like `v3.14.0a4t`.

## Mirror

You can configure the mirror by `VFOX_PYTHON_MIRROR` environment variable. The default value
is `https://www.python.org/ftp/python/`.
is `https://www.python.org/ftp/python/`.
6 changes: 5 additions & 1 deletion hooks/available.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
require("util")
function PLUGIN:Available(ctx)
return parseVersion()
if OS_TYPE == "windows" then
return parseVersion()
else
return parseVersionFromPyenv()
end
end
4 changes: 3 additions & 1 deletion hooks/pre_install.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
require("util")
function PLUGIN:PreInstall(ctx)
local version = ctx.version

if version == "latest" then
version = self:Available({})[1].version
end
if not checkIsReleaseVersion(version) then

if OS_TYPE == "windows" and not checkIsReleaseVersion(version) then
error("The current version is not released")
return
end
Expand Down
62 changes: 62 additions & 0 deletions lib/util.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local http = require("http")
local html = require("html")
local json = require("json")

-- get mirror
local PYTHON_URL = "https://www.python.org/ftp/python/"
Expand All @@ -10,6 +11,8 @@ if VFOX_PYTHON_MIRROR then
os.setenv("PYTHON_BUILD_MIRROR_URL", PYTHON_URL)
end

local version_vault_url = "https://version-vault.cdn.dog/pyenv-versions"

-- request headers
local REQUEST_HEADERS = {
["User-Agent"] = "vfox"
Expand Down Expand Up @@ -231,6 +234,7 @@ function linuxCompile(ctx)
error("remove build tool failed")
end
end

function getReleaseForWindows(version)
local archType = RUNTIME.archType
if archType == "386" then
Expand Down Expand Up @@ -259,7 +263,14 @@ function getReleaseForWindows(version)
print("url:\t" .. url)
error("No available installer found for current version")
end

function fixHeaders()
REQUEST_HEADERS["User-Agent"] = "vfox v" .. RUNTIME.version;
end

function parseVersion()
fixHeaders()

local resp, err = http.get({
url = PYTHON_URL,
headers = REQUEST_HEADERS
Expand Down Expand Up @@ -318,3 +329,54 @@ function compare_versions(v1, v2)

return 0
end

function parseVersionFromPyenv()
fixHeaders()
local resp, err = http.get({
url = version_vault_url,
headers = REQUEST_HEADERS
})
if err ~= nil or resp.status_code ~= 200 then
error("paring release info failed." .. err)
end
local result = {}
local jsonObj = json.decode(resp.body)

local tagName = jsonObj.tagName;
local versions = jsonObj.versions;

local numericVersions = {}
local namedVersions = {}

for _, version in ipairs(versions) do
if string.match(version, "^%d") then
table.insert(numericVersions, version)
else
table.insert(namedVersions, version)
end
end

table.sort(numericVersions, function(a, b)
return compare_versions(a, b) > 0
end)

table.sort(namedVersions, function(a, b)
return compare_versions(a, b) > 0
end)

for _, version in ipairs(numericVersions) do
table.insert(result, {
version = version,
note = ""
})
end

for _, version in ipairs(namedVersions) do
table.insert(result, {
version = version,
note = ""
})
end

return result
end
Loading