Skip to content

Commit

Permalink
Refine the stdout/stderr syntax on management commands #458
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Druez <tdruez@nexb.com>
  • Loading branch information
tdruez committed Jun 24, 2022
1 parent 0ed467d commit c3b6e84
Show file tree
Hide file tree
Showing 11 changed files with 26 additions and 36 deletions.
10 changes: 5 additions & 5 deletions scanpipe/management/commands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def handle_input_files(self, inputs_files):
self.project.add_input_source(filename, source="uploaded", save=True)

msg = "File(s) copied to the project inputs directory:"
self.stdout.write(self.style.SUCCESS(msg))
self.stdout.write(msg, self.style.SUCCESS)
msg = "\n".join(["- " + filename for filename in copied])
self.stdout.write(msg)

Expand All @@ -176,14 +176,14 @@ def handle_input_urls(self, input_urls):
if downloads:
self.project.add_downloads(downloads)
msg = "File(s) downloaded to the project inputs directory:"
self.stdout.write(self.style.SUCCESS(msg))
self.stdout.write(msg, self.style.SUCCESS)
msg = "\n".join(["- " + downloaded.filename for downloaded in downloads])
self.stdout.write(msg)

if errors:
self.stderr.write(self.style.ERROR("Could not fetch URL(s):"))
msg = "\n".join(["- " + url for url in errors])
self.stderr.write(self.style.ERROR(msg))
msg = "Could not fetch URL(s):\n"
msg += "\n".join(["- " + url for url in errors])
self.stderr.write(msg)


def validate_input_files(file_locations):
Expand Down
2 changes: 1 addition & 1 deletion scanpipe/management/commands/add-pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ def handle(self, *pipeline_names, **options):
self.project.add_pipeline(pipeline_name)

msg = "Pipeline(s) added to the project"
self.stdout.write(self.style.SUCCESS(msg))
self.stdout.write(msg, self.style.SUCCESS)
2 changes: 1 addition & 1 deletion scanpipe/management/commands/archive-project.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,4 @@ def handle(self, *inputs, **options):
raise CommandError(error)

msg = f"The {self.project} project has been archived."
self.stdout.write(self.style.SUCCESS(msg))
self.stdout.write(msg, self.style.SUCCESS)
2 changes: 1 addition & 1 deletion scanpipe/management/commands/create-project.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def handle(self, *args, **options):

project.save()
msg = f"Project {name} created with work directory {project.work_directory}"
self.stdout.write(self.style.SUCCESS(msg))
self.stdout.write(msg, self.style.SUCCESS)

for pipeline_name in pipeline_names:
project.add_pipeline(pipeline_name)
Expand Down
2 changes: 1 addition & 1 deletion scanpipe/management/commands/create-user.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def handle(self, *args, **options):

if options["verbosity"] >= 1:
msg = f"User {username} created with API key: {token.key}"
self.stdout.write(self.style.SUCCESS(msg))
self.stdout.write(msg, self.style.SUCCESS)

def _validate_username(self, username):
"""
Expand Down
2 changes: 1 addition & 1 deletion scanpipe/management/commands/delete-project.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,4 @@ def handle(self, *inputs, **options):
self.project.delete()

msg = f"All the {self.project} project data have been removed."
self.stdout.write(self.style.SUCCESS(msg))
self.stdout.write(msg, self.style.SUCCESS)
16 changes: 6 additions & 10 deletions scanpipe/management/commands/execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def handle(self, *args, **options):

run.execute_task_async()
msg = f"{run.pipeline_name} added to the tasks queue for execution."
self.stdout.write(self.style.SUCCESS(msg))
self.stdout.write(msg, self.style.SUCCESS)
sys.exit(0)

self.stdout.write(f"Start the {run.pipeline_name} pipeline execution...")
Expand All @@ -68,20 +68,16 @@ def handle(self, *args, **options):
tasks.execute_pipeline_task(run.pk)
except KeyboardInterrupt:
run.set_task_stopped()
self.stderr.write(self.style.ERROR("Pipeline execution stopped."))
sys.exit(1)
raise CommandError("Pipeline execution stopped.")
except Exception as e:
run.set_task_ended(exitcode=1, output=str(e))
self.stderr.write(self.style.ERROR(e))
sys.exit(1)
raise CommandError(e)

run.refresh_from_db()

if run.task_succeeded:
msg = f"{run.pipeline_name} successfully executed on project {self.project}"
self.stdout.write(self.style.SUCCESS(msg))
self.stdout.write(msg, self.style.SUCCESS)
else:
msg = f"Error during {run.pipeline_name} execution:\n"
self.stderr.write(self.style.ERROR(msg))
self.stderr.write(run.task_output)
sys.exit(1)
msg = f"Error during {run.pipeline_name} execution:\n{run.task_output}"
raise CommandError(msg)
12 changes: 4 additions & 8 deletions scanpipe/management/commands/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,25 +87,21 @@ def add_arguments(self, parser):
def handle(self, *pipeline_names, **options):
if options["list"]:
for pipeline_name, pipeline_class in scanpipe_app.pipelines.items():
self.stdout.write("- " + self.style.SUCCESS(pipeline_name))
self.stdout.write("- " + pipeline_name, self.style.SUCCESS)
self.stdout.write(indent(pipeline_class.get_doc(), " "), ending="\n\n")
sys.exit(0)

if not is_graphviz_installed():
raise CommandError("Graphviz is not installed.")

if not pipeline_names:
self.stderr.write(
self.style.ERROR("The pipeline-names argument is required.")
)
sys.exit(1)
raise CommandError("The pipeline-names argument is required.")

outputs = []
for pipeline_name in pipeline_names:
pipeline_class = scanpipe_app.pipelines.get(pipeline_name)
if not pipeline_class:
self.stderr.write(self.style.ERROR(f"{pipeline_name} is not valid."))
sys.exit(1)
raise CommandError(f"{pipeline_name} is not valid.")

output_directory = options.get("output")
outputs.append(
Expand All @@ -114,7 +110,7 @@ def handle(self, *pipeline_names, **options):

separator = "\n - "
msg = f"Graph(s) generated:{separator}" + separator.join(outputs)
self.stdout.write(self.style.SUCCESS(msg))
self.stdout.write(msg, self.style.SUCCESS)

@staticmethod
def generate_graph_png(pipeline_name, pipeline_class, output_directory):
Expand Down
2 changes: 1 addition & 1 deletion scanpipe/management/commands/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@ def handle(self, *args, **options):

if isinstance(output_file, list):
output_file = "\n".join([str(path) for path in output_file])
self.stdout.write(self.style.SUCCESS(str(output_file)))
self.stdout.write(str(output_file), self.style.SUCCESS)
2 changes: 1 addition & 1 deletion scanpipe/management/commands/reset-project.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,4 @@ def handle(self, *inputs, **options):
f"All data, except inputs, for the {self.project} project have been "
f"removed."
)
self.stdout.write(self.style.SUCCESS(msg))
self.stdout.write(msg, self.style.SUCCESS)
10 changes: 4 additions & 6 deletions scanpipe/tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,12 +305,11 @@ def test_scanpipe_management_command_execute(self):

err = StringIO()
run2 = project.add_pipeline(self.pipeline_name)

expected = "Error during docker execution:\nError log"
with mock.patch("scanpipe.tasks.execute_pipeline_task", task_failure):
with self.assertRaisesMessage(SystemExit, "1"):
with self.assertRaisesMessage(CommandError, expected):
call_command("execute", *options, stdout=out, stderr=err)
expected = "Error during docker execution:"
self.assertIn(expected, err.getvalue())
self.assertIn("Error log", err.getvalue())
run2.refresh_from_db()
self.assertTrue(run2.task_failed)
self.assertEqual("Error log", run2.task_output)
Expand All @@ -319,9 +318,8 @@ def test_scanpipe_management_command_execute(self):
err = StringIO()
run3 = project.add_pipeline(self.pipeline_name)
with mock.patch("scanpipe.tasks.execute_pipeline_task", raise_interrupt):
with self.assertRaisesMessage(SystemExit, "1"):
with self.assertRaisesMessage(CommandError, "Pipeline execution stopped."):
call_command("execute", *options, stdout=out, stderr=err)
self.assertIn("Pipeline execution stopped.", err.getvalue())
run3.refresh_from_db()
run3 = Run.objects.get(pk=run3.pk)
self.assertTrue(run3.task_stopped)
Expand Down

0 comments on commit c3b6e84

Please # to comment.