2019-09-27 19:13:32 +02:00
|
|
|
"""Automation using nox.
|
2019-07-30 13:26:44 +02:00
|
|
|
"""
|
|
|
|
|
2019-09-28 20:12:49 +02:00
|
|
|
# The following comment should be removed at some point in the future.
|
|
|
|
# mypy: disallow-untyped-defs=False
|
|
|
|
|
2019-11-02 20:52:53 +01:00
|
|
|
import glob
|
2019-09-27 19:11:49 +02:00
|
|
|
import os
|
|
|
|
import shutil
|
2019-11-02 20:06:57 +01:00
|
|
|
import sys
|
2019-09-05 09:15:28 +02:00
|
|
|
|
2019-07-30 13:26:44 +02:00
|
|
|
import nox
|
|
|
|
|
2019-11-02 20:06:57 +01:00
|
|
|
sys.path.append(".")
|
|
|
|
from tools.automation import release # isort:skip # noqa
|
|
|
|
sys.path.pop()
|
|
|
|
|
2019-09-27 19:11:49 +02:00
|
|
|
nox.options.reuse_existing_virtualenvs = True
|
|
|
|
nox.options.sessions = ["lint"]
|
|
|
|
|
|
|
|
LOCATIONS = {
|
|
|
|
"common-wheels": "tests/data/common_wheels",
|
|
|
|
"protected-pip": "tools/tox_pip.py",
|
|
|
|
}
|
|
|
|
REQUIREMENTS = {
|
2020-02-11 14:40:27 +01:00
|
|
|
"docs": "tools/requirements/docs.txt",
|
2019-09-27 19:11:49 +02:00
|
|
|
"tests": "tools/requirements/tests.txt",
|
|
|
|
"common-wheels": "tools/requirements/tests-common_wheels.txt",
|
|
|
|
}
|
|
|
|
|
2019-10-13 23:32:00 +02:00
|
|
|
AUTHORS_FILE = "AUTHORS.txt"
|
|
|
|
VERSION_FILE = "src/pip/__init__.py"
|
|
|
|
|
2019-07-30 13:26:44 +02:00
|
|
|
|
2019-10-07 12:11:42 +02:00
|
|
|
def run_with_protected_pip(session, *arguments):
|
|
|
|
"""Do a session.run("pip", *arguments), using a "protected" pip.
|
2019-09-28 07:43:37 +02:00
|
|
|
|
|
|
|
This invokes a wrapper script, that forwards calls to original virtualenv
|
|
|
|
(stable) version, and not the code being tested. This ensures pip being
|
|
|
|
used is not the code being tested.
|
2019-09-27 19:11:49 +02:00
|
|
|
"""
|
2019-10-07 12:11:42 +02:00
|
|
|
env = {"VIRTUAL_ENV": session.virtualenv.location}
|
|
|
|
|
|
|
|
command = ("python", LOCATIONS["protected-pip"]) + arguments
|
|
|
|
kwargs = {"env": env, "silent": True}
|
|
|
|
session.run(*command, **kwargs)
|
2019-09-27 19:11:49 +02:00
|
|
|
|
|
|
|
|
|
|
|
def should_update_common_wheels():
|
|
|
|
# If the cache hasn't been created, create it.
|
|
|
|
if not os.path.exists(LOCATIONS["common-wheels"]):
|
|
|
|
return True
|
|
|
|
|
|
|
|
# If the requirements was updated after cache, we'll repopulate it.
|
|
|
|
cache_last_populated_at = os.path.getmtime(LOCATIONS["common-wheels"])
|
|
|
|
requirements_updated_at = os.path.getmtime(REQUIREMENTS["common-wheels"])
|
|
|
|
need_to_repopulate = requirements_updated_at > cache_last_populated_at
|
|
|
|
|
|
|
|
# Clear the stale cache.
|
|
|
|
if need_to_repopulate:
|
2019-10-14 09:00:43 +02:00
|
|
|
shutil.rmtree(LOCATIONS["common-wheels"], ignore_errors=True)
|
2019-09-27 19:11:49 +02:00
|
|
|
|
|
|
|
return need_to_repopulate
|
|
|
|
|
|
|
|
|
|
|
|
# -----------------------------------------------------------------------------
|
|
|
|
# Development Commands
|
2019-09-28 07:45:13 +02:00
|
|
|
# These are currently prototypes to evaluate whether we want to switch over
|
|
|
|
# completely to nox for all our automation. Contributors should prefer using
|
|
|
|
# `tox -e ...` until this note is removed.
|
2019-09-27 19:11:49 +02:00
|
|
|
# -----------------------------------------------------------------------------
|
2019-11-10 07:18:34 +01:00
|
|
|
@nox.session(python=["2.7", "3.5", "3.6", "3.7", "3.8", "pypy", "pypy3"])
|
2019-09-27 19:11:49 +02:00
|
|
|
def test(session):
|
|
|
|
# Get the common wheels.
|
|
|
|
if should_update_common_wheels():
|
2019-10-07 12:11:42 +02:00
|
|
|
run_with_protected_pip(
|
|
|
|
session,
|
2019-09-27 19:11:49 +02:00
|
|
|
"wheel",
|
|
|
|
"-w", LOCATIONS["common-wheels"],
|
|
|
|
"-r", REQUIREMENTS["common-wheels"],
|
2019-10-07 12:11:42 +02:00
|
|
|
)
|
2019-10-07 12:12:24 +02:00
|
|
|
else:
|
|
|
|
msg = (
|
|
|
|
"Re-using existing common-wheels at {}."
|
|
|
|
.format(LOCATIONS["common-wheels"])
|
|
|
|
)
|
|
|
|
session.log(msg)
|
2019-10-07 12:11:42 +02:00
|
|
|
|
2019-10-07 12:13:00 +02:00
|
|
|
# Build source distribution
|
|
|
|
sdist_dir = os.path.join(session.virtualenv.location, "sdist")
|
2019-11-10 07:18:13 +01:00
|
|
|
if os.path.exists(sdist_dir):
|
|
|
|
shutil.rmtree(sdist_dir, ignore_errors=True)
|
2019-10-07 12:13:00 +02:00
|
|
|
session.run(
|
|
|
|
"python", "setup.py", "sdist",
|
|
|
|
"--formats=zip", "--dist-dir", sdist_dir,
|
|
|
|
silent=True,
|
|
|
|
)
|
|
|
|
generated_files = os.listdir(sdist_dir)
|
|
|
|
assert len(generated_files) == 1
|
|
|
|
generated_sdist = os.path.join(sdist_dir, generated_files[0])
|
|
|
|
|
|
|
|
# Install source distribution
|
|
|
|
run_with_protected_pip(session, "install", generated_sdist)
|
2019-09-27 19:11:49 +02:00
|
|
|
|
2019-10-07 12:11:42 +02:00
|
|
|
# Install test dependencies
|
|
|
|
run_with_protected_pip(session, "install", "-r", REQUIREMENTS["tests"])
|
2019-09-27 19:11:49 +02:00
|
|
|
|
2019-09-27 19:55:32 +02:00
|
|
|
# Parallelize tests as much as possible, by default.
|
|
|
|
arguments = session.posargs or ["-n", "auto"]
|
|
|
|
|
2019-09-27 19:11:49 +02:00
|
|
|
# Run the tests
|
2019-09-28 07:43:37 +02:00
|
|
|
# LC_CTYPE is set to get UTF-8 output inside of the subprocesses that our
|
|
|
|
# tests use.
|
2019-09-27 19:55:32 +02:00
|
|
|
session.run("pytest", *arguments, env={"LC_CTYPE": "en_US.UTF-8"})
|
2019-09-27 19:11:49 +02:00
|
|
|
|
|
|
|
|
|
|
|
@nox.session
|
|
|
|
def docs(session):
|
2020-03-13 19:59:04 +01:00
|
|
|
session.install("-e", ".")
|
2019-09-27 19:11:49 +02:00
|
|
|
session.install("-r", REQUIREMENTS["docs"])
|
|
|
|
|
|
|
|
def get_sphinx_build_command(kind):
|
2019-09-28 07:43:37 +02:00
|
|
|
# Having the conf.py in the docs/html is weird but needed because we
|
|
|
|
# can not use a different configuration directory vs source directory
|
|
|
|
# on RTD currently. So, we'll pass "-c docs/html" here.
|
|
|
|
# See https://github.com/rtfd/readthedocs.org/issues/1543.
|
2019-09-27 19:11:49 +02:00
|
|
|
return [
|
|
|
|
"sphinx-build",
|
|
|
|
"-W",
|
2019-09-28 07:43:37 +02:00
|
|
|
"-c", "docs/html", # see note above
|
2019-09-27 19:11:49 +02:00
|
|
|
"-d", "docs/build/doctrees/" + kind,
|
|
|
|
"-b", kind,
|
|
|
|
"docs/" + kind,
|
|
|
|
"docs/build/" + kind,
|
|
|
|
]
|
|
|
|
|
|
|
|
session.run(*get_sphinx_build_command("html"))
|
|
|
|
session.run(*get_sphinx_build_command("man"))
|
|
|
|
|
|
|
|
|
|
|
|
@nox.session
|
|
|
|
def lint(session):
|
|
|
|
session.install("pre-commit")
|
|
|
|
|
|
|
|
if session.posargs:
|
|
|
|
args = session.posargs + ["--all-files"]
|
|
|
|
else:
|
|
|
|
args = ["--all-files", "--show-diff-on-failure"]
|
|
|
|
|
|
|
|
session.run("pre-commit", "run", *args)
|
|
|
|
|
|
|
|
|
2020-04-13 13:32:28 +02:00
|
|
|
@nox.session
|
|
|
|
def vendoring(session):
|
|
|
|
session.install("vendoring")
|
|
|
|
|
|
|
|
session.run("vendoring", "sync", ".", "-v")
|
|
|
|
|
|
|
|
|
2019-07-30 13:26:44 +02:00
|
|
|
# -----------------------------------------------------------------------------
|
2019-09-27 19:01:12 +02:00
|
|
|
# Release Commands
|
2019-07-30 13:26:44 +02:00
|
|
|
# -----------------------------------------------------------------------------
|
2019-11-02 20:06:57 +01:00
|
|
|
@nox.session(name="prepare-release")
|
|
|
|
def prepare_release(session):
|
2020-04-09 21:34:39 +02:00
|
|
|
version = release.get_version_from_arguments(session)
|
2019-11-02 20:06:57 +01:00
|
|
|
if not version:
|
2020-04-09 21:34:39 +02:00
|
|
|
session.error("Usage: nox -s prepare-release -- <version>")
|
2019-11-02 20:06:57 +01:00
|
|
|
|
|
|
|
session.log("# Ensure nothing is staged")
|
|
|
|
if release.modified_files_in_git("--staged"):
|
|
|
|
session.error("There are files staged in git")
|
|
|
|
|
|
|
|
session.log(f"# Updating {AUTHORS_FILE}")
|
|
|
|
release.generate_authors(AUTHORS_FILE)
|
|
|
|
if release.modified_files_in_git():
|
|
|
|
release.commit_file(
|
|
|
|
session, AUTHORS_FILE, message=f"Update {AUTHORS_FILE}",
|
2019-10-13 23:32:00 +02:00
|
|
|
)
|
|
|
|
else:
|
2019-11-02 20:06:57 +01:00
|
|
|
session.log(f"# No changes to {AUTHORS_FILE}")
|
2019-10-13 23:32:00 +02:00
|
|
|
|
2019-11-02 20:06:57 +01:00
|
|
|
session.log("# Generating NEWS")
|
|
|
|
release.generate_news(session, version)
|
2019-10-13 23:32:00 +02:00
|
|
|
|
2019-11-02 20:06:57 +01:00
|
|
|
session.log(f"# Bumping for release {version}")
|
|
|
|
release.update_version_file(version, VERSION_FILE)
|
|
|
|
release.commit_file(session, VERSION_FILE, message="Bump for release")
|
2019-10-13 23:32:00 +02:00
|
|
|
|
2019-11-02 20:06:57 +01:00
|
|
|
session.log("# Tagging release")
|
|
|
|
release.create_git_tag(session, version, message=f"Release {version}")
|
2019-10-13 23:32:00 +02:00
|
|
|
|
2019-11-02 20:06:57 +01:00
|
|
|
session.log("# Bumping for development")
|
|
|
|
next_dev_version = release.get_next_development_version(version)
|
|
|
|
release.update_version_file(next_dev_version, VERSION_FILE)
|
|
|
|
release.commit_file(session, VERSION_FILE, message="Bump for development")
|
2019-11-02 20:52:53 +01:00
|
|
|
|
|
|
|
|
|
|
|
@nox.session(name="build-release")
|
|
|
|
def build_release(session):
|
2020-04-09 21:34:39 +02:00
|
|
|
version = release.get_version_from_arguments(session)
|
2019-11-02 20:52:53 +01:00
|
|
|
if not version:
|
2019-11-10 07:43:08 +01:00
|
|
|
session.error("Usage: nox -s build-release -- YY.N[.P]")
|
2019-11-02 20:52:53 +01:00
|
|
|
|
|
|
|
session.log("# Ensure no files in dist/")
|
|
|
|
if release.have_files_in_folder("dist"):
|
2020-01-26 03:00:40 +01:00
|
|
|
session.error(
|
|
|
|
"There are files in dist/. Remove them and try again. "
|
|
|
|
"You can use `git clean -fxdi -- dist` command to do this"
|
|
|
|
)
|
2019-11-02 20:52:53 +01:00
|
|
|
|
|
|
|
session.log("# Install dependencies")
|
|
|
|
session.install("setuptools", "wheel", "twine")
|
|
|
|
|
2020-01-28 17:30:11 +01:00
|
|
|
with release.isolated_temporary_checkout(session, version) as build_dir:
|
2020-01-28 16:45:42 +01:00
|
|
|
session.log(
|
|
|
|
"# Start the build in an isolated, "
|
2020-01-28 17:30:11 +01:00
|
|
|
f"temporary Git checkout at {build_dir!s}",
|
2020-01-28 16:45:42 +01:00
|
|
|
)
|
2020-01-28 17:30:11 +01:00
|
|
|
with release.workdir(session, build_dir):
|
2020-02-03 11:12:47 +01:00
|
|
|
tmp_dists = build_dists(session)
|
2019-11-02 20:52:53 +01:00
|
|
|
|
2020-02-03 11:12:47 +01:00
|
|
|
tmp_dist_paths = (build_dir / p for p in tmp_dists)
|
|
|
|
session.log(f"# Copying dists from {build_dir}")
|
2020-04-21 04:19:09 +02:00
|
|
|
os.makedirs('dist', exist_ok=True)
|
|
|
|
for dist, final in zip(tmp_dist_paths, tmp_dists):
|
|
|
|
session.log(f"# Copying {dist} to {final}")
|
|
|
|
shutil.copy(dist, final)
|
2020-01-26 03:06:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
def build_dists(session):
|
2020-02-03 11:12:47 +01:00
|
|
|
"""Return dists with valid metadata."""
|
2020-01-25 13:40:45 +01:00
|
|
|
session.log(
|
|
|
|
"# Check if there's any Git-untracked files before building the wheel",
|
|
|
|
)
|
2020-01-28 17:21:02 +01:00
|
|
|
|
|
|
|
has_forbidden_git_untracked_files = any(
|
|
|
|
# Don't report the environment this session is running in
|
|
|
|
not untracked_file.startswith('.nox/build-release/')
|
2020-02-03 11:22:02 +01:00
|
|
|
for untracked_file in release.get_git_untracked_files()
|
2020-01-25 13:40:45 +01:00
|
|
|
)
|
2020-01-28 17:21:02 +01:00
|
|
|
if has_forbidden_git_untracked_files:
|
2020-01-25 13:40:45 +01:00
|
|
|
session.error(
|
2020-01-28 17:07:27 +01:00
|
|
|
"There are untracked files in the working directory. "
|
2020-01-25 13:40:45 +01:00
|
|
|
"Remove them and try again",
|
|
|
|
)
|
|
|
|
|
2019-11-02 20:52:53 +01:00
|
|
|
session.log("# Build distributions")
|
|
|
|
session.run("python", "setup.py", "sdist", "bdist_wheel", silent=True)
|
2020-02-03 11:12:47 +01:00
|
|
|
produced_dists = glob.glob("dist/*")
|
2019-11-02 20:52:53 +01:00
|
|
|
|
2020-02-03 11:12:47 +01:00
|
|
|
session.log(f"# Verify distributions: {', '.join(produced_dists)}")
|
|
|
|
session.run("twine", "check", *produced_dists, silent=True)
|
|
|
|
|
|
|
|
return produced_dists
|
2019-11-02 20:52:53 +01:00
|
|
|
|
2019-11-10 07:46:02 +01:00
|
|
|
|
|
|
|
@nox.session(name="upload-release")
|
|
|
|
def upload_release(session):
|
2020-04-09 21:34:39 +02:00
|
|
|
version = release.get_version_from_arguments(session)
|
2019-11-10 07:46:02 +01:00
|
|
|
if not version:
|
|
|
|
session.error("Usage: nox -s upload-release -- YY.N[.P]")
|
|
|
|
|
|
|
|
session.log("# Install dependencies")
|
|
|
|
session.install("twine")
|
|
|
|
|
|
|
|
distribution_files = glob.glob("dist/*")
|
|
|
|
session.log(f"# Distribution files: {distribution_files}")
|
|
|
|
|
|
|
|
# Sanity check: Make sure there's 2 distribution files.
|
|
|
|
count = len(distribution_files)
|
|
|
|
if count != 2:
|
|
|
|
session.error(
|
|
|
|
f"Expected 2 distribution files for upload, got {count}. "
|
|
|
|
f"Remove dist/ and run 'nox -s build-release -- {version}'"
|
|
|
|
)
|
|
|
|
# Sanity check: Make sure the files are correctly named.
|
2020-05-21 16:02:28 +02:00
|
|
|
distfile_names = map(os.path.basename, distribution_files)
|
2019-11-10 07:46:02 +01:00
|
|
|
expected_distribution_files = [
|
2020-05-21 12:32:47 +02:00
|
|
|
f"pip-{version}-py2.py3-none-any.whl",
|
|
|
|
f"pip-{version}.tar.gz",
|
2019-11-10 07:46:02 +01:00
|
|
|
]
|
2020-05-21 12:32:47 +02:00
|
|
|
if sorted(distfile_names) != sorted(expected_distribution_files):
|
2019-11-10 07:46:02 +01:00
|
|
|
session.error(
|
|
|
|
f"Distribution files do not seem to be for {version} release."
|
|
|
|
)
|
|
|
|
|
|
|
|
session.log("# Upload distributions")
|
|
|
|
session.run("twine", "upload", *distribution_files)
|