fix: refactor

This commit is contained in:
Barrett Ruth 2025-09-21 11:19:00 -04:00
parent a33e66680b
commit 58f9be5f9a
5 changed files with 62 additions and 46 deletions

View file

@ -1,5 +1,7 @@
local M = {}
local utils = require('cp.utils')
local function check_nvim_version()
if vim.fn.has('nvim-0.10.0') == 1 then
vim.health.ok('Neovim 0.10.0+ detected')
@ -22,8 +24,7 @@ local function check_uv()
end
local function check_python_env()
local plugin_path = debug.getinfo(1, 'S').source:sub(2)
plugin_path = vim.fn.fnamemodify(plugin_path, ':h:h:h')
local plugin_path = utils.get_plugin_path()
local venv_dir = plugin_path .. '/.venv'
if vim.fn.isdirectory(venv_dir) == 1 then
@ -34,8 +35,7 @@ local function check_python_env()
end
local function check_scrapers()
local plugin_path = debug.getinfo(1, 'S').source:sub(2)
plugin_path = vim.fn.fnamemodify(plugin_path, ':h:h:h')
local plugin_path = utils.get_plugin_path()
local scrapers = { 'atcoder.py', 'codeforces.py', 'cses.py' }
for _, scraper in ipairs(scrapers) do

View file

@ -3,6 +3,7 @@ local M = {}
local cache = require('cp.cache')
local logger = require('cp.log')
local scrape = require('cp.scrape')
local utils = require('cp.utils')
---@class cp.PlatformItem
---@field id string Platform identifier (e.g. "codeforces", "atcoder", "cses")
@ -36,12 +37,11 @@ end
local function get_contests_for_platform(platform)
local contests = {}
local function get_plugin_path()
local plugin_path = debug.getinfo(1, 'S').source:sub(2)
return vim.fn.fnamemodify(plugin_path, ':h:h:h')
if not utils.setup_python_env() then
return contests
end
local plugin_path = get_plugin_path()
local plugin_path = utils.get_plugin_path()
local cmd = {
'uv',
'run',

View file

@ -15,42 +15,13 @@ local M = {}
local cache = require('cp.cache')
local logger = require('cp.log')
local problem = require('cp.problem')
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 utils = require('cp.utils')
local function check_internet_connectivity()
local result = vim.system({ 'ping', '-c', '1', '-W', '3', '8.8.8.8' }, { text = true }):wait()
return result.code == 0
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 contest_id string
---@return {success: boolean, problems?: table[], error?: string}
@ -77,14 +48,14 @@ function M.scrape_contest_metadata(platform, contest_id)
}
end
if not setup_python_env() then
if not utils.setup_python_env() then
return {
success = false,
error = 'Python environment setup failed',
}
end
local plugin_path = get_plugin_path()
local plugin_path = utils.get_plugin_path()
local args = {
'uv',
@ -182,7 +153,7 @@ function M.scrape_problem(ctx)
}
end
if not setup_python_env() then
if not utils.setup_python_env() then
return {
success = false,
problem_id = ctx.problem_name,
@ -190,7 +161,7 @@ function M.scrape_problem(ctx)
}
end
local plugin_path = get_plugin_path()
local plugin_path = utils.get_plugin_path()
local args
if ctx.contest == 'cses' then
@ -308,11 +279,11 @@ function M.scrape_problems_parallel(platform, contest_id, problems, config)
return {}
end
if not setup_python_env() then
if not utils.setup_python_env() then
return {}
end
local plugin_path = get_plugin_path()
local plugin_path = utils.get_plugin_path()
local jobs = {}
for _, prob in ipairs(problems) do

44
lua/cp/utils.lua Normal file
View 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

View file

@ -1,8 +1,9 @@
local M = {}
local utils = require('cp.utils')
local function get_git_version()
local plugin_path = debug.getinfo(1, 'S').source:sub(2)
local plugin_root = vim.fn.fnamemodify(plugin_path, ':h:h:h')
local plugin_root = utils.get_plugin_path()
local result = vim
.system({ 'git', 'describe', '--tags', '--always', '--dirty' }, {