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 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") 45