Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.

Commit 8c06f78

Browse files
authored
fix: correct path to work with data folder structure (#1132)
1 parent 390111c commit 8c06f78

File tree

9 files changed

+88
-71
lines changed

9 files changed

+88
-71
lines changed

engine/commands/model_get_cmd.cc

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "config/yaml_config.h"
77
#include "trantor/utils/Logger.h"
88
#include "utils/cortex_utils.h"
9+
#include "utils/file_manager_utils.h"
910
#include "utils/logging_utils.h"
1011

1112
namespace commands {
@@ -14,15 +15,15 @@ ModelGetCmd::ModelGetCmd(std::string model_handle)
1415
: model_handle_(std::move(model_handle)) {}
1516

1617
void ModelGetCmd::Exec() {
17-
if (std::filesystem::exists(cortex_utils::models_folder) &&
18-
std::filesystem::is_directory(cortex_utils::models_folder)) {
18+
auto models_path = file_manager_utils::GetModelsContainerPath();
19+
if (std::filesystem::exists(models_path) &&
20+
std::filesystem::is_directory(models_path)) {
1921
CmdInfo ci(model_handle_);
2022
std::string model_file =
2123
ci.branch == "main" ? ci.model_name : ci.model_name + "-" + ci.branch;
2224
bool found_model = false;
2325
// Iterate through directory
24-
for (const auto& entry :
25-
std::filesystem::directory_iterator(cortex_utils::models_folder)) {
26+
for (const auto& entry : std::filesystem::directory_iterator(models_path)) {
2627

2728
if (entry.is_regular_file() && entry.path().stem() == model_file &&
2829
entry.path().extension() == ".yaml") {
@@ -137,7 +138,7 @@ void ModelGetCmd::Exec() {
137138
break;
138139
} catch (const std::exception& e) {
139140
CTL_ERR("Error reading yaml file '" << entry.path().string()
140-
<< "': " << e.what());
141+
<< "': " << e.what());
141142
}
142143
}
143144
}

engine/commands/model_list_cmd.cc

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,22 @@
88
#include <vector>
99
#include "config/yaml_config.h"
1010
#include "trantor/utils/Logger.h"
11+
#include "utils/file_manager_utils.h"
1112
#include "utils/logging_utils.h"
13+
1214
namespace commands {
1315

1416
void ModelListCmd::Exec() {
15-
if (std::filesystem::exists(cortex_utils::models_folder) &&
16-
std::filesystem::is_directory(cortex_utils::models_folder)) {
17+
auto models_path = file_manager_utils::GetModelsContainerPath();
18+
if (std::filesystem::exists(models_path) &&
19+
std::filesystem::is_directory(models_path)) {
1720
tabulate::Table table;
1821

1922
table.add_row({"(Index)", "ID", "engine", "version"});
2023
table.format().font_color(tabulate::Color::green);
2124
int count = 0;
2225
// Iterate through directory
23-
for (const auto& entry :
24-
std::filesystem::directory_iterator(cortex_utils::models_folder)) {
26+
for (const auto& entry : std::filesystem::directory_iterator(models_path)) {
2527
if (entry.is_regular_file() && entry.path().extension() == ".yaml") {
2628
try {
2729
count += 1;
@@ -32,7 +34,7 @@ void ModelListCmd::Exec() {
3234
model_config.engine, model_config.version});
3335
} catch (const std::exception& e) {
3436
CTL_ERR("Error reading yaml file '" << entry.path().string()
35-
<< "': " << e.what());
37+
<< "': " << e.what());
3638
}
3739
}
3840
}

engine/commands/run_cmd.cc

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "model_start_cmd.h"
88
#include "trantor/utils/Logger.h"
99
#include "utils/cortex_utils.h"
10+
#include "utils/file_manager_utils.h"
1011

1112
namespace commands {
1213

@@ -42,8 +43,9 @@ void RunCmd::Exec() {
4243

4344
// Start model
4445
config::YamlHandler yaml_handler;
45-
yaml_handler.ModelConfigFromFile(cortex_utils::GetCurrentPath() + "/models/" +
46-
model_file + ".yaml");
46+
yaml_handler.ModelConfigFromFile(
47+
file_manager_utils::GetModelsContainerPath().string() + "/" + model_file +
48+
".yaml");
4749
{
4850
ModelStartCmd msc(host_, port_, yaml_handler.GetModelConfig());
4951
if (!msc.Exec()) {
@@ -59,14 +61,11 @@ void RunCmd::Exec() {
5961
}
6062

6163
bool RunCmd::IsModelExisted(const std::string& model_id) {
62-
if (std::filesystem::exists(cortex_utils::GetCurrentPath() + "/" +
63-
cortex_utils::models_folder) &&
64-
std::filesystem::is_directory(cortex_utils::GetCurrentPath() + "/" +
65-
cortex_utils::models_folder)) {
64+
auto models_path = file_manager_utils::GetModelsContainerPath();
65+
if (std::filesystem::exists(models_path) &&
66+
std::filesystem::is_directory(models_path)) {
6667
// Iterate through directory
67-
for (const auto& entry : std::filesystem::directory_iterator(
68-
cortex_utils::GetCurrentPath() + "/" +
69-
cortex_utils::models_folder)) {
68+
for (const auto& entry : std::filesystem::directory_iterator(models_path)) {
7069
if (entry.is_regular_file() && entry.path().extension() == ".yaml") {
7170
try {
7271
config::YamlHandler handler;
@@ -85,10 +84,9 @@ bool RunCmd::IsModelExisted(const std::string& model_id) {
8584
}
8685

8786
bool RunCmd::IsEngineExisted(const std::string& e) {
88-
if (std::filesystem::exists(cortex_utils::GetCurrentPath() + "/" +
89-
"engines") &&
90-
std::filesystem::exists(cortex_utils::GetCurrentPath() + "/" +
91-
"engines/" + e)) {
87+
auto engines_path = file_manager_utils::GetEnginesContainerPath();
88+
if (std::filesystem::exists(engines_path) &&
89+
std::filesystem::exists(engines_path.string() + "/" + e)) {
9290
return true;
9391
}
9492
return false;

engine/controllers/command_line_parser.cc

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "httplib.h"
1818
#include "services/engine_service.h"
1919
#include "utils/cortex_utils.h"
20+
#include "utils/file_manager_utils.h"
2021
#include "utils/logging_utils.h"
2122

2223
CommandLineParser::CommandLineParser()
@@ -38,8 +39,9 @@ bool CommandLineParser::SetupCommand(int argc, char** argv) {
3839
std::string model_file =
3940
ci.branch == "main" ? ci.model_name : ci.model_name + "-" + ci.branch;
4041
config::YamlHandler yaml_handler;
41-
yaml_handler.ModelConfigFromFile(cortex_utils::GetCurrentPath() +
42-
"/models/" + model_file + ".yaml");
42+
yaml_handler.ModelConfigFromFile(
43+
file_manager_utils::GetModelsContainerPath().string() + "/" +
44+
model_file + ".yaml");
4345
commands::ModelStartCmd msc("127.0.0.1", 3928,
4446
yaml_handler.GetModelConfig());
4547
msc.Exec();
@@ -53,8 +55,9 @@ bool CommandLineParser::SetupCommand(int argc, char** argv) {
5355
std::string model_file =
5456
ci.branch == "main" ? ci.model_name : ci.model_name + "-" + ci.branch;
5557
config::YamlHandler yaml_handler;
56-
yaml_handler.ModelConfigFromFile(cortex_utils::GetCurrentPath() +
57-
"/models/" + model_file + ".yaml");
58+
yaml_handler.ModelConfigFromFile(
59+
file_manager_utils::GetModelsContainerPath().string() + "/" +
60+
model_file + ".yaml");
5861
commands::ModelStopCmd smc("127.0.0.1", 3928,
5962
yaml_handler.GetModelConfig());
6063
smc.Exec();
@@ -107,8 +110,9 @@ bool CommandLineParser::SetupCommand(int argc, char** argv) {
107110
std::string model_file =
108111
ci.branch == "main" ? ci.model_name : ci.model_name + "-" + ci.branch;
109112
config::YamlHandler yaml_handler;
110-
yaml_handler.ModelConfigFromFile(cortex_utils::GetCurrentPath() +
111-
"/models/" + model_file + ".yaml");
113+
yaml_handler.ModelConfigFromFile(
114+
file_manager_utils::GetModelsContainerPath().string() + "/" +
115+
model_file + ".yaml");
112116
commands::ChatCmd cc("127.0.0.1", 3928, yaml_handler.GetModelConfig());
113117
cc.Exec(msg);
114118
});

engine/controllers/models.cc

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "config/yaml_config.h"
33
#include "trantor/utils/Logger.h"
44
#include "utils/cortex_utils.h"
5+
#include "utils/file_manager_utils.h"
56
#include "utils/model_callback_utils.h"
67

78
void Models::PullModel(
@@ -49,11 +50,11 @@ void Models::ListModel(
4950
Json::Value ret;
5051
ret["object"] = "list";
5152
Json::Value data(Json::arrayValue);
52-
if (std::filesystem::exists(cortex_utils::models_folder) &&
53-
std::filesystem::is_directory(cortex_utils::models_folder)) {
53+
auto models_path = file_manager_utils::GetModelsContainerPath();
54+
if (std::filesystem::exists(models_path) &&
55+
std::filesystem::is_directory(models_path)) {
5456
// Iterate through directory
55-
for (const auto& entry :
56-
std::filesystem::directory_iterator(cortex_utils::models_folder)) {
57+
for (const auto& entry : std::filesystem::directory_iterator(models_path)) {
5758
if (entry.is_regular_file() && entry.path().extension() == ".yaml") {
5859
try {
5960
config::YamlHandler handler;

engine/controllers/server.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "utils/cortex_utils.h"
99
#include "utils/cpuid/cpu_info.h"
1010
#include "utils/logging_utils.h"
11+
#include "utils/file_manager_utils.h"
1112

1213
using namespace inferences;
1314
using json = nlohmann::json;
@@ -290,8 +291,9 @@ void server::LoadModel(const HttpRequestPtr& req,
290291

291292
std::string abs_path =
292293
(getenv("ENGINE_PATH") ? getenv("ENGINE_PATH")
293-
: cortex_utils::GetCurrentPath()) +
294+
: file_manager_utils::GetCortexDataPath().string()) +
294295
get_engine_path(engine_type);
296+
std::cout << abs_path << std::endl;
295297
engines_[engine_type].dl =
296298
std::make_unique<cortex_cpp::dylib>(abs_path, "engine");
297299

engine/main.cc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#include "controllers/command_line_parser.h"
55
#include "cortex-common/cortexpythoni.h"
66
#include "utils/archive_utils.h"
7-
#include "utils/config_yaml_utils.h"
87
#include "utils/cortex_utils.h"
98
#include "utils/dylib.h"
109
#include "utils/file_manager_utils.h"
@@ -26,7 +25,7 @@
2625
#endif
2726

2827
void RunServer() {
29-
auto config = config_yaml_utils::GetCortexConfig();
28+
auto config = file_manager_utils::GetCortexConfig();
3029
LOG_INFO << "Host: " << config.host << " Port: " << config.port << "\n";
3130

3231
// Create logs/ folder and setup log to file
@@ -127,7 +126,7 @@ void ForkProcess() {
127126
}
128127

129128
int main(int argc, char* argv[]) {
130-
{ config_yaml_utils::CreateConfigFileIfNotExist(); }
129+
{ file_manager_utils::CreateConfigFileIfNotExist(); }
131130

132131
// Check if this process is for python execution
133132
if (argc > 1) {

engine/utils/config_yaml_utils.h

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
#pragma once
12
#include <filesystem>
23
#include <fstream>
34
#include <iostream>
45
#include <string>
5-
#include "utils/file_manager_utils.h"
66
#include "utils/logging_utils.h"
77
#include "yaml-cpp/yaml.h"
88

@@ -39,24 +39,6 @@ inline void DumpYamlConfig(const CortexConfig& config,
3939
}
4040
}
4141

42-
inline void CreateConfigFileIfNotExist() {
43-
auto config_path = file_manager_utils::GetConfigurationPath();
44-
if (std::filesystem::exists(config_path)) {
45-
// already exists
46-
return;
47-
}
48-
CLI_LOG("Config file not found. Creating one at " + config_path.string());
49-
auto defaultDataFolderPath =
50-
file_manager_utils::GetHomeDirectoryPath() / kCortexFolderName;
51-
auto config = CortexConfig{
52-
.dataFolderPath = defaultDataFolderPath.string(),
53-
.host = kDefaultHost,
54-
.port = kDefaultPort,
55-
};
56-
std::cout << "config: " << config.dataFolderPath << "\n";
57-
DumpYamlConfig(config, config_path.string());
58-
}
59-
6042
inline CortexConfig FromYaml(const std::string& path,
6143
const std::string& variant) {
6244
std::filesystem::path config_file_path{path};
@@ -78,9 +60,5 @@ inline CortexConfig FromYaml(const std::string& path,
7860
}
7961
}
8062

81-
inline CortexConfig GetCortexConfig() {
82-
auto config_path = file_manager_utils::GetConfigurationPath();
83-
std::string variant = ""; // TODO: empty for now
84-
return FromYaml(config_path.string(), variant);
85-
}
63+
8664
} // namespace config_yaml_utils

engine/utils/file_manager_utils.h

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <string_view>
55
#include "logging_utils.h"
66
#include "services/download_service.h"
7+
#include "utils/config_yaml_utils.h"
78

89
#if defined(__APPLE__) && defined(__MACH__)
910
#include <mach-o/dyld.h>
@@ -84,23 +85,54 @@ inline std::filesystem::path GetConfigurationPath() {
8485
return configuration_path;
8586
}
8687

87-
inline std::filesystem::path GetCortexPath() {
88+
inline void CreateConfigFileIfNotExist() {
89+
auto config_path = file_manager_utils::GetConfigurationPath();
90+
if (std::filesystem::exists(config_path)) {
91+
// already exists
92+
return;
93+
}
94+
CLI_LOG("Config file not found. Creating one at " + config_path.string());
95+
auto defaultDataFolderPath =
96+
file_manager_utils::GetHomeDirectoryPath() / config_yaml_utils::kCortexFolderName;
97+
auto config = config_yaml_utils::CortexConfig{
98+
.dataFolderPath = defaultDataFolderPath.string(),
99+
.host = config_yaml_utils::kDefaultHost,
100+
.port = config_yaml_utils::kDefaultPort,
101+
};
102+
std::cout << "config: " << config.dataFolderPath << "\n";
103+
DumpYamlConfig(config, config_path.string());
104+
}
105+
106+
inline config_yaml_utils::CortexConfig GetCortexConfig() {
107+
auto config_path = GetConfigurationPath();
108+
std::string variant = ""; // TODO: empty for now
109+
return config_yaml_utils::FromYaml(config_path.string(), variant);
110+
}
111+
112+
inline std::filesystem::path GetCortexDataPath() {
88113
// TODO: We will need to support user to move the data folder to other place.
89114
// TODO: get the variant of cortex. As discussed, we will have: prod, beta, nightly
90-
// currently we will store cortex data at ~/.cortex
115+
// currently we will store cortex data at ~/cortexcpp
116+
auto config = GetCortexConfig();
117+
std::filesystem::path data_folder_path;
118+
if (!config.dataFolderPath.empty()) {
119+
data_folder_path =
120+
std::filesystem::path(config.dataFolderPath);
121+
} else {
122+
auto home_path = GetHomeDirectoryPath();
123+
data_folder_path = home_path / config_yaml_utils::kCortexFolderName;
124+
}
91125

92-
auto home_path = GetHomeDirectoryPath();
93-
auto cortex_path = home_path / ".cortex";
94-
if (!std::filesystem::exists(cortex_path)) {
126+
if (!std::filesystem::exists(data_folder_path)) {
95127
CTL_INF("Cortex home folder not found. Create one: " +
96-
cortex_path.string());
97-
std::filesystem::create_directory(cortex_path);
128+
data_folder_path.string());
129+
std::filesystem::create_directory(data_folder_path);
98130
}
99-
return cortex_path;
131+
return data_folder_path;
100132
}
101133

102134
inline std::filesystem::path GetModelsContainerPath() {
103-
auto cortex_path = GetCortexPath();
135+
auto cortex_path = GetCortexDataPath();
104136
auto models_container_path = cortex_path / "models";
105137

106138
if (!std::filesystem::exists(models_container_path)) {
@@ -113,7 +145,7 @@ inline std::filesystem::path GetModelsContainerPath() {
113145
}
114146

115147
inline std::filesystem::path GetEnginesContainerPath() {
116-
auto cortex_path = GetCortexPath();
148+
auto cortex_path = GetCortexDataPath();
117149
auto engines_container_path = cortex_path / "engines";
118150

119151
if (!std::filesystem::exists(engines_container_path)) {

0 commit comments

Comments
 (0)