• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2017 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"""Complain about invalid or missing entries in python_*.txt files.
16
17Problematic entries can be commented for temporary whitelisting.
18"""
19
20from __future__ import absolute_import
21from __future__ import division
22from __future__ import print_function
23
24import os
25import unittest
26
27
28def abs_path(path):
29  root = os.path.dirname(__file__)
30
31  for _ in range(3):
32    root = os.path.join(root, os.pardir)
33
34  path = os.path.join(root, path)
35  path = os.path.abspath(path)
36  return path
37
38
39def read_entries(test):
40  with open(abs_path(test.entries_file), "r") as f:
41    lines = f.readlines()
42
43  lines = [line.strip() for line in lines]
44  lines = [line for line in lines if line]
45
46  test.entries = []
47  test.whitelist = []
48
49  for line in lines:
50    # line is comment
51    if line.startswith("#"):
52      line = line[1:].strip()
53      # whitelist entry
54      if line.startswith("tensorflow/"):
55        test.whitelist.append(line)
56    # line has comment -> strip comment
57    elif line.find("#") != -1:
58      line = line[:line.find("#")].strip()
59      test.entries.append(line)
60    else:
61      test.entries.append(line)
62
63
64def test_invalid_directories(test):
65  for entry in test.entries:
66    if not os.path.isdir(abs_path(entry)):
67      problem = "'" + test.entries_file + "' contains invalid '" + entry + "'"
68      solution = ("Please remove the invalid entry (or add the missing "
69                  "directory).")
70      raise AssertionError(problem + "\n" + solution)
71
72
73def test_missing_directory(test, path):
74  if path in test.whitelist:
75    return
76
77  dir_exists = os.path.isdir(abs_path(path))
78  entry_exists = path in test.entries
79
80  if dir_exists and not entry_exists:
81    problem = "'" + test.entries_file + "' is missing '" + path + "'"
82    solution = "Please add the missing entry (comment to whitelist if needed)."
83    raise AssertionError(problem + "\n" + solution)
84
85
86class PythonModuleTest(unittest.TestCase):
87
88  def setUp(self):
89    self.entries_file = "tensorflow/contrib/cmake/python_modules.txt"
90    read_entries(self)
91
92  def testInvalidEntries(self):
93    test_invalid_directories(self)
94
95  def testMissingModules(self):
96    module_names = next(os.walk(abs_path("tensorflow/contrib")))[1]
97
98    for module_name in module_names:
99      path = "tensorflow/contrib/" + module_name
100
101      test_missing_directory(self, path + "/python")
102      test_missing_directory(self, path + "/python/ops")
103      test_missing_directory(self, path + "/python/kernels")
104      test_missing_directory(self, path + "/python/layers")
105
106
107class PythonProtoTest(unittest.TestCase):
108
109  def setUp(self):
110    self.entries_file = "tensorflow/contrib/cmake/python_protos.txt"
111    read_entries(self)
112
113  def testInvalidEntries(self):
114    test_invalid_directories(self)
115
116
117class PythonProtoCCTest(unittest.TestCase):
118
119  def setUp(self):
120    self.entries_file = "tensorflow/contrib/cmake/python_protos_cc.txt"
121    read_entries(self)
122
123  def testInvalidEntries(self):
124    test_invalid_directories(self)
125
126
127if __name__ == "__main__":
128  unittest.main()
129