Skip to content

Commit cbde0a8

Browse files
busunkim96dandhlee
authored andcommitted
chore: update templates
1 parent 7556fdd commit cbde0a8

File tree

1 file changed

+224
-0
lines changed

1 file changed

+224
-0
lines changed

automl/beta/noxfile.py

Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
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+
import sys
20+
21+
import nox
22+
23+
24+
# WARNING - WARNING - WARNING - WARNING - WARNING
25+
# WARNING - WARNING - WARNING - WARNING - WARNING
26+
# DO NOT EDIT THIS FILE EVER!
27+
# WARNING - WARNING - WARNING - WARNING - WARNING
28+
# WARNING - WARNING - WARNING - WARNING - WARNING
29+
30+
# Copy `noxfile_config.py` to your directory and modify it instead.
31+
32+
33+
# `TEST_CONFIG` dict is a configuration hook that allows users to
34+
# modify the test configurations. The values here should be in sync
35+
# with `noxfile_config.py`. Users will copy `noxfile_config.py` into
36+
# their directory and modify it.
37+
38+
TEST_CONFIG = {
39+
# You can opt out from the test for specific Python versions.
40+
'ignored_versions': ["2.7"],
41+
42+
# An envvar key for determining the project id to use. Change it
43+
# to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a
44+
# build specific Cloud project. You can also use your own string
45+
# to use your own Cloud project.
46+
'gcloud_project_env': 'GOOGLE_CLOUD_PROJECT',
47+
# 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT',
48+
49+
# A dictionary you want to inject into your test. Don't put any
50+
# secrets here. These values will override predefined values.
51+
'envs': {},
52+
}
53+
54+
55+
try:
56+
# Ensure we can import noxfile_config in the project's directory.
57+
sys.path.append('.')
58+
from noxfile_config import TEST_CONFIG_OVERRIDE
59+
except ImportError as e:
60+
print("No user noxfile_config found: detail: {}".format(e))
61+
TEST_CONFIG_OVERRIDE = {}
62+
63+
# Update the TEST_CONFIG with the user supplied values.
64+
TEST_CONFIG.update(TEST_CONFIG_OVERRIDE)
65+
66+
67+
def get_pytest_env_vars():
68+
"""Returns a dict for pytest invocation."""
69+
ret = {}
70+
71+
# Override the GCLOUD_PROJECT and the alias.
72+
env_key = TEST_CONFIG['gcloud_project_env']
73+
# This should error out if not set.
74+
ret['GOOGLE_CLOUD_PROJECT'] = os.environ[env_key]
75+
76+
# Apply user supplied envs.
77+
ret.update(TEST_CONFIG['envs'])
78+
return ret
79+
80+
81+
# DO NOT EDIT - automatically generated.
82+
# All versions used to tested samples.
83+
ALL_VERSIONS = ["2.7", "3.6", "3.7", "3.8"]
84+
85+
# Any default versions that should be ignored.
86+
IGNORED_VERSIONS = TEST_CONFIG['ignored_versions']
87+
88+
TESTED_VERSIONS = sorted([v for v in ALL_VERSIONS if v not in IGNORED_VERSIONS])
89+
90+
INSTALL_LIBRARY_FROM_SOURCE = bool(os.environ.get("INSTALL_LIBRARY_FROM_SOURCE", False))
91+
#
92+
# Style Checks
93+
#
94+
95+
96+
def _determine_local_import_names(start_dir):
97+
"""Determines all import names that should be considered "local".
98+
99+
This is used when running the linter to insure that import order is
100+
properly checked.
101+
"""
102+
file_ext_pairs = [os.path.splitext(path) for path in os.listdir(start_dir)]
103+
return [
104+
basename
105+
for basename, extension in file_ext_pairs
106+
if extension == ".py"
107+
or os.path.isdir(os.path.join(start_dir, basename))
108+
and basename not in ("__pycache__")
109+
]
110+
111+
112+
# Linting with flake8.
113+
#
114+
# We ignore the following rules:
115+
# E203: whitespace before ‘:’
116+
# E266: too many leading ‘#’ for block comment
117+
# E501: line too long
118+
# I202: Additional newline in a section of imports
119+
#
120+
# We also need to specify the rules which are ignored by default:
121+
# ['E226', 'W504', 'E126', 'E123', 'W503', 'E24', 'E704', 'E121']
122+
FLAKE8_COMMON_ARGS = [
123+
"--show-source",
124+
"--builtin=gettext",
125+
"--max-complexity=20",
126+
"--import-order-style=google",
127+
"--exclude=.nox,.cache,env,lib,generated_pb2,*_pb2.py,*_pb2_grpc.py",
128+
"--ignore=E121,E123,E126,E203,E226,E24,E266,E501,E704,W503,W504,I202",
129+
"--max-line-length=88",
130+
]
131+
132+
133+
@nox.session
134+
def lint(session):
135+
session.install("flake8", "flake8-import-order")
136+
137+
local_names = _determine_local_import_names(".")
138+
args = FLAKE8_COMMON_ARGS + [
139+
"--application-import-names",
140+
",".join(local_names),
141+
"."
142+
]
143+
session.run("flake8", *args)
144+
145+
146+
#
147+
# Sample Tests
148+
#
149+
150+
151+
PYTEST_COMMON_ARGS = ["--junitxml=sponge_log.xml"]
152+
153+
154+
def _session_tests(session, post_install=None):
155+
"""Runs py.test for a particular project."""
156+
if os.path.exists("requirements.txt"):
157+
session.install("-r", "requirements.txt")
158+
159+
if os.path.exists("requirements-test.txt"):
160+
session.install("-r", "requirements-test.txt")
161+
162+
if INSTALL_LIBRARY_FROM_SOURCE:
163+
session.install("-e", _get_repo_root())
164+
165+
if post_install:
166+
post_install(session)
167+
168+
session.run(
169+
"pytest",
170+
*(PYTEST_COMMON_ARGS + session.posargs),
171+
# Pytest will return 5 when no tests are collected. This can happen
172+
# on travis where slow and flaky tests are excluded.
173+
# See http://doc.pytest.org/en/latest/_modules/_pytest/main.html
174+
success_codes=[0, 5],
175+
env=get_pytest_env_vars()
176+
)
177+
178+
179+
@nox.session(python=ALL_VERSIONS)
180+
def py(session):
181+
"""Runs py.test for a sample using the specified version of Python."""
182+
if session.python in TESTED_VERSIONS:
183+
_session_tests(session)
184+
else:
185+
session.skip("SKIPPED: {} tests are disabled for this sample.".format(
186+
session.python
187+
))
188+
189+
190+
#
191+
# Readmegen
192+
#
193+
194+
195+
def _get_repo_root():
196+
""" Returns the root folder of the project. """
197+
# Get root of this repository. Assume we don't have directories nested deeper than 10 items.
198+
p = Path(os.getcwd())
199+
for i in range(10):
200+
if p is None:
201+
break
202+
if Path(p / ".git").exists():
203+
return str(p)
204+
p = p.parent
205+
raise Exception("Unable to detect repository root.")
206+
207+
208+
GENERATED_READMES = sorted([x for x in Path(".").rglob("*.rst.in")])
209+
210+
211+
@nox.session
212+
@nox.parametrize("path", GENERATED_READMES)
213+
def readmegen(session, path):
214+
"""(Re-)generates the readme for a sample."""
215+
session.install("jinja2", "pyyaml")
216+
dir_ = os.path.dirname(path)
217+
218+
if os.path.exists(os.path.join(dir_, "requirements.txt")):
219+
session.install("-r", os.path.join(dir_, "requirements.txt"))
220+
221+
in_file = os.path.join(dir_, "README.rst.in")
222+
session.run(
223+
"python", _get_repo_root() + "/scripts/readme-gen/readme_gen.py", in_file
224+
)

0 commit comments

Comments
 (0)