|
| 1 | +# Lint as: python3 |
| 2 | +# Copyright 2020 The TensorFlow Authors. All Rights Reserved. |
| 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 | +"""Generate TensorFlow Java reference docs for TensorFlow.org.""" |
| 17 | +from __future__ import absolute_import |
| 18 | +from __future__ import division |
| 19 | +from __future__ import print_function |
| 20 | + |
| 21 | +import pathlib |
| 22 | +import shutil |
| 23 | +import tempfile |
| 24 | +from git import Repo |
| 25 | + |
| 26 | +from absl import app |
| 27 | +from absl import flags |
| 28 | + |
| 29 | +from tensorflow_docs.api_generator import gen_java |
| 30 | + |
| 31 | +FLAGS = flags.FLAGS |
| 32 | +NDARRAY_VERSION = 'v1.0.0' |
| 33 | + |
| 34 | +# These flags are required by infrastructure, not all of them are used. |
| 35 | +flags.DEFINE_string('output_dir', '/tmp/java_api/', |
| 36 | + ("Use this branch as the root version and don't" |
| 37 | + ' create in version directory')) |
| 38 | + |
| 39 | +flags.DEFINE_string('site_path', 'java/api_docs/java', |
| 40 | + 'Path prefix in the _toc.yaml') |
| 41 | + |
| 42 | +flags.DEFINE_string('code_url_prefix', None, |
| 43 | + '[UNUSED] The url prefix for links to code.') |
| 44 | + |
| 45 | +flags.DEFINE_bool( |
| 46 | + 'search_hints', True, |
| 47 | + '[UNUSED] Include metadata search hints in the generated files') |
| 48 | + |
| 49 | +# __file__ is the path to this file |
| 50 | +TOOLS_DIR = pathlib.Path(__file__).resolve().parent |
| 51 | +REPO_ROOT = TOOLS_DIR.parent |
| 52 | + |
| 53 | + |
| 54 | +def checkout_ndarray(): |
| 55 | + repo_url = 'https://github.com/tensorflow/java-ndarray' |
| 56 | + local_repo_path = REPO_ROOT/'ndarray' |
| 57 | + if not pathlib.Path(local_repo_path).exists(): |
| 58 | + local_repo = Repo.clone_from(repo_url, local_repo_path) |
| 59 | + else: |
| 60 | + local_repo = Repo(local_repo_path) |
| 61 | + local_repo.remotes['origin'].fetch() |
| 62 | + local_repo.git.checkout(NDARRAY_VERSION) |
| 63 | + |
| 64 | + |
| 65 | +def overlay(from_root, to_root): |
| 66 | + for from_path in pathlib.Path(from_root).rglob('*'): |
| 67 | + relpath = from_path.relative_to(from_root) |
| 68 | + to_path = to_root/relpath |
| 69 | + if from_path.is_file(): |
| 70 | + assert not to_path.exists() |
| 71 | + shutil.copyfile(from_path, to_path) |
| 72 | + else: |
| 73 | + to_path.mkdir(exist_ok=True) |
| 74 | + |
| 75 | + |
| 76 | +def main(unused_argv): |
| 77 | + checkout_ndarray() |
| 78 | + merged_source = pathlib.Path(tempfile.mkdtemp()) |
| 79 | + (merged_source / 'java/org').mkdir(parents=True) |
| 80 | + |
| 81 | + shutil.copytree(REPO_ROOT/'tensorflow-core/tensorflow-core-api/src/main/java/org/tensorflow/', merged_source/'java/org/tensorflow') |
| 82 | + overlay(REPO_ROOT/'tensorflow-core/tensorflow-core-api/src/gen/java/org/tensorflow', merged_source/'java/org/tensorflow') |
| 83 | + shutil.copytree(REPO_ROOT/'tensorflow-core/tensorflow-core-native/src/gen/java/org/tensorflow/proto', merged_source/'java/org/tensorflow/proto') |
| 84 | + shutil.copytree(REPO_ROOT/'tensorflow-core/tensorflow-core-native/src/main/java/org/tensorflow/exceptions', merged_source/'java/org/tensorflow/exceptions') |
| 85 | + shutil.copytree(REPO_ROOT/'tensorflow-core/tensorflow-core-native/src/gen/java/org/tensorflow/internal/c_api', merged_source/'java/org/tensorflow/internal/c_api') |
| 86 | + shutil.copytree(REPO_ROOT/'tensorflow-framework/src/main/java/org/tensorflow/framework', merged_source/'java/org/tensorflow/framework') |
| 87 | + shutil.copytree(REPO_ROOT/'ndarray/ndarray/src/main/java/org/tensorflow/ndarray', merged_source/'java/org/tensorflow/ndarray') |
| 88 | + |
| 89 | + gen_java.gen_java_docs( |
| 90 | + package='org.tensorflow', |
| 91 | + source_path=merged_source / 'java', |
| 92 | + output_dir=pathlib.Path(FLAGS.output_dir), |
| 93 | + site_path=pathlib.Path(FLAGS.site_path), |
| 94 | + # Uncomment for local testing: |
| 95 | + # script_path=pathlib.Path(REPO_ROOT/'tools/run-javadoc-for-tf-local.sh'), |
| 96 | + ) |
| 97 | + |
| 98 | + |
| 99 | +if __name__ == '__main__': |
| 100 | + flags.mark_flags_as_required(['output_dir']) |
| 101 | + app.run(main) |
0 commit comments