Skip to content

Instantly share code, notes, and snippets.

@and-semakin
Last active August 8, 2019 14:43
Show Gist options
  • Save and-semakin/06d0323bb35f793f497439ec899a9ba9 to your computer and use it in GitHub Desktop.
Save and-semakin/06d0323bb35f793f497439ec899a9ba9 to your computer and use it in GitHub Desktop.
pre-commit hook to check source formatting with black & isort
#!/usr/bin/env python3
import subprocess
import sys
def compose_up(service):
subprocess.run(["docker-compose", "up", "-d", service])
def compose_exec(service, command) -> bool:
code, output = subprocess.getstatusoutput(
f"docker-compose exec -T {service} {command}"
)
print(output)
return code == 0
def check_ais() -> bool:
compose_up("ais2")
print("=== black ais/ ===")
black = compose_exec("ais2", "black --check --target-version py36 ais/")
print("=== isort ais/ ===")
isort = compose_exec("ais2", "isort --check-only --recursive ais/")
return black and isort
def check_selenium() -> bool:
compose_up("selenium-magic")
print("=== black selenium_magic/ ===")
black = compose_exec("selenium-magic", "black --check --target-version py37 selenium_magic/")
print("=== isort selenium_magic/ ===")
black = compose_exec("selenium-magic", "isort --check-only --recursive selenium_magic/")
return black and isort
def main():
changed_files = subprocess.check_output([
"git", "diff", "--cached", "--name-only", "--diff-filter=ACMR"
]).decode().strip().split('\n')
print("Changed files:", changed_files)
checks = []
if any(filename.startswith("ais/ais/") for filename in changed_files):
checks.append(check_ais())
if any(filename.startswith("selenium_magic/") for filename in changed_files):
checks.append(check_selenium())
exit_code = 0 if all(checks) else 1
print(f"=== Exiting with code {exit_code}. ===")
sys.exit(exit_code)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment