Port relative path 2022.3 (#15523)
* Add test to verify add_extension with relative path (#15212) * Add test to verify add_extension with relative path * Fix code style * Use std::string::find instead of std::regex * Remove unnecessary include * Add comments about generating relative path * Don't add empty tokens when splitting path * Resolve relative path to the extension * Method available independent of build_type
This commit is contained in:
@@ -98,6 +98,17 @@ std::string findPluginXML(const std::string& xmlFile) {
|
||||
|
||||
#endif
|
||||
|
||||
std::string resolve_extension_path(const std::string& path) {
|
||||
std::string retvalue;
|
||||
try {
|
||||
const std::string absolute_path = ov::util::get_absolute_file_path(path);
|
||||
retvalue = FileUtils::fileExist(absolute_path) ? absolute_path : path;
|
||||
} catch (const std::runtime_error&) {
|
||||
retvalue = path;
|
||||
}
|
||||
return retvalue;
|
||||
}
|
||||
|
||||
ov::util::FilePath getPluginPath(const std::string& pluginName, bool needAddSuffixes = false) {
|
||||
const auto ieLibraryPath = ie::getInferenceEngineLibraryPath();
|
||||
|
||||
@@ -2019,7 +2030,8 @@ void Core::add_extension(const ie::IExtensionPtr& extension) {
|
||||
|
||||
void Core::add_extension(const std::string& library_path) {
|
||||
try {
|
||||
add_extension(ov::detail::load_extensions(library_path));
|
||||
const std::string path = resolve_extension_path(library_path);
|
||||
add_extension(ov::detail::load_extensions(path));
|
||||
} catch (const std::runtime_error&) {
|
||||
try {
|
||||
// Try to load legacy extension
|
||||
@@ -2035,7 +2047,8 @@ void Core::add_extension(const std::string& library_path) {
|
||||
#ifdef OPENVINO_ENABLE_UNICODE_PATH_SUPPORT
|
||||
void Core::add_extension(const std::wstring& library_path) {
|
||||
try {
|
||||
add_extension(ov::detail::load_extensions(library_path));
|
||||
const std::string path = resolve_extension_path(ov::util::wstring_to_string(library_path));
|
||||
add_extension(ov::detail::load_extensions(ov::util::string_to_wstring(path)));
|
||||
} catch (const std::runtime_error&) {
|
||||
try {
|
||||
// Try to load legacy extension
|
||||
|
||||
@@ -190,6 +190,13 @@ std::string getIncorrectExtensionPath() {
|
||||
std::string("incorrect") + IE_BUILD_POSTFIX);
|
||||
}
|
||||
|
||||
std::string getRelativeOVExtensionPath() {
|
||||
std::string absolutePath =
|
||||
ov::util::make_plugin_library_name(CommonTestUtils::getExecutableDirectory(),
|
||||
std::string("openvino_template_extension") + IE_BUILD_POSTFIX);
|
||||
return CommonTestUtils::getRelativePath(CommonTestUtils::getCurrentWorkingDir(), absolutePath);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
class CustomOldIdentity : public ngraph::op::Op {
|
||||
@@ -358,4 +365,8 @@ TEST_F(OVExtensionTests, load_old_extension) {
|
||||
TEST_F(OVExtensionTests, load_incorrect_extension) {
|
||||
EXPECT_THROW(core.add_extension(getIncorrectExtensionPath()), ov::Exception);
|
||||
}
|
||||
#endif //defined(ENABLE_OV_IR_FRONTEND)
|
||||
|
||||
TEST_F(OVExtensionTests, load_relative) {
|
||||
EXPECT_NO_THROW(core.add_extension(getRelativeOVExtensionPath()));
|
||||
}
|
||||
#endif // defined(ENABLE_OV_IR_FRONTEND)
|
||||
|
||||
@@ -2,8 +2,11 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
//
|
||||
|
||||
#include <openvino/util/file_util.hpp>
|
||||
#include <cstring>
|
||||
#include <numeric>
|
||||
#include <openvino/util/file_util.hpp>
|
||||
#include <regex>
|
||||
#include <sstream>
|
||||
|
||||
#ifdef __APPLE__
|
||||
# include <mach-o/dyld.h>
|
||||
@@ -14,6 +17,8 @@
|
||||
# define NOMINMAX
|
||||
# endif
|
||||
# include <Windows.h>
|
||||
# include <direct.h>
|
||||
# include <stdlib.h>
|
||||
#else
|
||||
# include <dlfcn.h>
|
||||
# include <unistd.h>
|
||||
@@ -43,8 +48,87 @@ std::string getExecutableDirectory() {
|
||||
return ov::util::get_directory(path);
|
||||
}
|
||||
|
||||
std::string getModelFromTestModelZoo(const std::string & relModelPath) {
|
||||
std::string getCurrentWorkingDir() {
|
||||
std::string path;
|
||||
#ifdef _WIN32
|
||||
char * buffer = _getcwd(NULL, 0);
|
||||
if (buffer != NULL) {
|
||||
path = std::string(buffer);
|
||||
free(buffer);
|
||||
}
|
||||
#else
|
||||
char buffer[PATH_MAX];
|
||||
auto result = getcwd(buffer, sizeof(buffer));
|
||||
if (result != NULL) {
|
||||
path = std::string(buffer);
|
||||
} else {
|
||||
int error = errno;
|
||||
std::ostringstream str;
|
||||
str << "Can't get access to the current working directory, error:" << error;
|
||||
throw std::runtime_error(str.str());
|
||||
}
|
||||
#endif
|
||||
return path;
|
||||
}
|
||||
|
||||
std::string getModelFromTestModelZoo(const std::string& relModelPath) {
|
||||
return ov::util::path_join({CommonTestUtils::getExecutableDirectory(), relModelPath});
|
||||
}
|
||||
|
||||
} // namespace CommonTestUtils
|
||||
std::string getRelativePath(const std::string& from, const std::string& to) {
|
||||
auto split_path = [](const std::string& path) -> std::vector<std::string> {
|
||||
std::string sep{ov::util::FileTraits<char>::file_separator};
|
||||
std::vector<std::string> retvalue;
|
||||
size_t start = 0;
|
||||
size_t end = 0;
|
||||
std::string token;
|
||||
while ((end = path.find(sep, start)) != std::string::npos) {
|
||||
token = path.substr(start, end - start);
|
||||
start = end + 1;
|
||||
if (!token.empty())
|
||||
retvalue.push_back(token);
|
||||
}
|
||||
|
||||
token = path.substr(start);
|
||||
if (!token.empty()) {
|
||||
retvalue.push_back(token);
|
||||
}
|
||||
return retvalue;
|
||||
};
|
||||
|
||||
auto from_vec = split_path(from);
|
||||
auto to_vec = split_path(to);
|
||||
|
||||
auto mismatch_it = std::mismatch(from_vec.begin(), from_vec.end(), to_vec.begin());
|
||||
if (mismatch_it.first == from_vec.end() && mismatch_it.second == to_vec.end()) {
|
||||
return {};
|
||||
}
|
||||
|
||||
std::string separator(1, ov::util::FileTraits<char>::file_separator);
|
||||
std::string output;
|
||||
// generates path to the top common directory from the start directory
|
||||
if (mismatch_it.first != from_vec.end()) {
|
||||
// adds signs: "../" until it meets the top common directory
|
||||
// for example if start path is: /aaa/bbb/ddd/eee and destination path is: /aaa/bbb/cc/test_app
|
||||
// it generates: "../../"
|
||||
output += std::accumulate(mismatch_it.first,
|
||||
from_vec.end(),
|
||||
std::string{},
|
||||
[&separator](std::string& a, const std::string&) -> std::string {
|
||||
return a += ".." + separator;
|
||||
});
|
||||
}
|
||||
// adds path to the destination. If before generates path contains signs: "../",
|
||||
// for example if start path is: "/aaa/bbb/ddd/eee" and destination path is: "/aaa/bbb/cc/test_app"
|
||||
// To the generated path: "../../" adds: "cc/test_app",
|
||||
// the output path is: "../../cc/test_app"
|
||||
output += std::accumulate(mismatch_it.second,
|
||||
to_vec.end(),
|
||||
std::string{},
|
||||
[&separator](std::string& a, const std::string& b) -> std::string {
|
||||
return a.empty() ? a += b : a += separator + b;
|
||||
});
|
||||
return output;
|
||||
}
|
||||
|
||||
} // namespace CommonTestUtils
|
||||
|
||||
@@ -270,5 +270,7 @@ inline std::vector<std::string> readListFiles(const std::vector<std::string>& fi
|
||||
}
|
||||
|
||||
std::string getExecutableDirectory();
|
||||
std::string getCurrentWorkingDir();
|
||||
std::string getRelativePath(const std::string& from, const std::string& to);
|
||||
|
||||
} // namespace CommonTestUtils
|
||||
|
||||
Reference in New Issue
Block a user