/tensorflow/python/lib/io/file_io.i

https://gitlab.com/github-cloud-corporation/tensorflow · Swig · 179 lines · 147 code · 18 blank · 14 comment · 0 complexity · 3e8a942a43731fe385a76144cf13fed1 MD5 · raw file

  1. /* Copyright 2016 The TensorFlow Authors. All Rights Reserved.
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. ==============================================================================*/
  12. %include "tensorflow/python/lib/core/strings.i"
  13. %include "tensorflow/python/platform/base.i"
  14. %{
  15. #include "tensorflow/c/tf_status_helper.h"
  16. #include "tensorflow/core/framework/types.h"
  17. #include "tensorflow/core/lib/core/status.h"
  18. #include "tensorflow/core/lib/core/stringpiece.h"
  19. #include "tensorflow/core/lib/io/path.h"
  20. #include "tensorflow/core/lib/io/match.h"
  21. #include "tensorflow/core/platform/env.h"
  22. #include "tensorflow/core/platform/file_statistics.h"
  23. #include "tensorflow/core/protobuf/meta_graph.pb.h"
  24. %}
  25. %{
  26. inline bool FileExists(const string& filename) {
  27. return tensorflow::Env::Default()->FileExists(filename);
  28. }
  29. inline bool FileExists(const tensorflow::StringPiece& filename) {
  30. return tensorflow::Env::Default()->FileExists(filename.ToString());
  31. }
  32. inline void DeleteFile(const string& filename, TF_Status* out_status) {
  33. tensorflow::Status status = tensorflow::Env::Default()->DeleteFile(filename);
  34. if (!status.ok()) {
  35. Set_TF_Status_from_Status(out_status, status);
  36. }
  37. }
  38. string ReadFileToString(const string& filename, TF_Status* out_status) {
  39. string file_content;
  40. tensorflow::Status status = ReadFileToString(tensorflow::Env::Default(),
  41. filename, &file_content);
  42. if (!status.ok()) {
  43. Set_TF_Status_from_Status(out_status, status);
  44. }
  45. return file_content;
  46. }
  47. void WriteStringToFile(const string& filename, const string& file_content,
  48. TF_Status* out_status) {
  49. tensorflow::Status status = WriteStringToFile(tensorflow::Env::Default(),
  50. filename, file_content);
  51. if (!status.ok()) {
  52. Set_TF_Status_from_Status(out_status, status);
  53. }
  54. }
  55. std::vector<string> GetMatchingFiles(const string& filename,
  56. TF_Status* out_status) {
  57. std::vector<string> results;
  58. tensorflow::Status status =
  59. tensorflow::io::GetMatchingFiles(tensorflow::Env::Default(), filename,
  60. &results);
  61. if (!status.ok()) {
  62. Set_TF_Status_from_Status(out_status, status);
  63. }
  64. return results;
  65. }
  66. void CreateDir(const string& dirname, TF_Status* out_status) {
  67. tensorflow::Status status = tensorflow::Env::Default()->CreateDir(dirname);
  68. if (!status.ok() && status.code() != tensorflow::error::ALREADY_EXISTS) {
  69. Set_TF_Status_from_Status(out_status, status);
  70. }
  71. }
  72. void CopyFile(const string& oldpath, const string& newpath, bool overwrite,
  73. TF_Status* out_status) {
  74. // If overwrite is false and the newpath file exists then it's an error.
  75. if (!overwrite && FileExists(newpath)) {
  76. TF_SetStatus(out_status, TF_ALREADY_EXISTS, "file already exists");
  77. return;
  78. }
  79. string file_content;
  80. tensorflow::Status status = ReadFileToString(tensorflow::Env::Default(),
  81. oldpath, &file_content);
  82. if (!status.ok()) {
  83. Set_TF_Status_from_Status(out_status, status);
  84. return;
  85. }
  86. status = WriteStringToFile(tensorflow::Env::Default(), newpath, file_content);
  87. if (!status.ok()) {
  88. Set_TF_Status_from_Status(out_status, status);
  89. }
  90. }
  91. void RenameFile(const string& src, const string& target, bool overwrite,
  92. TF_Status* out_status) {
  93. // If overwrite is false and the target file exists then its an error.
  94. if (!overwrite && FileExists(target)) {
  95. TF_SetStatus(out_status, TF_ALREADY_EXISTS, "file already exists");
  96. return;
  97. }
  98. tensorflow::Status status = tensorflow::Env::Default()->RenameFile(src,
  99. target);
  100. if (!status.ok()) {
  101. Set_TF_Status_from_Status(out_status, status);
  102. }
  103. }
  104. using tensorflow::int64;
  105. void DeleteRecursively(const string& dirname, TF_Status* out_status) {
  106. int64 undeleted_files, undeleted_dirs;
  107. tensorflow::Status status = tensorflow::Env::Default()->DeleteRecursively(
  108. dirname, &undeleted_files, &undeleted_dirs);
  109. if (!status.ok()) {
  110. Set_TF_Status_from_Status(out_status, status);
  111. return;
  112. }
  113. if (undeleted_files > 0 || undeleted_dirs > 0) {
  114. TF_SetStatus(out_status, TF_PERMISSION_DENIED,
  115. "could not fully delete dir");
  116. return;
  117. }
  118. }
  119. bool IsDirectory(const string& dirname, TF_Status* out_status) {
  120. tensorflow::Status status = tensorflow::Env::Default()->IsDirectory(dirname);
  121. if (status.ok()) {
  122. return true;
  123. }
  124. // FAILED_PRECONDITION Status response means path exists but isn't a dir.
  125. if (status.code() != tensorflow::error::FAILED_PRECONDITION) {
  126. Set_TF_Status_from_Status(out_status, status);
  127. }
  128. return false;
  129. }
  130. using tensorflow::FileStatistics;
  131. void Stat(const string& filename, FileStatistics* stats,
  132. TF_Status* out_status) {
  133. tensorflow::Status status = tensorflow::Env::Default()->Stat(filename,
  134. stats);
  135. if (!status.ok()) {
  136. Set_TF_Status_from_Status(out_status, status);
  137. }
  138. }
  139. %}
  140. // Wrap the above functions.
  141. inline bool FileExists(const string& filename);
  142. inline void DeleteFile(const string& filename, TF_Status* out_status);
  143. string ReadFileToString(const string& filename, TF_Status* out_status);
  144. void WriteStringToFile(const string& filename, const string& file_content,
  145. TF_Status* out_status);
  146. std::vector<string> GetMatchingFiles(const string& filename,
  147. TF_Status* out_status);
  148. void CreateDir(const string& dirname, TF_Status* out_status);
  149. void CopyFile(const string& oldpath, const string& newpath, bool overwrite,
  150. TF_Status* out_status);
  151. void RenameFile(const string& oldname, const string& newname, bool overwrite,
  152. TF_Status* out_status);
  153. void DeleteRecursively(const string& dirname, TF_Status* out_status);
  154. bool IsDirectory(const string& dirname, TF_Status* out_status);
  155. void Stat(const string& filename, tensorflow::FileStatistics* stats,
  156. TF_Status* out_status);
  157. %include "tensorflow/core/lib/io/path.h"
  158. %include "tensorflow/core/platform/file_statistics.h"