publishers_11.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. def run_pub_scripts(pub, upload):
  2. """Spawn subshells in which the Publisher's editing scripts are run, with
  3. the upload's folder and the script's output folder as command line args.
  4. """
  5. task_logger = get_task_logger(__name__)
  6. try:
  7. # TODO: Handle unique file upload instance particulars
  8. task_logger.info("Editing upload for %s" % pub.name)
  9. previous_status = upload.status
  10. upload_path = upload.file_path
  11. upload_dir = os.path.dirname(upload_path)
  12. meta_path = os.path.join(upload_dir, "meta.json")
  13. pub_dir = pub.path
  14. pub_scripts = pub.get_editing_scripts()
  15. for script_path, stage_name in pub_scripts:
  16. # If at some point in the loop, one of the scripts changes the status,
  17. # then we cease updating it automatically.
  18. if upload.status == previous_status:
  19. previous_status = stage_name
  20. upload.status = stage_name
  21. upload.save()
  22. upload_id = str(upload.id)
  23. cmd = [script_path, upload_id, upload_dir, upload_path, meta_path]
  24. logpath = os.path.join(upload_dir, "publisher.log")
  25. # Spawn the test subprocess and wait for it to complete.
  26. with open(logpath, "a") as log_out:
  27. proc = subprocess.Popen(
  28. cmd, stdout=log_out, stderr=subprocess.STDOUT, cwd=pub_dir
  29. )
  30. success = proc.wait() == 0
  31. # success = run_script(pub_dir, script_path, str(upload.id), upload_dir, upload_path, meta_path)
  32. # The script may have updated the upload during execution, so we reload
  33. upload = models.ContentUpload.objects.get(pk=upload.pk)
  34. if success:
  35. task_logger.info(
  36. "Editing upload for %s finished %s" % (pub.name, script_path)
  37. )
  38. else:
  39. task_logger.error(
  40. "Editing for %s died during %s." % (pub.name, script_path)
  41. )
  42. upload.status = "Error: %s" % stage_name
  43. upload.save()
  44. # If either the script itself or we set the status to anything starting
  45. # with "Error" then we abort further processing here.
  46. if upload.status.startswith("Error") or upload.status.startswith("Waiting"):
  47. return
  48. # At this point every script has finished running and we have not returned
  49. # early due to an error, alright!
  50. upload.status = "Successfully Completed"
  51. upload.save()
  52. except Exception:
  53. tb = "\n".join(" " + s for s in traceback.format_exc().split("\n"))
  54. task_logger.error(
  55. "Exception in %s upload %d during %s\n%s"
  56. % (pub.name, upload.id, stage_name, tb)
  57. )
  58. upload.status = "Error: processing failed."
  59. upload.save()