Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | from django.core.management.base import BaseCommand, CommandError |
| 2 | from orm.models import LayerSource, ToasterSetting, Branch, Layer, Layer_Version |
| 3 | from orm.models import BitbakeVersion, Release, ReleaseDefaultLayer, ReleaseLayerSourcePriority |
| 4 | from django.db import IntegrityError |
| 5 | import os |
| 6 | |
| 7 | from checksettings import DN |
| 8 | |
| 9 | import logging |
| 10 | logger = logging.getLogger("toaster") |
| 11 | |
| 12 | def _reduce_canon_path(path): |
| 13 | components = [] |
| 14 | for c in path.split("/"): |
| 15 | if c == "..": |
| 16 | del components[-1] |
| 17 | elif c == ".": |
| 18 | pass |
| 19 | else: |
| 20 | components.append(c) |
| 21 | if len(components) < 2: |
| 22 | components.append('') |
| 23 | return "/".join(components) |
| 24 | |
| 25 | def _get_id_for_sourcetype(s): |
| 26 | for i in LayerSource.SOURCE_TYPE: |
| 27 | if s == i[1]: |
| 28 | return i[0] |
| 29 | raise Exception("Could not find definition for sourcetype '%s'. Valid source types are %s" % (str(s), ', '.join(map(lambda x: "'%s'" % x[1], LayerSource.SOURCE_TYPE )))) |
| 30 | |
| 31 | class Command(BaseCommand): |
| 32 | help = "Loads a toasterconf.json file in the database" |
| 33 | args = "filepath" |
| 34 | |
| 35 | |
| 36 | |
| 37 | def _import_layer_config(self, filepath): |
| 38 | if not os.path.exists(filepath) or not os.path.isfile(filepath): |
| 39 | raise Exception("Failed to find toaster config file %s ." % filepath) |
| 40 | |
| 41 | import json |
| 42 | data = json.loads(open(filepath, "r").read()) |
| 43 | |
| 44 | # verify config file validity before updating settings |
| 45 | for i in ['bitbake', 'releases', 'defaultrelease', 'config', 'layersources']: |
| 46 | assert i in data |
| 47 | |
| 48 | def _read_git_url_from_local_repository(address): |
| 49 | url = None |
| 50 | # we detect the remote name at runtime |
| 51 | import subprocess |
| 52 | (remote, remote_name) = address.split(":", 1) |
| 53 | cmd = subprocess.Popen("git remote -v", shell=True, cwd = os.path.dirname(filepath), stdout=subprocess.PIPE, stderr = subprocess.PIPE) |
| 54 | (out,err) = cmd.communicate() |
| 55 | if cmd.returncode != 0: |
| 56 | logging.warning("Error while importing layer vcs_url: git error: %s" % err) |
| 57 | for line in out.split("\n"): |
| 58 | try: |
| 59 | (name, path) = line.split("\t", 1) |
| 60 | if name == remote_name: |
| 61 | url = path.split(" ")[0] |
| 62 | break |
| 63 | except ValueError: |
| 64 | pass |
| 65 | if url == None: |
| 66 | logging.warning("Error while looking for remote \"%s\" in \"%s\"" % (remote_name, out)) |
| 67 | return url |
| 68 | |
| 69 | |
| 70 | # import bitbake data |
| 71 | for bvi in data['bitbake']: |
| 72 | bvo, created = BitbakeVersion.objects.get_or_create(name=bvi['name']) |
| 73 | if bvi['giturl'].startswith("remote:"): |
| 74 | bvo.giturl = _read_git_url_from_local_repository(bvi['giturl']) |
| 75 | if bvo.giturl is None: |
| 76 | logger.error("The toaster config file references the local git repo, but Toaster cannot detect it.\nYour local configuration for bitbake version %s is invalid. Make sure that the toasterconf.json file is correct." % bvi['name']) |
| 77 | |
| 78 | if bvo.giturl is None: |
| 79 | bvo.giturl = bvi['giturl'] |
| 80 | bvo.branch = bvi['branch'] |
| 81 | bvo.dirpath = bvi['dirpath'] |
| 82 | bvo.save() |
| 83 | |
| 84 | # set the layer sources |
| 85 | for lsi in data['layersources']: |
| 86 | assert 'sourcetype' in lsi |
| 87 | assert 'apiurl' in lsi |
| 88 | assert 'name' in lsi |
| 89 | assert 'branches' in lsi |
| 90 | |
| 91 | |
| 92 | if _get_id_for_sourcetype(lsi['sourcetype']) == LayerSource.TYPE_LAYERINDEX or lsi['apiurl'].startswith("/"): |
| 93 | apiurl = lsi['apiurl'] |
| 94 | else: |
| 95 | apiurl = _reduce_canon_path(os.path.join(DN(os.path.abspath(filepath)), lsi['apiurl'])) |
| 96 | |
| 97 | assert ((_get_id_for_sourcetype(lsi['sourcetype']) == LayerSource.TYPE_LAYERINDEX) or apiurl.startswith("/")), (lsi['sourcetype'],apiurl) |
| 98 | |
| 99 | try: |
| 100 | ls, created = LayerSource.objects.get_or_create(sourcetype = _get_id_for_sourcetype(lsi['sourcetype']), apiurl = apiurl) |
| 101 | ls.name = lsi['name'] |
| 102 | ls.save() |
| 103 | except IntegrityError as e: |
| 104 | logger.warning("IntegrityError %s \nWhile setting name %s for layer source %s " % (e, lsi['name'], ls)) |
| 105 | |
| 106 | |
| 107 | layerbranches = [] |
| 108 | for branchname in lsi['branches']: |
| 109 | bo, created = Branch.objects.get_or_create(layer_source = ls, name = branchname) |
| 110 | layerbranches.append(bo) |
| 111 | |
| 112 | if 'layers' in lsi: |
| 113 | for layerinfo in lsi['layers']: |
| 114 | lo, created = Layer.objects.get_or_create(layer_source = ls, name = layerinfo['name']) |
| 115 | if layerinfo['local_path'].startswith("/"): |
| 116 | lo.local_path = layerinfo['local_path'] |
| 117 | else: |
| 118 | lo.local_path = _reduce_canon_path(os.path.join(ls.apiurl, layerinfo['local_path'])) |
| 119 | |
| 120 | if not os.path.exists(lo.local_path): |
| 121 | logger.error("Local layer path %s must exists. Are you trying to import a layer that does not exist ? Check your local toasterconf.json" % lo.local_path) |
| 122 | |
| 123 | if layerinfo['vcs_url'].startswith("remote:"): |
| 124 | lo.vcs_url = _read_git_url_from_local_repository(layerinfo['vcs_url']) |
| 125 | if lo.vcs_url is None: |
| 126 | logger.error("The toaster config file references the local git repo, but Toaster cannot detect it.\nYour local configuration for layer %s is invalid. Make sure that the toasterconf.json file is correct." % layerinfo['name']) |
| 127 | |
| 128 | if lo.vcs_url is None: |
| 129 | lo.vcs_url = layerinfo['vcs_url'] |
| 130 | |
| 131 | if 'layer_index_url' in layerinfo: |
| 132 | lo.layer_index_url = layerinfo['layer_index_url'] |
| 133 | lo.save() |
| 134 | |
| 135 | for branch in layerbranches: |
| 136 | lvo, created = Layer_Version.objects.get_or_create(layer_source = ls, |
| 137 | up_branch = branch, |
| 138 | commit = branch.name, |
| 139 | layer = lo) |
| 140 | lvo.dirpath = layerinfo['dirpath'] |
| 141 | lvo.save() |
| 142 | # set releases |
| 143 | for ri in data['releases']: |
| 144 | bvo = BitbakeVersion.objects.get(name = ri['bitbake']) |
| 145 | assert bvo is not None |
| 146 | |
| 147 | ro, created = Release.objects.get_or_create(name = ri['name'], bitbake_version = bvo, branch_name = ri['branch']) |
| 148 | ro.description = ri['description'] |
| 149 | ro.helptext = ri['helptext'] |
| 150 | ro.save() |
| 151 | |
| 152 | # save layer source priority for release |
| 153 | for ls_name in ri['layersourcepriority'].keys(): |
| 154 | rlspo, created = ReleaseLayerSourcePriority.objects.get_or_create(release = ro, layer_source = LayerSource.objects.get(name=ls_name)) |
| 155 | rlspo.priority = ri['layersourcepriority'][ls_name] |
| 156 | rlspo.save() |
| 157 | |
| 158 | for dli in ri['defaultlayers']: |
| 159 | # find layers with the same name |
| 160 | ReleaseDefaultLayer.objects.get_or_create( release = ro, layer_name = dli) |
| 161 | |
| 162 | # set default release |
| 163 | if ToasterSetting.objects.filter(name = "DEFAULT_RELEASE").count() > 0: |
| 164 | ToasterSetting.objects.filter(name = "DEFAULT_RELEASE").update(value = data['defaultrelease']) |
| 165 | else: |
| 166 | ToasterSetting.objects.create(name = "DEFAULT_RELEASE", value = data['defaultrelease']) |
| 167 | |
| 168 | # set default config variables |
| 169 | for configname in data['config']: |
| 170 | if ToasterSetting.objects.filter(name = "DEFCONF_" + configname).count() > 0: |
| 171 | ToasterSetting.objects.filter(name = "DEFCONF_" + configname).update(value = data['config'][configname]) |
| 172 | else: |
| 173 | ToasterSetting.objects.create(name = "DEFCONF_" + configname, value = data['config'][configname]) |
| 174 | |
| 175 | |
| 176 | def handle(self, *args, **options): |
| 177 | if len(args) == 0: |
| 178 | raise CommandError("Need a path to the toasterconf.json file") |
| 179 | filepath = args[0] |
| 180 | self._import_layer_config(filepath) |
| 181 | |
| 182 | |
| 183 | |