From 30684f44402b4a14faab7361ec3377178572163e Mon Sep 17 00:00:00 2001 From: Piero Toffanin Date: Mon, 5 May 2025 03:06:25 -0400 Subject: [PATCH] Link task_output.txt <==> console_output.txt --- app/classes/console.py | 17 +++++++++++- app/migrations/0044_task_console_link.py | 35 ++++++++++++++++++++++++ app/models/task.py | 8 ++++-- 3 files changed, 56 insertions(+), 4 deletions(-) create mode 100644 app/migrations/0044_task_console_link.py diff --git a/app/classes/console.py b/app/classes/console.py index 3da805c5..b2709a38 100644 --- a/app/classes/console.py +++ b/app/classes/console.py @@ -46,6 +46,9 @@ class Console: try: if not os.path.isdir(self.base_dir): os.makedirs(self.base_dir, exist_ok=True) + + if os.path.isfile(self.file): + os.unlink(self.file) with open(self.file, "w", encoding="utf-8") as f: f.write(text) @@ -61,4 +64,16 @@ class Console: with open(self.file, "w", encoding="utf-8") as f: f.write(text) except OSError: - logger.warn("Cannot delink console file: %s" % self.file) \ No newline at end of file + logger.warn("Cannot delink console file: %s" % self.file) + + def link(self, src_file): + try: + if not os.path.isfile(src_file): + raise OSError("Source file does not exist") + + if os.path.isfile(self.file): + os.unlink(self.file) + + os.link(src_file, self.file) + except OSError: + logger.warn("Cannot link console file: %s --> %s" % (src_file, self.file)) \ No newline at end of file diff --git a/app/migrations/0044_task_console_link.py b/app/migrations/0044_task_console_link.py new file mode 100644 index 00000000..a2f5c7dc --- /dev/null +++ b/app/migrations/0044_task_console_link.py @@ -0,0 +1,35 @@ +# Generated by Django 2.2.27 on 2025-05-02 19:45 +from django.db import migrations +import os +from webodm import settings + + +def update_task_console_link(apps, schema_editor): + Task = apps.get_model('app', 'Task') + + for t in Task.objects.all(): + task_output = os.path.join(settings.MEDIA_ROOT, "project", str(t.project.id), "task", str(t.id), "assets", "task_output.txt") + console_output = os.path.join(settings.MEDIA_ROOT, "project", str(t.project.id), "task", str(t.id), "data", "console_output.txt") + + if os.path.isfile(task_output) and os.path.isfile(console_output): + print("Update {}".format(t)) + # Guarantee consistency, save space + try: + if os.path.isfile(console_output): + os.unlink(console_output) + + os.link(task_output, console_output) + except OSError: + print("Cannot link console file: %s --> %s" % (task_output, console_output)) + + +class Migration(migrations.Migration): + + dependencies = [ + ('app', '0043_task_crop'), + ] + + operations = [ + migrations.RunPython(update_task_console_link), + ] + diff --git a/app/models/task.py b/app/models/task.py index 9a712e0d..5f2e2a7b 100644 --- a/app/models/task.py +++ b/app/models/task.py @@ -480,9 +480,6 @@ class Task(models.Model): try: # Try to use hard links first shutil.copytree(self.task_path(), task.task_path(), copy_function=os.link) - - # Make sure the console output is not linked to the original task - task.console.delink() except Exception as e: logger.warning("Cannot duplicate task using hard links, will use normal copy instead: {}".format(str(e))) shutil.copytree(self.task_path(), task.task_path()) @@ -1048,6 +1045,11 @@ class Task(models.Model): self.import_url = "" else: self.console += gettext("Done!") + "\n" + + task_output = self.assets_path("task_output.txt") + if os.path.isfile(task_output): + # Guarantee consistency, save space + self.console.link(task_output) self.save()