• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2015 The TensorFlow Authors. All Rights Reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14# ==============================================================================
15"""Resource management library."""
16from __future__ import absolute_import
17from __future__ import division
18from __future__ import print_function
19
20import os as _os
21import sys as _sys
22
23from tensorflow.python.util import tf_inspect as _inspect
24from tensorflow.python.util.tf_export import tf_export
25
26
27@tf_export(v1=['resource_loader.load_resource'])
28def load_resource(path):
29  """Load the resource at given path, where path is relative to tensorflow/.
30
31  Args:
32    path: a string resource path relative to tensorflow/.
33
34  Returns:
35    The contents of that resource.
36
37  Raises:
38    IOError: If the path is not found, or the resource can't be opened.
39  """
40  tensorflow_root = (_os.path.join(
41      _os.path.dirname(__file__), _os.pardir, _os.pardir))
42  path = _os.path.join(tensorflow_root, path)
43  path = _os.path.abspath(path)
44  with open(path, 'rb') as f:
45    return f.read()
46
47
48# pylint: disable=protected-access
49@tf_export(v1=['resource_loader.get_data_files_path'])
50def get_data_files_path():
51  """Get a direct path to the data files colocated with the script.
52
53  Returns:
54    The directory where files specified in data attribute of py_test
55    and py_binary are stored.
56  """
57  return _os.path.dirname(_inspect.getfile(_sys._getframe(1)))
58
59
60@tf_export(v1=['resource_loader.get_root_dir_with_all_resources'])
61def get_root_dir_with_all_resources():
62  """Get a root directory containing all the data attributes in the build rule.
63
64  Returns:
65    The path to the specified file present in the data attribute of py_test
66    or py_binary. Falls back to returning the same as get_data_files_path if it
67    fails to detect a bazel runfiles directory.
68  """
69  script_dir = get_data_files_path()
70
71  # Create a history of the paths, because the data files are located relative
72  # to the repository root directory, which is directly under runfiles
73  # directory.
74  directories = [script_dir]
75  data_files_dir = ''
76
77  while True:
78    candidate_dir = directories[-1]
79    current_directory = _os.path.basename(candidate_dir)
80    if '.runfiles' in current_directory:
81      # Our file should never be directly under runfiles.
82      # If the history has only one item, it means we are directly inside the
83      # runfiles directory, something is wrong, fall back to the default return
84      # value, script directory.
85      if len(directories) > 1:
86        data_files_dir = directories[-2]
87
88      break
89    else:
90      new_candidate_dir = _os.path.dirname(candidate_dir)
91      # If we are at the root directory these two will be the same.
92      if new_candidate_dir == candidate_dir:
93        break
94      else:
95        directories.append(new_candidate_dir)
96
97  return data_files_dir or script_dir
98
99
100@tf_export(v1=['resource_loader.get_path_to_datafile'])
101def get_path_to_datafile(path):
102  """Get the path to the specified file in the data dependencies.
103
104  The path is relative to tensorflow/
105
106  Args:
107    path: a string resource path relative to tensorflow/
108
109  Returns:
110    The path to the specified file present in the data attribute of py_test
111    or py_binary.
112
113  Raises:
114    IOError: If the path is not found, or the resource can't be opened.
115  """
116  data_files_path = _os.path.dirname(_inspect.getfile(_sys._getframe(1)))
117  return _os.path.join(data_files_path, path)
118
119
120@tf_export(v1=['resource_loader.readahead_file_path'])
121def readahead_file_path(path, readahead='128M'):  # pylint: disable=unused-argument
122  """Readahead files not implemented; simply returns given path."""
123  return path
124