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