2626import subprocess
2727import sys
2828
29+ from script_utils import check_output
30+ from script_utils import get_changed_packages
31+ from script_utils import in_travis
32+ from script_utils import in_travis_pr
33+ from script_utils import travis_branch
34+
2935
3036PROJECT_ROOT = os .path .abspath (
3137 os .path .join (os .path .dirname (__file__ ), '..' ))
4450UNSET_SENTINEL = object () # Sentinel for argparser
4551
4652
47- def check_output (* args ):
48- """Run a command on the operation system.
49-
50- :type args: tuple
51- :param args: Keyword arguments to pass to ``subprocess.check_output``.
52-
53- :rtype: str
54- :returns: The raw STDOUT from the command (converted from bytes
55- if necessary).
56- """
57- cmd_output = subprocess .check_output (args )
58- # On Python 3, this returns bytes (from STDOUT), so we
59- # convert to a string.
60- cmd_output = cmd_output .decode ('utf-8' )
61- # Also strip the output since it usually has a trailing newline.
62- return cmd_output .strip ()
63-
64-
6553def get_package_directories ():
6654 """Get a list of directories containing sub-packages.
6755
@@ -83,6 +71,30 @@ def get_package_directories():
8371 return result
8472
8573
74+ def get_travis_directories (package_list ):
75+ """Get list of packages that need to be tested on Travis CI.
76+
77+ See: https://travis-ci.com/
78+
79+ If the current Travis build is for a pull request (PR), this will
80+ limit the directories to the ones impacted by the PR. Otherwise
81+ it will just test all package directories.
82+
83+ :type package_list: list
84+ :param package_list: The list of **all** valid packages with unit tests.
85+
86+ :rtype: list
87+ :returns: A list of all package directories where tests
88+ need to be run.
89+ """
90+ if in_travis_pr ():
91+ pr_against_branch = travis_branch ()
92+ return get_changed_packages ('HEAD' , pr_against_branch ,
93+ package_list )
94+ else :
95+ return package_list
96+
97+
8698def verify_packages (subset , all_packages ):
8799 """Verify that a subset of packages are among all packages.
88100
@@ -107,6 +119,9 @@ def get_test_packages():
107119 Filters the package list in the following order:
108120
109121 * Check command line for packages passed in as positional arguments
122+ * Check if in Travis, then limit the subset based on changes
123+ in a Pull Request ("push" builds to branches may not have
124+ any filtering)
110125 * Just use all packages
111126
112127 :rtype: list
@@ -120,6 +135,8 @@ def get_test_packages():
120135 if args .packages is not UNSET_SENTINEL :
121136 verify_packages (args .packages , all_packages )
122137 return sorted (args .packages )
138+ elif in_travis ():
139+ return get_travis_directories (all_packages )
123140 else :
124141 return all_packages
125142
0 commit comments