disable scraper disabling
This commit is contained in:
parent
0a320945a0
commit
aae98a5796
9 changed files with 88 additions and 166 deletions
|
|
@ -7,26 +7,28 @@
|
|||
---@field peak_mb number
|
||||
---@field signal string|nil
|
||||
|
||||
---@class SubstitutableCommand
|
||||
---@field source string substituted via '{source}'
|
||||
---@field binary string substitued via '{binary}'
|
||||
|
||||
local M = {}
|
||||
local constants = require('cp.constants')
|
||||
local logger = require('cp.log')
|
||||
local utils = require('cp.utils')
|
||||
|
||||
local filetype_to_language = constants.filetype_to_language
|
||||
|
||||
local function get_language_from_file(source_file, contest_config)
|
||||
local ext = vim.fn.fnamemodify(source_file, ':e')
|
||||
return filetype_to_language[ext] or contest_config.default_language
|
||||
end
|
||||
|
||||
---@param cmd_template string[]
|
||||
---@param substitutions SubstitutableCommand
|
||||
---@return string[] string normalized with substitutions
|
||||
local function substitute_template(cmd_template, substitutions)
|
||||
local out = {}
|
||||
for _, a in ipairs(cmd_template) do
|
||||
local s = a
|
||||
for k, v in pairs(substitutions) do
|
||||
s = s:gsub('{' .. k .. '}', v)
|
||||
for _, arg in ipairs(cmd_template) do
|
||||
if arg == '{source}' and substitutions.source then
|
||||
table.insert(out, substitutions.source)
|
||||
elseif arg == '{binary}' and substitutions.binary then
|
||||
table.insert(out, substitutions.binary)
|
||||
else
|
||||
table.insert(out, arg)
|
||||
end
|
||||
table.insert(out, s)
|
||||
end
|
||||
return out
|
||||
end
|
||||
|
|
@ -39,12 +41,10 @@ function M.build_command(cmd_template, executable, substitutions)
|
|||
return cmd
|
||||
end
|
||||
|
||||
function M.compile(language_config, substitutions)
|
||||
if not language_config.compile then
|
||||
return { code = 0, stdout = '' }
|
||||
end
|
||||
|
||||
local cmd = substitute_template(language_config.compile, substitutions)
|
||||
---@param compile_cmd string[]
|
||||
---@param substitutions SubstitutableCommand
|
||||
function M.compile(compile_cmd, substitutions)
|
||||
local cmd = substitute_template(compile_cmd, substitutions)
|
||||
local sh = table.concat(cmd, ' ') .. ' 2>&1'
|
||||
|
||||
local t0 = vim.uv.hrtime()
|
||||
|
|
@ -164,32 +164,19 @@ function M.run(cmd, stdin, timeout_ms, memory_mb)
|
|||
}
|
||||
end
|
||||
|
||||
function M.compile_problem(contest_config, is_debug)
|
||||
function M.compile_problem()
|
||||
local state = require('cp.state')
|
||||
local source_file = state.get_source_file()
|
||||
if not source_file then
|
||||
return { success = false, output = 'No source file found.' }
|
||||
end
|
||||
|
||||
local language = get_language_from_file(source_file, contest_config)
|
||||
local language_config = contest_config[language]
|
||||
if not language_config then
|
||||
return { success = false, output = ('No configuration for language %s.'):format(language) }
|
||||
end
|
||||
|
||||
local binary_file = state.get_binary_file()
|
||||
local substitutions = { source = source_file, binary = binary_file }
|
||||
|
||||
local chosen = (is_debug and language_config.debug) and language_config.debug
|
||||
or language_config.compile
|
||||
if not chosen then
|
||||
local config = require('cp.config').get_config()
|
||||
local platform = state.get_platform() or ''
|
||||
local language = config.platforms[platform].default_language
|
||||
local compile_config = config.platforms[platform][language].compile
|
||||
if not compile_config then
|
||||
return { success = true, output = nil }
|
||||
end
|
||||
|
||||
local saved = language_config.compile
|
||||
language_config.compile = chosen
|
||||
local r = M.compile(language_config, substitutions)
|
||||
language_config.compile = saved
|
||||
local substitutions = { source = state.get_source_file(), binary = state.get_binary_file() }
|
||||
local r = M.compile(compile_config, substitutions)
|
||||
|
||||
if r.code ~= 0 then
|
||||
return { success = false, output = r.stdout or 'unknown error' }
|
||||
|
|
|
|||
|
|
@ -31,8 +31,11 @@
|
|||
|
||||
local M = {}
|
||||
local cache = require('cp.cache')
|
||||
local config = require('cp.config').get_config()
|
||||
local constants = require('cp.constants')
|
||||
local execute = require('cp.runner.execute')
|
||||
local logger = require('cp.log')
|
||||
local state = require('cp.state')
|
||||
|
||||
---@type RunPanelState
|
||||
local run_panel_state = {
|
||||
|
|
@ -90,42 +93,35 @@ local function create_sentinal_panel_data(test_cases)
|
|||
return out
|
||||
end
|
||||
|
||||
---@param language_config LanguageConfig
|
||||
---@param substitutions table<string, string>
|
||||
---@param cmd string[]
|
||||
---@param executable string
|
||||
---@return string[]
|
||||
local function build_command(language_config, substitutions)
|
||||
local execute = require('cp.runner.execute')
|
||||
return execute.build_command(language_config.test, language_config.executable, substitutions)
|
||||
local function build_command(cmd, executable, substitutions)
|
||||
return execute.build_command(cmd, executable, substitutions)
|
||||
end
|
||||
|
||||
---@param contest_config ContestConfig
|
||||
---@param cp_config cp.Config
|
||||
---@param test_case RanTestCase
|
||||
---@return { status: "pass"|"fail"|"tle"|"mle", actual: string, actual_highlights: Highlight[], error: string, stderr: string, time_ms: number, code: integer, ok: boolean, signal: string, tled: boolean, mled: boolean, rss_mb: number }
|
||||
local function run_single_test_case(contest_config, cp_config, test_case)
|
||||
local state = require('cp.state')
|
||||
local exec = require('cp.runner.execute')
|
||||
|
||||
local function run_single_test_case(test_case)
|
||||
local source_file = state.get_source_file()
|
||||
local ext = vim.fn.fnamemodify(source_file or '', ':e')
|
||||
local lang_name = constants.filetype_to_language[ext] or contest_config.default_language
|
||||
local language_config = contest_config[lang_name]
|
||||
|
||||
local binary_file = state.get_binary_file()
|
||||
local substitutions = { source = source_file, binary = binary_file }
|
||||
|
||||
local cmd = build_command(language_config, substitutions)
|
||||
local platform_config = config.platforms[state.get_platform() or '']
|
||||
local language_config = platform_config[platform_config.default_language]
|
||||
local cmd = build_command(language_config.test, language_config.executable, substitutions)
|
||||
local stdin_content = (test_case.input or '') .. '\n'
|
||||
local timeout_ms = (run_panel_state.constraints and run_panel_state.constraints.timeout_ms) or 0
|
||||
local memory_mb = run_panel_state.constraints and run_panel_state.constraints.memory_mb or 0
|
||||
|
||||
local r = exec.run(cmd, stdin_content, timeout_ms, memory_mb)
|
||||
local r = execute.run(cmd, stdin_content, timeout_ms, memory_mb)
|
||||
|
||||
local ansi = require('cp.ui.ansi')
|
||||
local out = r.stdout or ''
|
||||
local highlights = {}
|
||||
if out ~= '' then
|
||||
if cp_config.run_panel.ansi then
|
||||
if config.run_panel.ansi then
|
||||
local parsed = ansi.parse_ansi_text(out)
|
||||
out = table.concat(parsed.lines, '\n')
|
||||
highlights = parsed.highlights
|
||||
|
|
@ -134,7 +130,7 @@ local function run_single_test_case(contest_config, cp_config, test_case)
|
|||
end
|
||||
end
|
||||
|
||||
local max_lines = cp_config.run_panel.max_output_lines
|
||||
local max_lines = config.run_panel.max_output_lines
|
||||
local lines = vim.split(out, '\n')
|
||||
if #lines > max_lines then
|
||||
local trimmed = {}
|
||||
|
|
@ -180,9 +176,8 @@ local function run_single_test_case(contest_config, cp_config, test_case)
|
|||
}
|
||||
end
|
||||
|
||||
---@param state table
|
||||
---@return boolean
|
||||
function M.load_test_cases(state)
|
||||
function M.load_test_cases()
|
||||
local tcs = cache.get_test_cases(
|
||||
state.get_platform() or '',
|
||||
state.get_contest_id() or '',
|
||||
|
|
@ -201,18 +196,16 @@ function M.load_test_cases(state)
|
|||
return #tcs > 0
|
||||
end
|
||||
|
||||
---@param contest_config ContestConfig
|
||||
---@param cp_config cp.Config
|
||||
---@param index number
|
||||
---@return boolean
|
||||
function M.run_test_case(contest_config, cp_config, index)
|
||||
function M.run_test_case(index)
|
||||
local tc = run_panel_state.test_cases[index]
|
||||
if not tc then
|
||||
return false
|
||||
end
|
||||
|
||||
tc.status = 'running'
|
||||
local r = run_single_test_case(contest_config, cp_config, tc)
|
||||
local r = run_single_test_case(tc)
|
||||
|
||||
tc.status = r.status
|
||||
tc.actual = r.actual
|
||||
|
|
@ -230,13 +223,11 @@ function M.run_test_case(contest_config, cp_config, index)
|
|||
return true
|
||||
end
|
||||
|
||||
---@param contest_config ContestConfig
|
||||
---@param cp_config cp.Config
|
||||
---@return RanTestCase[]
|
||||
function M.run_all_test_cases(contest_config, cp_config)
|
||||
function M.run_all_test_cases()
|
||||
local results = {}
|
||||
for i = 1, #run_panel_state.test_cases do
|
||||
M.run_test_case(contest_config, cp_config, i)
|
||||
M.run_test_case(i)
|
||||
results[i] = run_panel_state.test_cases[i]
|
||||
end
|
||||
return results
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue