1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
6 // Created by user on 19.10.18.
9 #include "test_model_repo.hpp"
10 #include "test_model_path.hpp"
16 # include <os/windows/w_dirent.h>
21 #include <gtest/gtest.h>
25 static std::string getDirname (std::string filePath) {
26 std::vector<char> input(filePath.begin(), filePath.end());
28 return dirname(&*input.begin());
31 static std::string getDirname (std::string filePath) {
32 char dirname[_MAX_DIR];
33 _splitpath(filePath.c_str(), nullptr, dirname, nullptr, nullptr);
38 const char* getModelPathNonFatal() noexcept {
40 const char* models_path = std::getenv("MODELS_PATH");
42 if (models_path == nullptr && MODELS_PATH == nullptr) {
46 if (models_path == nullptr) {
57 static std::string get_models_path() {
58 const char* models_path = getModelPathNonFatal();
60 if (nullptr == models_path) {
61 ::testing::AssertionFailure() << "MODELS_PATH not defined";
64 return std::string(models_path);
67 static bool exist(const std::string& name) {
68 std::ifstream file(name);
69 if(!file) // If the file was not found, then file is 0, i.e. !file=1 or true.
70 return false; // The file was not found.
71 else // If the file was found, then file is non-0.
72 return true; // The file was found.
75 static std::vector<std::string> getModelsDirs() {
76 auto repo_list = get_model_repo();
77 int last_delimiter = 0;
78 std::vector<std::string> folders;
80 auto folderDelimiter = repo_list.find(':', last_delimiter);
81 if (folderDelimiter == std::string::npos) {
84 auto nextDelimiter = repo_list.find(';', last_delimiter);
85 folders.push_back(repo_list.substr(last_delimiter, folderDelimiter - last_delimiter));
87 if (nextDelimiter == std::string::npos) {
91 last_delimiter = nextDelimiter + 1;
96 ModelsPath::operator std::string() const {
98 std::vector<std::string> absModelsPath;
99 for (auto & path : getModelsDirs()) {
100 absModelsPath.push_back(get_models_path() + kPathSeparator + "src" + kPathSeparator + path + _rel_path.str());
101 if (exist(absModelsPath.back())) {
102 return absModelsPath.back();
104 //checking models for precision encoded in folder name
105 auto dirname = getDirname(absModelsPath.back());
106 std::vector<std::pair<std::string, std::string>> stdprecisions = {
113 auto filename = absModelsPath.back().substr(dirname.size() + 1);
115 for (auto &precision : stdprecisions) {
116 auto havePrecision = filename.find(precision.first);
117 if (havePrecision == std::string::npos) continue;
119 auto newName = filename.replace(havePrecision, precision.first.size(), "");
120 newName = dirname + kPathSeparator + precision.second + kPathSeparator + newName;
122 if (exist(newName)) {
129 auto getModelsDirname = [](std::string path) -> std::string {
130 std::string dir = getDirname(path);
133 if (stat(dir.c_str(), &sb) != 0 || !S_ISDIR(sb.st_mode)) {
139 for (auto & path : absModelsPath) {
140 std::string publicDir = getModelsDirname(path);
142 if (!publicDir.empty()) {
146 std::stringstream errorMsg;
147 errorMsg<< "path to model invalid, models found at: \n";
149 for (auto & path : absModelsPath) {
150 errorMsg << path <<"\n";
152 errorMsg << "also searched by parent directory names: \n";
153 for (auto & path : absModelsPath) {
154 errorMsg << getDirname(path) << "\n";
157 std::cout << errorMsg.str();
158 ::testing::AssertionFailure() << errorMsg.str();
160 // doesn't matter what to return here