summaryrefslogtreecommitdiffstats
path: root/qtip/web/bench/views.py
blob: 6de5e4ccd9f5eb1106a4548b794b7b7be4f733ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
##############################################################################
# Copyright (c) 2017 akhil.batra@research.iiit.ac.in and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from multiprocessing import Process

from django.contrib.auth.mixins import LoginRequiredMixin
from django.views.generic.edit import CreateView, UpdateView
from django.views.generic.list import ListView
from django.views.generic.detail import DetailView
from django.views import View
from django.shortcuts import render, redirect
from django.core.files.base import ContentFile
from django.utils import timezone

import forms
import models
import utils


class ReposView(LoginRequiredMixin, CreateView):
    model = models.Repo
    fields = '__all__'

    def get_context_data(self, **kwargs):
        context = super(ReposView, self).get_context_data(**kwargs)
        context["repos"] = self.model.objects.all()
        return context


class RepoUpdate(LoginRequiredMixin, UpdateView):
    model = models.Repo
    fields = '__all__'

    def get_context_data(self, **kwargs):
        context = super(RepoUpdate, self).get_context_data(**kwargs)
        context["repos"] = self.model.objects.all()
        return context


class Run(LoginRequiredMixin, View):
    template_name = 'bench/run.html'
    form_class = forms.TaskForm

    def get(self, request):
        task_form = self.form_class()
        return render(request, self.template_name, {'form': task_form})

    def post(self, request):
        task_form = self.form_class(request.POST)
        if task_form.is_valid():
            new_task = task_form.save()
            new_task.log.save("run_%s.log" % new_task.pk, ContentFile(''))
            p = Process(target=self.start_task, args=(new_task,))
            p.start()
            return redirect('tasks')
            p.join()

    def start_task(self, task):
        task = models.Task.objects.get(pk=task.pk)
        task.status = 'IP'
        task.save()
        with open(task.log.path, "a") as logfile:
            for line in utils.run(task.repo):
                logfile.write(line)
        now = timezone.now()
        task = models.Task.objects.get(pk=task.pk)
        task.end_time = now
        task.status = 'F'
        task.save()


class Logs(LoginRequiredMixin, ListView):
    model = models.Task


class TaskView(LoginRequiredMixin, DetailView):
    model = models.Task

    def get_context_data(self, **kwargs):
        context = super(TaskView, self).get_context_data(**kwargs)
        try:
            with open(context['object'].log.path, "r") as log_file:
                context['log'] = log_file.read()
        except ValueError:
            context['log'] = "No log to show"
        return context