|
| 1 | +# Copyright 2019 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 | +from __future__ import print_function |
| 16 | + |
| 17 | +import os |
| 18 | +from pathlib import Path |
| 19 | + |
| 20 | +import nox |
| 21 | + |
| 22 | + |
| 23 | +# DO NOT EDIT - automatically generated. |
| 24 | +# All versions used to tested samples. |
| 25 | +ALL_VERSIONS = ["2.7", "3.6", "3.7", "3.8"] |
| 26 | + |
| 27 | +# Any default versions that should be ignored. |
| 28 | +IGNORED_VERSIONS = ["2.7", "3.8"] |
| 29 | + |
| 30 | +TESTED_VERSIONS = sorted([v for v in ALL_VERSIONS if v not in IGNORED_VERSIONS]) |
| 31 | + |
| 32 | +# |
| 33 | +# Style Checks |
| 34 | +# |
| 35 | + |
| 36 | + |
| 37 | +# Ignore I202 "Additional newline in a section of imports." to accommodate |
| 38 | +# region tags in import blocks. Since we specify an explicit ignore, we also |
| 39 | +# have to explicitly ignore the list of default ignores: |
| 40 | +# `E121,E123,E126,E226,E24,E704,W503,W504` as shown by `flake8 --help`. |
| 41 | +def _determine_local_import_names(start_dir): |
| 42 | + """Determines all import names that should be considered "local". |
| 43 | +
|
| 44 | + This is used when running the linter to insure that import order is |
| 45 | + properly checked. |
| 46 | + """ |
| 47 | + file_ext_pairs = [os.path.splitext(path) for path in os.listdir(start_dir)] |
| 48 | + return [ |
| 49 | + basename |
| 50 | + for basename, extension in file_ext_pairs |
| 51 | + if extension == ".py" |
| 52 | + or os.path.isdir(os.path.join(start_dir, basename)) |
| 53 | + and basename not in ("__pycache__") |
| 54 | + ] |
| 55 | + |
| 56 | + |
| 57 | +FLAKE8_COMMON_ARGS = [ |
| 58 | + "--show-source", |
| 59 | + "--builtin=gettext", |
| 60 | + "--max-complexity=20", |
| 61 | + "--import-order-style=google", |
| 62 | + "--exclude=.nox,.cache,env,lib,generated_pb2,*_pb2.py,*_pb2_grpc.py", |
| 63 | + "--ignore=E121,E123,E126,E203,E226,E24,E266,E501,E704,W503,W504,I100,I201,I202", |
| 64 | + "--max-line-length=88", |
| 65 | +] |
| 66 | + |
| 67 | + |
| 68 | +@nox.session |
| 69 | +def lint(session): |
| 70 | + session.install("flake8", "flake8-import-order") |
| 71 | + |
| 72 | + local_names = _determine_local_import_names(".") |
| 73 | + args = FLAKE8_COMMON_ARGS + [ |
| 74 | + "--application-import-names", |
| 75 | + ",".join(local_names), |
| 76 | + ".", |
| 77 | + ] |
| 78 | + session.run("flake8", *args) |
| 79 | + |
| 80 | + |
| 81 | +# |
| 82 | +# Sample Tests |
| 83 | +# |
| 84 | + |
| 85 | + |
| 86 | +PYTEST_COMMON_ARGS = ["--junitxml=sponge_log.xml"] |
| 87 | + |
| 88 | + |
| 89 | +def _session_tests(session, post_install=None): |
| 90 | + """Runs py.test for a particular project.""" |
| 91 | + if os.path.exists("requirements.txt"): |
| 92 | + session.install("-r", "requirements.txt") |
| 93 | + |
| 94 | + if os.path.exists("requirements-test.txt"): |
| 95 | + session.install("-r", "requirements-test.txt") |
| 96 | + |
| 97 | + if post_install: |
| 98 | + post_install(session) |
| 99 | + |
| 100 | + session.run( |
| 101 | + "pytest", |
| 102 | + *(PYTEST_COMMON_ARGS + session.posargs), |
| 103 | + # Pytest will return 5 when no tests are collected. This can happen |
| 104 | + # on travis where slow and flaky tests are excluded. |
| 105 | + # See http://doc.pytest.org/en/latest/_modules/_pytest/main.html |
| 106 | + success_codes=[0, 5] |
| 107 | + ) |
| 108 | + |
| 109 | + |
| 110 | +@nox.session(python=ALL_VERSIONS) |
| 111 | +def py(session): |
| 112 | + """Runs py.test for a sample using the specified version of Python.""" |
| 113 | + if session.python in TESTED_VERSIONS: |
| 114 | + _session_tests(session) |
| 115 | + else: |
| 116 | + print("SKIPPED: {} tests are disabled for this sample.".format(session.python)) |
| 117 | + |
| 118 | + |
| 119 | +# |
| 120 | +# Readmegen |
| 121 | +# |
| 122 | + |
| 123 | + |
| 124 | +def _get_repo_root(): |
| 125 | + """ Returns the root folder of the project. """ |
| 126 | + # Get root of this repository. Assume we don't have directories nested deeper than 10 items. |
| 127 | + p = Path(os.getcwd()) |
| 128 | + for i in range(10): |
| 129 | + if p is None: |
| 130 | + break |
| 131 | + if Path(p / ".git").exists(): |
| 132 | + return str(p) |
| 133 | + p = p.parent |
| 134 | + raise Exception("Unable to detect repository root.") |
| 135 | + |
| 136 | + |
| 137 | +GENERATED_READMES = sorted([x for x in Path(".").rglob("*.rst.in")]) |
| 138 | + |
| 139 | + |
| 140 | +@nox.session |
| 141 | +@nox.parametrize("path", GENERATED_READMES) |
| 142 | +def readmegen(session, path): |
| 143 | + """(Re-)generates the readme for a sample.""" |
| 144 | + session.install("jinja2", "pyyaml") |
| 145 | + |
| 146 | + if os.path.exists(os.path.join(path, "requirements.txt")): |
| 147 | + session.install("-r", os.path.join(path, "requirements.txt")) |
| 148 | + |
| 149 | + in_file = os.path.join(path, "README.rst.in") |
| 150 | + session.run( |
| 151 | + "python", _get_repo_root() + "/scripts/readme-gen/readme_gen.py", in_file |
| 152 | + ) |
0 commit comments