Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | # |
| 2 | # BitBake Toaster Implementation |
| 3 | # |
| 4 | # Copyright (C) 2014 Intel Corporation |
| 5 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 6 | # SPDX-License-Identifier: GPL-2.0-only |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 7 | # |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 8 | |
| 9 | from django.views.decorators.cache import cache_control |
| 10 | from django.core.urlresolvers import reverse |
| 11 | from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger |
| 12 | from django.http import HttpResponseBadRequest, HttpResponse |
| 13 | from django.utils import timezone |
| 14 | from django.utils.html import escape |
| 15 | from datetime import timedelta |
| 16 | from django.utils import formats |
| 17 | from toastergui.templatetags.projecttags import json as jsonfilter |
| 18 | import json |
| 19 | import os |
| 20 | import tempfile |
| 21 | import subprocess |
| 22 | import toastermain |
| 23 | from django.views.decorators.csrf import csrf_exempt |
| 24 | |
| 25 | |
| 26 | @csrf_exempt |
| 27 | def 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") |