-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnoxfile.py
216 lines (167 loc) · 9.36 KB
/
noxfile.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import logging
import nox # noqa
from pathlib import Path # noqa
import sys
# add parent folder to python path so that we can import noxfile_utils.py
# note that you need to "pip install -r noxfile-requiterements.txt" for this file to work.
sys.path.append(str(Path(__file__).parent / "ci_tools"))
from nox_utils import PY27, PY37, PY36, PY35, PY38, power_session, rm_folder, rm_file, PowerSession # noqa
pkg_name = "qdscreen"
gh_org = "python-qds"
gh_repo = "qdscreen"
ALL_PY_VERSIONS = [PY38, PY37, PY36, PY35, PY27]
ENVS = {
PY27: {"coverage": False, "pkg_specs": {"pip": ">10"}},
PY35: {"coverage": False, "pkg_specs": {"pip": ">10"}},
PY36: {"coverage": False, "pkg_specs": {"pip": ">19"}},
PY38: {"coverage": False, "pkg_specs": {"pip": ">19"}},
# IMPORTANT: this should be last so that the folder docs/reports is not deleted afterwards
PY37: {"coverage": True, "pkg_specs": {"pip": ">19"}}, # , "pytest-html": "1.9.0"
}
# set the default activated sessions, minimal for CI
nox.options.sessions = ["tests"] # , "docs", "gh_pages"
nox.options.reuse_existing_virtualenvs = True # this can be done using -r
# if platform.system() == "Windows": >> always use this for better control
nox.options.default_venv_backend = "conda"
# os.environ["NO_COLOR"] = "True" # nox.options.nocolor = True does not work
# nox.options.verbose = True
nox_logger = logging.getLogger("nox")
# nox_logger.setLevel(logging.INFO) NO !!!! this prevents the "verbose" nox flag to work !
class Folders:
root = Path(__file__).parent
runlogs = root / Path(nox.options.envdir or ".nox") / "_runlogs"
runlogs.mkdir(parents=True, exist_ok=True)
dist = root / "dist"
site = root / "site"
site_reports = site / "reports"
reports_root = root / "docs" / "reports"
test_reports = reports_root / "junit"
coverage_reports = reports_root / "coverage"
coverage_xml = coverage_reports / "coverage.xml"
@power_session(envs=ENVS, logsdir=Folders.runlogs)
def tests(session: PowerSession, coverage, pkg_specs):
"""Run the test suite, including test reports generation and coverage reports. """
# As soon as this runs, we delete the target site and coverage files to avoid reporting wrong coverage/etc.
rm_folder(Folders.site)
rm_folder(Folders.reports_root)
# delete the .coverage files if any (they are not supposed to be any, but just in case)
rm_file(Folders.root / ".coverage")
rm_file(Folders.root / "coverage.xml")
# CI-only dependencies
# Did we receive a flag through positional arguments ? (nox -s tests -- <flag>)
install_keyrings_alt = False
if len(session.posargs) == 1:
assert session.posargs[0] == "keyrings.alt"
install_keyrings_alt = True
elif len(session.posargs) > 1:
raise ValueError("Only a single positional argument is accepted, received: %r" % session.posargs)
# uncomment and edit if you wish to uninstall something without deleting the whole env
# session.run2("pip uninstall pytest-asyncio --yes")
# install all requirements
session.install_reqs(setup=True, install=True, tests=True, versions_dct=pkg_specs)
# install CI-only dependencies
if install_keyrings_alt:
session.install2("keyrings.alt")
# list all (conda list alone does not work correctly on github actions)
session.run2("conda list")
conda_prefix = Path(session.bin)
if conda_prefix.name == "bin":
conda_prefix = conda_prefix.parent
session.run2("conda list", env={"CONDA_PREFIX": str(conda_prefix), "CONDA_DEFAULT_ENV": session.get_session_id()})
# install self so that it is recognized by pytest
session.run2("pip install -e . --no-deps")
# check that it can be imported even from a different folder
session.run2(['python', '-c', '"import os; os.chdir(\'./docs/\'); import %s"' % pkg_name])
# finally run all tests
if not coverage:
# simple: pytest only
session.run2("python -m pytest -v %s/tests/" % pkg_name)
else:
# coverage + junit html reports + badge generation
session.install_reqs(phase="coverage", phase_reqs=["coverage", "pytest-html", "requests", "xunitparser"],
versions_dct=pkg_specs)
# --coverage + junit html reports
session.run2("coverage run --source {pkg_name} "
"-m pytest --junitxml={dst}/junit.xml --html={dst}/report.html -v {pkg_name}/tests/"
"".format(pkg_name=pkg_name, dst=Folders.test_reports))
# session.run2("coverage report") # this shows in terminal + fails under XX%, same as --cov-report term --cov-fail-under=70 # noqa
session.run2("coverage xml -o {covxml}".format(covxml=Folders.coverage_xml))
session.run2("coverage html -d {dst}".format(dst=Folders.coverage_reports))
# delete this intermediate file, it is not needed anymore
rm_file(Folders.root / ".coverage")
# --generates the badge for the test results and fail build if less than x% tests pass
nox_logger.info("Generating badge for tests coverage")
session.run2("python ci_tools/generate-junit-badge.py 100 %s" % Folders.test_reports)
@power_session(python=[PY37])
def docs(session: PowerSession):
"""Generates the doc and serves it on a local http server. Pass '-- build' to build statically instead."""
session.install_reqs(phase="docs", phase_reqs=["mkdocs-material", "mkdocs", "pymdown-extensions", "pygments"])
if session.posargs:
# use posargs instead of "serve"
session.run2("mkdocs -f ./docs/mkdocs.yml %s" % " ".join(session.posargs))
else:
session.run2("mkdocs serve -f ./docs/mkdocs.yml")
@power_session(python=[PY37])
def publish(session: PowerSession):
"""Deploy the docs+reports on github pages. Note: this rebuilds the docs"""
session.install_reqs(phase="mkdocs", phase_reqs=["mkdocs-material", "mkdocs", "pymdown-extensions", "pygments"])
# possibly rebuild the docs in a static way (mkdocs serve does not build locally)
session.run2("mkdocs build -f ./docs/mkdocs.yml")
# check that the doc has been generated with coverage
if not Folders.site_reports.exists():
raise ValueError("Test reports have not been built yet. Please run 'nox -s tests-3.7' first")
# publish the docs
session.run2("mkdocs gh-deploy -f ./docs/mkdocs.yml")
# publish the coverage - now in github actions only
# session.install_reqs(phase="codecov", phase_reqs=["codecov", "keyring"])
# # keyring set https://app.codecov.io/gh/<org>/<repo> token
# import keyring # (note that this import is not from the session env but the main nox env)
# codecov_token = keyring.get_password("https://app.codecov.io/gh/<org>/<repo>>", "token")
# # note: do not use --root nor -f ! otherwise "There was an error processing coverage reports"
# session.run2('codecov -t %s -f %s' % (codecov_token, Folders.coverage_xml))
@power_session(python=[PY37])
def release(session: PowerSession):
"""Create a release on github corresponding to the latest tag"""
# Get current tag using setuptools_scm and make sure this is not a dirty/dev one
from setuptools_scm import get_version # (note that this import is not from the session env but the main nox env)
from setuptools_scm.version import guess_next_dev_version
version = []
def my_scheme(version_):
version.append(version_)
return guess_next_dev_version(version_)
current_tag = get_version(".", version_scheme=my_scheme)
# create the package
session.install_reqs(phase="setup.py#dist", phase_reqs=["setuptools_scm"])
rm_folder(Folders.dist)
session.run2("python setup.py sdist bdist_wheel")
if version[0].dirty or not version[0].exact:
raise ValueError("You need to execute this action on a clean tag version with no local changes.")
# Did we receive a token through positional arguments ? (nox -s release -- <token>)
if len(session.posargs) == 1:
# Run from within github actions - no need to publish on pypi
gh_token = session.posargs[0]
publish_on_pypi = False
elif len(session.posargs) == 0:
# Run from local commandline - assume we want to manually publish on PyPi
publish_on_pypi = True
# keyring set https://docs.github.com/en/rest token
import keyring # (note that this import is not from the session env but the main nox env)
gh_token = keyring.get_password("https://docs.github.com/en/rest", "token")
assert len(gh_token) > 0
else:
raise ValueError("Only a single positional arg is allowed for now")
# publish the package on PyPi
if publish_on_pypi:
# keyring set https://upload.pypi.org/legacy/ your-username
# keyring set https://test.pypi.org/legacy/ your-username
session.install_reqs(phase="PyPi", phase_reqs=["twine"])
session.run2("twine upload dist/* -u smarie") # -r testpypi
# create the github release
session.install_reqs(phase="release", phase_reqs=["click", "PyGithub"])
session.run2("python ci_tools/github_release.py -s {gh_token} "
"--repo-slug {gh_org}/{gh_repo} -cf ./docs/changelog.md "
"-d https://{gh_org}.github.io/{gh_repo}/changelog/ {tag}"
"".format(gh_token=gh_token, gh_org=gh_org, gh_repo=gh_repo, tag=current_tag))
# if __name__ == '__main__':
# # allow this file to be executable for easy debugging in any IDE
# nox.run(globals())