You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

common.cc 4.3 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /**
  2. * Copyright 2020 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "debug/common.h"
  17. #include <memory>
  18. #include <optional>
  19. #include "utils/system/env.h"
  20. #include "utils/system/file_system.h"
  21. #include "utils/log_adapter.h"
  22. #include "utils/context/ms_context.h"
  23. namespace mindspore {
  24. std::optional<std::string> Common::GetRealPath(const std::string &input_path) {
  25. std::string out_path;
  26. auto path_split_pos = input_path.find_last_of('/');
  27. if (path_split_pos == std::string::npos) {
  28. path_split_pos = input_path.find_last_of('\\');
  29. }
  30. // get real path
  31. char real_path[PATH_MAX] = {0};
  32. if (path_split_pos != std::string::npos) {
  33. std::string prefix_path = input_path.substr(0, path_split_pos);
  34. if (prefix_path.length() >= PATH_MAX) {
  35. MS_LOG(ERROR) << "Prefix path is too longer!";
  36. return std::nullopt;
  37. }
  38. std::string last_path = input_path.substr(path_split_pos, input_path.length() - path_split_pos);
  39. auto ret = CreateNotExistDirs(prefix_path);
  40. if (!ret) {
  41. MS_LOG(ERROR) << "CreateNotExistDirs Failed!";
  42. return std::nullopt;
  43. }
  44. if (nullptr == realpath(prefix_path.c_str(), real_path)) {
  45. MS_LOG(ERROR) << "dir " << prefix_path << " does not exit.";
  46. return std::nullopt;
  47. }
  48. out_path = std::string(real_path) + last_path;
  49. }
  50. if (path_split_pos == std::string::npos) {
  51. if (input_path.length() >= PATH_MAX) {
  52. MS_LOG(ERROR) << "Prefix path is too longer!";
  53. return std::nullopt;
  54. }
  55. if (nullptr == realpath(input_path.c_str(), real_path)) {
  56. MS_LOG(ERROR) << "File " << input_path << " does not exit, it will be created.";
  57. }
  58. out_path = std::string(real_path);
  59. }
  60. return out_path;
  61. }
  62. bool Common::CreateNotExistDirs(const std::string &path) {
  63. std::shared_ptr<system::FileSystem> fs = system::Env::GetFileSystem();
  64. MS_EXCEPTION_IF_NULL(fs);
  65. char temp_path[PATH_MAX] = {0};
  66. if (path.length() > PATH_MAX) {
  67. MS_LOG(ERROR) << "Path lens is max than " << PATH_MAX;
  68. return false;
  69. }
  70. for (uint32_t i = 0; i < path.length(); i++) {
  71. temp_path[i] = path[i];
  72. if (temp_path[i] == '\\' || temp_path[i] == '/') {
  73. if (i != 0) {
  74. char tmp_char = temp_path[i];
  75. temp_path[i] = '\0';
  76. std::string path_handle(temp_path);
  77. if (!fs->FileExist(path_handle)) {
  78. MS_LOG(INFO) << "Dir " << path_handle << " does not exit, creating...";
  79. if (!fs->CreateDir(path_handle)) {
  80. MS_LOG(ERROR) << "Create " << path_handle << " dir error";
  81. return false;
  82. }
  83. }
  84. temp_path[i] = tmp_char;
  85. }
  86. }
  87. }
  88. if (!fs->FileExist(path)) {
  89. MS_LOG(INFO) << "Dir " << path << " does not exit, creating...";
  90. if (!fs->CreateDir(path)) {
  91. MS_LOG(ERROR) << "Create " << path << " dir error";
  92. return false;
  93. }
  94. }
  95. return true;
  96. }
  97. std::optional<std::string> Common::GetConfigFile(const std::string &env) {
  98. if (env.empty()) {
  99. MS_LOG(EXCEPTION) << "Invalid env";
  100. }
  101. auto config_path_str = std::getenv(env.c_str());
  102. if (config_path_str == nullptr) {
  103. MS_LOG(ERROR) << "Please export env:" << env;
  104. return {};
  105. }
  106. MS_LOG(INFO) << "Async Dump Getenv env:" << env << "=" << config_path_str;
  107. std::string dump_config_file(config_path_str);
  108. std::shared_ptr<system::FileSystem> fs = system::Env::GetFileSystem();
  109. MS_EXCEPTION_IF_NULL(fs);
  110. if (!fs->FileExist(dump_config_file)) {
  111. MS_LOG(ERROR) << dump_config_file << " not exist.";
  112. return {};
  113. }
  114. auto suffix = dump_config_file.substr(dump_config_file.find_last_of('.') + 1);
  115. if (suffix != "json") {
  116. MS_LOG(EXCEPTION) << "[DataDump] dump config file suffix only support json! But got:." << suffix;
  117. }
  118. return dump_config_file;
  119. }
  120. } // namespace mindspore