summaryrefslogtreecommitdiffstats
path: root/bitbake/lib/toaster/toastergui/views.py
diff options
context:
space:
mode:
Diffstat (limited to 'bitbake/lib/toaster/toastergui/views.py')
-rwxr-xr-xbitbake/lib/toaster/toastergui/views.py94
1 files changed, 76 insertions, 18 deletions
diff --git a/bitbake/lib/toaster/toastergui/views.py b/bitbake/lib/toaster/toastergui/views.py
index b5ff9b1d53..eb323ec81d 100755
--- a/bitbake/lib/toaster/toastergui/views.py
+++ b/bitbake/lib/toaster/toastergui/views.py
@@ -2914,9 +2914,6 @@ if toastermain.settings.MANAGED:
2914 if artifact_type == "imagefile": 2914 if artifact_type == "imagefile":
2915 file_name = Target_Image_File.objects.get(target__build = b, pk = artifact_id).file_name 2915 file_name = Target_Image_File.objects.get(target__build = b, pk = artifact_id).file_name
2916 2916
2917 elif artifact_type == "cookerlog":
2918 file_name = b.cooker_log_path
2919
2920 elif artifact_type == "buildartifact": 2917 elif artifact_type == "buildartifact":
2921 file_name = BuildArtifact.objects.get(build = b, pk = artifact_id).file_name 2918 file_name = BuildArtifact.objects.get(build = b, pk = artifact_id).file_name
2922 2919
@@ -2935,26 +2932,87 @@ if toastermain.settings.MANAGED:
2935 2932
2936 2933
2937 def build_artifact(request, build_id, artifact_type, artifact_id): 2934 def build_artifact(request, build_id, artifact_type, artifact_id):
2938 b = Build.objects.get(pk=build_id) 2935 if artifact_type in ["cookerlog"]:
2939 if b.buildrequest is None or b.buildrequest.environment is None: 2936 # these artifacts are saved after building, so they are on the server itself
2940 raise Exception("Artifact not available for download (missing build request or build environment)") 2937 def _mimetype_for_artifact(path):
2938 try:
2939 import magic
2940
2941 # fair warning: this is a mess; there are multiple competing and incompatible
2942 # magic modules floating around, so we try some of the most common combinations
2943
2944 try: # we try ubuntu's python-magic 5.4
2945 m = magic.open(magic.MAGIC_MIME_TYPE)
2946 m.load()
2947 return m.file(path)
2948 except AttributeError:
2949 pass
2950
2951 try: # we try python-magic 0.4.6
2952 m = magic.Magic(magic.MAGIC_MIME)
2953 return m.from_file(path)
2954 except AttributeError:
2955 pass
2956
2957 try: # we try pip filemagic 1.6
2958 m = magic.Magic(flags=magic.MAGIC_MIME_TYPE)
2959 return m.id_filename(path)
2960 except AttributeError:
2961 pass
2962
2963 return "binary/octet-stream"
2964 except ImportError:
2965 return "binary/octet-stream"
2966 try:
2967 # match code with runbuilds.Command.archive()
2968 build_artifact_storage_dir = os.path.join(ToasterSetting.objects.get(name="ARTIFACTS_STORAGE_DIR").value, "%d" % int(build_id))
2969 file_name = os.path.join(build_artifact_storage_dir, "cooker_log.txt")
2970
2971 fsock = open(file_name, "r")
2972 content_type=_mimetype_for_artifact(file_name)
2941 2973
2942 file_name = _file_name_for_artifact(b, artifact_type, artifact_id) 2974 response = HttpResponse(fsock, content_type = content_type)
2943 fsock = None 2975
2944 content_type='application/force-download' 2976 response['Content-Disposition'] = 'attachment; filename=' + os.path.basename(file_name)
2977 return response
2978 except IOError:
2979 context = {
2980 'build' : Build.objects.get(pk = build_id),
2981 }
2982 return render(request, "unavailable_artifact.html", context)
2945 2983
2946 if file_name is None:
2947 raise Exception("Could not handle artifact %s id %s" % (artifact_type, artifact_id))
2948 else: 2984 else:
2949 content_type = b.buildrequest.environment.get_artifact_type(file_name) 2985 # retrieve the artifact directly from the build environment
2950 fsock = b.buildrequest.environment.get_artifact(file_name) 2986 return _get_be_artifact(request, build_id, artifact_type, artifact_id)
2951 file_name = os.path.basename(file_name) # we assume that the build environment system has the same path conventions as host
2952 2987
2953 response = HttpResponse(fsock, content_type = content_type)
2954 2988
2955 # returns a file from the environment 2989 def _get_be_artifact(request, build_id, artifact_type, artifact_id):
2956 response['Content-Disposition'] = 'attachment; filename=' + file_name 2990 try:
2957 return response 2991 b = Build.objects.get(pk=build_id)
2992 if b.buildrequest is None or b.buildrequest.environment is None:
2993 raise Exception("Artifact not available for download (missing build request or build environment)")
2994
2995 file_name = _file_name_for_artifact(b, artifact_type, artifact_id)
2996 fsock = None
2997 content_type='application/force-download'
2998
2999 if file_name is None:
3000 raise Exception("Could not handle artifact %s id %s" % (artifact_type, artifact_id))
3001 else:
3002 content_type = b.buildrequest.environment.get_artifact_type(file_name)
3003 fsock = b.buildrequest.environment.get_artifact(file_name)
3004 file_name = os.path.basename(file_name) # we assume that the build environment system has the same path conventions as host
3005
3006 response = HttpResponse(fsock, content_type = content_type)
3007
3008 # returns a file from the environment
3009 response['Content-Disposition'] = 'attachment; filename=' + file_name
3010 return response
3011 except IOError:
3012 context = {
3013 'build' : Build.objects.get(pk = build_id),
3014 }
3015 return render(request, "unavailable_artifact.html", context)
2958 3016
2959 3017
2960 3018