|
| 1 | +# Copyright 2017 Google LLC |
| 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 | +"""Print a list of packages which require testing.""" |
| 16 | + |
| 17 | +import subprocess |
| 18 | +import pathlib |
| 19 | + |
| 20 | +import ci_diff_helper |
| 21 | + |
| 22 | + |
| 23 | +def print_environment(environment): |
| 24 | + print("-> CI environment:") |
| 25 | + print('Branch', environment.branch) |
| 26 | + print('PR', environment.pr) |
| 27 | + print('In PR', environment.in_pr) |
| 28 | + print('Repo URL', environment.repo_url) |
| 29 | + if environment.in_pr: |
| 30 | + print('PR Base', environment.base) |
| 31 | + |
| 32 | + |
| 33 | +def get_base(environment): |
| 34 | + if environment.in_pr: |
| 35 | + return environment.base |
| 36 | + else: |
| 37 | + # If we're not in a PR, just calculate the changes between this commit |
| 38 | + # and its parent. |
| 39 | + return 'HEAD~1' |
| 40 | + |
| 41 | + |
| 42 | +def get_changed_files(base): |
| 43 | + return subprocess.check_output([ |
| 44 | + 'git', 'diff', '--name-only', f'{base}..HEAD', |
| 45 | + ], stderr=subprocess.DEVNULL).decode('utf8').strip().split('\n') |
| 46 | + |
| 47 | + |
| 48 | +def determine_changed_packages(changed_files): |
| 49 | + packages = [ |
| 50 | + path.parent for path in pathlib.Path('.').glob('*/nox.py') |
| 51 | + ] |
| 52 | + |
| 53 | + changed_packages = set() |
| 54 | + for file in changed_files: |
| 55 | + file = pathlib.Path(file) |
| 56 | + for package in packages: |
| 57 | + if package in file.parents: |
| 58 | + changed_packages.add(package) |
| 59 | + |
| 60 | + return changed_packages |
| 61 | + |
| 62 | + |
| 63 | +def main(): |
| 64 | + environment = ci_diff_helper.get_config() |
| 65 | + print_environment(environment) |
| 66 | + base = get_base(environment) |
| 67 | + changed_files = get_changed_files(base) |
| 68 | + packages = determine_changed_packages(changed_files) |
| 69 | + |
| 70 | + print(f"Comparing against {base}.") |
| 71 | + print("-> Changed packages:") |
| 72 | + |
| 73 | + for package in packages: |
| 74 | + print(package) |
| 75 | + |
| 76 | + |
| 77 | +main() |
0 commit comments