Skip to content

Commit fcb65e3

Browse files
authored
chore: generate python samples templates in owlbot.py (#108)
Generate python samples templates in owlbot.py
1 parent 15b1db7 commit fcb65e3

File tree

1 file changed

+33
-31
lines changed

1 file changed

+33
-31
lines changed

privateca/snippets/noxfile.py

+33-31
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2021 Google LLC
1+
# Copyright 2019 Google LLC
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
@@ -28,8 +28,9 @@
2828
# WARNING - WARNING - WARNING - WARNING - WARNING
2929
# WARNING - WARNING - WARNING - WARNING - WARNING
3030

31-
# Copy `noxfile_config.py` to your directory and modify it instead.
31+
BLACK_VERSION = "black==19.10b0"
3232

33+
# Copy `noxfile_config.py` to your directory and modify it instead.
3334

3435
# `TEST_CONFIG` dict is a configuration hook that allows users to
3536
# modify the test configurations. The values here should be in sync
@@ -38,29 +39,31 @@
3839

3940
TEST_CONFIG = {
4041
# You can opt out from the test for specific Python versions.
41-
"ignored_versions": ["2.7"],
42+
'ignored_versions': [],
43+
4244
# Old samples are opted out of enforcing Python type hints
4345
# All new samples should feature them
44-
"enforce_type_hints": False,
46+
'enforce_type_hints': False,
47+
4548
# An envvar key for determining the project id to use. Change it
4649
# to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a
4750
# build specific Cloud project. You can also use your own string
4851
# to use your own Cloud project.
49-
"gcloud_project_env": "GOOGLE_CLOUD_PROJECT",
52+
'gcloud_project_env': 'GOOGLE_CLOUD_PROJECT',
5053
# 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT',
5154
# If you need to use a specific version of pip,
5255
# change pip_version_override to the string representation
5356
# of the version number, for example, "20.2.4"
5457
"pip_version_override": None,
5558
# A dictionary you want to inject into your test. Don't put any
5659
# secrets here. These values will override predefined values.
57-
"envs": {},
60+
'envs': {},
5861
}
5962

6063

6164
try:
6265
# Ensure we can import noxfile_config in the project's directory.
63-
sys.path.append(".")
66+
sys.path.append('.')
6467
from noxfile_config import TEST_CONFIG_OVERRIDE
6568
except ImportError as e:
6669
print("No user noxfile_config found: detail: {}".format(e))
@@ -75,33 +78,33 @@ def get_pytest_env_vars() -> Dict[str, str]:
7578
ret = {}
7679

7780
# Override the GCLOUD_PROJECT and the alias.
78-
env_key = TEST_CONFIG["gcloud_project_env"]
81+
env_key = TEST_CONFIG['gcloud_project_env']
7982
# This should error out if not set.
80-
ret["GOOGLE_CLOUD_PROJECT"] = os.environ[env_key]
81-
ret["GCLOUD_PROJECT"] = os.environ[env_key] # deprecated
83+
ret['GOOGLE_CLOUD_PROJECT'] = os.environ[env_key]
8284

8385
# Apply user supplied envs.
84-
ret.update(TEST_CONFIG["envs"])
86+
ret.update(TEST_CONFIG['envs'])
8587
return ret
8688

8789

8890
# DO NOT EDIT - automatically generated.
89-
# All versions used to tested samples.
90-
ALL_VERSIONS = ["2.7", "3.6", "3.7", "3.8", "3.9"]
91+
# All versions used to test samples.
92+
ALL_VERSIONS = ["3.6", "3.7", "3.8", "3.9"]
9193

9294
# Any default versions that should be ignored.
93-
IGNORED_VERSIONS = TEST_CONFIG["ignored_versions"]
95+
IGNORED_VERSIONS = TEST_CONFIG['ignored_versions']
9496

9597
TESTED_VERSIONS = sorted([v for v in ALL_VERSIONS if v not in IGNORED_VERSIONS])
9698

97-
INSTALL_LIBRARY_FROM_SOURCE = bool(os.environ.get("INSTALL_LIBRARY_FROM_SOURCE", False))
99+
INSTALL_LIBRARY_FROM_SOURCE = os.environ.get("INSTALL_LIBRARY_FROM_SOURCE", False) in ("True", "true")
98100
#
99101
# Style Checks
100102
#
101103

102104

103105
def _determine_local_import_names(start_dir: str) -> List[str]:
104106
"""Determines all import names that should be considered "local".
107+
105108
This is used when running the linter to insure that import order is
106109
properly checked.
107110
"""
@@ -138,7 +141,7 @@ def _determine_local_import_names(start_dir: str) -> List[str]:
138141

139142
@nox.session
140143
def lint(session: nox.sessions.Session) -> None:
141-
if not TEST_CONFIG["enforce_type_hints"]:
144+
if not TEST_CONFIG['enforce_type_hints']:
142145
session.install("flake8", "flake8-import-order")
143146
else:
144147
session.install("flake8", "flake8-import-order", "flake8-annotations")
@@ -147,24 +150,21 @@ def lint(session: nox.sessions.Session) -> None:
147150
args = FLAKE8_COMMON_ARGS + [
148151
"--application-import-names",
149152
",".join(local_names),
150-
".",
153+
"."
151154
]
152155
session.run("flake8", *args)
153-
154-
155156
#
156157
# Black
157158
#
158159

159160

160161
@nox.session
161162
def blacken(session: nox.sessions.Session) -> None:
162-
session.install("black")
163+
session.install(BLACK_VERSION)
163164
python_files = [path for path in os.listdir(".") if path.endswith(".py")]
164165

165166
session.run("black", *python_files)
166167

167-
168168
#
169169
# Sample Tests
170170
#
@@ -173,9 +173,7 @@ def blacken(session: nox.sessions.Session) -> None:
173173
PYTEST_COMMON_ARGS = ["--junitxml=sponge_log.xml"]
174174

175175

176-
def _session_tests(
177-
session: nox.sessions.Session, post_install: Callable = None
178-
) -> None:
176+
def _session_tests(session: nox.sessions.Session, post_install: Callable = None) -> None:
179177
if TEST_CONFIG["pip_version_override"]:
180178
pip_version = TEST_CONFIG["pip_version_override"]
181179
session.install(f"pip=={pip_version}")
@@ -205,7 +203,7 @@ def _session_tests(
205203
# on travis where slow and flaky tests are excluded.
206204
# See http://doc.pytest.org/en/latest/_modules/_pytest/main.html
207205
success_codes=[0, 5],
208-
env=get_pytest_env_vars(),
206+
env=get_pytest_env_vars()
209207
)
210208

211209

@@ -215,9 +213,9 @@ def py(session: nox.sessions.Session) -> None:
215213
if session.python in TESTED_VERSIONS:
216214
_session_tests(session)
217215
else:
218-
session.skip(
219-
"SKIPPED: {} tests are disabled for this sample.".format(session.python)
220-
)
216+
session.skip("SKIPPED: {} tests are disabled for this sample.".format(
217+
session.python
218+
))
221219

222220

223221
#
@@ -226,15 +224,19 @@ def py(session: nox.sessions.Session) -> None:
226224

227225

228226
def _get_repo_root() -> Optional[str]:
229-
"""Returns the root folder of the project."""
230-
# Get root of this repository.
231-
# Assume we don't have directories nested deeper than 10 items.
227+
""" Returns the root folder of the project. """
228+
# Get root of this repository. Assume we don't have directories nested deeper than 10 items.
232229
p = Path(os.getcwd())
233230
for i in range(10):
234231
if p is None:
235232
break
236233
if Path(p / ".git").exists():
237234
return str(p)
235+
# .git is not available in repos cloned via Cloud Build
236+
# setup.py is always in the library's root, so use that instead
237+
# https://github.com/googleapis/synthtool/issues/792
238+
if Path(p / "setup.py").exists():
239+
return str(p)
238240
p = p.parent
239241
raise Exception("Unable to detect repository root.")
240242

0 commit comments

Comments
 (0)