Gilles Boccon-Gibod | 6ac91f7 | 2022-05-16 19:42:31 -0700 | [diff] [blame] | 1 | # Copyright 2021-2022 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 | # https://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 | """ |
| 16 | Invoke tasks |
| 17 | """ |
Gilles Boccon-Gibod | c2959da | 2022-12-10 09:29:51 -0800 | [diff] [blame] | 18 | |
| 19 | # ----------------------------------------------------------------------------- |
| 20 | # Imports |
| 21 | # ----------------------------------------------------------------------------- |
Gilles Boccon-Gibod | 6ac91f7 | 2022-05-16 19:42:31 -0700 | [diff] [blame] | 22 | import os |
| 23 | |
Gilles Boccon-Gibod | c2959da | 2022-12-10 09:29:51 -0800 | [diff] [blame] | 24 | from invoke import task, call, Collection |
Gilles Boccon-Gibod | 99758e4 | 2023-01-20 00:20:50 -0800 | [diff] [blame] | 25 | from invoke.exceptions import Exit, UnexpectedExit |
Gilles Boccon-Gibod | 6ac91f7 | 2022-05-16 19:42:31 -0700 | [diff] [blame] | 26 | |
Gilles Boccon-Gibod | c2959da | 2022-12-10 09:29:51 -0800 | [diff] [blame] | 27 | |
| 28 | # ----------------------------------------------------------------------------- |
Gilles Boccon-Gibod | 6ac91f7 | 2022-05-16 19:42:31 -0700 | [diff] [blame] | 29 | ROOT_DIR = os.path.dirname(os.path.realpath(__file__)) |
| 30 | |
| 31 | ns = Collection() |
| 32 | |
Gilles Boccon-Gibod | c2959da | 2022-12-10 09:29:51 -0800 | [diff] [blame] | 33 | |
| 34 | # ----------------------------------------------------------------------------- |
| 35 | # Build |
| 36 | # ----------------------------------------------------------------------------- |
Gilles Boccon-Gibod | 6ac91f7 | 2022-05-16 19:42:31 -0700 | [diff] [blame] | 37 | build_tasks = Collection() |
Ray | 216ce2a | 2022-08-16 14:50:30 -0400 | [diff] [blame] | 38 | ns.add_collection(build_tasks, name="build") |
Gilles Boccon-Gibod | 6ac91f7 | 2022-05-16 19:42:31 -0700 | [diff] [blame] | 39 | |
Gilles Boccon-Gibod | 135df0d | 2022-12-10 08:53:51 -0800 | [diff] [blame] | 40 | |
Gilles Boccon-Gibod | c2959da | 2022-12-10 09:29:51 -0800 | [diff] [blame] | 41 | # ----------------------------------------------------------------------------- |
Gilles Boccon-Gibod | 6ac91f7 | 2022-05-16 19:42:31 -0700 | [diff] [blame] | 42 | @task |
Gilles Boccon-Gibod | 1f78243 | 2022-08-16 13:37:26 -0700 | [diff] [blame] | 43 | def build(ctx, install=False): |
| 44 | if install: |
| 45 | ctx.run('python -m pip install .[build]') |
| 46 | |
Ray | 216ce2a | 2022-08-16 14:50:30 -0400 | [diff] [blame] | 47 | ctx.run("python -m build") |
Gilles Boccon-Gibod | 6ac91f7 | 2022-05-16 19:42:31 -0700 | [diff] [blame] | 48 | |
Gilles Boccon-Gibod | 135df0d | 2022-12-10 08:53:51 -0800 | [diff] [blame] | 49 | |
Gilles Boccon-Gibod | c2959da | 2022-12-10 09:29:51 -0800 | [diff] [blame] | 50 | # ----------------------------------------------------------------------------- |
Gilles Boccon-Gibod | 6ac91f7 | 2022-05-16 19:42:31 -0700 | [diff] [blame] | 51 | @task |
Ray | 216ce2a | 2022-08-16 14:50:30 -0400 | [diff] [blame] | 52 | def release_build(ctx): |
Gilles Boccon-Gibod | 1f78243 | 2022-08-16 13:37:26 -0700 | [diff] [blame] | 53 | build(ctx, install=True) |
Ray | 216ce2a | 2022-08-16 14:50:30 -0400 | [diff] [blame] | 54 | |
Gilles Boccon-Gibod | 135df0d | 2022-12-10 08:53:51 -0800 | [diff] [blame] | 55 | |
Gilles Boccon-Gibod | c2959da | 2022-12-10 09:29:51 -0800 | [diff] [blame] | 56 | # ----------------------------------------------------------------------------- |
Ray | 216ce2a | 2022-08-16 14:50:30 -0400 | [diff] [blame] | 57 | @task |
| 58 | def mkdocs(ctx): |
| 59 | ctx.run("mkdocs build -f docs/mkdocs/mkdocs.yml") |
| 60 | |
Gilles Boccon-Gibod | 135df0d | 2022-12-10 08:53:51 -0800 | [diff] [blame] | 61 | |
Gilles Boccon-Gibod | c2959da | 2022-12-10 09:29:51 -0800 | [diff] [blame] | 62 | # ----------------------------------------------------------------------------- |
| 63 | build_tasks.add_task(build, default=True) |
| 64 | build_tasks.add_task(release_build, name="release") |
Ray | 216ce2a | 2022-08-16 14:50:30 -0400 | [diff] [blame] | 65 | build_tasks.add_task(mkdocs, name="mkdocs") |
| 66 | |
Gilles Boccon-Gibod | c2959da | 2022-12-10 09:29:51 -0800 | [diff] [blame] | 67 | |
| 68 | # ----------------------------------------------------------------------------- |
| 69 | # Test |
| 70 | # ----------------------------------------------------------------------------- |
Ray | 216ce2a | 2022-08-16 14:50:30 -0400 | [diff] [blame] | 71 | test_tasks = Collection() |
| 72 | ns.add_collection(test_tasks, name="test") |
| 73 | |
Alan Rosenthal | f56ac14 | 2022-11-28 14:11:27 -0500 | [diff] [blame] | 74 | |
Gilles Boccon-Gibod | c2959da | 2022-12-10 09:29:51 -0800 | [diff] [blame] | 75 | # ----------------------------------------------------------------------------- |
Alan Rosenthal | f56ac14 | 2022-11-28 14:11:27 -0500 | [diff] [blame] | 76 | @task(incrementable=["verbose"]) |
Gilles Boccon-Gibod | c2959da | 2022-12-10 09:29:51 -0800 | [diff] [blame] | 77 | def test(ctx, match=None, junit=False, install=False, html=False, verbose=0): |
Ray | 216ce2a | 2022-08-16 14:50:30 -0400 | [diff] [blame] | 78 | # Install the package before running the tests |
| 79 | if install: |
| 80 | ctx.run("python -m pip install .[test]") |
Gilles Boccon-Gibod | 1f78243 | 2022-08-16 13:37:26 -0700 | [diff] [blame] | 81 | |
Gilles Boccon-Gibod | 6ac91f7 | 2022-05-16 19:42:31 -0700 | [diff] [blame] | 82 | args = "" |
| 83 | if junit: |
| 84 | args += "--junit-xml test-results.xml" |
Gilles Boccon-Gibod | c2959da | 2022-12-10 09:29:51 -0800 | [diff] [blame] | 85 | if match is not None: |
| 86 | args += f" -k '{match}'" |
Alan Rosenthal | a3e4674 | 2022-11-23 11:26:29 -0500 | [diff] [blame] | 87 | if html: |
Alan Rosenthal | f56ac14 | 2022-11-28 14:11:27 -0500 | [diff] [blame] | 88 | args += " --html results.html" |
| 89 | if verbose > 0: |
| 90 | args += f" -{'v' * verbose}" |
| 91 | ctx.run(f"python -m pytest {os.path.join(ROOT_DIR, 'tests')} {args}") |
Gilles Boccon-Gibod | 6ac91f7 | 2022-05-16 19:42:31 -0700 | [diff] [blame] | 92 | |
Gilles Boccon-Gibod | 135df0d | 2022-12-10 08:53:51 -0800 | [diff] [blame] | 93 | |
Gilles Boccon-Gibod | c2959da | 2022-12-10 09:29:51 -0800 | [diff] [blame] | 94 | # ----------------------------------------------------------------------------- |
Gilles Boccon-Gibod | 1f78243 | 2022-08-16 13:37:26 -0700 | [diff] [blame] | 95 | @task |
| 96 | def release_test(ctx): |
| 97 | test(ctx, install=True) |
| 98 | |
Gilles Boccon-Gibod | 135df0d | 2022-12-10 08:53:51 -0800 | [diff] [blame] | 99 | |
Gilles Boccon-Gibod | c2959da | 2022-12-10 09:29:51 -0800 | [diff] [blame] | 100 | # ----------------------------------------------------------------------------- |
| 101 | test_tasks.add_task(test, default=True) |
Gilles Boccon-Gibod | 1f78243 | 2022-08-16 13:37:26 -0700 | [diff] [blame] | 102 | test_tasks.add_task(release_test, name="release") |
Gilles Boccon-Gibod | c2959da | 2022-12-10 09:29:51 -0800 | [diff] [blame] | 103 | |
| 104 | # ----------------------------------------------------------------------------- |
| 105 | # Project |
| 106 | # ----------------------------------------------------------------------------- |
| 107 | project_tasks = Collection() |
| 108 | ns.add_collection(project_tasks, name="project") |
| 109 | |
| 110 | |
| 111 | # ----------------------------------------------------------------------------- |
| 112 | @task |
| 113 | def lint(ctx, disable='C,R', errors_only=False): |
| 114 | options = [] |
| 115 | if disable: |
| 116 | options.append(f"--disable={disable}") |
| 117 | if errors_only: |
| 118 | options.append("-E") |
| 119 | |
| 120 | if errors_only: |
| 121 | qualifier = ' (errors only)' |
| 122 | else: |
| 123 | qualifier = f' (disabled: {disable})' if disable else '' |
| 124 | |
| 125 | print(f">>> Running the linter{qualifier}...") |
| 126 | try: |
| 127 | ctx.run(f"pylint {' '.join(options)} bumble apps examples tasks.py") |
| 128 | print("The linter is happy. β
π π'") |
Gilles Boccon-Gibod | 99758e4 | 2023-01-20 00:20:50 -0800 | [diff] [blame] | 129 | except UnexpectedExit as exc: |
Gilles Boccon-Gibod | c2959da | 2022-12-10 09:29:51 -0800 | [diff] [blame] | 130 | print("Please check your code against the linter messages. β") |
Gilles Boccon-Gibod | 99758e4 | 2023-01-20 00:20:50 -0800 | [diff] [blame] | 131 | raise Exit(code=1) from exc |
Gilles Boccon-Gibod | c2959da | 2022-12-10 09:29:51 -0800 | [diff] [blame] | 132 | |
| 133 | |
| 134 | # ----------------------------------------------------------------------------- |
| 135 | @task |
| 136 | def format_code(ctx, check=False, diff=False): |
| 137 | options = [] |
| 138 | if check: |
| 139 | options.append("--check") |
| 140 | if diff: |
| 141 | options.append("--diff") |
| 142 | |
| 143 | print(">>> Running the formatter...") |
| 144 | try: |
| 145 | ctx.run(f"black -S {' '.join(options)} .") |
Gilles Boccon-Gibod | 99758e4 | 2023-01-20 00:20:50 -0800 | [diff] [blame] | 146 | except UnexpectedExit as exc: |
Gilles Boccon-Gibod | c2959da | 2022-12-10 09:29:51 -0800 | [diff] [blame] | 147 | print("Please run 'invoke project.format' or 'black .' to format the code. β") |
Gilles Boccon-Gibod | 99758e4 | 2023-01-20 00:20:50 -0800 | [diff] [blame] | 148 | raise Exit(code=1) from exc |
Gilles Boccon-Gibod | c2959da | 2022-12-10 09:29:51 -0800 | [diff] [blame] | 149 | |
| 150 | |
| 151 | # ----------------------------------------------------------------------------- |
Gilles Boccon-Gibod | 99758e4 | 2023-01-20 00:20:50 -0800 | [diff] [blame] | 152 | @task |
| 153 | def check_types(ctx): |
| 154 | checklist = ["apps", "bumble", "examples", "tests", "tasks.py"] |
| 155 | try: |
| 156 | ctx.run(f"mypy {' '.join(checklist)}") |
| 157 | except UnexpectedExit as exc: |
| 158 | print("Please check your code against the mypy messages.") |
| 159 | raise Exit(code=1) from exc |
| 160 | |
| 161 | |
| 162 | # ----------------------------------------------------------------------------- |
| 163 | @task( |
| 164 | pre=[ |
| 165 | call(format_code, check=True), |
| 166 | call(lint, errors_only=True), |
| 167 | call(check_types), |
| 168 | test, |
| 169 | ] |
| 170 | ) |
Gilles Boccon-Gibod | c2959da | 2022-12-10 09:29:51 -0800 | [diff] [blame] | 171 | def pre_commit(_ctx): |
| 172 | print("All good!") |
| 173 | |
| 174 | |
| 175 | # ----------------------------------------------------------------------------- |
| 176 | project_tasks.add_task(lint) |
| 177 | project_tasks.add_task(format_code, name="format") |
Gilles Boccon-Gibod | 99758e4 | 2023-01-20 00:20:50 -0800 | [diff] [blame] | 178 | project_tasks.add_task(check_types, name="check-types") |
Gilles Boccon-Gibod | c2959da | 2022-12-10 09:29:51 -0800 | [diff] [blame] | 179 | project_tasks.add_task(pre_commit) |
Gilles Boccon-Gibod | 640b9cd | 2023-06-04 10:24:07 -0700 | [diff] [blame] | 180 | |
| 181 | |
| 182 | # ----------------------------------------------------------------------------- |
| 183 | # Web |
| 184 | # ----------------------------------------------------------------------------- |
| 185 | web_tasks = Collection() |
| 186 | ns.add_collection(web_tasks, name="web") |
| 187 | |
| 188 | |
| 189 | # ----------------------------------------------------------------------------- |
| 190 | @task |
| 191 | def serve(ctx, port=8000): |
| 192 | """ |
| 193 | Run a simple HTTP server for the examples under the `web` directory. |
| 194 | """ |
| 195 | import http.server |
| 196 | |
| 197 | address = ("", port) |
Gilles Boccon-Gibod | 71e11b7 | 2023-08-15 15:20:48 -0700 | [diff] [blame] | 198 | |
Gilles Boccon-Gibod | 640b9cd | 2023-06-04 10:24:07 -0700 | [diff] [blame] | 199 | class Handler(http.server.SimpleHTTPRequestHandler): |
| 200 | def __init__(self, *args, **kwargs): |
| 201 | super().__init__(*args, directory="web", **kwargs) |
Gilles Boccon-Gibod | 71e11b7 | 2023-08-15 15:20:48 -0700 | [diff] [blame] | 202 | |
Gilles Boccon-Gibod | 640b9cd | 2023-06-04 10:24:07 -0700 | [diff] [blame] | 203 | server = http.server.HTTPServer(address, Handler) |
| 204 | print(f"Now serving on port {port} πΈοΈ") |
| 205 | server.serve_forever() |
| 206 | |
| 207 | |
| 208 | # ----------------------------------------------------------------------------- |
| 209 | web_tasks.add_task(serve) |