Add access to TEST_UNDECLARED_OUTPUTS_DIR which takes into account Bazel's use

of `/` as a path separator on Windows.

Note: This will have no impact until a later change to use `\` as path separator
is checked in.
PiperOrigin-RevId: 296005736
Change-Id: Id7ada3b06e38399fd17df76fb5c8d3b0ea70e0e2
This commit is contained in:
Brian Atkinson 2020-02-19 10:52:26 -08:00 committed by TensorFlower Gardener
parent 64d98345d3
commit 0763de0044
3 changed files with 42 additions and 9 deletions
tensorflow/core

View File

@ -40,6 +40,27 @@ namespace {
const char kPathSep[] = "/";
bool FixBazelEnvPath(const char* path, string* out) {
if (path == nullptr) return false;
if (out == nullptr) return true;
*out = path;
#ifdef PLATFORM_WINDOWS
// On Windows, paths generated by Bazel are always use `/` as the path
// separator. This prevents normal path management. In the event there are no
// `\` in the path, we convert all `/` to `\`.
if (out->find('\\') != string::npos) return path;
for (size_t pos = out->find('/'); pos != string::npos;
pos = out->find('/', pos + 1)) {
(*out)[pos] = kPathSep[0];
}
#endif
return true;
}
} // namespace
string JoinPathImpl(std::initializer_list<StringPiece> paths) {
@ -308,5 +329,9 @@ string GetTempFilename(const string& extension) {
#endif
}
bool GetTestUndeclaredOutputsDir(string* dir) {
return internal::FixBazelEnvPath(getenv("TEST_UNDECLARED_OUTPUTS_DIR"), dir);
}
} // namespace io
} // namespace tensorflow

View File

@ -92,6 +92,15 @@ string CreateURI(tensorflow::StringPiece scheme, tensorflow::StringPiece host,
// Creates a temporary file name with an extension.
string GetTempFilename(const string& extension);
// Reads the TEST_UNDECLARED_OUTPUTS_DIR environment variable, and if set
// assigns `dir` to the value. `dir` is not modified if the environment variable
// is unset. Returns true if the environment variable is set, otherwise false.
// Passing `dir` as nullptr, will just probe for the environment variable.
//
// Note: This function obviates the need to deal with Bazel's odd path decisions
// on Windows, and should be preferred over a simple `getenv`.
bool GetTestUndeclaredOutputsDir(string* dir);
} // namespace io
} // namespace tensorflow

View File

@ -23,6 +23,7 @@ limitations under the License.
#include "tensorflow/core/lib/strings/proto_serialization.h"
#include "tensorflow/core/platform/env.h"
#include "tensorflow/core/platform/mutex.h"
#include "tensorflow/core/platform/path.h"
namespace tensorflow {
@ -78,13 +79,14 @@ template <class T>
string WriteTextProtoToUniqueFile(Env* env, const string& name,
const char* proto_type, T& proto,
const string& dirname) {
const char* dir = nullptr;
string dir;
if (!dirname.empty()) {
dir = dirname.c_str();
dir = dirname;
} else {
dir = getenv("TF_DUMP_GRAPH_PREFIX");
const char* prefix = getenv("TF_DUMP_GRAPH_PREFIX");
if (prefix != nullptr) dir = prefix;
}
if (!dir) {
if (dir.empty()) {
LOG(WARNING)
<< "Failed to dump " << name << " because dump location is not "
<< " specified through either TF_DUMP_GRAPH_PREFIX environment "
@ -94,18 +96,15 @@ string WriteTextProtoToUniqueFile(Env* env, const string& name,
if (absl::EqualsIgnoreCase(dir, "sponge") ||
absl::EqualsIgnoreCase(dir, "test_undeclared_outputs_dir")) {
const char* tmp_dir = getenv("TEST_UNDECLARED_OUTPUTS_DIR");
if (tmp_dir == nullptr) {
if (!io::GetTestUndeclaredOutputsDir(&dir)) {
LOG(WARNING) << "TF_DUMP_GRAPH_PREFIX=sponge, but "
"TEST_UNDECLARED_OUTPUT_DIRS is not set, dumping to log";
dir = "-";
} else {
dir = tmp_dir;
}
}
string filepath = "NULL";
if (std::strncmp(dir, "-", 2) == 0) {
if (dir == "-") {
LOG(INFO) << proto.DebugString();
filepath = "LOG(INFO)";
} else {