Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow ~/ in path for transform_graph #15894

Merged
merged 2 commits into from
Apr 16, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
70 changes: 62 additions & 8 deletions tensorflow/tools/graph_transforms/transform_graph.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ limitations under the License.
#include "tensorflow/core/util/command_line_flags.h"
#include "tensorflow/tools/graph_transforms/file_utils.h"
#include "tensorflow/tools/graph_transforms/transform_utils.h"
#if !defined(PLATFORM_WINDOWS)
#include <pwd.h>
#endif

namespace tensorflow {
namespace graph_transforms {
Expand Down Expand Up @@ -130,16 +133,64 @@ Status ParseTransformParameters(const string& transforms_string,
return Status::OK();
}

std::string ExpandPath(const std::string& path_string) {
#if defined(PLATFORM_WINDOWS)
return path_string;
#else
if (path_string.empty() || path_string[0] != '~') {
return path_string;
}

const char* home = NULL;
std::string::size_type prefix = path_string.find_first_of('/');
if (path_string.length() == 1 || prefix == 1) {
// The value of $HOME, e.g., ~/foo
home = getenv("HOME");
if (!home) {
// If HOME is not available, get uid
struct passwd* pw = getpwuid(getuid());
if (pw) {
home = pw->pw_dir;
}
}
} else {
// The value of ~user, e.g., ~user/foo
std::string user(path_string, 1, (prefix == std::string::npos)
? std::string::npos
: prefix - 1);
struct passwd* pw = getpwnam(user.c_str());
if (pw) {
home = pw->pw_dir;
}
}

if (!home) {
return path_string;
}

string path(home);
if (prefix == std::string::npos) {
return path;
}

if (path.length() == 0 || path[path.length() - 1] != '/') {
path += '/';
}
path += path_string.substr(prefix + 1);
return path;
#endif
}

int ParseFlagsAndTransformGraph(int argc, char* argv[], bool init_main) {
string in_graph = "";
string out_graph = "";
string in_graph_string = "";
string out_graph_string = "";
string inputs_string = "";
string outputs_string = "";
string transforms_string = "";
bool output_as_text = false;
std::vector<Flag> flag_list = {
Flag("in_graph", &in_graph, "input graph file name"),
Flag("out_graph", &out_graph, "output graph file name"),
Flag("in_graph", &in_graph_string, "input graph file name"),
Flag("out_graph", &out_graph_string, "output graph file name"),
Flag("inputs", &inputs_string, "inputs"),
Flag("outputs", &outputs_string, "outputs"),
Flag("transforms", &transforms_string, "list of transforms"),
Expand All @@ -166,11 +217,11 @@ int ParseFlagsAndTransformGraph(int argc, char* argv[], bool init_main) {
LOG(ERROR) << "Unknown argument " << argv[1] << ".\n" << usage;
return -1;
}
if (in_graph.empty()) {
if (in_graph_string.empty()) {
LOG(ERROR) << "in_graph graph can't be empty.\n" << usage;
return -1;
}
if (out_graph.empty()) {
if (out_graph_string.empty()) {
LOG(ERROR) << "out_graph graph can't be empty.\n" << usage;
return -1;
}
Expand All @@ -179,6 +230,9 @@ int ParseFlagsAndTransformGraph(int argc, char* argv[], bool init_main) {
return -1;
}

string in_graph = ExpandPath(in_graph_string);
string out_graph = ExpandPath(out_graph_string);

std::vector<string> inputs = str_util::Split(inputs_string, ',');
std::vector<string> outputs = str_util::Split(outputs_string, ',');
TransformParameters transform_params;
Expand All @@ -197,7 +251,7 @@ int ParseFlagsAndTransformGraph(int argc, char* argv[], bool init_main) {
GraphDef graph_def;
Status load_status = LoadTextOrBinaryGraphFile(in_graph, &graph_def);
if (!load_status.ok()) {
LOG(ERROR) << "Loading graph '" << in_graph << "' failed with "
LOG(ERROR) << "Loading graph '" << in_graph_string << "' failed with "
<< load_status.error_message();
LOG(ERROR) << usage;
return -1;
Expand All @@ -219,7 +273,7 @@ int ParseFlagsAndTransformGraph(int argc, char* argv[], bool init_main) {
save_status = WriteBinaryProto(Env::Default(), out_graph, graph_def);
}
if (!save_status.ok()) {
LOG(ERROR) << "Saving graph '" << out_graph << "' failed with "
LOG(ERROR) << "Saving graph '" << out_graph_string << "' failed with "
<< save_status.error_message();
return -1;
}
Expand Down