summaryrefslogtreecommitdiff
path: root/src/other_tools
diff options
context:
space:
mode:
authorKlaus Aehlig <klaus.aehlig@huawei.com>2025-02-20 12:05:12 +0100
committerKlaus Aehlig <klaus.aehlig@huawei.com>2025-03-10 16:28:59 +0100
commit3c2d0d460485bde1e2ccf2d45e6f438adff4ce47 (patch)
treed9ce1dfc1a42ad19d13de30c93223bf25cf6e2c1 /src/other_tools
parentf26497d1ed48042c210603c921ba065988e5ad04 (diff)
downloadjustbuild-3c2d0d460485bde1e2ccf2d45e6f438adff4ce47.tar.gz
just-mr: honor invocation-log parameters when launching
Diffstat (limited to 'src/other_tools')
-rw-r--r--src/other_tools/just_mr/TARGETS4
-rw-r--r--src/other_tools/just_mr/launch.cpp63
-rw-r--r--src/other_tools/just_mr/launch.hpp1
-rw-r--r--src/other_tools/just_mr/main.cpp1
4 files changed, 69 insertions, 0 deletions
diff --git a/src/other_tools/just_mr/TARGETS b/src/other_tools/just_mr/TARGETS
index 7a4fa589..3e3b82c8 100644
--- a/src/other_tools/just_mr/TARGETS
+++ b/src/other_tools/just_mr/TARGETS
@@ -274,16 +274,20 @@
, "setup"
, "setup_utils"
, "utils"
+ , ["@", "fmt", "", "fmt"]
, ["@", "json", "", "json"]
, ["src/buildtool/build_engine/expression", "expression"]
, ["src/buildtool/build_engine/expression", "expression_ptr_interface"]
, ["src/buildtool/common", "clidefaults"]
, ["src/buildtool/common", "user_structs"]
+ , ["src/buildtool/execution_api/common", "ids"]
+ , ["src/buildtool/file_system", "file_system_manager"]
, ["src/buildtool/logging", "log_level"]
, ["src/buildtool/logging", "logging"]
, ["src/buildtool/storage", "garbage_collector"]
, ["src/buildtool/storage", "repository_garbage_collector"]
, ["src/utils/cpp", "file_locking"]
+ , ["src/utils/cpp", "path"]
]
}
, "mirrors":
diff --git a/src/other_tools/just_mr/launch.cpp b/src/other_tools/just_mr/launch.cpp
index 7714711b..8744ffa3 100644
--- a/src/other_tools/just_mr/launch.cpp
+++ b/src/other_tools/just_mr/launch.cpp
@@ -24,6 +24,7 @@
#include <cerrno> // for errno
#include <cstdlib>
#include <cstring> // for strerror()
+#include <ctime>
#include <exception>
#include <filesystem>
#include <functional>
@@ -34,12 +35,16 @@
#include <unordered_map>
#include <vector>
+#include "fmt/chrono.h"
+#include "fmt/core.h"
#include "nlohmann/json.hpp"
#include "src/buildtool/build_engine/expression/configuration.hpp"
#include "src/buildtool/build_engine/expression/expression.hpp"
#include "src/buildtool/build_engine/expression/expression_ptr.hpp"
#include "src/buildtool/common/clidefaults.hpp"
#include "src/buildtool/common/user_structs.hpp"
+#include "src/buildtool/execution_api/common/ids.hpp"
+#include "src/buildtool/file_system/file_system_manager.hpp"
#include "src/buildtool/logging/log_level.hpp"
#include "src/buildtool/logging/logger.hpp"
#include "src/buildtool/storage/config.hpp"
@@ -50,8 +55,10 @@
#include "src/other_tools/just_mr/setup_utils.hpp"
#include "src/other_tools/just_mr/utils.hpp"
#include "src/utils/cpp/file_locking.hpp"
+#include "src/utils/cpp/path.hpp"
auto CallJust(std::optional<std::filesystem::path> const& config_file,
+ InvocationLogArguments const& invocation_log,
MultiRepoCommonArguments const& common_args,
MultiRepoSetupArguments const& setup_args,
MultiRepoJustSubCmdsArguments const& just_cmd_args,
@@ -253,6 +260,40 @@ auto CallJust(std::optional<std::filesystem::path> const& config_file,
cmd.emplace_back(subcmd_arg);
}
}
+ std::optional<std::filesystem::path> log_dir = std::nullopt;
+ auto invocation_time = std::time(nullptr);
+
+ // Check invocation logging
+ if (invocation_log.directory) {
+ auto dir = *invocation_log.directory;
+ if (invocation_log.project_id) {
+ if (not IsValidFileName(*invocation_log.project_id)) {
+ Logger::Log(LogLevel::Error,
+ "Invalid file name for poject id: {}",
+ nlohmann::json(*invocation_log.project_id).dump());
+ std::exit(kExitClargsError);
+ }
+ dir = dir / *invocation_log.project_id;
+ }
+ else {
+ dir = dir / "unknown";
+ }
+ std::string uuid = CreateUUID();
+ auto invocation_id = fmt::format(
+ "{:%Y-%m-%d-%H:%M}-{}", fmt::gmtime(invocation_time), uuid);
+ dir = dir / invocation_id;
+ if (FileSystemManager::CreateDirectoryExclusive(dir)) {
+ Logger::Log(
+ LogLevel::Info, "Invocation logged at {}", dir.string());
+ log_dir = dir;
+ }
+ else {
+ Logger::Log(LogLevel::Warning,
+ "Failed to create directory {} for invocation logging",
+ nlohmann::json(dir.string()).dump());
+ }
+ }
+
// add (remaining) args given by user as clargs
for (auto it = just_cmd_args.additional_just_args.begin() +
additional_args_offset;
@@ -261,6 +302,28 @@ auto CallJust(std::optional<std::filesystem::path> const& config_file,
cmd.emplace_back(*it);
}
+ // Write invocation metadata, if requested
+ if (log_dir and invocation_log.metadata) {
+ if (not IsValidFileName(*invocation_log.metadata)) {
+ Logger::Log(LogLevel::Error,
+ "Invlaid file name for metadata file: {}",
+ nlohmann::json(*invocation_log.metadata).dump());
+ std::exit(kExitClargsError);
+ }
+
+ auto meta = nlohmann::json::object();
+ meta["time"] = invocation_time;
+ meta["cmdline"] = cmd;
+ // "configuration" -- the blob-identifier of the multi-repo
+ // configuration
+ auto file_name = *log_dir / *invocation_log.metadata;
+ if (not FileSystemManager::WriteFile(meta.dump(2), file_name)) {
+ Logger::Log(LogLevel::Warning,
+ "Failed to write metadata file {}.",
+ nlohmann::json(file_name).dump());
+ }
+ }
+
Logger::Log(
LogLevel::Info, "Setup finished, exec {}", nlohmann::json(cmd).dump());
diff --git a/src/other_tools/just_mr/launch.hpp b/src/other_tools/just_mr/launch.hpp
index 14eb0aea..6bade92e 100644
--- a/src/other_tools/just_mr/launch.hpp
+++ b/src/other_tools/just_mr/launch.hpp
@@ -27,6 +27,7 @@
/// \brief Runs execvp for configured command. Only returns if execvp fails.
[[nodiscard]] auto CallJust(
std::optional<std::filesystem::path> const& config_file,
+ InvocationLogArguments const& invocation_log,
MultiRepoCommonArguments const& common_args,
MultiRepoSetupArguments const& setup_args,
MultiRepoJustSubCmdsArguments const& just_cmd_args,
diff --git a/src/other_tools/just_mr/main.cpp b/src/other_tools/just_mr/main.cpp
index ef91acc3..c2587681 100644
--- a/src/other_tools/just_mr/main.cpp
+++ b/src/other_tools/just_mr/main.cpp
@@ -385,6 +385,7 @@ auto main(int argc, char* argv[]) -> int {
if (arguments.cmd == SubCommand::kJustDo or
arguments.cmd == SubCommand::kJustSubCmd) {
return CallJust(config_file,
+ arguments.invocation_log,
arguments.common,
arguments.setup,
arguments.just_cmd,