aboutsummaryrefslogtreecommitdiffstats
path: root/lib/srtgui/typeaheads.py
blob: fb26cf0b20a0d15945e0497730722f515d74392a (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
#
# BitBake Toaster Implementation
#
# Copyright (C) 2015        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 subprocess

from srtgui.widgets import ToasterTypeAhead
from django.urls import reverse
from django.core.cache import cache

###
### SAMPLE TOASTER CODE TO BE ADAPTED TO SRTOOL
###

class LayersTypeAhead(ToasterTypeAhead):
    """ Typeahead for layers available and not added in the current project's
    configuration """

    def apply_search(self, search_term, prj, request):
        layers = prj.get_all_compatible_layer_versions()
        layers = layers.order_by('layer__name')

        # Unlike the other typeaheads we also don't want to show suggestions
        # for layers already in the project unless required such as when adding
        # layerdeps to a new layer.
        if "include_added" in request.GET and \
           request.GET['include_added'] != "true":
            layers = layers.exclude(
                pk__in=prj.get_project_layer_versions(pk=True))

        primary_results = layers.filter(layer__name__istartswith=search_term)
        secondary_results = layers.filter(
            layer__name__icontains=search_term).exclude(
                pk__in=primary_results)

        results = []

        for layer_version in list(primary_results) + list(secondary_results):
            vcs_reference = layer_version.get_vcs_reference()

            detail = "[ %s | %s ]" % (layer_version.layer.vcs_url,
                                      vcs_reference)
            needed_fields = {
                'id': layer_version.pk,
                'name': layer_version.layer.name,
                'layerdetailurl': layer_version.get_detailspage_url(prj.pk),
                'xhrLayerUrl': reverse('xhr_layer',
                                       args=(prj.pk, layer_version.pk)),
                'vcs_url': layer_version.layer.vcs_url,
                'vcs_reference': vcs_reference,
                'detail': detail,
                'local_source_dir': layer_version.layer.local_source_dir,
            }

            results.append(needed_fields)

        return results


class MachinesTypeAhead(ToasterTypeAhead):
    """ Typeahead for all the machines available in the current project's
    configuration """

    def apply_search(self, search_term, prj, request):
        machines = prj.get_available_machines()
        machines = machines.order_by("name")

        primary_results = machines.filter(name__istartswith=search_term)
        secondary_results = machines.filter(
            name__icontains=search_term).exclude(pk__in=primary_results)
        tertiary_results = machines.filter(
            layer_version__layer__name__icontains=search_term).exclude(
                pk__in=primary_results).exclude(pk__in=secondary_results)

        results = []

        for machine in list(primary_results) + list(secondary_results) + \
                list(tertiary_results):

            detail = "[ %s ]" % (machine.layer_version.layer.name)
            needed_fields = {
                'id': machine.pk,
                'name': machine.name,
                'detail': detail,
            }

            results.append(needed_fields)
        return results


class DistrosTypeAhead(ToasterTypeAhead):
    """ Typeahead for all the distros available in the current project's
    configuration """
    def __init__(self):
        super(DistrosTypeAhead, self).__init__()

    def apply_search(self, search_term, prj, request):
        distros = prj.get_available_distros()
        distros = distros.order_by("name")

        primary_results = distros.filter(name__istartswith=search_term)
        secondary_results = distros.filter(name__icontains=search_term).exclude(pk__in=primary_results)
        tertiary_results = distros.filter(layer_version__layer__name__icontains=search_term).exclude(pk__in=primary_results).exclude(pk__in=secondary_results)

        results = []

        for distro in list(primary_results) + list(secondary_results) + list(tertiary_results):

            detail = "[ %s ]" % (distro.layer_version.layer.name)
            needed_fields = {
                'id' : distro.pk,
                'name' : distro.name,
                'detail' : detail,
            }

            results.append(needed_fields)

        return results


class RecipesTypeAhead(ToasterTypeAhead):
    """ Typeahead for all the recipes available in the current project's
    configuration """
    def apply_search(self, search_term, prj, request):
        recipes = prj.get_available_recipes()
        recipes = recipes.order_by("name")

        primary_results = recipes.filter(name__istartswith=search_term)
        secondary_results = recipes.filter(
            name__icontains=search_term).exclude(pk__in=primary_results)
        tertiary_results = recipes.filter(
            layer_version__layer__name__icontains=search_term).exclude(
                pk__in=primary_results).exclude(pk__in=secondary_results)

        results = []

        for recipe in list(primary_results) + list(secondary_results) + \
                list(tertiary_results):

            detail = "[ %s ]" % (recipe.layer_version.layer.name)
            needed_fields = {
                'id': recipe.pk,
                'name': recipe.name,
                'detail': detail,
            }

            results.append(needed_fields)

        return results


class GitRevisionTypeAhead(ToasterTypeAhead):
    def apply_search(self, search_term, prj, request):
        results = []
        git_url = request.GET.get('git_url')
        ls_remote = cache.get(git_url)

        if ls_remote is None:
            ls_remote = subprocess.check_output(['git', 'ls-remote', git_url],
                                                universal_newlines=True)
            ls_remote = ls_remote.splitlines()
            # Avoid fetching the list of git refs on each new input
            cache.set(git_url, ls_remote, 120)

        for rev in ls_remote:
            git_rev = str(rev).split("/")[-1:][0]
            # "HEAD" has a special meaning in Toaster...  YOCTO #9924
            if "HEAD" in git_rev:
                continue

            if git_rev.startswith(search_term):
                results.append({'name': git_rev,
                                'detail': '[ %s ]' % str(rev)})

        return results