Skip to content

Instantly share code, notes, and snippets.

@mawillcockson
Created August 29, 2022 01:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mawillcockson/6edc106b0357b727c96482b66eda89d2 to your computer and use it in GitHub Desktop.
Save mawillcockson/6edc106b0357b727c96482b66eda89d2 to your computer and use it in GitHub Desktop.
git bisect script for PyCQA/isort#1949
"""
intended to be used with git bisect:
git bisect start "5.10.1" "5.0.0"
git bisect run python ../isort_check.py
"""
import subprocess
import sys
from pathlib import Path
from tempfile import TemporaryDirectory
GOOD_VERSION = 0
BAD_VERSION = 1
ABORT_BISECT = 128
PROBLEM_CONTENT = """import sys
if True:
# comment
import sys
"""
OK_CONTENT = """import sys
if True:
# comment
import sys
"""
FAILURE_MESSAGE_PORTIONS = [
"Broken 1 paths",
"Imports are incorrectly sorted and/or formatted",
]
def msg(message: str) -> None:
"print a message"
print(f"----------{message}----------")
def main(tmpdir_name: str) -> None:
"check the current isort source"
msg(tmpdir_name)
tmpdir = Path(tmpdir_name).resolve(strict=True)
venv = tmpdir / "isort-venv"
venv.mkdir()
problem_file = tmpdir / "problem.py"
problem_file.write_text(PROBLEM_CONTENT)
ok_file = tmpdir / "ok.py"
ok_file.write_text(OK_CONTENT)
result = subprocess.run(
[sys.executable, "-m", "venv", "--upgrade-deps", str(venv)], check=False
)
if result.returncode != 0:
msg("problem creating venv")
raise SystemExit(ABORT_BISECT)
venv_python = (
venv / "Scripts" / "python.exe"
if sys.platform == "win32"
else venv / "bin" / "python"
)
if not venv_python.is_file():
msg("cannot find isort-venv python")
raise SystemExit(ABORT_BISECT)
result = subprocess.run(
[str(venv_python), "-m", "pip", "install", str(Path().cwd()), "toml"],
check=False,
)
if result.returncode != 0:
msg("problem install isort from source")
raise SystemExit(ABORT_BISECT)
problem_result = subprocess.run(
[str(venv_python), "-m", "isort", "--check", str(problem_file)],
check=False,
capture_output=True,
text=True,
)
if problem_result.stdout:
print(problem_result.stdout)
if problem_result.stderr:
print(problem_result.stderr)
ok_result = subprocess.run(
[str(venv_python), "-m", "isort", "--check", str(ok_file)],
check=False,
capture_output=True,
text=True,
)
if ok_result.stdout:
print(ok_result.stdout)
if ok_result.stderr:
print(ok_result.stderr)
if problem_result.returncode == 0 and ok_result.returncode == 0:
msg("good version")
raise SystemExit(GOOD_VERSION)
if ok_result.returncode == 0 and any(
m in problem_result.stdout or m in problem_result.stderr
for m in FAILURE_MESSAGE_PORTIONS
):
msg("bad version")
raise SystemExit(BAD_VERSION)
msg("problem with isort")
raise SystemExit(ABORT_BISECT)
if __name__ == "__main__":
with TemporaryDirectory(prefix="git-bisect-") as name:
main(tmpdir_name=name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment