From bcc368cb054c1693fe834a142102ac2ece34b394 Mon Sep 17 00:00:00 2001 From: Dima Date: Mon, 16 Dec 2024 18:22:41 +0300 Subject: [PATCH] merge classes --- .../jenkins/library/steps/WithCoverage.groovy | 114 +++++++++++++++++- .../library/utils/CoverageUtils.groovy | 113 ----------------- 2 files changed, 109 insertions(+), 118 deletions(-) delete mode 100644 src/ru/pulsar/jenkins/library/utils/CoverageUtils.groovy diff --git a/src/ru/pulsar/jenkins/library/steps/WithCoverage.groovy b/src/ru/pulsar/jenkins/library/steps/WithCoverage.groovy index d1a6b800..f1542a75 100644 --- a/src/ru/pulsar/jenkins/library/steps/WithCoverage.groovy +++ b/src/ru/pulsar/jenkins/library/steps/WithCoverage.groovy @@ -4,7 +4,7 @@ import ru.pulsar.jenkins.library.IStepExecutor import ru.pulsar.jenkins.library.configuration.JobConfiguration import ru.pulsar.jenkins.library.configuration.StepCoverageOptions import ru.pulsar.jenkins.library.ioc.ContextRegistry -import ru.pulsar.jenkins.library.utils.CoverageUtils +import ru.pulsar.jenkins.library.utils.FileUtils import ru.pulsar.jenkins.library.utils.Logger class WithCoverage implements Serializable { @@ -28,22 +28,23 @@ class WithCoverage implements Serializable { return } - def context = CoverageUtils.prepareContext(config, coverageOptions) + def context = prepareContext(config, coverageOptions) IStepExecutor steps = ContextRegistry.getContext().getStepExecutor() steps.lock(context.lockableResource) { try { - CoverageUtils.startCoverage(steps, config, context, stage) + startCoverage(steps, config, context, stage) body() - CoverageUtils.stopCoverage(steps, config, context) + stopCoverage(steps, config, context) steps.stash(stage.getCoverageStashName(), stage.getCoverageStashPath(), true) } catch (Exception e) { - throw new Exception("При выполнении блока произошла ошибка: ${e}") + Logger.println("При выполнении блока произошла ошибка: ${e.message}") + throw e } finally { String pidsFilePath = "build/${stage.getStageSlug()}-pids" @@ -78,4 +79,107 @@ class WithCoverage implements Serializable { } } } + + static List getPIDs(String name) { + + IStepExecutor steps = ContextRegistry.getContext().getStepExecutor() + + String pids + def script + + if (steps.isUnix()) { + script = "ps -C '$name' -o pid= || true" + pids = steps.sh(script, false, true, 'UTF-8') + } else { + script = """@echo off + chcp 65001 > nul + tasklist | findstr "${name}" > nul + if errorlevel 1 ( + exit /b 0 + ) else ( + for /f "tokens=2" %%a in ('tasklist ^| findstr "${name}"') do (@echo %%a) + )""" + pids = steps.bat(script, false, true, 'UTF-8') + } + return pids.split('\r?\n').toList() + } + + static CoverageContext prepareContext(JobConfiguration config, StepCoverageOptions options) { + + IStepExecutor steps = ContextRegistry.getContext().getStepExecutor() + def env = steps.env() + + def coverageOpts = config.coverageOptions + def port = options.dbgsPort + def currentDbgsPids = getPIDs("dbgs") + def currentCoverage41CPids = getPIDs("Coverage41C") + def lockableResource = "${env.NODE_NAME}_$port" + + return new CoverageContext(lockableResource, config.srcDir, coverageOpts, port, currentDbgsPids, currentCoverage41CPids) + + } + + static void startCoverage(IStepExecutor steps, JobConfiguration config, CoverageContext coverageContext, Coverable stage) { + + def env = steps.env() + def srcDir = config.srcDir + def workspaceDir = FileUtils.getFilePath("$env.WORKSPACE") + + def coverageOpts = config.coverageOptions + + String dbgsPath = findDbgs(steps, config) + + steps.start(dbgsPath, "--addr=127.0.0.1 --port=${coverageContext.port}") + steps.start(coverageOpts.coverage41CPath, "start -i DefAlias -u http://127.0.0.1:${coverageContext.port} -P $workspaceDir -s $srcDir -o ${stage.getCoverageStashPath()}") + steps.cmd("${coverageOpts.coverage41CPath} check -i DefAlias -u http://127.0.0.1:${coverageContext.port}") + + def newDbgsPids = getPIDs("dbgs") + def newCoverage41CPids = getPIDs("Coverage41C") + + newDbgsPids.removeAll(coverageContext.dbgsPids) + newCoverage41CPids.removeAll(coverageContext.coverage41CPids) + + newDbgsPids.addAll(newCoverage41CPids) + def pids = newDbgsPids.join(" ") + + steps.writeFile(stage.getCoveragePidsPath(), pids, 'UTF-8') + + Logger.println("PID процессов dbgs и Coverage41C для ${stage.getStageSlug()}: $pids") + } + + static void stopCoverage(IStepExecutor steps, JobConfiguration config, CoverageContext coverageContext) { + + def coverageOpts = config.coverageOptions + + steps.cmd("${coverageOpts.coverage41CPath} stop -i DefAlias -u http://127.0.0.1:$coverageContext.port") + } + + static String findDbgs(IStepExecutor steps, JobConfiguration config) { + if (steps == null || config == null) { + throw new IllegalArgumentException("Некорректные параметры поиска dbgs") + } + + String dbgsPath = config.coverageOptions.dbgsPath + if (!dbgsPath.isEmpty()) { + Logger.println("Использую путь к dbgs из параметра dbgsPath: $dbgsPath") + return dbgsPath.strip() + } + + final dbgsFindScriptPath = "build/tmp/dbgs_${System.currentTimeMillis()}.os" + final dbgsPathResult = "build/tmp/dbgsPath_${System.currentTimeMillis()}" + + def dbgsFindScript = steps.libraryResource("dbgs.os") + steps.writeFile(dbgsFindScriptPath, dbgsFindScript, 'UTF-8') + + steps.cmd("oscript ${dbgsFindScriptPath} ${config.v8version} > ${dbgsPathResult}") + dbgsPath = steps.readFile(dbgsPathResult).strip() + + if (dbgsPath.isEmpty()) { + steps.error("Не удалось найти путь к dbgs") + } + + Logger.println("Найден путь к dbgs: ${dbgsPath}") + return dbgsPath + } + } diff --git a/src/ru/pulsar/jenkins/library/utils/CoverageUtils.groovy b/src/ru/pulsar/jenkins/library/utils/CoverageUtils.groovy deleted file mode 100644 index 28700eef..00000000 --- a/src/ru/pulsar/jenkins/library/utils/CoverageUtils.groovy +++ /dev/null @@ -1,113 +0,0 @@ -package ru.pulsar.jenkins.library.utils - -import ru.pulsar.jenkins.library.IStepExecutor -import ru.pulsar.jenkins.library.configuration.JobConfiguration -import ru.pulsar.jenkins.library.configuration.StepCoverageOptions -import ru.pulsar.jenkins.library.ioc.ContextRegistry -import ru.pulsar.jenkins.library.steps.Coverable -import ru.pulsar.jenkins.library.steps.CoverageContext - -class CoverageUtils { - static List getPIDs(String name) { - - IStepExecutor steps = ContextRegistry.getContext().getStepExecutor() - - String pids - def script - - if (steps.isUnix()) { - script = "ps -C '$name' -o pid= || true" - pids = steps.sh(script, false, true, 'UTF-8') - } else { - script = """@echo off - chcp 65001 > nul - tasklist | findstr "${name}" > nul - if errorlevel 1 ( - exit /b 0 - ) else ( - for /f "tokens=2" %%a in ('tasklist ^| findstr "${name}"') do (@echo %%a) - )""" - pids = steps.bat(script, false, true, 'UTF-8') - } - return pids.split('\r?\n').toList() - } - - static CoverageContext prepareContext(JobConfiguration config, StepCoverageOptions options) { - - IStepExecutor steps = ContextRegistry.getContext().getStepExecutor() - def env = steps.env() - - def coverageOpts = config.coverageOptions - def port = options.dbgsPort - def currentDbgsPids = getPIDs("dbgs") - def currentCoverage41CPids = getPIDs("Coverage41C") - def lockableResource = "${env.NODE_NAME}_$port" - - return new CoverageContext(lockableResource, config.srcDir, coverageOpts, port, currentDbgsPids, currentCoverage41CPids) - - } - - static void startCoverage(IStepExecutor steps, JobConfiguration config, CoverageContext coverageContext, Coverable stage) { - - def env = steps.env() - def srcDir = config.srcDir - def workspaceDir = FileUtils.getFilePath("$env.WORKSPACE") - - def coverageOpts = config.coverageOptions - - String dbgsPath = findDbgs(steps, config) - - steps.start(dbgsPath, "--addr=127.0.0.1 --port=${coverageContext.port}") - steps.start(coverageOpts.coverage41CPath, "start -i DefAlias -u http://127.0.0.1:${coverageContext.port} -P $workspaceDir -s $srcDir -o ${stage.getCoverageStashPath()}") - steps.cmd("${coverageOpts.coverage41CPath} check -i DefAlias -u http://127.0.0.1:${coverageContext.port}") - - def newDbgsPids = getPIDs("dbgs") - def newCoverage41CPids = getPIDs("Coverage41C") - - newDbgsPids.removeAll(coverageContext.dbgsPids) - newCoverage41CPids.removeAll(coverageContext.coverage41CPids) - - newDbgsPids.addAll(newCoverage41CPids) - def pids = newDbgsPids.join(" ") - - steps.writeFile(stage.getCoveragePidsPath(), pids, 'UTF-8') - - Logger.println("PID процессов dbgs и Coverage41C для ${stage.getStageSlug()}: $pids") - } - - static void stopCoverage(IStepExecutor steps, JobConfiguration config, CoverageContext coverageContext) { - - def coverageOpts = config.coverageOptions - - steps.cmd("${coverageOpts.coverage41CPath} stop -i DefAlias -u http://127.0.0.1:$coverageContext.port") - } - - static String findDbgs(IStepExecutor steps, JobConfiguration config) { - if (steps == null || config == null) { - throw new IllegalArgumentException("Некорректные параметры поиска dbgs") - } - - String dbgsPath = config.coverageOptions.dbgsPath - if (!dbgsPath.isEmpty()) { - Logger.println("Использую путь к dbgs из параметра dbgsPath: $dbgsPath") - return dbgsPath.strip() - } - - final dbgsFindScriptPath = "build/tmp/dbgs_${System.currentTimeMillis()}.os" - final dbgsPathResult = "build/tmp/dbgsPath_${System.currentTimeMillis()}" - - def dbgsFindScript = steps.libraryResource("dbgs.os") - steps.writeFile(dbgsFindScriptPath, dbgsFindScript, 'UTF-8') - - steps.cmd("oscript ${dbgsFindScriptPath} ${config.v8version} > ${dbgsPathResult}") - dbgsPath = steps.readFile(dbgsPathResult).strip() - - if (dbgsPath.isEmpty()) { - steps.error("Не удалось найти путь к dbgs") - } - - Logger.println("Найден путь к dbgs: ${dbgsPath}") - return dbgsPath - } - -}