blob: c708b415e1db210928ae493f91a91e72acd09d59 [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
9from django.views.decorators.cache import cache_control
10from django.core.urlresolvers import reverse
11from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
12from django.http import HttpResponseBadRequest, HttpResponse
13from django.utils import timezone
14from django.utils.html import escape
15from datetime import timedelta
16from django.utils import formats
17from toastergui.templatetags.projecttags import json as jsonfilter
18import json
19import os
20import tempfile
21import subprocess
22import toastermain
23from django.views.decorators.csrf import csrf_exempt
24
25
26@csrf_exempt
27def eventfile(request):
28 """ Receives a file by POST, and runs toaster-eventreply on this file """
29 if request.method != "POST":
30 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")
31
32 # write temporary file
33 (handle, abstemppath) = tempfile.mkstemp(dir="/tmp/")
34 with os.fdopen(handle, "w") as tmpfile:
35 for chunk in request.FILES['eventlog'].chunks():
36 tmpfile.write(chunk)
37 tmpfile.close()
38
39 # compute the path to "bitbake/bin/toaster-eventreplay"
40 from os.path import dirname as DN
41 import_script = os.path.join(DN(DN(DN(DN(os.path.abspath(__file__))))), "bin/toaster-eventreplay")
42 if not os.path.exists(import_script):
43 raise Exception("script missing %s" % import_script)
44 scriptenv = os.environ.copy()
45 scriptenv["DATABASE_URL"] = toastermain.settings.getDATABASE_URL()
46
47 # run the data loading process and return the results
48 importer = subprocess.Popen([import_script, abstemppath], stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=scriptenv)
49 (out, err) = importer.communicate()
50 if importer.returncode == 0:
51 os.remove(abstemppath)
52 return HttpResponse("== Retval %d\n== STDOUT\n%s\n\n== STDERR\n%s" % (importer.returncode, out, err), content_type="text/plain;utf8")