Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 40 additions & 2 deletions engine/commands/cortex_upd_cmd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "utils/archive_utils.h"
#include "utils/file_manager_utils.h"
#include "utils/logging_utils.h"
#include "utils/scope_exit.h"
#include "utils/system_info_utils.h"
#include "utils/url_parser.h"

Expand All @@ -23,6 +24,16 @@ void CortexUpdCmd::Exec(std::string v) {
ssc.Exec();
}
}

// Try to remove cortex temp folder if it exists first
try {
auto n = std::filesystem::remove_all(
std::filesystem::temp_directory_path() / "cortex");
CTL_INF("Deleted " << n << " files or directories");
} catch (const std::exception& e) {
CTL_WRN(e.what());
}

if (CORTEX_VARIANT == file_manager_utils::kProdVariant) {
if (!GetStable(v))
return;
Expand Down Expand Up @@ -75,9 +86,18 @@ bool CortexUpdCmd::GetStable(const std::string& v) {

// Replace binary file
auto executable_path = file_manager_utils::GetExecutableFolderContainerPath();
auto src = std::filesystem::temp_directory_path() / "cortex" / kCortexBinary /
GetCortexBinary();
auto src =
std::filesystem::temp_directory_path() / "cortex" / GetCortexBinary();
auto dst = executable_path / GetCortexBinary();
utils::ScopeExit se([]() {
auto cortex_tmp = std::filesystem::temp_directory_path() / "cortex";
try {
auto n = std::filesystem::remove_all(cortex_tmp);
CTL_INF("Deleted " << n << " files or directories");
} catch (const std::exception& e) {
CTL_WRN(e.what());
}
});
return ReplaceBinaryInflight(src, dst);
}

Expand Down Expand Up @@ -135,6 +155,15 @@ bool CortexUpdCmd::GetBeta(const std::string& v) {
auto src =
std::filesystem::temp_directory_path() / "cortex" / GetCortexBinary();
auto dst = executable_path / GetCortexBinary();
utils::ScopeExit se([]() {
auto cortex_tmp = std::filesystem::temp_directory_path() / "cortex";
try {
auto n = std::filesystem::remove_all(cortex_tmp);
CTL_INF("Deleted " << n << " files or directories");
} catch (const std::exception& e) {
CTL_WRN(e.what());
}
});
return ReplaceBinaryInflight(src, dst);
}

Expand Down Expand Up @@ -264,6 +293,15 @@ bool CortexUpdCmd::GetNightly(const std::string& v) {
auto src =
std::filesystem::temp_directory_path() / "cortex" / GetCortexBinary();
auto dst = executable_path / GetCortexBinary();
utils::ScopeExit se([]() {
auto cortex_tmp = std::filesystem::temp_directory_path() / "cortex";
try {
auto n = std::filesystem::remove_all(cortex_tmp);
CTL_INF("Deleted " << n << " files or directories");
} catch (const std::exception& e) {
CTL_WRN(e.what());
}
});
return ReplaceBinaryInflight(src, dst);
}
} // namespace commands
3 changes: 3 additions & 0 deletions engine/e2e-test/test_cortex_update.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import pytest
from test_runner import run
import tempfile
import os


class TestCortexUpdate:
Expand All @@ -10,3 +12,4 @@ def test_cortex_update(self):
exit_code, output, error = run("Update cortex", ["update"])
assert exit_code == 0, "Something went wrong"
assert "Updated cortex sucessfully" in output
assert os.path.exists(os.path.join(tempfile.gettempdir()), 'cortex') == False
15 changes: 15 additions & 0 deletions engine/utils/scope_exit.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once

namespace utils {
template <typename F>
struct ScopeExit {
ScopeExit(F&& f) : f_(std::forward<F>(f)) {}
~ScopeExit() { f_(); }
F f_;
};

template <typename F>
ScopeExit<F> makeScopeExit(F&& f) {
return ScopeExit<F>(std::forward<F>(f));
};
} // namespace utils