diff --git a/scanpipe/management/commands/__init__.py b/scanpipe/management/commands/__init__.py index f883877bd..4ff63d16e 100644 --- a/scanpipe/management/commands/__init__.py +++ b/scanpipe/management/commands/__init__.py @@ -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) @@ -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): diff --git a/scanpipe/management/commands/add-pipeline.py b/scanpipe/management/commands/add-pipeline.py index 908a194f4..eab782e63 100644 --- a/scanpipe/management/commands/add-pipeline.py +++ b/scanpipe/management/commands/add-pipeline.py @@ -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) diff --git a/scanpipe/management/commands/archive-project.py b/scanpipe/management/commands/archive-project.py index c723d5375..9240858b9 100644 --- a/scanpipe/management/commands/archive-project.py +++ b/scanpipe/management/commands/archive-project.py @@ -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) diff --git a/scanpipe/management/commands/create-project.py b/scanpipe/management/commands/create-project.py index 3482e8e29..5d9846954 100644 --- a/scanpipe/management/commands/create-project.py +++ b/scanpipe/management/commands/create-project.py @@ -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) diff --git a/scanpipe/management/commands/create-user.py b/scanpipe/management/commands/create-user.py index 95f6d8fc7..8e55bb860 100644 --- a/scanpipe/management/commands/create-user.py +++ b/scanpipe/management/commands/create-user.py @@ -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): """ diff --git a/scanpipe/management/commands/delete-project.py b/scanpipe/management/commands/delete-project.py index eacb0fc91..e3ff67b1d 100644 --- a/scanpipe/management/commands/delete-project.py +++ b/scanpipe/management/commands/delete-project.py @@ -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) diff --git a/scanpipe/management/commands/execute.py b/scanpipe/management/commands/execute.py index aa077fcf6..b2f441044 100644 --- a/scanpipe/management/commands/execute.py +++ b/scanpipe/management/commands/execute.py @@ -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...") @@ -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) diff --git a/scanpipe/management/commands/graph.py b/scanpipe/management/commands/graph.py index 379f08597..318765b71 100644 --- a/scanpipe/management/commands/graph.py +++ b/scanpipe/management/commands/graph.py @@ -87,7 +87,7 @@ 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) @@ -95,17 +95,13 @@ def handle(self, *pipeline_names, **options): 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( @@ -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): diff --git a/scanpipe/management/commands/output.py b/scanpipe/management/commands/output.py index 3f20c0b7b..5f84a698d 100644 --- a/scanpipe/management/commands/output.py +++ b/scanpipe/management/commands/output.py @@ -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) diff --git a/scanpipe/management/commands/reset-project.py b/scanpipe/management/commands/reset-project.py index 2f37b62c3..77c305b70 100644 --- a/scanpipe/management/commands/reset-project.py +++ b/scanpipe/management/commands/reset-project.py @@ -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) diff --git a/scanpipe/tests/test_commands.py b/scanpipe/tests/test_commands.py index 46e9c47f6..fc53ba224 100644 --- a/scanpipe/tests/test_commands.py +++ b/scanpipe/tests/test_commands.py @@ -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) @@ -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)