Anthony DiGirolamo | fb41f92 | 2021-03-02 14:32:01 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # Copyright 2021 The Pigweed Authors |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | # use this file except in compliance with the License. You may obtain a copy of |
| 6 | # the License at |
| 7 | # |
| 8 | # https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | # License for the specific language governing permissions and limitations under |
| 14 | # the License. |
| 15 | """Example presubmit check script.""" |
| 16 | |
| 17 | import argparse |
| 18 | import logging |
| 19 | import os |
| 20 | from pathlib import Path |
| 21 | import re |
| 22 | import sys |
| 23 | |
| 24 | try: |
| 25 | import pw_cli.log |
| 26 | except ImportError: |
| 27 | print('ERROR: Activate the environment before running presubmits!', |
| 28 | file=sys.stderr) |
| 29 | sys.exit(2) |
| 30 | |
| 31 | import pw_presubmit |
Rob Mohr | 7f2e41a | 2021-10-21 14:09:10 -0700 | [diff] [blame] | 32 | from pw_presubmit import ( |
| 33 | build, |
| 34 | cli, |
| 35 | cpp_checks, |
| 36 | format_code, |
| 37 | git_repo, |
| 38 | inclusive_language, |
| 39 | install_hook, |
| 40 | python_checks, |
| 41 | PresubmitContext, |
| 42 | ) |
Anthony DiGirolamo | fb41f92 | 2021-03-02 14:32:01 -0800 | [diff] [blame] | 43 | |
| 44 | _LOG = logging.getLogger(__name__) |
| 45 | |
| 46 | # Set up variables for key project paths. |
| 47 | try: |
| 48 | PROJECT_ROOT = Path(os.environ['PIGWEED_EXPERIMENTAL_ROOT']) |
| 49 | except KeyError: |
| 50 | print( |
| 51 | 'ERROR: The presubmit checks must be run in the sample project\'s root' |
| 52 | ' directory', |
| 53 | file=sys.stderr) |
| 54 | sys.exit(2) |
| 55 | |
| 56 | PIGWEED_ROOT = PROJECT_ROOT / 'third_party' / 'pigweed' |
| 57 | REPOS = ( |
| 58 | PROJECT_ROOT, |
| 59 | PIGWEED_ROOT, |
| 60 | PROJECT_ROOT / 'third_party' / 'nanopb', |
| 61 | ) |
| 62 | |
Anthony DiGirolamo | fb41f92 | 2021-03-02 14:32:01 -0800 | [diff] [blame] | 63 | # Rerun the build if files with these extensions change. |
| 64 | _BUILD_EXTENSIONS = frozenset( |
| 65 | ['.rst', '.gn', '.gni', *format_code.C_FORMAT.extensions]) |
| 66 | |
| 67 | |
| 68 | # |
| 69 | # Presubmit checks |
| 70 | # |
| 71 | def default_build(ctx: PresubmitContext): |
| 72 | """Creates a default build.""" |
Rob Mohr | 9c4c767 | 2022-08-18 14:49:15 +0000 | [diff] [blame] | 73 | build.gn_gen(ctx) |
Rob Mohr | fa6c95f | 2022-08-16 23:26:11 +0000 | [diff] [blame] | 74 | build.ninja(ctx) |
Anthony DiGirolamo | fb41f92 | 2021-03-02 14:32:01 -0800 | [diff] [blame] | 75 | |
| 76 | |
| 77 | def check_for_git_changes(_: PresubmitContext): |
| 78 | """Checks that repositories have all changes commited.""" |
| 79 | checked_repos = (PIGWEED_ROOT, *REPOS) |
| 80 | changes = [r for r in checked_repos if git_repo.has_uncommitted_changes(r)] |
| 81 | for repo in changes: |
| 82 | _LOG.error('There are uncommitted changes in the %s repo!', repo.name) |
| 83 | if changes: |
| 84 | _LOG.warning( |
| 85 | 'Commit or stash pending changes before running the presubmit.') |
| 86 | raise pw_presubmit.PresubmitFailure |
| 87 | |
| 88 | |
| 89 | # Avoid running some checks on certain paths. |
| 90 | PATH_EXCLUSIONS = ( |
| 91 | re.compile(r'^external/'), |
| 92 | re.compile(r'^third_party/'), |
| 93 | re.compile(r'^vendor/'), |
| 94 | ) |
| 95 | |
Anthony DiGirolamo | fb41f92 | 2021-03-02 14:32:01 -0800 | [diff] [blame] | 96 | # |
| 97 | # Presubmit check programs |
| 98 | # |
Rob Mohr | 7f2e41a | 2021-10-21 14:09:10 -0700 | [diff] [blame] | 99 | OTHER_CHECKS = ( |
| 100 | build.gn_gen_check, |
| 101 | inclusive_language.inclusive_language.with_filter(exclude=PATH_EXCLUSIONS), |
| 102 | ) |
Rob Mohr | d5e996c | 2021-05-24 11:51:16 -0700 | [diff] [blame] | 103 | |
Anthony DiGirolamo | fb41f92 | 2021-03-02 14:32:01 -0800 | [diff] [blame] | 104 | QUICK = ( |
| 105 | # List some presubmit checks to run |
| 106 | default_build, |
| 107 | # Use the upstream formatting checks, with custom path filters applied. |
Wyatt Hepler | 87969a8 | 2022-07-29 00:26:00 +0000 | [diff] [blame] | 108 | format_code.presubmit_checks(), |
Anthony DiGirolamo | fb41f92 | 2021-03-02 14:32:01 -0800 | [diff] [blame] | 109 | ) |
Rob Mohr | d5e996c | 2021-05-24 11:51:16 -0700 | [diff] [blame] | 110 | |
Rob Mohr | f831e41 | 2021-08-03 13:56:59 -0700 | [diff] [blame] | 111 | LINTFORMAT = ( |
| 112 | # Use the upstream formatting checks, with custom path filters applied. |
Wyatt Hepler | 87969a8 | 2022-07-29 00:26:00 +0000 | [diff] [blame] | 113 | format_code.presubmit_checks(), |
| 114 | python_checks.gn_python_lint, |
| 115 | cpp_checks.pragma_once, |
Rob Mohr | f831e41 | 2021-08-03 13:56:59 -0700 | [diff] [blame] | 116 | ) |
| 117 | |
Anthony DiGirolamo | fb41f92 | 2021-03-02 14:32:01 -0800 | [diff] [blame] | 118 | FULL = ( |
Wyatt Hepler | 87969a8 | 2022-07-29 00:26:00 +0000 | [diff] [blame] | 119 | cpp_checks.pragma_once, |
Anthony DiGirolamo | fb41f92 | 2021-03-02 14:32:01 -0800 | [diff] [blame] | 120 | QUICK, # Add all checks from the 'quick' program |
| 121 | # Use the upstream Python checks, with custom path filters applied. |
Wyatt Hepler | 87969a8 | 2022-07-29 00:26:00 +0000 | [diff] [blame] | 122 | python_checks.gn_python_check, |
Anthony DiGirolamo | fb41f92 | 2021-03-02 14:32:01 -0800 | [diff] [blame] | 123 | ) |
Rob Mohr | d5e996c | 2021-05-24 11:51:16 -0700 | [diff] [blame] | 124 | |
| 125 | PROGRAMS = pw_presubmit.Programs( |
| 126 | quick=QUICK, |
| 127 | full=FULL, |
Rob Mohr | f831e41 | 2021-08-03 13:56:59 -0700 | [diff] [blame] | 128 | lintformat=LINTFORMAT, |
Rob Mohr | d5e996c | 2021-05-24 11:51:16 -0700 | [diff] [blame] | 129 | other_checks=OTHER_CHECKS, |
| 130 | ) |
Anthony DiGirolamo | fb41f92 | 2021-03-02 14:32:01 -0800 | [diff] [blame] | 131 | |
| 132 | |
Wyatt Hepler | 87969a8 | 2022-07-29 00:26:00 +0000 | [diff] [blame] | 133 | def run(install: bool, exclude: list, **presubmit_args) -> int: |
Anthony DiGirolamo | fb41f92 | 2021-03-02 14:32:01 -0800 | [diff] [blame] | 134 | """Process the --install argument then invoke pw_presubmit.""" |
| 135 | |
| 136 | # Install the presubmit Git pre-push hook, if requested. |
| 137 | if install: |
Rob Mohr | 7f2e41a | 2021-10-21 14:09:10 -0700 | [diff] [blame] | 138 | install_hook.install_hook(__file__, 'pre-push', ['--base', 'HEAD~'], |
| 139 | git_repo.root()) |
Anthony DiGirolamo | fb41f92 | 2021-03-02 14:32:01 -0800 | [diff] [blame] | 140 | return 0 |
| 141 | |
Wyatt Hepler | 87969a8 | 2022-07-29 00:26:00 +0000 | [diff] [blame] | 142 | exclude.extend(PATH_EXCLUSIONS) |
| 143 | return cli.run(root=PROJECT_ROOT, exclude=exclude, **presubmit_args) |
Anthony DiGirolamo | fb41f92 | 2021-03-02 14:32:01 -0800 | [diff] [blame] | 144 | |
| 145 | |
| 146 | def main() -> int: |
| 147 | """Run the presubmit checks for this repository.""" |
| 148 | parser = argparse.ArgumentParser(description=__doc__) |
| 149 | cli.add_arguments(parser, PROGRAMS, 'quick') |
| 150 | |
| 151 | # Define an option for installing a Git pre-push hook for this script. |
| 152 | parser.add_argument( |
| 153 | '--install', |
| 154 | action='store_true', |
| 155 | help='Install the presubmit as a Git pre-push hook and exit.') |
| 156 | |
| 157 | return run(**vars(parser.parse_args())) |
| 158 | |
| 159 | |
| 160 | if __name__ == '__main__': |
| 161 | pw_cli.log.install(logging.INFO) |
| 162 | sys.exit(main()) |