diff --git a/tensorboard/BUILD b/tensorboard/BUILD index da0ddce219..a9f81cdf8f 100644 --- a/tensorboard/BUILD +++ b/tensorboard/BUILD @@ -105,7 +105,6 @@ py_library( srcs_version = "PY2AND3", visibility = ["//visibility:public"], deps = [ - "//tensorboard:expect_tensorflow_installed", "//tensorboard/plugins:base_plugin", "//tensorboard/plugins/audio:audio_plugin", "//tensorboard/plugins/beholder:beholder_plugin", @@ -303,6 +302,7 @@ py_library( deps = [ "//tensorboard:expect_tensorflow_installed", "//tensorboard/util", + "//tensorboard/util:platform_util", "@org_pythonhosted_six", ], ) diff --git a/tensorboard/backend/event_processing/BUILD b/tensorboard/backend/event_processing/BUILD index 00c4d268d8..b2e375866f 100644 --- a/tensorboard/backend/event_processing/BUILD +++ b/tensorboard/backend/event_processing/BUILD @@ -73,6 +73,7 @@ py_library( srcs_version = "PY2AND3", deps = [ "//tensorboard/compat:tensorflow", + "//tensorboard/util:platform_util", ], ) diff --git a/tensorboard/backend/event_processing/event_file_loader.py b/tensorboard/backend/event_processing/event_file_loader.py index 2163c6c4ad..4934eeca00 100644 --- a/tensorboard/backend/event_processing/event_file_loader.py +++ b/tensorboard/backend/event_processing/event_file_loader.py @@ -21,6 +21,7 @@ import inspect from tensorboard.compat import tf +from tensorboard.util import platform_util class RawEventFileLoader(object): @@ -29,7 +30,7 @@ class RawEventFileLoader(object): def __init__(self, file_path): if file_path is None: raise ValueError('A file path is required') - file_path = tf.resource_loader.readahead_file_path(file_path) + file_path = platform_util.readahead_file_path(file_path) tf.logging.debug('Opening a record reader pointing at %s', file_path) with tf.errors.raise_exception_on_not_ok_status() as status: self._reader = tf.pywrap_tensorflow.PyRecordReader_New( diff --git a/tensorboard/compat/tensorflow_stub/__init__.py b/tensorboard/compat/tensorflow_stub/__init__.py index f45480b969..c0d0e8c6d2 100644 --- a/tensorboard/compat/tensorflow_stub/__init__.py +++ b/tensorboard/compat/tensorflow_stub/__init__.py @@ -36,6 +36,5 @@ from . import gfile # noqa from . import logging # noqa from . import pywrap_tensorflow # noqa -from . import resource_loader # noqa from . import tensor_manip # noqa from . import tensor_shape # noqa diff --git a/tensorboard/compat/tensorflow_stub/resource_loader.py b/tensorboard/compat/tensorflow_stub/resource_loader.py deleted file mode 100644 index a9b9a1f94a..0000000000 --- a/tensorboard/compat/tensorflow_stub/resource_loader.py +++ /dev/null @@ -1,120 +0,0 @@ -# Copyright 2015 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Resource management library.""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import inspect as _inspect -import os as _os -import sys as _sys - - -# @tf_export('resource_loader.load_resource') -def load_resource(path): - """Load the resource at given path, where path is relative to tensorflow/. - - Args: - path: a string resource path relative to tensorflow/. - - Returns: - The contents of that resource. - - Raises: - IOError: If the path is not found, or the resource can't be opened. - """ - tensorflow_root = _os.path.join(_os.path.dirname(__file__), _os.pardir, _os.pardir) - path = _os.path.join(tensorflow_root, path) - path = _os.path.abspath(path) - with open(path, "rb") as f: - return f.read() - - -# pylint: disable=protected-access -# @tf_export('resource_loader.get_data_files_path') -def get_data_files_path(): - """Get a direct path to the data files colocated with the script. - - Returns: - The directory where files specified in data attribute of py_test - and py_binary are stored. - """ - return _os.path.dirname(_inspect.getfile(_sys._getframe(1))) - - -# @tf_export('resource_loader.get_root_dir_with_all_resources') -def get_root_dir_with_all_resources(): - """Get a root directory containing all the data attributes in the build rule. - - Returns: - The path to the specified file present in the data attribute of py_test - or py_binary. Falls back to returning the same as get_data_files_path if it - fails to detect a bazel runfiles directory. - """ - script_dir = get_data_files_path() - - # Create a history of the paths, because the data files are located relative - # to the repository root directory, which is directly under runfiles - # directory. - directories = [script_dir] - data_files_dir = "" - - while True: - candidate_dir = directories[-1] - current_directory = _os.path.basename(candidate_dir) - if ".runfiles" in current_directory: - # Our file should never be directly under runfiles. - # If the history has only one item, it means we are directly inside the - # runfiles directory, something is wrong, fall back to the default return - # value, script directory. - if len(directories) > 1: - data_files_dir = directories[-2] - - break - else: - new_candidate_dir = _os.path.dirname(candidate_dir) - # If we are at the root directory these two will be the same. - if new_candidate_dir == candidate_dir: - break - else: - directories.append(new_candidate_dir) - - return data_files_dir or script_dir - - -# @tf_export('resource_loader.get_path_to_datafile') -def get_path_to_datafile(path): - """Get the path to the specified file in the data dependencies. - - The path is relative to tensorflow/ - - Args: - path: a string resource path relative to tensorflow/ - - Returns: - The path to the specified file present in the data attribute of py_test - or py_binary. - - Raises: - IOError: If the path is not found, or the resource can't be opened. - """ - data_files_path = _os.path.dirname(_inspect.getfile(_sys._getframe(1))) - return _os.path.join(data_files_path, path) - - -# @tf_export('resource_loader.readahead_file_path') -def readahead_file_path(path, readahead="128M"): # pylint: disable=unused-argument - """Readahead files not implemented; simply returns given path.""" - return path diff --git a/tensorboard/default.py b/tensorboard/default.py index 42d140aadb..fccfe4f1f2 100644 --- a/tensorboard/default.py +++ b/tensorboard/default.py @@ -47,7 +47,6 @@ from tensorboard.plugins.projector import projector_plugin from tensorboard.plugins.scalar import scalars_plugin from tensorboard.plugins.text import text_plugin -import tensorflow as tf logger = logging.getLogger(__name__) @@ -81,18 +80,3 @@ def get_plugins(): :rtype: list[Union[base_plugin.TBLoader, Type[base_plugin.TBPlugin]]] """ return _PLUGINS[:] - -def get_assets_zip_provider(): - """Opens stock TensorBoard web assets collection. - - Returns: - Returns function that returns a newly opened file handle to zip file - containing static assets for stock TensorBoard, or None if webfiles.zip - could not be found. The value the callback returns must be closed. The - paths inside the zip file are considered absolute paths on the web server. - """ - path = os.path.join(tf.resource_loader.get_data_files_path(), 'webfiles.zip') - if not os.path.exists(path): - logger.warning('webfiles.zip static assets not found: %s', path) - return None - return lambda: open(path, 'rb') diff --git a/tensorboard/loader.py b/tensorboard/loader.py index ddcf950c71..11430157e1 100644 --- a/tensorboard/loader.py +++ b/tensorboard/loader.py @@ -37,6 +37,7 @@ import six from tensorboard import db +from tensorboard.util import platform_util from tensorboard.util import util import tensorflow as tf @@ -145,7 +146,7 @@ def close(self): def _open(self): with tf.errors.raise_exception_on_not_ok_status() as status: return tf.pywrap_tensorflow.PyRecordReader_New( - tf.resource_loader.readahead_file_path(tf.compat.as_bytes(self.path)), + platform_util.readahead_file_path(tf.compat.as_bytes(self.path)), self._offset, tf.compat.as_bytes(''), status) def __str__(self): diff --git a/tensorboard/main.py b/tensorboard/main.py index a187db4cef..0ab309eb3c 100644 --- a/tensorboard/main.py +++ b/tensorboard/main.py @@ -54,7 +54,7 @@ def run_main(): """Initializes flags and calls main().""" program.setup_environment() tensorboard = program.TensorBoard(default.get_plugins(), - default.get_assets_zip_provider()) + program.get_default_assets_zip_provider()) try: from absl import app # Import this to check that app.run() will accept the flags_parser argument. diff --git a/tensorboard/main_notf.py b/tensorboard/main_notf.py index 39c8492607..e3b8215d96 100644 --- a/tensorboard/main_notf.py +++ b/tensorboard/main_notf.py @@ -81,28 +81,11 @@ def get_notf_plugins(): """ return _NOTF_PLUGINS[:] - -def get_assets_zip_provider(): - """Opens stock TensorBoard web assets collection. - - Returns: - Returns function that returns a newly opened file handle to zip file - containing static assets for stock TensorBoard, or None if webfiles.zip - could not be found. The value the callback returns must be closed. The - paths inside the zip file are considered absolute paths on the web server. - """ - path = os.path.join(tf.resource_loader.get_data_files_path(), 'webfiles.zip') - if not os.path.exists(path): - logger.warning('webfiles.zip static assets not found: %s', path) - return None - return lambda: open(path, 'rb') - - def run_main(): """Initializes flags and calls main().""" program.setup_environment() tensorboard = program.TensorBoard(get_notf_plugins(), - get_assets_zip_provider()) + program.get_default_assets_zip_provider()) try: from absl import app # Import this to check that app.run() will accept the flags_parser argument. diff --git a/tensorboard/program.py b/tensorboard/program.py index 114319b60d..a7de39c337 100644 --- a/tensorboard/program.py +++ b/tensorboard/program.py @@ -39,6 +39,7 @@ import socket import sys import threading +import inspect from werkzeug import serving @@ -74,6 +75,21 @@ def setup_environment(): # Content-Length header, or do chunked encoding for streaming. serving.WSGIRequestHandler.protocol_version = 'HTTP/1.1' +def get_default_assets_zip_provider(): + """Opens stock TensorBoard web assets collection. + + Returns: + Returns function that returns a newly opened file handle to zip file + containing static assets for stock TensorBoard, or None if webfiles.zip + could not be found. The value the callback returns must be closed. The + paths inside the zip file are considered absolute paths on the web server. + """ + path = os.path.join(os.path.dirname(inspect.getfile(sys._getframe(1))), + 'webfiles.zip') + if not os.path.exists(path): + logger.warning('webfiles.zip static assets not found: %s', path) + return None + return lambda: open(path, 'rb') class TensorBoard(object): """Class for running TensorBoard. @@ -106,8 +122,7 @@ def __init__(self, from tensorboard import default plugins = default.get_plugins() if assets_zip_provider is None: - from tensorboard import default - assets_zip_provider = default.get_assets_zip_provider() + assets_zip_provider = get_default_assets_zip_provider() if server_class is None: server_class = WerkzeugServer def make_loader(plugin): diff --git a/tensorboard/util/BUILD b/tensorboard/util/BUILD index e640db42f2..d5d9babaff 100644 --- a/tensorboard/util/BUILD +++ b/tensorboard/util/BUILD @@ -73,6 +73,11 @@ py_test( ], ) +py_library( + name = "platform_util", + srcs = ["platform_util.py"], + srcs_version = "PY2AND3", +) py_library( name = "test_util", diff --git a/tensorboard/util/platform_util.py b/tensorboard/util/platform_util.py new file mode 100644 index 0000000000..89cd74c943 --- /dev/null +++ b/tensorboard/util/platform_util.py @@ -0,0 +1,25 @@ +# Copyright 2018 The TensorFlow Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""TensorBoard helper routine for platform. +""" + +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function + + +def readahead_file_path(path, unused_readahead=None): + """Readahead files not implemented; simply returns given path.""" + return path