diff options
Diffstat (limited to 'bitbake/lib/toaster/toastergui/api.py')
-rw-r--r-- | bitbake/lib/toaster/toastergui/api.py | 161 |
1 files changed, 161 insertions, 0 deletions
diff --git a/bitbake/lib/toaster/toastergui/api.py b/bitbake/lib/toaster/toastergui/api.py index be18090daf..8876409964 100644 --- a/bitbake/lib/toaster/toastergui/api.py +++ b/bitbake/lib/toaster/toastergui/api.py | |||
@@ -20,11 +20,13 @@ | |||
20 | 20 | ||
21 | import re | 21 | import re |
22 | import logging | 22 | import logging |
23 | from collections import Counter | ||
23 | 24 | ||
24 | from orm.models import Project, ProjectTarget, Build, Layer_Version | 25 | from orm.models import Project, ProjectTarget, Build, Layer_Version |
25 | from orm.models import LayerVersionDependency, LayerSource, ProjectLayer | 26 | from orm.models import LayerVersionDependency, LayerSource, ProjectLayer |
26 | from orm.models import Recipe, CustomImageRecipe, CustomImagePackage | 27 | from orm.models import Recipe, CustomImageRecipe, CustomImagePackage |
27 | from orm.models import Layer, Target, Package, Package_Dependency | 28 | from orm.models import Layer, Target, Package, Package_Dependency |
29 | from orm.models import ProjectVariable | ||
28 | from bldcontrol.models import BuildRequest | 30 | from bldcontrol.models import BuildRequest |
29 | from bldcontrol import bbcontroller | 31 | from bldcontrol import bbcontroller |
30 | 32 | ||
@@ -772,3 +774,162 @@ class XhrCustomRecipePackages(View): | |||
772 | except CustomImageRecipe.DoesNotExist: | 774 | except CustomImageRecipe.DoesNotExist: |
773 | return error_response("Tried to remove package that wasn't" | 775 | return error_response("Tried to remove package that wasn't" |
774 | " present") | 776 | " present") |
777 | |||
778 | |||
779 | class XhrProject(View): | ||
780 | """ Create, delete or edit a project | ||
781 | |||
782 | Entry point: /xhr_project/<project_id> | ||
783 | """ | ||
784 | def post(self, request, *args, **kwargs): | ||
785 | """ | ||
786 | Edit project control | ||
787 | |||
788 | Args: | ||
789 | layerAdd = layer_version_id layer_version_id ... | ||
790 | layerDel = layer_version_id layer_version_id ... | ||
791 | projectName = new_project_name | ||
792 | machineName = new_machine_name | ||
793 | |||
794 | Returns: | ||
795 | {"error": "ok"} | ||
796 | or | ||
797 | {"error": <error message>} | ||
798 | """ | ||
799 | try: | ||
800 | prj = Project.objects.get(pk=kwargs['project_id']) | ||
801 | except Project.DoesNotExist: | ||
802 | return error_response("No such project") | ||
803 | |||
804 | # Add layers | ||
805 | if 'layerAdd' in request.POST and len(request.POST['layerAdd']) > 0: | ||
806 | for layer_version_id in request.POST['layerAdd'].split(','): | ||
807 | try: | ||
808 | lv = Layer_Version.objects.get(pk=int(layer_version_id)) | ||
809 | ProjectLayer.objects.get_or_create(project=prj, | ||
810 | layercommit=lv) | ||
811 | except Layer_Version.DoesNotExist: | ||
812 | return error_response("Layer version %s asked to add " | ||
813 | "doesn't exist" % layer_version_id) | ||
814 | |||
815 | # Remove layers | ||
816 | if 'layerDel' in request.POST and len(request.POST['layerDel']) > 0: | ||
817 | layer_version_ids = request.POST['layerDel'].split(',') | ||
818 | ProjectLayer.objects.filter( | ||
819 | project=prj, | ||
820 | layercommit_id__in=layer_version_ids).delete() | ||
821 | |||
822 | # Project name change | ||
823 | if 'projectName' in request.POST: | ||
824 | prj.name = request.POST['projectName'] | ||
825 | prj.save() | ||
826 | |||
827 | # Machine name change | ||
828 | if 'machineName' in request.POST: | ||
829 | machinevar = prj.projectvariable_set.get(name="MACHINE") | ||
830 | machinevar.value = request.POST['machineName'] | ||
831 | machinevar.save() | ||
832 | |||
833 | return JsonResponse({"error": "ok"}) | ||
834 | |||
835 | def get(self, request, *args, **kwargs): | ||
836 | """ | ||
837 | Returns: | ||
838 | json object representing the current project | ||
839 | or: | ||
840 | {"error": <error message>} | ||
841 | """ | ||
842 | |||
843 | try: | ||
844 | project = Project.objects.get(pk=kwargs['project_id']) | ||
845 | except Project.DoesNotExist: | ||
846 | return error_response("Project %s does not exist" % | ||
847 | kwargs['project_id']) | ||
848 | |||
849 | # Create the frequently built targets list | ||
850 | |||
851 | freqtargets = Counter(Target.objects.filter( | ||
852 | Q(build__project=project), | ||
853 | ~Q(build__outcome=Build.IN_PROGRESS) | ||
854 | ).order_by("target").values_list("target", flat=True)) | ||
855 | |||
856 | freqtargets = freqtargets.most_common(5) | ||
857 | |||
858 | # We now have the targets in order of frequency but if there are two | ||
859 | # with the same frequency then we need to make sure those are in | ||
860 | # alphabetical order without losing the frequency ordering | ||
861 | |||
862 | tmp = [] | ||
863 | switch = None | ||
864 | for i, freqtartget in enumerate(freqtargets): | ||
865 | target, count = freqtartget | ||
866 | try: | ||
867 | target_next, count_next = freqtargets[i+1] | ||
868 | if count == count_next and target > target_next: | ||
869 | switch = target | ||
870 | continue | ||
871 | except IndexError: | ||
872 | pass | ||
873 | |||
874 | tmp.append(target) | ||
875 | |||
876 | if switch: | ||
877 | tmp.append(switch) | ||
878 | switch = None | ||
879 | |||
880 | freqtargets = tmp | ||
881 | |||
882 | layers = [] | ||
883 | for layer in project.projectlayer_set.all(): | ||
884 | layers.append({ | ||
885 | "id": layer.layercommit.pk, | ||
886 | "name": layer.layercommit.layer.name, | ||
887 | "vcs_url": layer.layercommit.layer.vcs_url, | ||
888 | "local_source_dir": layer.layercommit.layer.local_source_dir, | ||
889 | "vcs_reference": layer.layercommit.get_vcs_reference(), | ||
890 | "url": layer.layercommit.layer.layer_index_url, | ||
891 | "layerdetailurl": layer.layercommit.get_detailspage_url( | ||
892 | project.pk), | ||
893 | "layersource": layer.layercommit.layer_source | ||
894 | }) | ||
895 | |||
896 | data = { | ||
897 | "name": project.name, | ||
898 | "layers": layers, | ||
899 | "freqtargets": freqtargets, | ||
900 | } | ||
901 | |||
902 | if project.release is not None: | ||
903 | data['release'] = { | ||
904 | "id": project.release.pk, | ||
905 | "name": project.release.name, | ||
906 | "description": project.release.description | ||
907 | } | ||
908 | |||
909 | try: | ||
910 | data["machine"] = {"name": | ||
911 | project.projectvariable_set.get( | ||
912 | name="MACHINE").value} | ||
913 | except ProjectVariable.DoesNotExist: | ||
914 | data["machine"] = None | ||
915 | try: | ||
916 | data["distro"] = project.projectvariable_set.get( | ||
917 | name="DISTRO").value | ||
918 | except ProjectVariable.DoesNotExist: | ||
919 | data["distro"] = "-- not set yet" | ||
920 | |||
921 | data['error'] = "ok" | ||
922 | |||
923 | return JsonResponse(data) | ||
924 | |||
925 | def put(self, request, *args, **kwargs): | ||
926 | # TODO create new project api | ||
927 | return HttpResponse() | ||
928 | |||
929 | def delete(self, request, *args, **kwargs): | ||
930 | try: | ||
931 | Project.objects.get(kwargs['project_id']).delete() | ||
932 | except Project.DoesNotExist: | ||
933 | return error_response("Project %s does not exist" % | ||
934 | kwargs['project_id']) | ||
935 | return JsonResponse({"error": "ok"}) | ||