aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorElliot Smith <elliot.smith@intel.com>2015-09-11 13:57:30 -0700
committerRichard Purdie <richard.purdie@linuxfoundation.org>2015-09-18 09:04:24 +0100
commit8d3aa2d46ebab7a59e57234f0b3f6fc3225a13e8 (patch)
treed453196139843fb019e70f5dcd39353673feb787
parent5697bbcc88edad85891f66d28b8803a9c9d27ff2 (diff)
downloadbitbake-8d3aa2d46ebab7a59e57234f0b3f6fc3225a13e8.tar.gz
toaster: Don't def a function for each call to build_artifact()
Cache the mimetype object and only define the function for getting a mimetype once. Also ensure that filemagic is listed as a requirement of toaster. Doing this also means we can remove the code which tries multiple different "magic" libraries, as we know we have the right version available. Signed-off-by: Elliot Smith <elliot.smith@intel.com> Signed-off-by: brian avery <avery.brian@gmail.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
-rwxr-xr-xlib/toaster/toastergui/views.py42
-rw-r--r--toaster-requirements.txt1
2 files changed, 11 insertions, 32 deletions
diff --git a/lib/toaster/toastergui/views.py b/lib/toaster/toastergui/views.py
index 784272fd7..e918b052f 100755
--- a/lib/toaster/toastergui/views.py
+++ b/lib/toaster/toastergui/views.py
@@ -47,10 +47,19 @@ import json
from os.path import dirname
import itertools
+import magic
import logging
logger = logging.getLogger("toaster")
+class MimeTypeFinder(object):
+ _magic = magic.Magic(flags = magic.MAGIC_MIME_TYPE)
+
+ # returns the mimetype for a file path
+ @classmethod
+ def get_mimetype(self, path):
+ return self._magic.id_filename(path)
+
# all new sessions should come through the landing page;
# determine in which mode we are running in, and redirect appropriately
def landing(request):
@@ -68,8 +77,6 @@ def landing(request):
return render(request, 'landing.html', context)
-
-
# returns a list for most recent builds;
def _get_latest_builds(prj=None):
queryset = Build.objects.all()
@@ -2710,40 +2717,11 @@ if True:
def build_artifact(request, build_id, artifact_type, artifact_id):
if artifact_type in ["cookerlog"]:
- def _mimetype_for_artifact(path):
- try:
- import magic
-
- # fair warning: this is a mess; there are multiple competing and incompatible
- # magic modules floating around, so we try some of the most common combinations
-
- try: # we try ubuntu's python-magic 5.4
- m = magic.open(magic.MAGIC_MIME_TYPE)
- m.load()
- return m.file(path)
- except AttributeError:
- pass
-
- try: # we try python-magic 0.4.6
- m = magic.Magic(magic.MAGIC_MIME)
- return m.from_file(path)
- except AttributeError:
- pass
-
- try: # we try pip filemagic 1.6
- m = magic.Magic(flags=magic.MAGIC_MIME_TYPE)
- return m.id_filename(path)
- except AttributeError:
- pass
-
- return "binary/octet-stream"
- except ImportError:
- return "binary/octet-stream"
try:
build = Build.objects.get(pk = build_id)
file_name = build.cooker_log_path
fsock = open(file_name, "r")
- content_type = _mimetype_for_artifact(file_name)
+ content_type = MimeTypeFinder.get_mimetype(file_name)
response = HttpResponse(fsock, content_type = content_type)
diff --git a/toaster-requirements.txt b/toaster-requirements.txt
index 19b529372..1d92d5e3a 100644
--- a/toaster-requirements.txt
+++ b/toaster-requirements.txt
@@ -2,3 +2,4 @@ Django==1.6
South==0.8.4
argparse==1.2.1
wsgiref==0.1.2
+filemagic==1.6