1/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14==============================================================================*/
15// Converts all *.pbtxt files in a directory from Multiline to proto format.
16#include "tensorflow/core/framework/op_gen_lib.h"
17#include "tensorflow/core/lib/io/path.h"
18#include "tensorflow/core/platform/env.h"
19#include "tensorflow/core/platform/init_main.h"
20
21namespace tensorflow {
22
23namespace {
24constexpr char kApiDefFilePattern[] = "*.pbtxt";
25
26Status ConvertFilesFromMultiline(const string& input_dir,
27                                 const string& output_dir) {
28  Env* env = Env::Default();
29
30  const string file_pattern = io::JoinPath(input_dir, kApiDefFilePattern);
31  std::vector<string> matching_paths;
32  TF_CHECK_OK(env->GetMatchingPaths(file_pattern, &matching_paths));
33
34  if (!env->IsDirectory(output_dir).ok()) {
35    TF_RETURN_IF_ERROR(env->CreateDir(output_dir));
36  }
37
38  for (const auto& path : matching_paths) {
39    string contents;
40    TF_RETURN_IF_ERROR(tensorflow::ReadFileToString(env, path, &contents));
41    contents = tensorflow::PBTxtFromMultiline(contents);
42    string output_path = io::JoinPath(output_dir, io::Basename(path));
43    // Write contents to output_path
44    TF_RETURN_IF_ERROR(
45        tensorflow::WriteStringToFile(env, output_path, contents));
46  }
47  return Status::OK();
48}
49}  // namespace
50}  // namespace tensorflow
51
52int main(int argc, char* argv[]) {
53  tensorflow::port::InitMain(argv[0], &argc, &argv);
54
55  const std::string usage =
56      "Usage: convert_from_multiline input_dir output_dir";
57  if (argc != 3) {
58    std::cerr << usage << std::endl;
59    return -1;
60  }
61  TF_CHECK_OK(tensorflow::ConvertFilesFromMultiline(argv[1], argv[2]));
62  return 0;
63}
64