Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | # |
| 2 | # BitBake Toaster Implementation |
| 3 | # |
| 4 | # Copyright (C) 2014 Intel Corporation |
| 5 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 6 | # SPDX-License-Identifier: GPL-2.0-only |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 7 | # |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 8 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame^] | 9 | from django.urls import reverse |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 10 | from django.http import HttpResponseBadRequest, HttpResponse |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 11 | import os |
| 12 | import tempfile |
| 13 | import subprocess |
| 14 | import toastermain |
| 15 | from django.views.decorators.csrf import csrf_exempt |
| 16 | |
| 17 | |
| 18 | @csrf_exempt |
| 19 | def eventfile(request): |
| 20 | """ Receives a file by POST, and runs toaster-eventreply on this file """ |
| 21 | if request.method != "POST": |
| 22 | 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") |
| 23 | |
| 24 | # write temporary file |
| 25 | (handle, abstemppath) = tempfile.mkstemp(dir="/tmp/") |
| 26 | with os.fdopen(handle, "w") as tmpfile: |
| 27 | for chunk in request.FILES['eventlog'].chunks(): |
| 28 | tmpfile.write(chunk) |
| 29 | tmpfile.close() |
| 30 | |
| 31 | # compute the path to "bitbake/bin/toaster-eventreplay" |
| 32 | from os.path import dirname as DN |
| 33 | import_script = os.path.join(DN(DN(DN(DN(os.path.abspath(__file__))))), "bin/toaster-eventreplay") |
| 34 | if not os.path.exists(import_script): |
| 35 | raise Exception("script missing %s" % import_script) |
| 36 | scriptenv = os.environ.copy() |
| 37 | scriptenv["DATABASE_URL"] = toastermain.settings.getDATABASE_URL() |
| 38 | |
| 39 | # run the data loading process and return the results |
| 40 | importer = subprocess.Popen([import_script, abstemppath], stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=scriptenv) |
| 41 | (out, err) = importer.communicate() |
| 42 | if importer.returncode == 0: |
| 43 | os.remove(abstemppath) |
| 44 | return HttpResponse("== Retval %d\n== STDOUT\n%s\n\n== STDERR\n%s" % (importer.returncode, out, err), content_type="text/plain;utf8") |