views.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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.views.decorators.cache import cache_control
  9. from django.core.urlresolvers import reverse
  10. from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
  11. from django.http import HttpResponseBadRequest, HttpResponse
  12. from django.utils import timezone
  13. from django.utils.html import escape
  14. from datetime import timedelta
  15. from django.utils import formats
  16. from toastergui.templatetags.projecttags import json as jsonfilter
  17. import json
  18. import os
  19. import tempfile
  20. import subprocess
  21. import toastermain
  22. from django.views.decorators.csrf import csrf_exempt
  23. @csrf_exempt
  24. def eventfile(request):
  25. """ Receives a file by POST, and runs toaster-eventreply on this file """
  26. if request.method != "POST":
  27. 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")
  28. # write temporary file
  29. (handle, abstemppath) = tempfile.mkstemp(dir="/tmp/")
  30. with os.fdopen(handle, "w") as tmpfile:
  31. for chunk in request.FILES['eventlog'].chunks():
  32. tmpfile.write(chunk)
  33. tmpfile.close()
  34. # compute the path to "bitbake/bin/toaster-eventreplay"
  35. from os.path import dirname as DN
  36. import_script = os.path.join(DN(DN(DN(DN(os.path.abspath(__file__))))), "bin/toaster-eventreplay")
  37. if not os.path.exists(import_script):
  38. raise Exception("script missing %s" % import_script)
  39. scriptenv = os.environ.copy()
  40. scriptenv["DATABASE_URL"] = toastermain.settings.getDATABASE_URL()
  41. # run the data loading process and return the results
  42. importer = subprocess.Popen([import_script, abstemppath], stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=scriptenv)
  43. (out, err) = importer.communicate()
  44. if importer.returncode == 0:
  45. os.remove(abstemppath)
  46. return HttpResponse("== Retval %d\n== STDOUT\n%s\n\n== STDERR\n%s" % (importer.returncode, out, err), content_type="text/plain;utf8")