1# 2# BitBake Toaster Implementation 3# 4# Copyright (C) 2014 Intel Corporation 5# 6# SPDX-License-Identifier: GPL-2.0-only 7# 8 9from django.urls import reverse 10from django.http import HttpResponseBadRequest, HttpResponse 11import os 12import tempfile 13import subprocess 14import toastermain 15from django.views.decorators.csrf import csrf_exempt 16 17from toastermain.logs import log_view_mixin 18 19 20@csrf_exempt 21@log_view_mixin 22def 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") 48