Skip to content

Commit

Permalink
Simplify script to check __init__.py files (#10488)
Browse files Browse the repository at this point in the history
Now that we only have empty `__init__.py`, we can simplify our script to simply check that all files are empty.

This also simplifies the test for `reversion_test.py` to no longer run Pants, which allows us to move the script to its proper home in `build-support/bin`.

[ci skip-rust]
[ci skip-build-wheels]
  • Loading branch information
Eric-Arellano committed Jul 28, 2020
1 parent 6f48083 commit 659780e
Show file tree
Hide file tree
Showing 20 changed files with 100 additions and 416 deletions.
7 changes: 0 additions & 7 deletions .gitattributes

This file was deleted.

12 changes: 12 additions & 0 deletions build-support/bin/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ files(
sources = ['*.sh'],
)

python_tests(name="tests")

python_binary(
name = 'bootstrap_and_deploy_ci_pants_pex',
sources = ['bootstrap_and_deploy_ci_pants_pex.py'],
Expand All @@ -16,6 +18,11 @@ python_binary(
sources = ['check_banned_imports.py'],
)

python_binary(
name = 'check_inits',
sources = ['check_inits.py'],
)

python_binary(
name = 'ci',
sources = ['ci.py'],
Expand Down Expand Up @@ -54,6 +61,11 @@ python_binary(
sources = ['get_rbe_token.py'],
)

python_binary(
name='reversion',
sources=["reversion.py"],
)

python_binary(
name = 'shellcheck',
sources = ['shellcheck.py'],
Expand Down
33 changes: 33 additions & 0 deletions build-support/bin/check_inits.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env python3
# Copyright 2020 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

import itertools
from pathlib import Path

from common import die

DIRS_TO_CHECK = (
"src",
"tests",
"pants-plugins",
"examples",
"build-support/bin",
"build-support/migration-support",
)


def main() -> None:
files = itertools.chain.from_iterable(
[Path().glob(f"{d}/**/__init__.py") for d in DIRS_TO_CHECK]
)
bad_inits = [f for f in files if bool(f.read_text())]
if bad_inits:
die(
"All `__init__.py` file should be empty, but the following had content: "
f"{', '.join(str(f) for f in bad_inits)}"
)


if __name__ == "__main__":
main()
30 changes: 0 additions & 30 deletions build-support/bin/check_packages.sh

This file was deleted.

5 changes: 0 additions & 5 deletions build-support/bin/pre-commit.sh

This file was deleted.

6 changes: 2 additions & 4 deletions build-support/bin/release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -278,10 +278,8 @@ function reversion_whls() {
local output_version=$3

for whl in "${src_dir}"/*.whl; do
run_local_pants -lwarn run src/python/pants/releases:reversion -- \
--glob='pants/VERSION' \
"${whl}" "${dest_dir}" "${output_version}" \
|| die "Could not reversion whl ${whl} to ${output_version}"
run_local_pants run build-support/bin/reversion.py -- --glob='pants/VERSION' "${whl}" \
"${dest_dir}" "${output_version}" || die "Could not reversion whl ${whl} to ${output_version}"
done
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,7 @@ def reversion(args):
print("Wrote whl with version {} to {}.\n".format(args.target_version, dst_whl_file))


def main():
"""Given an input whl file and target version, create a copy of the whl with that version.
This is accomplished via string replacement in files matching a list of globs. Pass the optional
`--glob` argument to add additional globs: ie `--glob='thing-to-match*.txt'`.
"""
def create_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser()
parser.add_argument("whl_file", help="The input whl file.")
parser.add_argument("dest_dir", help="The destination directory for the output whl.")
Expand All @@ -168,7 +163,16 @@ def main():
default=["*.dist-info/*", "*-nspkg.pth"],
help="Globs (fnmatch) to rewrite within the whl: may be specified multiple times.",
)
args = parser.parse_args()
return parser


def main():
"""Given an input whl file and target version, create a copy of the whl with that version.
This is accomplished via string replacement in files matching a list of globs. Pass the optional
`--glob` argument to add additional globs: ie `--glob='thing-to-match*.txt'`.
"""
args = create_parser().parse_args()
reversion(args)


Expand Down
40 changes: 40 additions & 0 deletions build-support/bin/reversion_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Copyright 2017 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

import os
import shutil

import requests
from pex.bin import pex as pex_main
from reversion import create_parser, reversion

from pants.util.contextutil import temporary_dir


def test_reversion() -> None:
with temporary_dir() as dest_dir:
# Download an input whl.
name_template = "virtualenv-{}-py2.py3-none-any.whl"
input_name = name_template.format("15.1.0")
url = (
"https://files.pythonhosted.org/packages/6f/86/"
"3dc328ee7b1a6419ebfac7896d882fba83c48e3561d22ddddf38294d3e83/{}".format(input_name)
)
input_whl_file = os.path.join(dest_dir, input_name)
with open(input_whl_file, "wb") as f:
shutil.copyfileobj(requests.get(url, stream=True).raw, f)

# Rewrite it.
output_version = "9.1.9"
output_name = name_template.format(output_version)
output_whl_file = os.path.join(dest_dir, output_name)

args = create_parser().parse_args([input_whl_file, dest_dir, output_version])
reversion(args)

assert os.path.isfile(output_whl_file) is True

# Confirm that it can be consumed.
output_pex_file = os.path.join(dest_dir, "out.pex")
pex_main.main(["--disable-cache", "-o", output_pex_file, output_whl_file])
assert os.path.isfile(output_pex_file) is True
17 changes: 2 additions & 15 deletions build-support/githooks/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,6 @@ if [[ -n "${GIT_DIR}" && "${GIT_DIR}" != /* ]]; then
export GIT_DIR
fi

DIRS_TO_CHECK=(
src
tests
pants-plugins
examples
build-support/bin
)

# TODO(#7068): Fix all these checks to only act on staged files with
# `git diff --cached --name-only`!

# TODO: test all the scripts in this file in test_git_hooks.py, remove uses of `|| exit 1`, and add an
# integration test!
set -e

MERGE_BASE="$(git_merge_base)"
Expand Down Expand Up @@ -71,8 +58,8 @@ echo "* Checking shell scripts via shellcheck"
echo "* Checking shell scripts via our custom linter"
./build-support/bin/check_shell.sh || exit 1

echo "* Checking packages"
./build-support/bin/check_packages.sh "${DIRS_TO_CHECK[@]}" || exit 1
echo "* Checking __init__.py files"
./build-support/bin/check_inits.py || exit 1

echo "* Checking headers"
./pants validate '**' || exit 1
Expand Down
18 changes: 0 additions & 18 deletions src/python/pants/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,3 @@ resources(
name='version-resource',
sources=['VERSION'],
)

# NB: This is used for the integration test `test_reversion.py`. It includes all transitive
# dependencies to ensure that the chroot can run `src/python/pants/releases:reversion` properly.
files(
name='reversion_file_with_dependencies',
sources=[
'__init__.py',
'releases/BUILD',
'releases/__init__.py',
'releases/reversion.py',
'util/BUILD',
'util/__init__.py',
'util/contextutil.py',
'util/dirutil.py',
'util/strutil.py',
'util/tarutil.py',
],
)
54 changes: 0 additions & 54 deletions src/python/pants/process/xargs.py

This file was deleted.

10 changes: 0 additions & 10 deletions src/python/pants/releases/BUILD

This file was deleted.

Empty file.
6 changes: 0 additions & 6 deletions tests/README.md

This file was deleted.

0 comments on commit 659780e

Please sign in to comment.