Skip to content

Commit 31d2f51

Browse files
committed
On Travis, follow package dependency graph to expand package list.
1 parent 64f0a91 commit 31d2f51

File tree

6 files changed

+101
-18
lines changed

6 files changed

+101
-18
lines changed

scripts/generate_json_docs.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from parinx.errors import MethodParsingException
2727
import six
2828

29+
from script_utils import PROJECT_ROOT
2930
from verify_included_modules import get_public_modules
3031

3132

@@ -601,7 +602,7 @@ def main():
601602
parser.add_argument('--tag', help='The version of the documentation.',
602603
default='master')
603604
parser.add_argument('--basepath', help='Path to the library.',
604-
default=os.path.join(os.path.dirname(__file__), '..'))
605+
default=PROJECT_ROOT)
605606
parser.add_argument('--show-toc', help='Prints partial table of contents',
606607
default=False)
607608
args = parser.parse_args()
@@ -635,18 +636,17 @@ def main():
635636
}
636637
}
637638

638-
BASE_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
639-
BASE_JSON_DOCS_DIR = os.path.join(BASE_DIR, 'docs', 'json')
639+
BASE_JSON_DOCS_DIR = os.path.join(PROJECT_ROOT, 'docs', 'json')
640640

641-
DOCS_BUILD_DIR = os.path.join(BASE_DIR, 'docs', '_build')
641+
DOCS_BUILD_DIR = os.path.join(PROJECT_ROOT, 'docs', '_build')
642642
JSON_DOCS_DIR = os.path.join(DOCS_BUILD_DIR, 'json', args.tag)
643643
LIB_DIR = os.path.abspath(args.basepath)
644644

645645
library_dir = os.path.join(LIB_DIR, 'google', 'cloud')
646646
public_mods = get_public_modules(library_dir,
647647
base_package='google.cloud')
648648

649-
generate_module_docs(public_mods, JSON_DOCS_DIR, BASE_DIR, toc)
649+
generate_module_docs(public_mods, JSON_DOCS_DIR, PROJECT_ROOT, toc)
650650
generate_doc_types_json(public_mods,
651651
os.path.join(JSON_DOCS_DIR, 'types.json'))
652652
package_files(JSON_DOCS_DIR, DOCS_BUILD_DIR, BASE_JSON_DOCS_DIR)

scripts/make_datastore_grpc.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@
2020
import sys
2121
import tempfile
2222

23+
from script_utils import PROJECT_ROOT
2324

24-
ROOT_DIR = os.path.abspath(
25-
os.path.join(os.path.dirname(__file__), '..'))
26-
PROTOS_DIR = os.path.join(ROOT_DIR, 'googleapis-pb')
25+
26+
PROTOS_DIR = os.path.join(PROJECT_ROOT, 'googleapis-pb')
2727
PROTO_PATH = os.path.join(PROTOS_DIR, 'google', 'datastore',
2828
'v1', 'datastore.proto')
29-
GRPC_ONLY_FILE = os.path.join(ROOT_DIR, 'datastore',
29+
GRPC_ONLY_FILE = os.path.join(PROJECT_ROOT, 'datastore',
3030
'google', 'cloud', 'datastore',
3131
'_generated', 'datastore_grpc_pb2.py')
3232
GRPCIO_VIRTUALENV = os.getenv('GRPCIO_VIRTUALENV')

scripts/run_pylint.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
from script_utils import LOCAL_REMOTE_ENV
3434
from script_utils import in_travis
3535
from script_utils import in_travis_pr
36+
from script_utils import PROJECT_ROOT
3637
from script_utils import travis_branch
3738

3839

@@ -48,7 +49,7 @@
4849
os.path.join('google', 'cloud', '__init__.py'),
4950
'setup.py',
5051
]
51-
SCRIPTS_DIR = os.path.abspath(os.path.dirname(__file__))
52+
SCRIPTS_DIR = os.path.join(PROJECT_ROOT, 'scripts')
5253
PRODUCTION_RC = os.path.join(SCRIPTS_DIR, 'pylintrc_default')
5354
TEST_RC = os.path.join(SCRIPTS_DIR, 'pylintrc_reduced')
5455
TEST_DISABLED_MESSAGES = [

scripts/run_unit_tests.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@
2727
import sys
2828

2929
from script_utils import check_output
30+
from script_utils import follow_dependencies
3031
from script_utils import get_changed_packages
3132
from script_utils import in_travis
3233
from script_utils import in_travis_pr
34+
from script_utils import PROJECT_ROOT
3335
from script_utils import travis_branch
3436

3537

36-
PROJECT_ROOT = os.path.abspath(
37-
os.path.join(os.path.dirname(__file__), '..'))
3838
IGNORED_DIRECTORIES = (
3939
'appveyor',
4040
'docs',
@@ -121,7 +121,9 @@ def get_test_packages():
121121
* Check command line for packages passed in as positional arguments
122122
* Check if in Travis, then limit the subset based on changes
123123
in a Pull Request ("push" builds to branches may not have
124-
any filtering)
124+
any filtering). Once the filtered list of **changed** packages
125+
is found, the package dependency graph is used to add any
126+
additional packages which depend on the changed packages.
125127
* Just use all packages
126128
127129
:rtype: list
@@ -136,7 +138,8 @@ def get_test_packages():
136138
verify_packages(args.packages, all_packages)
137139
return sorted(args.packages)
138140
elif in_travis():
139-
return get_travis_directories(all_packages)
141+
changed_packages = get_travis_directories(all_packages)
142+
return follow_dependencies(changed_packages, all_packages)
140143
else:
141144
return all_packages
142145

scripts/script_utils.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@
1818
import subprocess
1919

2020

21+
PROJECT_ROOT = os.path.abspath(
22+
os.path.join(os.path.dirname(__file__), '..'))
2123
LOCAL_REMOTE_ENV = 'GOOGLE_CLOUD_TESTING_REMOTE'
2224
LOCAL_BRANCH_ENV = 'GOOGLE_CLOUD_TESTING_BRANCH'
2325
IN_TRAVIS_ENV = 'TRAVIS'
2426
TRAVIS_PR_ENV = 'TRAVIS_PULL_REQUEST'
2527
TRAVIS_BRANCH_ENV = 'TRAVIS_BRANCH'
28+
PACKAGE_PREFIX = 'google-cloud-'
2629

2730

2831
def in_travis():
@@ -159,3 +162,79 @@ def get_changed_packages(blob_name1, blob_name2, package_list):
159162
result.add(file_root)
160163

161164
return sorted(result)
165+
166+
167+
def get_required_packages(file_contents):
168+
"""Get required packages from a requirements.txt file.
169+
170+
.. note::
171+
172+
This could be done in a bit more complete way via
173+
https://pypi.python.org/pypi/requirements-parser
174+
175+
:type file_contents: str
176+
:param file_contents: The contents of a requirements.txt file.
177+
178+
:rtype: list
179+
:returns: The list of required packages.
180+
"""
181+
requirements = file_contents.strip().split('\n')
182+
result = []
183+
for required in requirements:
184+
parts = required.split()
185+
result.append(parts[0])
186+
return result
187+
188+
189+
def get_dependency_graph(package_list):
190+
"""Get a directed graph of package dependencies.
191+
192+
:type package_list: list
193+
:param package_list: The list of **all** valid packages.
194+
195+
:rtype: dict
196+
:returns: A dictionary where keys are packages and values are
197+
the set of packages that depend on the key.
198+
"""
199+
result = {package: set() for package in package_list}
200+
for package in package_list:
201+
reqs_file = os.path.join(PROJECT_ROOT, package,
202+
'requirements.txt')
203+
with open(reqs_file, 'r') as file_obj:
204+
file_contents = file_obj.read()
205+
206+
requirements = get_required_packages(file_contents)
207+
for requirement in requirements:
208+
if not requirement.startswith(PACKAGE_PREFIX):
209+
continue
210+
_, req_package = requirement.split(PACKAGE_PREFIX)
211+
req_package = req_package.replace('-', '_')
212+
result[req_package].add(package)
213+
214+
return result
215+
216+
217+
def follow_dependencies(subset, package_list):
218+
"""Get a directed graph of packages dependency.
219+
220+
:type subset: list
221+
:param subset: List of a subset of package names.
222+
223+
:type package_list: list
224+
:param package_list: The list of **all** valid packages.
225+
226+
:rtype: list
227+
:returns: An expanded list of packages containing everything
228+
in ``subset`` and any packages that depend on those.
229+
"""
230+
dependency_graph = get_dependency_graph(package_list)
231+
232+
curr_pkgs = None
233+
updated_pkgs = set(subset)
234+
while curr_pkgs != updated_pkgs:
235+
curr_pkgs = updated_pkgs
236+
updated_pkgs = set(curr_pkgs)
237+
for package in curr_pkgs:
238+
updated_pkgs.update(dependency_graph[package])
239+
240+
return sorted(curr_pkgs)

scripts/verify_included_modules.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@
2424

2525
from sphinx.ext.intersphinx import fetch_inventory
2626

27+
from script_utils import PROJECT_ROOT
2728

28-
BASE_DIR = os.path.abspath(
29-
os.path.join(os.path.dirname(__file__), '..'))
30-
DOCS_DIR = os.path.join(BASE_DIR, 'docs')
29+
30+
DOCS_DIR = os.path.join(PROJECT_ROOT, 'docs')
3131
IGNORED_PREFIXES = ('test_', '_')
3232
IGNORED_MODULES = frozenset([
3333
'google.cloud.__init__',
@@ -153,7 +153,7 @@ def verify_modules(build_root='_build'):
153153

154154
public_mods = set()
155155
for package in PACKAGES:
156-
library_dir = os.path.join(BASE_DIR, package, 'google', 'cloud')
156+
library_dir = os.path.join(PROJECT_ROOT, package, 'google', 'cloud')
157157
package_mods = get_public_modules(library_dir,
158158
base_package='google.cloud')
159159
public_mods.update(package_mods)

0 commit comments

Comments
 (0)