summaryrefslogtreecommitdiffstats
path: root/bitbake/lib/toaster/toastergui/views.py
blob: 7cb9b42379205064e2f288123a4bedfe08d99bb0 (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#
# BitBake Toaster Implementation
#
# Copyright (C) 2013        Intel Corporation
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

import operator

from django.db.models import Q
from django.shortcuts import render
from orm.models import Build, Target, Task, Layer, Layer_Version, Recipe, LogMessage, Variable
from orm.models import Task_Dependency, Recipe_Dependency, Package, Package_File, Package_Dependency
from orm.models import Target_Installed_Package
from django.views.decorators.cache import cache_control
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger


def _build_page_range(paginator, index = 1):
    try:
        page = paginator.page(index)
    except PageNotAnInteger:
        page = paginator.page(1)
    except  EmptyPage:
        page = paginator.page(paginator.num_pages)

    page.page_range = [page.number]
    crt_range = 0
    for i in range(1,5):
        if (page.number + i) <= paginator.num_pages:
            page.page_range = page.page_range + [ page.number + i]
            crt_range +=1
        if (page.number - i) > 0:
            page.page_range =  [page.number -i] + page.page_range
            crt_range +=1
        if crt_range == 4:
            break
    return page

@cache_control(no_store=True)
def build(request):
    template = 'build.html'
    logs = LogMessage.objects.all()

    build_info = _build_page_range(Paginator(Build.objects.order_by("-id"), 10),request.GET.get('page', 1))

    context = {'objects': build_info, 'logs': logs ,
        'hideshowcols' : [
                {'name': 'Output', 'order':10},
                {'name': 'Log', 'order':11},
            ]}

    return render(request, template, context)


def _find_task_revdep(task):
    tp = []
    for p in Task_Dependency.objects.filter(depends_on=task):
        tp.append(p.task);
    return tp

def _find_task_provider(task):
    task_revdeps = _find_task_revdep(task)
    for tr in task_revdeps:
        if tr.outcome != Task.OUTCOME_COVERED:
            return tr
    for tr in task_revdeps:
        trc = _find_task_provider(tr)
        if trc is not None:
            return trc
    return None

def task(request, build_id):
    template = 'task.html'

    tasks = _build_page_range(Paginator(Task.objects.filter(build=build_id), 100),request.GET.get('page', 1))

    for t in tasks:
        if t.outcome == Task.OUTCOME_COVERED:
            t.provider = _find_task_provider(t)

    context = {'build': Build.objects.filter(pk=build_id)[0], 'objects': tasks}

    return render(request, template, context)

def configuration(request, build_id):
    template = 'configuration.html'
    variables = _build_page_range(Paginator(Variable.objects.filter(build=build_id), 50), request.GET.get('page', 1))
    context = {'build': Build.objects.filter(pk=build_id)[0], 'objects' : variables}
    return render(request, template, context)

def bpackage(request, build_id):
    template = 'bpackage.html'
    packages = Package.objects.filter(build = build_id)
    context = {'build': Build.objects.filter(pk=build_id)[0], 'objects' : packages}
    return render(request, template, context)

def bfile(request, build_id, package_id):
    template = 'bfile.html'
    files = Package_File.objects.filter(package = package_id)
    context = {'build': Build.objects.filter(pk=build_id)[0], 'objects' : files}
    return render(request, template, context)

def tpackage(request, build_id, target_id):
    template = 'package.html'
    packages = map(lambda x: x.package, list(Target_Installed_Package.objects.filter(target=target_id)))
    context = {'build': Build.objects.filter(pk=build_id)[0], 'objects' : packages}
    return render(request, template, context)

def layer(request):
    template = 'layer.html'
    layer_info = Layer.objects.all()

    for li in layer_info:
        li.versions = Layer_Version.objects.filter(layer = li)
        for liv in li.versions:
            liv.count = Recipe.objects.filter(layer_version__id = liv.id).count()

    context = {'objects': layer_info}

    return render(request, template, context)


def layer_versions_recipes(request, layerversion_id):
    template = 'recipe.html'
    recipes = Recipe.objects.filter(layer_version__id = layerversion_id)

    context = {'objects': recipes,
            'layer_version' : Layer_Version.objects.filter( id = layerversion_id )[0]
    }

    return render(request, template, context)

#### API

import json
from django.core import serializers
from django.http import HttpResponse, HttpResponseBadRequest


def model_explorer(request, model_name):

    DESCENDING = 'desc'
    response_data = {}
    model_mapping = {
        'build': Build,
        'target': Target,
        'task': Task,
        'task_dependency': Task_Dependency,
        'package': Package,
        'layer': Layer,
        'layerversion': Layer_Version,
        'recipe': Recipe,
        'recipe_dependency': Recipe_Dependency,
        'package': Package,
        'package_dependency': Package_Dependency,
        'build_file': Package_File,
        'variable': Variable,
        'logmessage': LogMessage,
        }

    if model_name not in model_mapping.keys():
        return HttpResponseBadRequest()

    model = model_mapping[model_name]

    try:
        limit = int(request.GET.get('limit', 0))
    except ValueError:
        limit = 0

    try:
        offset = int(request.GET.get('offset', 0))
    except ValueError:
        offset = 0

    ordering_string, invalid = _validate_input(request.GET.get('orderby', ''),
                                               model)
    if invalid:
        return HttpResponseBadRequest()

    filter_string, invalid = _validate_input(request.GET.get('filter', ''),
                                             model)
    if invalid:
        return HttpResponseBadRequest()

    search_term = request.GET.get('search', '')

    if filter_string:
        filter_terms = _get_filtering_terms(filter_string)
        try:
            queryset = model.objects.filter(**filter_terms)
        except ValueError:
            queryset = []
    else:
        queryset = model.objects.all()

    if search_term:
        queryset = _get_search_results(search_term, queryset, model)

    if ordering_string and queryset:
        column, order = ordering_string.split(':')
        if order.lower() == DESCENDING:
            queryset = queryset.order_by('-' + column)
        else:
            queryset = queryset.order_by(column)

    if offset and limit:
        queryset = queryset[offset:(offset+limit)]
    elif offset:
        queryset = queryset[offset:]
    elif limit:
        queryset = queryset[:limit]

    if queryset:
        response_data['count'] = queryset.count()
    else:
        response_data['count'] = 0

    response_data['list'] = serializers.serialize('json', queryset)

    return HttpResponse(json.dumps(response_data),
                        content_type='application/json')

def _get_filtering_terms(filter_string):

    search_terms = filter_string.split(":")
    keys = search_terms[0].split(',')
    values = search_terms[1].split(',')

    return dict(zip(keys, values))

def _validate_input(input, model):

    invalid = 0

    if input:
        input_list = input.split(":")

        # Check we have only one colon
        if len(input_list) != 2:
            invalid = 1
            return None, invalid

        # Check we have an equal number of terms both sides of the colon
        if len(input_list[0].split(',')) != len(input_list[1].split(',')):
            invalid = 1
            return None, invalid

        # Check we are looking for a valid field
        valid_fields = model._meta.get_all_field_names()
        for field in input_list[0].split(','):
            if field not in valid_fields:
                invalid = 1
                return None, invalid

    return input, invalid

def _get_search_results(search_term, queryset, model):
    search_objects = []
    for st in search_term.split(" "):
        q_map = map(lambda x: Q(**{x+'__icontains': st}),
                model.search_allowed_fields)

        search_objects.append(reduce(operator.or_, q_map))
    search_object = reduce(operator.and_, search_objects)
    queryset = queryset.filter(search_object)

    return queryset