blob: bdf38ae6e8907d4cbac15203ba18f85ad79904e1 [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
Andrew Geissler20137392023-10-12 04:59:14 -060017from toastermain.logs import log_view_mixin
18
Patrick Williamsc124f4f2015-09-15 14:41:29 -050019
20@csrf_exempt
Andrew Geissler20137392023-10-12 04:59:14 -060021@log_view_mixin
Patrick Williamsc124f4f2015-09-15 14:41:29 -050022def eventfile(request):
23 """ Receives a file by POST, and runs toaster-eventreply on this file """
24 if request.method != "POST":
25 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")
26
27 # write temporary file
28 (handle, abstemppath) = tempfile.mkstemp(dir="/tmp/")
29 with os.fdopen(handle, "w") as tmpfile:
30 for chunk in request.FILES['eventlog'].chunks():
31 tmpfile.write(chunk)
32 tmpfile.close()
33
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
42 # run the data loading process and return the results
43 importer = subprocess.Popen([import_script, abstemppath], stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=scriptenv)
44 (out, err) = importer.communicate()
45 if importer.returncode == 0:
46 os.remove(abstemppath)
47 return HttpResponse("== Retval %d\n== STDOUT\n%s\n\n== STDERR\n%s" % (importer.returncode, out, err), content_type="text/plain;utf8")