Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1 | # |
| 2 | # ex:ts=4:sw=4:sts=4:et |
| 3 | # -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- |
| 4 | # |
| 5 | # BitBake Toaster Implementation |
| 6 | # |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 7 | # Copyright (C) 2016-2017 Intel Corporation |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 8 | # |
| 9 | # This program is free software; you can redistribute it and/or modify |
| 10 | # it under the terms of the GNU General Public License version 2 as |
| 11 | # published by the Free Software Foundation. |
| 12 | # |
| 13 | # This program is distributed in the hope that it will be useful, |
| 14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | # GNU General Public License for more details. |
| 17 | # |
| 18 | # You should have received a copy of the GNU General Public License along |
| 19 | # with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 22 | from django.core.management.base import BaseCommand |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 23 | |
| 24 | from orm.models import LayerSource, Layer, Release, Layer_Version |
| 25 | from orm.models import LayerVersionDependency, Machine, Recipe |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 26 | from orm.models import Distro |
| 27 | from orm.models import ToasterSetting |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 28 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 29 | import os |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 30 | import sys |
| 31 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 32 | import logging |
| 33 | import threading |
| 34 | import time |
| 35 | logger = logging.getLogger("toaster") |
| 36 | |
| 37 | DEFAULT_LAYERINDEX_SERVER = "http://layers.openembedded.org/layerindex/api/" |
| 38 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame^] | 39 | # Add path to bitbake modules for layerindexlib |
| 40 | # lib/toaster/orm/management/commands/lsupdates.py (abspath) |
| 41 | # lib/toaster/orm/management/commands (dirname) |
| 42 | # lib/toaster/orm/management (dirname) |
| 43 | # lib/toaster/orm (dirname) |
| 44 | # lib/toaster/ (dirname) |
| 45 | # lib/ (dirname) |
| 46 | path = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) |
| 47 | sys.path.insert(0, path) |
| 48 | |
| 49 | import layerindexlib |
| 50 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 51 | |
| 52 | class Spinner(threading.Thread): |
| 53 | """ A simple progress spinner to indicate download/parsing is happening""" |
| 54 | def __init__(self, *args, **kwargs): |
| 55 | super(Spinner, self).__init__(*args, **kwargs) |
| 56 | self.setDaemon(True) |
| 57 | self.signal = True |
| 58 | |
| 59 | def run(self): |
| 60 | os.system('setterm -cursor off') |
| 61 | while self.signal: |
| 62 | for char in ["/", "-", "\\", "|"]: |
| 63 | sys.stdout.write("\r" + char) |
| 64 | sys.stdout.flush() |
| 65 | time.sleep(0.25) |
| 66 | os.system('setterm -cursor on') |
| 67 | |
| 68 | def stop(self): |
| 69 | self.signal = False |
| 70 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 71 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 72 | class Command(BaseCommand): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 73 | args = "" |
| 74 | help = "Updates locally cached information from a layerindex server" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 75 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 76 | def mini_progress(self, what, i, total): |
| 77 | i = i + 1 |
| 78 | pec = (float(i)/float(total))*100 |
| 79 | |
| 80 | sys.stdout.write("\rUpdating %s %d%%" % |
| 81 | (what, |
| 82 | pec)) |
| 83 | sys.stdout.flush() |
| 84 | if int(pec) is 100: |
| 85 | sys.stdout.write("\n") |
| 86 | sys.stdout.flush() |
| 87 | |
| 88 | def update(self): |
| 89 | """ |
| 90 | Fetches layer, recipe and machine information from a layerindex |
| 91 | server |
| 92 | """ |
| 93 | os.system('setterm -cursor off') |
| 94 | |
| 95 | self.apiurl = DEFAULT_LAYERINDEX_SERVER |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 96 | if ToasterSetting.objects.filter(name='CUSTOM_LAYERINDEX_SERVER').count() == 1: |
| 97 | self.apiurl = ToasterSetting.objects.get(name = 'CUSTOM_LAYERINDEX_SERVER').value |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 98 | |
| 99 | assert self.apiurl is not None |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 100 | |
| 101 | # update branches; only those that we already have names listed in the |
| 102 | # Releases table |
| 103 | whitelist_branch_names = [rel.branch_name |
| 104 | for rel in Release.objects.all()] |
| 105 | if len(whitelist_branch_names) == 0: |
| 106 | raise Exception("Failed to make list of branches to fetch") |
| 107 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame^] | 108 | logger.info("Fetching metadata for %s", |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 109 | " ".join(whitelist_branch_names)) |
| 110 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame^] | 111 | # We require a non-empty bb.data, but we can fake it with a dictionary |
| 112 | layerindex = layerindexlib.LayerIndex({"DUMMY" : "VALUE"}) |
| 113 | |
| 114 | http_progress = Spinner() |
| 115 | http_progress.start() |
| 116 | |
| 117 | if whitelist_branch_names: |
| 118 | url_branches = ";branch=%s" % ','.join(whitelist_branch_names) |
| 119 | else: |
| 120 | url_branches = "" |
| 121 | layerindex.load_layerindex("%s%s" % (self.apiurl, url_branches)) |
| 122 | |
| 123 | http_progress.stop() |
| 124 | |
| 125 | # We know we're only processing one entry, so we reference it here |
| 126 | # (this is cheating...) |
| 127 | index = layerindex.indexes[0] |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 128 | |
| 129 | # Map the layer index branches to toaster releases |
| 130 | li_branch_id_to_toaster_release = {} |
| 131 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame^] | 132 | logger.info("Processing releases") |
| 133 | |
| 134 | total = len(index.branches) |
| 135 | for i, id in enumerate(index.branches): |
| 136 | li_branch_id_to_toaster_release[id] = \ |
| 137 | Release.objects.get(name=index.branches[id].name) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 138 | self.mini_progress("Releases", i, total) |
| 139 | |
| 140 | # keep a track of the layerindex (li) id mappings so that |
| 141 | # layer_versions can be created for these layers later on |
| 142 | li_layer_id_to_toaster_layer_id = {} |
| 143 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame^] | 144 | logger.info("Processing layers") |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 145 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame^] | 146 | total = len(index.layerItems) |
| 147 | for i, id in enumerate(index.layerItems): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 148 | try: |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame^] | 149 | l, created = Layer.objects.get_or_create(name=index.layerItems[id].name) |
| 150 | l.up_date = index.layerItems[id].updated |
| 151 | l.summary = index.layerItems[id].summary |
| 152 | l.description = index.layerItems[id].description |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 153 | |
| 154 | if created: |
| 155 | # predefined layers in the fixtures (for example poky.xml) |
| 156 | # always preempt the Layer Index for these values |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame^] | 157 | l.vcs_url = index.layerItems[id].vcs_url |
| 158 | l.vcs_web_url = index.layerItems[id].vcs_web_url |
| 159 | l.vcs_web_tree_base_url = index.layerItems[id].vcs_web_tree_base_url |
| 160 | l.vcs_web_file_base_url = index.layerItems[id].vcs_web_file_base_url |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 161 | l.save() |
| 162 | except Layer.MultipleObjectsReturned: |
| 163 | logger.info("Skipped %s as we found multiple layers and " |
| 164 | "don't know which to update" % |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame^] | 165 | index.layerItems[id].name) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 166 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame^] | 167 | li_layer_id_to_toaster_layer_id[id] = l.pk |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 168 | |
| 169 | self.mini_progress("layers", i, total) |
| 170 | |
| 171 | # update layer_versions |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame^] | 172 | logger.info("Processing layer versions") |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 173 | |
| 174 | # Map Layer index layer_branch object id to |
| 175 | # layer_version toaster object id |
| 176 | li_layer_branch_id_to_toaster_lv_id = {} |
| 177 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame^] | 178 | total = len(index.layerBranches) |
| 179 | for i, id in enumerate(index.layerBranches): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 180 | # release as defined by toaster map to layerindex branch |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame^] | 181 | release = li_branch_id_to_toaster_release[index.layerBranches[id].branch_id] |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 182 | |
| 183 | try: |
| 184 | lv, created = Layer_Version.objects.get_or_create( |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 185 | layer=Layer.objects.get( |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame^] | 186 | pk=li_layer_id_to_toaster_layer_id[index.layerBranches[id].layer_id]), |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 187 | release=release |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 188 | ) |
| 189 | except KeyError: |
| 190 | logger.warning( |
| 191 | "No such layerindex layer referenced by layerbranch %d" % |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame^] | 192 | index.layerBranches[id].layer_id) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 193 | continue |
| 194 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 195 | if created: |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame^] | 196 | lv.release = li_branch_id_to_toaster_release[index.layerBranches[id].branch_id] |
| 197 | lv.up_date = index.layerBranches[id].updated |
| 198 | lv.commit = index.layerBranches[id].actual_branch |
| 199 | lv.dirpath = index.layerBranches[id].vcs_subdir |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 200 | lv.save() |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 201 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame^] | 202 | li_layer_branch_id_to_toaster_lv_id[index.layerBranches[id].id] =\ |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 203 | lv.pk |
| 204 | self.mini_progress("layer versions", i, total) |
| 205 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame^] | 206 | logger.info("Processing layer version dependencies") |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 207 | |
| 208 | dependlist = {} |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame^] | 209 | for id in index.layerDependencies: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 210 | try: |
| 211 | lv = Layer_Version.objects.get( |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame^] | 212 | pk=li_layer_branch_id_to_toaster_lv_id[index.layerDependencies[id].layerbranch_id]) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 213 | except Layer_Version.DoesNotExist as e: |
| 214 | continue |
| 215 | |
| 216 | if lv not in dependlist: |
| 217 | dependlist[lv] = [] |
| 218 | try: |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame^] | 219 | layer_id = li_layer_id_to_toaster_layer_id[index.layerDependencies[id].dependency_id] |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 220 | |
| 221 | dependlist[lv].append( |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 222 | Layer_Version.objects.get(layer__pk=layer_id, |
| 223 | release=lv.release)) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 224 | |
| 225 | except Layer_Version.DoesNotExist: |
| 226 | logger.warning("Cannot find layer version (ls:%s)," |
| 227 | "up_id:%s lv:%s" % |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame^] | 228 | (self, index.layerDependencies[id].dependency_id, lv)) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 229 | |
| 230 | total = len(dependlist) |
| 231 | for i, lv in enumerate(dependlist): |
| 232 | LayerVersionDependency.objects.filter(layer_version=lv).delete() |
| 233 | for lvd in dependlist[lv]: |
| 234 | LayerVersionDependency.objects.get_or_create(layer_version=lv, |
| 235 | depends_on=lvd) |
| 236 | self.mini_progress("Layer version dependencies", i, total) |
| 237 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 238 | # update Distros |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame^] | 239 | logger.info("Processing distro information") |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 240 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame^] | 241 | total = len(index.distros) |
| 242 | for i, id in enumerate(index.distros): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 243 | distro, created = Distro.objects.get_or_create( |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame^] | 244 | name=index.distros[id].name, |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 245 | layer_version=Layer_Version.objects.get( |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame^] | 246 | pk=li_layer_branch_id_to_toaster_lv_id[index.distros[id].layerbranch_id])) |
| 247 | distro.up_date = index.distros[id].updated |
| 248 | distro.name = index.distros[id].name |
| 249 | distro.description = index.distros[id].description |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 250 | distro.save() |
| 251 | self.mini_progress("distros", i, total) |
| 252 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 253 | # update machines |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame^] | 254 | logger.info("Processing machine information") |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 255 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame^] | 256 | total = len(index.machines) |
| 257 | for i, id in enumerate(index.machines): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 258 | mo, created = Machine.objects.get_or_create( |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame^] | 259 | name=index.machines[id].name, |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 260 | layer_version=Layer_Version.objects.get( |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame^] | 261 | pk=li_layer_branch_id_to_toaster_lv_id[index.machines[id].layerbranch_id])) |
| 262 | mo.up_date = index.machines[id].updated |
| 263 | mo.name = index.machines[id].name |
| 264 | mo.description = index.machines[id].description |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 265 | mo.save() |
| 266 | self.mini_progress("machines", i, total) |
| 267 | |
| 268 | # update recipes; paginate by layer version / layer branch |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame^] | 269 | logger.info("Processing recipe information") |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 270 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame^] | 271 | total = len(index.recipes) |
| 272 | for i, id in enumerate(index.recipes): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 273 | try: |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame^] | 274 | lv_id = li_layer_branch_id_to_toaster_lv_id[index.recipes[id].layerbranch_id] |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 275 | lv = Layer_Version.objects.get(pk=lv_id) |
| 276 | |
| 277 | ro, created = Recipe.objects.get_or_create( |
| 278 | layer_version=lv, |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame^] | 279 | name=index.recipes[id].pn |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 280 | ) |
| 281 | |
| 282 | ro.layer_version = lv |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame^] | 283 | ro.up_date = index.recipes[id].updated |
| 284 | ro.name = index.recipes[id].pn |
| 285 | ro.version = index.recipes[id].pv |
| 286 | ro.summary = index.recipes[id].summary |
| 287 | ro.description = index.recipes[id].description |
| 288 | ro.section = index.recipes[id].section |
| 289 | ro.license = index.recipes[id].license |
| 290 | ro.homepage = index.recipes[id].homepage |
| 291 | ro.bugtracker = index.recipes[id].bugtracker |
| 292 | ro.file_path = index.recipes[id].fullpath |
| 293 | ro.is_image = 'image' in index.recipes[id].inherits.split() |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 294 | ro.save() |
| 295 | except Exception as e: |
| 296 | logger.warning("Failed saving recipe %s", e) |
| 297 | |
| 298 | self.mini_progress("recipes", i, total) |
| 299 | |
| 300 | os.system('setterm -cursor on') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 301 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 302 | def handle(self, **options): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 303 | self.update() |