Skip to content

Commit c669021

Browse files
Suppress python callstacks on lint/ts/scss command failure
1 parent ba791ab commit c669021

File tree

5 files changed

+20
-12
lines changed

5 files changed

+20
-12
lines changed

evap/evaluation/management/commands/precommit.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import os.path
2-
import subprocess # nosec
32
import sys
43

54
from django.core.management import call_command
65
from django.core.management.base import BaseCommand
76

7+
from evap.evaluation.management.commands.tools import run_exit_if_nonzero_return_code
8+
89

910
class Command(BaseCommand):
1011
args = ""
@@ -19,7 +20,7 @@ def handle(self, *args, **options):
1920
call_command("typecheck")
2021

2122
# subprocess call so our sys.argv check in settings.py works
22-
subprocess.run(["./manage.py", "test"], check=False) # nosec
23+
run_exit_if_nonzero_return_code(["./manage.py", "test"])
2324

2425
call_command("format")
2526
call_command("lint")

evap/evaluation/management/commands/scss.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import subprocess # nosec
2-
31
from django.conf import settings
42
from django.core.management.base import BaseCommand, CommandError
53

4+
from evap.evaluation.management.commands.tools import run_exit_if_nonzero_return_code
5+
66

77
class Command(BaseCommand):
88
def add_arguments(self, parser):
@@ -34,7 +34,7 @@ def handle(self, *args, **options):
3434
command += ["--style", "compressed", "--no-source-map"]
3535

3636
try:
37-
subprocess.run(command, check=True) # nosec
37+
run_exit_if_nonzero_return_code(command)
3838
except FileNotFoundError as e:
3939
raise CommandError("Could not find sass command") from e
4040
except KeyboardInterrupt:

evap/evaluation/management/commands/tools.py

+8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logging
2+
import subprocess
23
import sys
34

45
from django.conf import settings
@@ -47,3 +48,10 @@ def logged_call_command(stdout, *args, **kwargs):
4748
"""Log execution of management command with all args."""
4849
stdout.write("Executing python manage.py " + " ".join(list(args) + [f"{a}={b}" for a, b in kwargs.items()]))
4950
call_command(*args, **kwargs)
51+
52+
53+
def run_exit_if_nonzero_return_code(command: list) -> None:
54+
exit_code = subprocess.run(command, check=False).returncode
55+
if exit_code != 0:
56+
sys.exit(exit_code)
57+
# don't sys.exit() on return code 0 so that precommit.py can chain commands

evap/evaluation/management/commands/ts.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import argparse
2-
import subprocess # nosec
32

43
from django.conf import settings
54
from django.core.management import call_command
65
from django.core.management.base import BaseCommand, CommandError
76

7+
from evap.evaluation.management.commands.tools import run_exit_if_nonzero_return_code
8+
89

910
class Command(BaseCommand):
1011
def add_arguments(self, parser: argparse.ArgumentParser):
@@ -36,13 +37,11 @@ def handle(self, *args, **options):
3637

3738
def run_command(self, command):
3839
try:
39-
subprocess.run(command, check=True) # nosec
40+
run_exit_if_nonzero_return_code(command)
4041
except FileNotFoundError as e:
4142
raise CommandError(f"Could not find {command[0]} command") from e
4243
except KeyboardInterrupt:
4344
pass
44-
except subprocess.CalledProcessError as e:
45-
raise CommandError("Error during command execution", returncode=e.returncode) from e
4645

4746
def compile(self, watch=False, fresh=False, **_options):
4847
static_directory = settings.STATICFILES_DIRS[0]
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import subprocess # nosec
2-
31
from django.core.management.base import BaseCommand
42

3+
from evap.evaluation.management.commands.tools import run_exit_if_nonzero_return_code
4+
55

66
class Command(BaseCommand):
77
args = ""
88
help = "Runs the type checker (mypy)"
99
requires_migrations_checks = False
1010

1111
def handle(self, *args, **options):
12-
subprocess.run(["mypy"], check=True) # nosec
12+
run_exit_if_nonzero_return_code(["mypy"])

0 commit comments

Comments
 (0)