diff options
Diffstat (limited to 'import-layers/yocto-poky/bitbake/lib/toaster/toastergui/api.py')
-rw-r--r-- | import-layers/yocto-poky/bitbake/lib/toaster/toastergui/api.py | 166 |
1 files changed, 157 insertions, 9 deletions
diff --git a/import-layers/yocto-poky/bitbake/lib/toaster/toastergui/api.py b/import-layers/yocto-poky/bitbake/lib/toaster/toastergui/api.py index ae1f150770..1a6507c3f6 100644 --- a/import-layers/yocto-poky/bitbake/lib/toaster/toastergui/api.py +++ b/import-layers/yocto-poky/bitbake/lib/toaster/toastergui/api.py @@ -20,6 +20,7 @@ import re import logging +import json from collections import Counter from orm.models import Project, ProjectTarget, Build, Layer_Version @@ -136,14 +137,63 @@ class XhrBuildRequest(View): class XhrLayer(View): - """ Get and Update Layer information """ + """ Delete, Get, Add and Update Layer information + + Methods: GET POST DELETE PUT + """ + + def get(self, request, *args, **kwargs): + """ + Get layer information + + Method: GET + Entry point: /xhr_layer/<project id>/<layerversion_id> + """ + + try: + layer_version = Layer_Version.objects.get( + pk=kwargs['layerversion_id']) + + project = Project.objects.get(pk=kwargs['pid']) + + project_layers = ProjectLayer.objects.filter( + project=project).values_list("layercommit_id", + flat=True) + + ret = { + 'error': 'ok', + 'id': layer_version.pk, + 'name': layer_version.layer.name, + 'layerdetailurl': + layer_version.get_detailspage_url(project.pk), + 'vcs_ref': layer_version.get_vcs_reference(), + 'vcs_url': layer_version.layer.vcs_url, + 'local_source_dir': layer_version.layer.local_source_dir, + 'layerdeps': { + "list": [ + { + "id": dep.id, + "name": dep.layer.name, + "layerdetailurl": + dep.get_detailspage_url(project.pk), + "vcs_url": dep.layer.vcs_url, + "vcs_reference": dep.get_vcs_reference() + } + for dep in layer_version.get_alldeps(project.id)] + }, + 'projectlayers': list(project_layers) + } + + return JsonResponse(ret) + except Layer_Version.DoesNotExist: + error_response("No such layer") def post(self, request, *args, **kwargs): """ Update a layer - Entry point: /xhr_layer/<layerversion_id> Method: POST + Entry point: /xhr_layer/<layerversion_id> Args: vcs_url, dirpath, commit, up_branch, summary, description, @@ -201,9 +251,100 @@ class XhrLayer(View): return error_response("Could not update layer version entry: %s" % e) - return JsonResponse({"error": "ok"}) + return error_response("ok") + + def put(self, request, *args, **kwargs): + """ Add a new layer + + Method: PUT + Entry point: /xhr_layer/<project id>/ + Args: + project_id, name, + [vcs_url, dir_path, git_ref], [local_source_dir], [layer_deps + (csv)] + + """ + try: + project = Project.objects.get(pk=kwargs['pid']) + + layer_data = json.loads(request.body.decode('utf-8')) + + # We require a unique layer name as otherwise the lists of layers + # becomes very confusing + existing_layers = \ + project.get_all_compatible_layer_versions().values_list( + "layer__name", + flat=True) + + add_to_project = False + layer_deps_added = [] + if 'add_to_project' in layer_data: + add_to_project = True + + if layer_data['name'] in existing_layers: + return JsonResponse({"error": "layer-name-exists"}) + + layer = Layer.objects.create(name=layer_data['name']) + + layer_version = Layer_Version.objects.create( + layer=layer, + project=project, + layer_source=LayerSource.TYPE_IMPORTED) + + # Local layer + if ('local_source_dir' in layer_data) and layer.local_source_dir: + layer.local_source_dir = layer_data['local_source_dir'] + # git layer + elif 'vcs_url' in layer_data: + layer.vcs_url = layer_data['vcs_url'] + layer_version.dirpath = layer_data['dir_path'] + layer_version.commit = layer_data['git_ref'] + layer_version.branch = layer_data['git_ref'] + + layer.save() + layer_version.save() + + if add_to_project: + ProjectLayer.objects.get_or_create( + layercommit=layer_version, project=project) + + # Add the layer dependencies + if 'layer_deps' in layer_data: + for layer_dep_id in layer_data['layer_deps'].split(","): + layer_dep = Layer_Version.objects.get(pk=layer_dep_id) + LayerVersionDependency.objects.get_or_create( + layer_version=layer_version, depends_on=layer_dep) + + # Add layer deps to the project if specified + if add_to_project: + created, pl = ProjectLayer.objects.get_or_create( + layercommit=layer_dep, project=project) + layer_deps_added.append( + {'name': layer_dep.layer.name, + 'layerdetailurl': + layer_dep.get_detailspage_url(project.pk)}) + + except Layer_Version.DoesNotExist: + return error_response("layer-dep-not-found") + except Project.DoesNotExist: + return error_response("project-not-found") + except KeyError: + return error_response("incorrect-parameters") + + return JsonResponse({'error': "ok", + 'imported_layer': { + 'name': layer.name, + 'layerdetailurl': + layer_version.get_detailspage_url()}, + 'deps_added': layer_deps_added}) def delete(self, request, *args, **kwargs): + """ Delete an imported layer + + Method: DELETE + Entry point: /xhr_layer/<projed id>/<layerversion_id> + + """ try: # We currently only allow Imported layers to be deleted layer_version = Layer_Version.objects.get( @@ -291,10 +432,13 @@ class XhrCustomRecipe(View): return error_response("recipe-already-exists") # create layer 'Custom layer' and verion if needed - layer = Layer.objects.get_or_create( + layer, l_created = Layer.objects.get_or_create( name=CustomImageRecipe.LAYER_NAME, - summary="Layer for custom recipes", - vcs_url="file:///toaster_created_layer")[0] + summary="Layer for custom recipes") + + if l_created: + layer.local_source_dir = "toaster_created_layer" + layer.save() # Check if we have a layer version already # We don't use get_or_create here because the dirpath will change @@ -303,9 +447,10 @@ class XhrCustomRecipe(View): Q(layer=layer) & Q(build=None)).last() if lver is None: - lver, created = Layer_Version.objects.get_or_create( + lver, lv_created = Layer_Version.objects.get_or_create( project=params['project'], layer=layer, + layer_source=LayerSource.TYPE_LOCAL, dirpath="toaster_created_layer") # Add a dependency on our layer to the base recipe's layer @@ -319,7 +464,7 @@ class XhrCustomRecipe(View): optional=False) # Create the actual recipe - recipe, created = CustomImageRecipe.objects.get_or_create( + recipe, r_created = CustomImageRecipe.objects.get_or_create( name=request.POST["name"], base_recipe=params["base"], project=params["project"], @@ -329,7 +474,7 @@ class XhrCustomRecipe(View): # If we created the object then setup these fields. They may get # overwritten later on and cause the get_or_create to create a # duplicate if they've changed. - if created: + if r_created: recipe.file_path = request.POST["name"] recipe.license = "MIT" recipe.version = "0.1" @@ -789,6 +934,9 @@ class XhrProject(View): "url": layer.layercommit.layer.layer_index_url, "layerdetailurl": layer.layercommit.get_detailspage_url( project.pk), + "xhrLayerUrl": reverse("xhr_layer", + args=(project.pk, + layer.layercommit.pk)), "layersource": layer.layercommit.layer_source }) |