forked from googleapis/google-cloud-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathverify_included_modules.py
More file actions
205 lines (170 loc) · 6.14 KB
/
verify_included_modules.py
File metadata and controls
205 lines (170 loc) · 6.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# Copyright 2016 Google Inc.
#
# 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.
"""Check if all public modules are included in our docs."""
from __future__ import print_function
import argparse
import os
import sys
import warnings
from sphinx.ext.intersphinx import fetch_inventory
from script_utils import PROJECT_ROOT
DOCS_DIR = os.path.join(PROJECT_ROOT, 'docs')
IGNORED_PREFIXES = ('test_', '_')
IGNORED_MODULES = frozenset([
'google.cloud',
'google.cloud.bigquery',
'google.cloud.bigtable',
'google.cloud.dns',
'google.cloud.error_reporting',
'google.cloud.language',
'google.cloud.logging',
'google.cloud.logging.handlers',
'google.cloud.logging.handlers.transports',
'google.cloud.monitoring',
'google.cloud.pubsub',
'google.cloud.resource_manager',
'google.cloud.speech',
'google.cloud.storage',
'google.cloud.streaming',
'google.cloud.streaming.buffered_stream',
'google.cloud.streaming.exceptions',
'google.cloud.streaming.http_wrapper',
'google.cloud.streaming.stream_slice',
'google.cloud.streaming.transfer',
'google.cloud.streaming.util',
'google.cloud.translate',
'google.cloud.vision',
'google.cloud.vision.fixtures',
])
PACKAGES = (
'bigquery',
'bigtable',
'core',
'datastore',
'dns',
'error_reporting',
'language',
'logging',
'monitoring',
'pubsub',
'resource_manager',
'runtimeconfig',
'speech',
'storage',
'translate',
'vision',
)
class SphinxApp(object):
"""Mock app to interact with Sphinx helpers."""
warn = warnings.warn
srcdir = DOCS_DIR
def is_valid_module(filename):
"""Determines if a filename is a valid Python module.
Assumes if is just the end of a path (i.e. does not contain
``os.path.sep``.
:type filename: str
:param filename: The name of a file.
:rtype: bool
:returns: Flag indicating if the filename is valid.
"""
if not filename.endswith('.py'):
return False
if filename == '__init__.py':
return True
for prefix in IGNORED_PREFIXES:
if filename.startswith(prefix):
return False
return True
def get_public_modules(path, base_package=None):
"""Get list of all public modules relative to a path.
:type path: str
:param path: The path containing the python modules.
:type base_package: str
:param base_package: (Optional) A package to prepend in
front of the path.
:rtype: list
:returns: List of all modules found.
"""
result = []
for subdir, _, files in os.walk(path):
# Skip folders that start with _.
if any([part.startswith('_')
for part in subdir.split(os.path.sep)]):
continue
_, rel_dir = subdir.split(path)
rel_dir = rel_dir.lstrip(os.path.sep)
for filename in files:
if is_valid_module(filename):
mod_name, _ = os.path.splitext(filename)
rel_path = os.path.join(rel_dir, mod_name)
if base_package is not None:
rel_path = os.path.join(base_package, rel_path)
# Turn into a Python module rather than a file path.
rel_path = rel_path.replace(os.path.sep, '.')
if mod_name == '__init__':
result.append(rel_path[:-len('.__init__')])
else:
result.append(rel_path)
return result
def verify_modules(build_root='_build'):
"""Verify modules included.
:type build_root: str
:param build_root: The root of the directory where docs are built into.
Defaults to ``_build``.
"""
object_inventory_relpath = os.path.join(build_root, 'html', 'objects.inv')
mock_uri = ''
inventory = fetch_inventory(SphinxApp, mock_uri,
object_inventory_relpath)
sphinx_mods = set(inventory['py:module'].keys())
public_mods = set()
for package in PACKAGES:
library_dir = os.path.join(PROJECT_ROOT, package, 'google', 'cloud')
package_mods = get_public_modules(library_dir,
base_package='google.cloud')
public_mods.update(package_mods)
if not sphinx_mods <= public_mods:
unexpected_mods = sphinx_mods - public_mods
message = ['Unexpected error. There were modules referenced by '
'Sphinx that are not among the public modules.']
message.extend(['- %s' % (mod,) for mod in unexpected_mods])
print('\n'.join(message), file=sys.stderr)
sys.exit(1)
undocumented_mods = public_mods - sphinx_mods
# Remove ignored modules.
undocumented_mods -= IGNORED_MODULES
if undocumented_mods:
message_parts = ['Found undocumented public modules:']
message_parts.extend(['- ' + mod_name
for mod_name in sorted(undocumented_mods)])
print('\n'.join(message_parts), file=sys.stderr)
sys.exit(1)
def get_parser():
"""Get simple ``argparse`` parser to determine package.
:rtype: :class:`argparse.ArgumentParser`
:returns: The parser for this script.
"""
description = ('Run check that all google-cloud '
'modules are included in docs.')
parser = argparse.ArgumentParser(description=description)
parser.add_argument('--build-root', dest='build_root',
help='The root directory where docs are located.')
return parser
def main():
"""Main script to verify modules included."""
parser = get_parser()
args = parser.parse_args()
verify_modules(build_root=args.build_root)
if __name__ == '__main__':
main()