blob: 58c650f8fcc8770933da42db8e97e7b50401b4b8 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001#
2# BitBake Toaster Implementation
3#
4# Copyright (C) 2015 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
Brad Bishop6e60e8b2018-02-01 10:27:11 -050019import subprocess
20
Patrick Williamsc124f4f2015-09-15 14:41:29 -050021from toastergui.widgets import ToasterTypeAhead
22from orm.models import Project
23from django.core.urlresolvers import reverse
Brad Bishop6e60e8b2018-02-01 10:27:11 -050024from django.core.cache import cache
25
Patrick Williamsc124f4f2015-09-15 14:41:29 -050026
27class LayersTypeAhead(ToasterTypeAhead):
28 """ Typeahead for layers available and not added in the current project's
29 configuration """
Patrick Williamsc124f4f2015-09-15 14:41:29 -050030
31 def apply_search(self, search_term, prj, request):
Patrick Williamsf1e5d692016-03-30 15:21:19 -050032 layers = prj.get_all_compatible_layer_versions()
Patrick Williamsc124f4f2015-09-15 14:41:29 -050033 layers = layers.order_by('layer__name')
34
35 # Unlike the other typeaheads we also don't want to show suggestions
36 # for layers already in the project unless required such as when adding
37 # layerdeps to a new layer.
Brad Bishop6e60e8b2018-02-01 10:27:11 -050038 if "include_added" in request.GET and \
39 request.GET['include_added'] != "true":
Patrick Williamsf1e5d692016-03-30 15:21:19 -050040 layers = layers.exclude(
41 pk__in=prj.get_project_layer_versions(pk=True))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050042
43 primary_results = layers.filter(layer__name__istartswith=search_term)
Brad Bishop6e60e8b2018-02-01 10:27:11 -050044 secondary_results = layers.filter(
45 layer__name__icontains=search_term).exclude(
46 pk__in=primary_results)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050047
48 results = []
49
50 for layer_version in list(primary_results) + list(secondary_results):
51 vcs_reference = layer_version.get_vcs_reference()
52
53 detail = "[ %s | %s ]" % (layer_version.layer.vcs_url,
54 vcs_reference)
55 needed_fields = {
Brad Bishop6e60e8b2018-02-01 10:27:11 -050056 'id': layer_version.pk,
57 'name': layer_version.layer.name,
58 'layerdetailurl': layer_version.get_detailspage_url(prj.pk),
59 'xhrLayerUrl': reverse('xhr_layer',
60 args=(prj.pk, layer_version.pk)),
61 'vcs_url': layer_version.layer.vcs_url,
62 'vcs_reference': vcs_reference,
63 'detail': detail,
64 'local_source_dir': layer_version.layer.local_source_dir,
Patrick Williamsc124f4f2015-09-15 14:41:29 -050065 }
66
67 results.append(needed_fields)
68
69 return results
70
Brad Bishop6e60e8b2018-02-01 10:27:11 -050071
Patrick Williamsc124f4f2015-09-15 14:41:29 -050072class MachinesTypeAhead(ToasterTypeAhead):
73 """ Typeahead for all the machines available in the current project's
74 configuration """
Patrick Williamsc124f4f2015-09-15 14:41:29 -050075
76 def apply_search(self, search_term, prj, request):
77 machines = prj.get_available_machines()
78 machines = machines.order_by("name")
79
80 primary_results = machines.filter(name__istartswith=search_term)
Brad Bishop6e60e8b2018-02-01 10:27:11 -050081 secondary_results = machines.filter(
82 name__icontains=search_term).exclude(pk__in=primary_results)
83 tertiary_results = machines.filter(
84 layer_version__layer__name__icontains=search_term).exclude(
85 pk__in=primary_results).exclude(pk__in=secondary_results)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050086
87 results = []
88
Brad Bishop6e60e8b2018-02-01 10:27:11 -050089 for machine in list(primary_results) + list(secondary_results) + \
90 list(tertiary_results):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050091
92 detail = "[ %s ]" % (machine.layer_version.layer.name)
93 needed_fields = {
Brad Bishop6e60e8b2018-02-01 10:27:11 -050094 'id': machine.pk,
95 'name': machine.name,
96 'detail': detail,
Patrick Williamsc124f4f2015-09-15 14:41:29 -050097 }
98
99 results.append(needed_fields)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500100 return results
101
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500102
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500103class RecipesTypeAhead(ToasterTypeAhead):
104 """ Typeahead for all the recipes available in the current project's
105 configuration """
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500106 def apply_search(self, search_term, prj, request):
107 recipes = prj.get_available_recipes()
108 recipes = recipes.order_by("name")
109
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500110 primary_results = recipes.filter(name__istartswith=search_term)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500111 secondary_results = recipes.filter(
112 name__icontains=search_term).exclude(pk__in=primary_results)
113 tertiary_results = recipes.filter(
114 layer_version__layer__name__icontains=search_term).exclude(
115 pk__in=primary_results).exclude(pk__in=secondary_results)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500116
117 results = []
118
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500119 for recipe in list(primary_results) + list(secondary_results) + \
120 list(tertiary_results):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500121
122 detail = "[ %s ]" % (recipe.layer_version.layer.name)
123 needed_fields = {
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500124 'id': recipe.pk,
125 'name': recipe.name,
126 'detail': detail,
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500127 }
128
129 results.append(needed_fields)
130
131 return results
132
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500133
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500134class ProjectsTypeAhead(ToasterTypeAhead):
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500135 """ Typeahead for all the projects, except for command line builds """
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500136 def apply_search(self, search_term, prj, request):
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500137 projects = Project.objects.exclude(is_default=True).order_by("name")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500138
139 primary_results = projects.filter(name__istartswith=search_term)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500140 secondary_results = projects.filter(
141 name__icontains=search_term).exclude(pk__in=primary_results)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500142
143 results = []
144
145 for project in list(primary_results) + list(secondary_results):
146 needed_fields = {
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500147 'id': project.pk,
148 'name': project.name,
149 'detail': "",
150 'projectPageUrl': reverse('project', args=(project.pk,))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500151 }
152
153 results.append(needed_fields)
154
155 return results
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500156
157
158class GitRevisionTypeAhead(ToasterTypeAhead):
159 def apply_search(self, search_term, prj, request):
160 results = []
161 git_url = request.GET.get('git_url')
162 ls_remote = cache.get(git_url)
163
164 if ls_remote is None:
165 ls_remote = subprocess.check_output(['git', 'ls-remote', git_url],
166 universal_newlines=True)
167 ls_remote = ls_remote.splitlines()
168 # Avoid fetching the list of git refs on each new input
169 cache.set(git_url, ls_remote, 120)
170
171 for rev in ls_remote:
172 git_rev = str(rev).split("/")[-1:][0]
173 # "HEAD" has a special meaning in Toaster... YOCTO #9924
174 if "HEAD" in git_rev:
175 continue
176
177 if git_rev.startswith(search_term):
178 results.append({'name': git_rev,
179 'detail': '[ %s ]' % str(rev)})
180
181 return results