fix: refactor
This commit is contained in:
parent
a33e66680b
commit
58f9be5f9a
5 changed files with 62 additions and 46 deletions
|
|
@ -1,5 +1,7 @@
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
|
local utils = require('cp.utils')
|
||||||
|
|
||||||
local function check_nvim_version()
|
local function check_nvim_version()
|
||||||
if vim.fn.has('nvim-0.10.0') == 1 then
|
if vim.fn.has('nvim-0.10.0') == 1 then
|
||||||
vim.health.ok('Neovim 0.10.0+ detected')
|
vim.health.ok('Neovim 0.10.0+ detected')
|
||||||
|
|
@ -22,8 +24,7 @@ local function check_uv()
|
||||||
end
|
end
|
||||||
|
|
||||||
local function check_python_env()
|
local function check_python_env()
|
||||||
local plugin_path = debug.getinfo(1, 'S').source:sub(2)
|
local plugin_path = utils.get_plugin_path()
|
||||||
plugin_path = vim.fn.fnamemodify(plugin_path, ':h:h:h')
|
|
||||||
local venv_dir = plugin_path .. '/.venv'
|
local venv_dir = plugin_path .. '/.venv'
|
||||||
|
|
||||||
if vim.fn.isdirectory(venv_dir) == 1 then
|
if vim.fn.isdirectory(venv_dir) == 1 then
|
||||||
|
|
@ -34,8 +35,7 @@ local function check_python_env()
|
||||||
end
|
end
|
||||||
|
|
||||||
local function check_scrapers()
|
local function check_scrapers()
|
||||||
local plugin_path = debug.getinfo(1, 'S').source:sub(2)
|
local plugin_path = utils.get_plugin_path()
|
||||||
plugin_path = vim.fn.fnamemodify(plugin_path, ':h:h:h')
|
|
||||||
|
|
||||||
local scrapers = { 'atcoder.py', 'codeforces.py', 'cses.py' }
|
local scrapers = { 'atcoder.py', 'codeforces.py', 'cses.py' }
|
||||||
for _, scraper in ipairs(scrapers) do
|
for _, scraper in ipairs(scrapers) do
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ local M = {}
|
||||||
local cache = require('cp.cache')
|
local cache = require('cp.cache')
|
||||||
local logger = require('cp.log')
|
local logger = require('cp.log')
|
||||||
local scrape = require('cp.scrape')
|
local scrape = require('cp.scrape')
|
||||||
|
local utils = require('cp.utils')
|
||||||
|
|
||||||
---@class cp.PlatformItem
|
---@class cp.PlatformItem
|
||||||
---@field id string Platform identifier (e.g. "codeforces", "atcoder", "cses")
|
---@field id string Platform identifier (e.g. "codeforces", "atcoder", "cses")
|
||||||
|
|
@ -36,12 +37,11 @@ end
|
||||||
local function get_contests_for_platform(platform)
|
local function get_contests_for_platform(platform)
|
||||||
local contests = {}
|
local contests = {}
|
||||||
|
|
||||||
local function get_plugin_path()
|
if not utils.setup_python_env() then
|
||||||
local plugin_path = debug.getinfo(1, 'S').source:sub(2)
|
return contests
|
||||||
return vim.fn.fnamemodify(plugin_path, ':h:h:h')
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local plugin_path = get_plugin_path()
|
local plugin_path = utils.get_plugin_path()
|
||||||
local cmd = {
|
local cmd = {
|
||||||
'uv',
|
'uv',
|
||||||
'run',
|
'run',
|
||||||
|
|
|
||||||
|
|
@ -15,42 +15,13 @@ local M = {}
|
||||||
local cache = require('cp.cache')
|
local cache = require('cp.cache')
|
||||||
local logger = require('cp.log')
|
local logger = require('cp.log')
|
||||||
local problem = require('cp.problem')
|
local problem = require('cp.problem')
|
||||||
|
local utils = require('cp.utils')
|
||||||
local function get_plugin_path()
|
|
||||||
local plugin_path = debug.getinfo(1, 'S').source:sub(2)
|
|
||||||
return vim.fn.fnamemodify(plugin_path, ':h:h:h')
|
|
||||||
end
|
|
||||||
|
|
||||||
local function check_internet_connectivity()
|
local function check_internet_connectivity()
|
||||||
local result = vim.system({ 'ping', '-c', '1', '-W', '3', '8.8.8.8' }, { text = true }):wait()
|
local result = vim.system({ 'ping', '-c', '1', '-W', '3', '8.8.8.8' }, { text = true }):wait()
|
||||||
return result.code == 0
|
return result.code == 0
|
||||||
end
|
end
|
||||||
|
|
||||||
local function setup_python_env()
|
|
||||||
local plugin_path = get_plugin_path()
|
|
||||||
local venv_dir = plugin_path .. '/.venv'
|
|
||||||
|
|
||||||
if vim.fn.executable('uv') == 0 then
|
|
||||||
logger.log(
|
|
||||||
'uv is not installed. Install it to enable problem scraping: https://docs.astral.sh/uv/',
|
|
||||||
vim.log.levels.WARN
|
|
||||||
)
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
|
|
||||||
if vim.fn.isdirectory(venv_dir) == 0 then
|
|
||||||
logger.log('setting up Python environment for scrapers...')
|
|
||||||
local result = vim.system({ 'uv', 'sync' }, { cwd = plugin_path, text = true }):wait()
|
|
||||||
if result.code ~= 0 then
|
|
||||||
logger.log('failed to setup Python environment: ' .. result.stderr, vim.log.levels.ERROR)
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
logger.log('python environment setup complete')
|
|
||||||
end
|
|
||||||
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
|
|
||||||
---@param platform string
|
---@param platform string
|
||||||
---@param contest_id string
|
---@param contest_id string
|
||||||
---@return {success: boolean, problems?: table[], error?: string}
|
---@return {success: boolean, problems?: table[], error?: string}
|
||||||
|
|
@ -77,14 +48,14 @@ function M.scrape_contest_metadata(platform, contest_id)
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
if not setup_python_env() then
|
if not utils.setup_python_env() then
|
||||||
return {
|
return {
|
||||||
success = false,
|
success = false,
|
||||||
error = 'Python environment setup failed',
|
error = 'Python environment setup failed',
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
local plugin_path = get_plugin_path()
|
local plugin_path = utils.get_plugin_path()
|
||||||
|
|
||||||
local args = {
|
local args = {
|
||||||
'uv',
|
'uv',
|
||||||
|
|
@ -182,7 +153,7 @@ function M.scrape_problem(ctx)
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
if not setup_python_env() then
|
if not utils.setup_python_env() then
|
||||||
return {
|
return {
|
||||||
success = false,
|
success = false,
|
||||||
problem_id = ctx.problem_name,
|
problem_id = ctx.problem_name,
|
||||||
|
|
@ -190,7 +161,7 @@ function M.scrape_problem(ctx)
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
local plugin_path = get_plugin_path()
|
local plugin_path = utils.get_plugin_path()
|
||||||
|
|
||||||
local args
|
local args
|
||||||
if ctx.contest == 'cses' then
|
if ctx.contest == 'cses' then
|
||||||
|
|
@ -308,11 +279,11 @@ function M.scrape_problems_parallel(platform, contest_id, problems, config)
|
||||||
return {}
|
return {}
|
||||||
end
|
end
|
||||||
|
|
||||||
if not setup_python_env() then
|
if not utils.setup_python_env() then
|
||||||
return {}
|
return {}
|
||||||
end
|
end
|
||||||
|
|
||||||
local plugin_path = get_plugin_path()
|
local plugin_path = utils.get_plugin_path()
|
||||||
local jobs = {}
|
local jobs = {}
|
||||||
|
|
||||||
for _, prob in ipairs(problems) do
|
for _, prob in ipairs(problems) do
|
||||||
|
|
|
||||||
44
lua/cp/utils.lua
Normal file
44
lua/cp/utils.lua
Normal file
|
|
@ -0,0 +1,44 @@
|
||||||
|
local M = {}
|
||||||
|
|
||||||
|
local logger = require('cp.log')
|
||||||
|
|
||||||
|
---@return string
|
||||||
|
function M.get_plugin_path()
|
||||||
|
local plugin_path = debug.getinfo(1, 'S').source:sub(2)
|
||||||
|
return vim.fn.fnamemodify(plugin_path, ':h:h:h')
|
||||||
|
end
|
||||||
|
|
||||||
|
local python_env_setup = false
|
||||||
|
|
||||||
|
---@return boolean success
|
||||||
|
function M.setup_python_env()
|
||||||
|
if python_env_setup then
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
|
local plugin_path = M.get_plugin_path()
|
||||||
|
local venv_dir = plugin_path .. '/.venv'
|
||||||
|
|
||||||
|
if vim.fn.executable('uv') == 0 then
|
||||||
|
logger.log(
|
||||||
|
'uv is not installed. Install it to enable problem scraping: https://docs.astral.sh/uv/',
|
||||||
|
vim.log.levels.WARN
|
||||||
|
)
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
if vim.fn.isdirectory(venv_dir) == 0 then
|
||||||
|
logger.log('setting up Python environment for scrapers...')
|
||||||
|
local result = vim.system({ 'uv', 'sync' }, { cwd = plugin_path, text = true }):wait()
|
||||||
|
if result.code ~= 0 then
|
||||||
|
logger.log('failed to setup Python environment: ' .. result.stderr, vim.log.levels.ERROR)
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
logger.log('python environment setup complete')
|
||||||
|
end
|
||||||
|
|
||||||
|
python_env_setup = true
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
|
return M
|
||||||
|
|
@ -1,8 +1,9 @@
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
|
local utils = require('cp.utils')
|
||||||
|
|
||||||
local function get_git_version()
|
local function get_git_version()
|
||||||
local plugin_path = debug.getinfo(1, 'S').source:sub(2)
|
local plugin_root = utils.get_plugin_path()
|
||||||
local plugin_root = vim.fn.fnamemodify(plugin_path, ':h:h:h')
|
|
||||||
|
|
||||||
local result = vim
|
local result = vim
|
||||||
.system({ 'git', 'describe', '--tags', '--always', '--dirty' }, {
|
.system({ 'git', 'describe', '--tags', '--always', '--dirty' }, {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue