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 | # |
| 6 | # This program is free software; you can redistribute it and/or modify |
| 7 | # it under the terms of the GNU General Public License version 2 as |
| 8 | # published by the Free Software Foundation. |
| 9 | # |
| 10 | # This program is distributed in the hope that it will be useful, |
| 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | # GNU General Public License for more details. |
| 14 | # |
| 15 | # You should have received a copy of the GNU General Public License along |
| 16 | # with this program; if not, write to the Free Software Foundation, Inc., |
| 17 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | |
| 19 | from django.views.decorators.cache import cache_control |
| 20 | from django.core.urlresolvers import reverse |
| 21 | from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger |
| 22 | from django.http import HttpResponseBadRequest, HttpResponse |
| 23 | from django.utils import timezone |
| 24 | from django.utils.html import escape |
| 25 | from datetime import timedelta |
| 26 | from django.utils import formats |
| 27 | from toastergui.templatetags.projecttags import json as jsonfilter |
| 28 | import json |
| 29 | import os |
| 30 | import tempfile |
| 31 | import subprocess |
| 32 | import toastermain |
| 33 | from django.views.decorators.csrf import csrf_exempt |
| 34 | |
| 35 | |
| 36 | @csrf_exempt |
| 37 | def eventfile(request): |
| 38 | """ Receives a file by POST, and runs toaster-eventreply on this file """ |
| 39 | if request.method != "POST": |
| 40 | 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") |
| 41 | |
| 42 | # write temporary file |
| 43 | (handle, abstemppath) = tempfile.mkstemp(dir="/tmp/") |
| 44 | with os.fdopen(handle, "w") as tmpfile: |
| 45 | for chunk in request.FILES['eventlog'].chunks(): |
| 46 | tmpfile.write(chunk) |
| 47 | tmpfile.close() |
| 48 | |
| 49 | # compute the path to "bitbake/bin/toaster-eventreplay" |
| 50 | from os.path import dirname as DN |
| 51 | import_script = os.path.join(DN(DN(DN(DN(os.path.abspath(__file__))))), "bin/toaster-eventreplay") |
| 52 | if not os.path.exists(import_script): |
| 53 | raise Exception("script missing %s" % import_script) |
| 54 | scriptenv = os.environ.copy() |
| 55 | scriptenv["DATABASE_URL"] = toastermain.settings.getDATABASE_URL() |
| 56 | |
| 57 | # run the data loading process and return the results |
| 58 | importer = subprocess.Popen([import_script, abstemppath], stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=scriptenv) |
| 59 | (out, err) = importer.communicate() |
| 60 | if importer.returncode == 0: |
| 61 | os.remove(abstemppath) |
| 62 | return HttpResponse("== Retval %d\n== STDOUT\n%s\n\n== STDERR\n%s" % (importer.returncode, out, err), content_type="text/plain;utf8") |