From 2eca4875cdb51502418bfcb20b6fad37ddeb63bb Mon Sep 17 00:00:00 2001 From: Simon Quigley Date: Sun, 15 Dec 2024 00:52:47 -0600 Subject: [PATCH] First attempt at consolidating C++ code --- build-packages-cpp/CMakeLists.txt | 25 ------- build-packages-cpp/common.h | 10 --- build-packages-cpp/update_maintainer.h | 4 - cpp/CMakeLists.txt | 75 +++++++++++++++++++ .../main.cpp => cpp/build-packages.cpp | 23 +++++- {build-packages-cpp => cpp}/common.cpp | 15 ++++ cpp/common.h | 25 +++++++ .../main.cpp => cpp/fetch-indexes.cpp | 4 +- .../update-maintainer.cpp | 51 +++++++++++-- cpp/update-maintainer.h | 19 +++++ {fetch-indexes-cpp => cpp}/utilities.cpp | 0 {fetch-indexes-cpp => cpp}/utilities.h | 5 +- fetch-indexes-cpp/CMakeLists.txt | 17 ----- 13 files changed, 201 insertions(+), 72 deletions(-) delete mode 100644 build-packages-cpp/CMakeLists.txt delete mode 100644 build-packages-cpp/common.h delete mode 100644 build-packages-cpp/update_maintainer.h create mode 100644 cpp/CMakeLists.txt rename build-packages-cpp/main.cpp => cpp/build-packages.cpp (96%) rename {build-packages-cpp => cpp}/common.cpp (84%) create mode 100644 cpp/common.h rename fetch-indexes-cpp/main.cpp => cpp/fetch-indexes.cpp (99%) rename build-packages-cpp/update_maintainer.cpp => cpp/update-maintainer.cpp (83%) create mode 100644 cpp/update-maintainer.h rename {fetch-indexes-cpp => cpp}/utilities.cpp (100%) rename {fetch-indexes-cpp => cpp}/utilities.h (96%) delete mode 100644 fetch-indexes-cpp/CMakeLists.txt diff --git a/build-packages-cpp/CMakeLists.txt b/build-packages-cpp/CMakeLists.txt deleted file mode 100644 index 4ba80b1..0000000 --- a/build-packages-cpp/CMakeLists.txt +++ /dev/null @@ -1,25 +0,0 @@ -cmake_minimum_required(VERSION 3.21) -project(build-packages CXX) - -set(CMAKE_CXX_STANDARD 23) -set(CMAKE_CXX_STANDARD_REQUIRED ON) -set(CMAKE_CXX_EXTENSIONS OFF) - -find_package(yaml-cpp REQUIRED) -find_package(PkgConfig REQUIRED) -pkg_check_modules(LIBGIT2 REQUIRED IMPORTED_TARGET libgit2) - -add_library(common SHARED common.cpp) -target_link_libraries(common yaml-cpp) - -add_library(update_maintainer SHARED update_maintainer.cpp) - -add_executable(build-packages main.cpp) -target_link_libraries(build-packages common update_maintainer PkgConfig::LIBGIT2 yaml-cpp) - -set(CMAKE_INSTALL_RPATH "$ORIGIN/lib") -set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE) - -install(TARGETS common LIBRARY DESTINATION lib) -install(TARGETS update_maintainer LIBRARY DESTINATION lib) -install(TARGETS build-packages RUNTIME DESTINATION .) diff --git a/build-packages-cpp/common.h b/build-packages-cpp/common.h deleted file mode 100644 index 86dfc56..0000000 --- a/build-packages-cpp/common.h +++ /dev/null @@ -1,10 +0,0 @@ -#pragma once -#include -#include -#include -#include - -std::string parse_version(const std::filesystem::path &changelog_path); -void run_command(const std::vector &cmd, const std::optional &cwd = std::nullopt, bool show_output=false); -void clean_old_logs(const std::filesystem::path &log_dir, int max_age_seconds=86400); -void create_tarball(const std::string &name, const std::filesystem::path &source_dir, const std::vector &exclusions); diff --git a/build-packages-cpp/update_maintainer.h b/build-packages-cpp/update_maintainer.h deleted file mode 100644 index 6cc0d1d..0000000 --- a/build-packages-cpp/update_maintainer.h +++ /dev/null @@ -1,4 +0,0 @@ -#pragma once -#include - -void update_maintainer(const std::string &debian_directory, bool verbose=false); diff --git a/cpp/CMakeLists.txt b/cpp/CMakeLists.txt new file mode 100644 index 0000000..f6840ff --- /dev/null +++ b/cpp/CMakeLists.txt @@ -0,0 +1,75 @@ +cmake_minimum_required(VERSION 3.21) +project(lubuntuci CXX) + +# Set C++ standard +set(CMAKE_CXX_STANDARD 23) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_CXX_EXTENSIONS OFF) + +# Specify output directories +set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) + +# Find required packages +find_package(yaml-cpp REQUIRED) +find_package(PkgConfig REQUIRED) +pkg_check_modules(LIBGIT2 REQUIRED IMPORTED_TARGET libgit2) +find_package(CURL REQUIRED) +find_package(ZLIB REQUIRED) + +# Locate Launchpadlib +# Assuming Launchpadlib is installed in /srv/lubuntu-ci/repos/ci-tools/lib/ +# Adjust the path if necessary +find_library(LAUNCHPADLIB_LIB NAMES launchpadlib liblaunchpad REQUIRED PATHS /srv/lubuntu-ci/repos/ci-tools/lib/ NO_DEFAULT_PATH) +if(NOT LAUNCHPADLIB_LIB) + message(FATAL_ERROR "launchpadlib not found") +endif() + +# Include directories for Launchpadlib +include_directories(/srv/lubuntu-ci/repos/ci-tools/include/launchpadlib-cpp) + +# Create the shared library liblubuntuci.so +add_library(lubuntuci SHARED common.cpp utilities.cpp) +target_include_directories(lubuntuci PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) +target_link_libraries(lubuntuci PRIVATE yaml-cpp) + +# Ensure liblubuntuci has proper RPATH +set_target_properties(lubuntuci PROPERTIES + BUILD_WITH_INSTALL_RPATH TRUE + INSTALL_RPATH "$ORIGIN/lib" +) + +# build-packages executable +add_executable(build-packages build-packages.cpp) +target_link_libraries(build-packages PRIVATE lubuntuci PkgConfig::LIBGIT2 yaml-cpp ${LIBGIT2_LIBRARIES} ${YAML_CPP_LIBRARIES} ${LAUNCHPADLIB_LIB}) + +# fetch-indexes executable +add_executable(fetch-indexes fetch-indexes.cpp utilities.cpp) +target_include_directories(fetch-indexes PRIVATE ${CURL_INCLUDE_DIRS} ${ZLIB_INCLUDE_DIRS} /srv/lubuntu-ci/repos/ci-tools/include/launchpadlib-cpp) +target_link_libraries(fetch-indexes PRIVATE lubuntuci CURL::libcurl ZLIB::ZLIB yaml-cpp ${LAUNCHPADLIB_LIB}) + +# update-maintainer executable +add_executable(update-maintainer update-maintainer.cpp) +# Link only necessary libraries, avoiding liblubuntuci +target_include_directories(update-maintainer PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} /srv/lubuntu-ci/repos/ci-tools/include/launchpadlib-cpp) +target_link_libraries(update-maintainer PRIVATE yaml-cpp ${LAUNCHPADLIB_LIB}) + +# Ensure executables have proper RPATH +set_target_properties(build-packages fetch-indexes update-maintainer PROPERTIES + BUILD_WITH_INSTALL_RPATH TRUE + INSTALL_RPATH "$ORIGIN/lib" +) + +# Install targets +install(TARGETS lubuntuci + LIBRARY DESTINATION lib +) + +install(TARGETS build-packages fetch-indexes update-maintainer + RUNTIME DESTINATION bin +) + +# Install headers +install(FILES common.h update-maintainer.h utilities.h + DESTINATION include/lubuntuci +) diff --git a/build-packages-cpp/main.cpp b/cpp/build-packages.cpp similarity index 96% rename from build-packages-cpp/main.cpp rename to cpp/build-packages.cpp index 11f4c22..36b1f9f 100644 --- a/build-packages-cpp/main.cpp +++ b/cpp/build-packages.cpp @@ -1,3 +1,18 @@ +// Copyright (C) 2024 Simon Quigley +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + #include "common.h" #include "update_maintainer.h" #include @@ -333,8 +348,8 @@ int main(int argc, char** argv) { std::string epoch; std::string upstream_version = version_match; if(auto pos=version_match.find(':'); pos!=std::string::npos) { - epoch = version_match.substr(0,pos); - upstream_version = version_match.substr(pos+1); + epoch=version_match.substr(0,pos); + upstream_version=version_match.substr(pos+1); } if(auto pos=upstream_version.find('-'); pos!=std::string::npos) { upstream_version=upstream_version.substr(0,pos); @@ -409,7 +424,7 @@ int main(int argc, char** argv) { log_info("Completed upload of changes to "+upload_target); for(auto &file: devel_changes_files) { if(!file.empty()) { - run_source_lintian(name, file); + run_source_lintian(name,file); } } } catch (...) { @@ -521,7 +536,7 @@ int main(int argc, char** argv) { fs::path tarball_source = fs::path(BASE_DIR)/(name+"_MAIN.orig.tar.gz"); fs::path tarball_dest = fs::path(BASE_DIR)/(name+"_"+release_version_no_epoch+".orig.tar.gz"); fs::copy_file(tarball_source,tarball_dest,fs::copy_options::overwrite_existing); - + std::string version_for_dch = epoch.empty()? release_version_no_epoch : (epoch+":"+release_version_no_epoch); std::map env_map; diff --git a/build-packages-cpp/common.cpp b/cpp/common.cpp similarity index 84% rename from build-packages-cpp/common.cpp rename to cpp/common.cpp index c40ba6d..ef8f5a8 100644 --- a/build-packages-cpp/common.cpp +++ b/cpp/common.cpp @@ -1,3 +1,18 @@ +// Copyright (C) 2024 Simon Quigley +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + #include "common.h" #include #include diff --git a/cpp/common.h b/cpp/common.h new file mode 100644 index 0000000..9021d63 --- /dev/null +++ b/cpp/common.h @@ -0,0 +1,25 @@ +// Copyright (C) 2024 Simon Quigley +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +#pragma once +#include +#include +#include +#include + +std::string parse_version(const std::filesystem::path &changelog_path); +void run_command(const std::vector &cmd, const std::optional &cwd = std::nullopt, bool show_output=false); +void clean_old_logs(const std::filesystem::path &log_dir, int max_age_seconds=86400); +void create_tarball(const std::string &name, const std::filesystem::path &source_dir, const std::vector &exclusions); diff --git a/fetch-indexes-cpp/main.cpp b/cpp/fetch-indexes.cpp similarity index 99% rename from fetch-indexes-cpp/main.cpp rename to cpp/fetch-indexes.cpp index 2ef2f7e..eec336e 100644 --- a/fetch-indexes-cpp/main.cpp +++ b/cpp/fetch-indexes.cpp @@ -13,6 +13,8 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . +#include "utilities.h" + #include #include #include @@ -201,7 +203,7 @@ void processRelease(const std::string& RELEASE, const YAML::Node& config) { // Get current timestamp std::time_t now_c = std::time(nullptr); char timestamp[20]; - std::strftime(timestamp, sizeof(timestamp), "%Y-%m-%d_%H:%M:%S", std::localtime(&now_c)); + std::strftime(timestamp, sizeof(timestamp), "%Y%m%dT%H%M%S", std::gmtime(&now_c)); std::string BRITNEY_TIMESTAMP(timestamp); std::cout << "Release: " << RELEASE << std::endl; diff --git a/build-packages-cpp/update_maintainer.cpp b/cpp/update-maintainer.cpp similarity index 83% rename from build-packages-cpp/update_maintainer.cpp rename to cpp/update-maintainer.cpp index 01c3422..db8191e 100644 --- a/build-packages-cpp/update_maintainer.cpp +++ b/cpp/update-maintainer.cpp @@ -1,4 +1,19 @@ -#include "update_maintainer.h" +// Copyright (C) 2024 Simon Quigley +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +#include "update-maintainer.h" #include #include #include @@ -57,19 +72,13 @@ static std::string get_distribution(const fs::path &changelog_file) { if(!f) throw MaintainerUpdateException("Unable to open changelog."); std::string first_line; std::getline(f, first_line); - // Format: "pkg (ver) dist; urgency=..." - // find ') ' size_t pos = first_line.find(')'); if(pos == std::string::npos) throw MaintainerUpdateException("Invalid changelog format"); - // after ') ', next token is distribution until space - // skip ')' pos++; while(pos < first_line.size() && std::isspace((unsigned char)first_line[pos])) pos++; - // now read until space or ';' size_t start = pos; while(pos < first_line.size() && !std::isspace((unsigned char)first_line[pos]) && first_line[pos] != ';') pos++; std::string dist = first_line.substr(start, pos - start); - // remove -proposed-updates etc size_t dashpos = dist.find('-'); if (dashpos != std::string::npos) { dist = dist.substr(0, dashpos); @@ -202,3 +211,31 @@ void update_maintainer(const std::string &debian_directory, bool verbose) { update_maintainer_file(*control_file, distribution, verbose); } + +int main(int argc, char** argv) { + if(argc < 2) { + std::cerr << "Usage: update-maintainer [--verbose]" << std::endl; + return 1; + } + + std::string debian_directory = argv[1]; + bool verbose = false; + if(argc >=3 ) { + std::string flag = argv[2]; + if(flag == "--verbose") { + verbose = true; + } + } + + try { + update_maintainer(debian_directory, verbose); + if(verbose) { + std::cout << "Maintainer updated successfully." << std::endl; + } + } catch(const MaintainerUpdateException& e) { + std::cerr << "Error: " << e.what() << std::endl; + return 1; + } + + return 0; +} diff --git a/cpp/update-maintainer.h b/cpp/update-maintainer.h new file mode 100644 index 0000000..f2403c7 --- /dev/null +++ b/cpp/update-maintainer.h @@ -0,0 +1,19 @@ +// Copyright (C) 2024 Simon Quigley +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +#pragma once +#include + +void update_maintainer(const std::string &debian_directory, bool verbose=false); diff --git a/fetch-indexes-cpp/utilities.cpp b/cpp/utilities.cpp similarity index 100% rename from fetch-indexes-cpp/utilities.cpp rename to cpp/utilities.cpp diff --git a/fetch-indexes-cpp/utilities.h b/cpp/utilities.h similarity index 96% rename from fetch-indexes-cpp/utilities.h rename to cpp/utilities.h index 58d0fc8..9bac7ce 100644 --- a/fetch-indexes-cpp/utilities.h +++ b/cpp/utilities.h @@ -13,8 +13,7 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -#ifndef UTILITIES_H -#define UTILITIES_H +#pragma once #include #include @@ -38,5 +37,3 @@ void downloadFileWithTimestamping(const std::string& url, const std::filesystem: // Helper function for libcurl write callback size_t write_data(void* ptr, size_t size, size_t nmemb, void* stream); - -#endif // UTILITIES_H diff --git a/fetch-indexes-cpp/CMakeLists.txt b/fetch-indexes-cpp/CMakeLists.txt deleted file mode 100644 index bb4d744..0000000 --- a/fetch-indexes-cpp/CMakeLists.txt +++ /dev/null @@ -1,17 +0,0 @@ -cmake_minimum_required(VERSION 3.10) -project(fetch-indexes) - -set(CMAKE_CXX_STANDARD 23) -set(CMAKE_CXX_STANDARD_REQUIRED True) - -find_package(CURL REQUIRED) -find_package(ZLIB REQUIRED) -find_package(YAML-CPP REQUIRED) -include_directories(${CURL_INCLUDE_DIRS}) -include_directories(${ZLIB_INCLUDE_DIRS}) -include_directories(${YAML_CPP_INCLUDE_DIR}) -include_directories(/srv/lubuntu-ci/repos/ci-tools/include/launchpadlib-cpp) - -add_executable(fetch-indexes main.cpp utilities.cpp) - -target_link_libraries(fetch-indexes ${CURL_LIBRARIES} ${ZLIB_LIBRARIES} yaml-cpp /srv/lubuntu-ci/repos/ci-tools/lib/liblaunchpad.so)