blob: 04cd8b3dd4df78de02ae233b4d1069dddf55e5e3 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001#
2# BitBake Toaster Implementation
3#
4# Copyright (C) 2014 Intel Corporation
5#
Brad Bishopc342db32019-05-15 21:57:59 -04006# SPDX-License-Identifier: GPL-2.0-only
Patrick Williamsc124f4f2015-09-15 14:41:29 -05007#
Patrick Williamsc124f4f2015-09-15 14:41:29 -05008
Andrew Geissler82c905d2020-04-13 13:39:40 -05009from django.urls import reverse
Patrick Williamsc124f4f2015-09-15 14:41:29 -050010from django.http import HttpResponseBadRequest, HttpResponse
Patrick Williamsc124f4f2015-09-15 14:41:29 -050011import os
12import tempfile
13import subprocess
14import toastermain
15from django.views.decorators.csrf import csrf_exempt
16
17
18@csrf_exempt
19def 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")