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.4 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. namespace mindspore {
  23. std::optional<std::string> Common::GetRealPath(const std::string &input_path) {
  24. std::string out_path;
  25. auto path_split_pos = input_path.find_last_of('/');
  26. if (path_split_pos == std::string::npos) {
  27. path_split_pos = input_path.find_last_of('\\');
  28. }
  29. // get real path
  30. char real_path[PATH_MAX] = {0};
  31. if (path_split_pos != std::string::npos) {
  32. std::string prefix_path = input_path.substr(0, path_split_pos);
  33. if (prefix_path.length() >= PATH_MAX) {
  34. MS_LOG(ERROR) << "Prefix path is too longer!";
  35. return std::nullopt;
  36. }
  37. std::string last_path = input_path.substr(path_split_pos, input_path.length() - path_split_pos);
  38. auto ret = CreateNotExistDirs(prefix_path);
  39. if (!ret) {
  40. MS_LOG(ERROR) << "CreateNotExistDirs Failed!";
  41. return std::nullopt;
  42. }
  43. if (nullptr == realpath(prefix_path.c_str(), real_path)) {
  44. MS_LOG(ERROR) << "dir " << prefix_path << " does not exit.";
  45. return std::nullopt;
  46. }
  47. out_path = std::string(real_path) + last_path;
  48. }
  49. if (path_split_pos == std::string::npos) {
  50. if (input_path.length() >= PATH_MAX) {
  51. MS_LOG(ERROR) << "Prefix path is too longer!";
  52. return std::nullopt;
  53. }
  54. if (nullptr == realpath(input_path.c_str(), real_path)) {
  55. MS_LOG(ERROR) << "File " << input_path << " does not exit, it will be created.";
  56. }
  57. out_path = std::string(real_path);
  58. }
  59. return out_path;
  60. }
  61. bool Common::CreateNotExistDirs(const std::string &path) {
  62. std::shared_ptr<system::FileSystem> fs = system::Env::GetFileSystem();
  63. MS_EXCEPTION_IF_NULL(fs);
  64. char temp_path[PATH_MAX] = {0};
  65. if (path.length() > PATH_MAX) {
  66. MS_LOG(ERROR) << "Path lens is max than " << PATH_MAX;
  67. return false;
  68. }
  69. for (uint32_t i = 0; i < path.length(); i++) {
  70. temp_path[i] = path[i];
  71. if (temp_path[i] == '\\' || temp_path[i] == '/') {
  72. if (i != 0) {
  73. char tmp_char = temp_path[i];
  74. temp_path[i] = '\0';
  75. std::string path_handle(temp_path);
  76. if (!fs->FileExist(path_handle)) {
  77. MS_LOG(INFO) << "Dir " << path_handle << " does not exit, creating...";
  78. if (!fs->CreateDir(path_handle)) {
  79. MS_LOG(ERROR) << "Create " << path_handle << " dir error";
  80. return false;
  81. }
  82. }
  83. temp_path[i] = tmp_char;
  84. }
  85. }
  86. }
  87. if (!fs->FileExist(path)) {
  88. MS_LOG(INFO) << "Dir " << path << " does not exit, creating...";
  89. if (!fs->CreateDir(path)) {
  90. MS_LOG(ERROR) << "Create " << path << " dir error";
  91. return false;
  92. }
  93. }
  94. return true;
  95. }
  96. std::optional<std::string> Common::GetConfigFile(const std::string &env) {
  97. if (env.empty()) {
  98. MS_LOG(EXCEPTION) << "Invalid env";
  99. }
  100. auto config_path_str = std::getenv(env.c_str());
  101. if (config_path_str == nullptr) {
  102. MS_LOG(ERROR) << "Please export env:" << env;
  103. return {};
  104. }
  105. MS_LOG(INFO) << "Async Dump Getenv env:" << env << "=" << config_path_str;
  106. std::string dump_config_file(config_path_str);
  107. std::shared_ptr<system::FileSystem> fs = system::Env::GetFileSystem();
  108. MS_EXCEPTION_IF_NULL(fs);
  109. if (!fs->FileExist(dump_config_file)) {
  110. MS_LOG(ERROR) << dump_config_file << " not exist.";
  111. return {};
  112. }
  113. auto point_pos = dump_config_file.find_last_of('.');
  114. if (point_pos == std::string::npos) {
  115. MS_LOG(EXCEPTION) << "Invalid json file name:" << dump_config_file;
  116. }
  117. auto suffix = dump_config_file.substr(point_pos + 1);
  118. if (suffix != "json") {
  119. MS_LOG(EXCEPTION) << "[DataDump] dump config file suffix only support json! But got:." << suffix;
  120. }
  121. return dump_config_file;
  122. }
  123. } // namespace mindspore