views.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #
  2. # BitBake Toaster Implementation
  3. #
  4. # Copyright (C) 2014 Intel Corporation
  5. #
  6. # SPDX-License-Identifier: GPL-2.0-only
  7. #
  8. from django.urls import reverse
  9. from django.http import HttpResponseBadRequest, HttpResponse
  10. import os
  11. import tempfile
  12. import subprocess
  13. import toastermain
  14. from django.views.decorators.csrf import csrf_exempt
  15. @csrf_exempt
  16. def eventfile(request):
  17. """ Receives a file by POST, and runs toaster-eventreply on this file """
  18. if request.method != "POST":
  19. return HttpResponseBadRequest("This API only accepts POST requests. Post a file with:\n\ncurl -F eventlog=@bitbake_eventlog.json %s\n" % request.build_absolute_uri(reverse('eventfile')), content_type="text/plain;utf8")
  20. # write temporary file
  21. (handle, abstemppath) = tempfile.mkstemp(dir="/tmp/")
  22. with os.fdopen(handle, "w") as tmpfile:
  23. for chunk in request.FILES['eventlog'].chunks():
  24. tmpfile.write(chunk)
  25. tmpfile.close()
  26. # compute the path to "bitbake/bin/toaster-eventreplay"
  27. from os.path import dirname as DN
  28. import_script = os.path.join(DN(DN(DN(DN(os.path.abspath(__file__))))), "bin/toaster-eventreplay")
  29. if not os.path.exists(import_script):
  30. raise Exception("script missing %s" % import_script)
  31. scriptenv = os.environ.copy()
  32. scriptenv["DATABASE_URL"] = toastermain.settings.getDATABASE_URL()
  33. # run the data loading process and return the results
  34. importer = subprocess.Popen([import_script, abstemppath], stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=scriptenv)
  35. (out, err) = importer.communicate()
  36. if importer.returncode == 0:
  37. os.remove(abstemppath)
  38. return HttpResponse("== Retval %d\n== STDOUT\n%s\n\n== STDERR\n%s" % (importer.returncode, out, err), content_type="text/plain;utf8")