Use BufferSearch from c++utilities

This commit is contained in:
Martchus 2021-07-16 16:01:23 +02:00
parent bc8a0549b2
commit e5f4057c61
5 changed files with 13 additions and 105 deletions

View File

@ -80,7 +80,7 @@ set(LINK_TESTS_AGAINST_APP_TARGET ON)
set(CONFIGURATION_PACKAGE_SUFFIX set(CONFIGURATION_PACKAGE_SUFFIX
"" ""
CACHE STRING "sets the suffix for find_package() calls to packages configured via c++utilities") CACHE STRING "sets the suffix for find_package() calls to packages configured via c++utilities")
find_package(c++utilities${CONFIGURATION_PACKAGE_SUFFIX} 5.10.3 REQUIRED) find_package(c++utilities${CONFIGURATION_PACKAGE_SUFFIX} 5.11.0 REQUIRED)
use_cpp_utilities(VISIBILITY PUBLIC) use_cpp_utilities(VISIBILITY PUBLIC)
# find boost libraries # find boost libraries

View File

@ -293,43 +293,6 @@ void BuildProcessSession::conclude()
buildAction->m_ongoingProcesses.erase(m_logFilePath); buildAction->m_ongoingProcesses.erase(m_logFilePath);
} }
void BufferSearch::operator()(const BuildProcessSession::BufferType &buffer, std::size_t bufferSize)
{
if (m_hasResult || (!m_giveUpTerm.empty() && m_giveUpTermIterator == m_giveUpTerm.end())) {
return;
}
for (auto i = buffer->data(), end = buffer->data() + bufferSize; i != end; ++i) {
const auto currentChar = *i;
if (m_searchTermIterator == m_searchTerm.end()) {
for (const auto &terminationChar : m_terminationChars) {
if (currentChar == terminationChar) {
m_hasResult = true;
break;
}
}
if (m_hasResult) {
m_callback(std::move(m_result));
return;
}
m_result += currentChar;
continue;
}
if (currentChar == *m_searchTermIterator) {
++m_searchTermIterator;
} else {
m_searchTermIterator = m_searchTerm.begin();
}
if (m_giveUpTerm.empty()) {
continue;
}
if (currentChar == *m_giveUpTermIterator) {
++m_giveUpTermIterator;
} else {
m_giveUpTermIterator = m_giveUpTerm.begin();
}
}
}
std::shared_ptr<BuildProcessSession> BuildAction::makeBuildProcess( std::shared_ptr<BuildProcessSession> BuildAction::makeBuildProcess(
std::string &&displayName, std::string &&logFilePath, ProcessHandler &&handler, AssociatedLocks &&locks) std::string &&displayName, std::string &&logFilePath, ProcessHandler &&handler, AssociatedLocks &&locks)
{ {

View File

@ -32,6 +32,10 @@ namespace boost::process {
class child; class child;
} }
namespace CppUtilities {
class BufferSearch;
}
namespace LibRepoMgr { namespace LibRepoMgr {
enum class BuildActionAccess { enum class BuildActionAccess {
@ -269,35 +273,6 @@ template <typename... ChildArgs> void BuildProcessSession::launch(ChildArgs &&..
readMoreFromPipe(); readMoreFromPipe();
} }
/// \brief The BufferSearch struct invokes a callback if an initially given search term occurs in consecutively specified buffers.
struct LIBREPOMGR_EXPORT BufferSearch {
BufferSearch(
std::string_view searchTerm, std::string_view terminationChars, std::string_view giveUpTerm, std::function<void(std::string &&)> &&callback);
void operator()(const BuildProcessSession::BufferType &buffer, std::size_t bufferSize);
private:
const std::string_view m_searchTerm;
const std::string_view m_terminationChars;
const std::string_view m_giveUpTerm;
const std::function<void(std::string &&)> m_callback;
std::string_view::const_iterator m_searchTermIterator;
std::string_view::const_iterator m_giveUpTermIterator;
std::string m_result;
bool m_hasResult;
};
inline BufferSearch::BufferSearch(
std::string_view searchTerm, std::string_view terminationChars, std::string_view giveUpTerm, std::function<void(std::string &&)> &&callback)
: m_searchTerm(searchTerm)
, m_terminationChars(terminationChars)
, m_giveUpTerm(giveUpTerm)
, m_callback(std::move(callback))
, m_searchTermIterator(m_searchTerm.begin())
, m_giveUpTermIterator(m_giveUpTerm.begin())
, m_hasResult(false)
{
}
struct ProcessResult; struct ProcessResult;
/// \brief The InternalBuildAction struct contains internal details (which are not serialized / accessible via the web API) and helpers. /// \brief The InternalBuildAction struct contains internal details (which are not serialized / accessible via the web API) and helpers.
@ -620,7 +595,8 @@ private:
void checkBuildErrors(BatchProcessingSession::ContainerType &&failedPackages); void checkBuildErrors(BatchProcessingSession::ContainerType &&failedPackages);
void dumpBuildProgress(); void dumpBuildProgress();
void addLogFile(std::string &&logFilePath); void addLogFile(std::string &&logFilePath);
void assignNewVersion(const std::string &packageName, PackageBuildProgress &packageProgress, std::string &&updatedVersionInfo); void assignNewVersion(
const std::string &packageName, PackageBuildProgress &packageProgress, CppUtilities::BufferSearch &, std::string &&updatedVersionInfo);
void copyPkgbuildToOriginalSourceDirectory( void copyPkgbuildToOriginalSourceDirectory(
const std::string &packageName, PackageBuildProgress &packageProgress, const std::string &buildDirectory); const std::string &packageName, PackageBuildProgress &packageProgress, const std::string &buildDirectory);
PackageStagingNeeded checkWhetherStagingIsNeededAndPopulateRebuildList( PackageStagingNeeded checkWhetherStagingIsNeededAndPopulateRebuildList(

View File

@ -9,6 +9,7 @@
#include <c++utilities/conversion/stringbuilder.h> #include <c++utilities/conversion/stringbuilder.h>
#include <c++utilities/conversion/stringconversion.h> #include <c++utilities/conversion/stringconversion.h>
#include <c++utilities/io/ansiescapecodes.h> #include <c++utilities/io/ansiescapecodes.h>
#include <c++utilities/io/buffersearch.h>
#include <c++utilities/io/inifile.h> #include <c++utilities/io/inifile.h>
#include <c++utilities/io/misc.h> #include <c++utilities/io/misc.h>
#include <c++utilities/io/path.h> #include <c++utilities/io/path.h>
@ -929,9 +930,10 @@ InvocationResult ConductBuild::invokeMakechrootpkg(
std::bind(&ConductBuild::handleMakechrootpkgErrorsAndAddPackageToRepo, this, makepkgchrootSession, std::ref(packageName), std::bind(&ConductBuild::handleMakechrootpkgErrorsAndAddPackageToRepo, this, makepkgchrootSession, std::ref(packageName),
std::ref(packageProgress), std::placeholders::_1, std::placeholders::_2)); std::ref(packageProgress), std::placeholders::_1, std::placeholders::_2));
processSession->registerNewDataHandler(BufferSearch("Updated version: ", "\e\n", "Starting build", processSession->registerNewDataHandler(BufferSearch("Updated version: ", "\e\n", "Starting build",
std::bind(&ConductBuild::assignNewVersion, this, std::ref(packageName), std::ref(packageProgress), std::placeholders::_1))); std::bind(
&ConductBuild::assignNewVersion, this, std::ref(packageName), std::ref(packageProgress), std::placeholders::_1, std::placeholders::_2)));
processSession->registerNewDataHandler(BufferSearch("Synchronizing chroot copy", "\n", std::string_view(), processSession->registerNewDataHandler(BufferSearch("Synchronizing chroot copy", "\n", std::string_view(),
[processSession = processSession.get()](std::string &&) { processSession->locks().pop_back(); })); [processSession = processSession.get()](BufferSearch &, std::string &&) { processSession->locks().pop_back(); }));
// lock the chroot directory to prevent other build tasks from using it // lock the chroot directory to prevent other build tasks from using it
m_buildAction->log()(Phrases::InfoMessage, "Building ", packageName, '\n'); m_buildAction->log()(Phrases::InfoMessage, "Building ", packageName, '\n');
@ -1406,7 +1408,8 @@ void ConductBuild::addLogFile(std::string &&logFilePath)
m_buildAction->logfiles.emplace_back(std::move(logFilePath)); m_buildAction->logfiles.emplace_back(std::move(logFilePath));
} }
void ConductBuild::assignNewVersion(const std::string &packageName, PackageBuildProgress &packageProgress, string &&updatedVersionInfo) void ConductBuild::assignNewVersion(
const std::string &packageName, PackageBuildProgress &packageProgress, BufferSearch &, std::string &&updatedVersionInfo)
{ {
auto updatedVersionInfoParts = splitString(updatedVersionInfo, " ", EmptyPartsTreat::Omit); auto updatedVersionInfoParts = splitString(updatedVersionInfo, " ", EmptyPartsTreat::Omit);
if (updatedVersionInfoParts.empty()) { if (updatedVersionInfoParts.empty()) {

View File

@ -39,7 +39,6 @@ class BuildActionsTests : public TestFixture {
CPPUNIT_TEST(testLogging); CPPUNIT_TEST(testLogging);
CPPUNIT_TEST(testProcessSession); CPPUNIT_TEST(testProcessSession);
CPPUNIT_TEST(testBuildActionProcess); CPPUNIT_TEST(testBuildActionProcess);
CPPUNIT_TEST(testBufferSearch);
CPPUNIT_TEST(testParsingInfoFromPkgFiles); CPPUNIT_TEST(testParsingInfoFromPkgFiles);
CPPUNIT_TEST(testPreparingBuild); CPPUNIT_TEST(testPreparingBuild);
CPPUNIT_TEST(testConductingBuild); CPPUNIT_TEST(testConductingBuild);
@ -53,7 +52,6 @@ public:
void testLogging(); void testLogging();
void testProcessSession(); void testProcessSession();
void testBuildActionProcess(); void testBuildActionProcess();
void testBufferSearch();
void testParsingInfoFromPkgFiles(); void testParsingInfoFromPkgFiles();
void testPreparingBuild(); void testPreparingBuild();
void testConductingBuild(); void testConductingBuild();
@ -270,38 +268,6 @@ void BuildActionsTests::testBuildActionProcess()
TESTUTILS_ASSERT_LIKE_FLAGS("PID logged", ".*Launched \"test\", PID\\: [0-9]+.*\n.*"s, std::regex::extended, m_buildAction->output); TESTUTILS_ASSERT_LIKE_FLAGS("PID logged", ".*Launched \"test\", PID\\: [0-9]+.*\n.*"s, std::regex::extended, m_buildAction->output);
} }
/*!
* \brief Tests the BufferSearch class.
*/
void BuildActionsTests::testBufferSearch()
{
// make a buffer
BuildProcessSession::BufferPoolType bufferPool(30);
auto buffer = bufferPool.newBuffer();
// setup testing the search
std::string expectedResult;
bool hasResult = false;
BufferSearch bs("Updated version: ", "\e\n", "Starting build", [&expectedResult, &hasResult](std::string &&result) {
CPPUNIT_ASSERT_EQUAL(expectedResult, result);
CPPUNIT_ASSERT_MESSAGE("callback only invoked once", !hasResult);
hasResult = true;
});
// feed data into the search
bs(buffer, 0);
std::strcpy(buffer->data(), "Starting Updated");
bs(buffer, 16);
std::strcpy(buffer->data(), " version: some ");
bs(buffer, 15);
expectedResult = "some version number";
std::strcpy(buffer->data(), "version number\emore chars");
bs(buffer, 25);
CPPUNIT_ASSERT(hasResult);
std::strcpy(buffer->data(), "... Starting build ...");
bs(buffer, 22);
}
/*! /*!
* \brief Tests the ReloadLibraryDependencies build action. * \brief Tests the ReloadLibraryDependencies build action.
*/ */