diff options
author | Elliot Smith <elliot.smith@intel.com> | 2015-09-11 13:57:30 -0700 |
---|---|---|
committer | Richard Purdie <richard.purdie@linuxfoundation.org> | 2015-09-18 09:05:33 +0100 |
commit | 460e4c2c2c0411347442c30aedda6e0830fede16 (patch) | |
tree | b9b2789f2b4ff7e1c659d7859c7bfffe62565508 | |
parent | b6d1d2acd58c949f7b8879fe5087c8846ac51a54 (diff) | |
download | poky-460e4c2c2c0411347442c30aedda6e0830fede16.tar.gz |
bitbake: 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.
(Bitbake rev: 8d3aa2d46ebab7a59e57234f0b3f6fc3225a13e8)
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-x | bitbake/lib/toaster/toastergui/views.py | 42 | ||||
-rw-r--r-- | bitbake/toaster-requirements.txt | 1 |
2 files changed, 11 insertions, 32 deletions
diff --git a/bitbake/lib/toaster/toastergui/views.py b/bitbake/lib/toaster/toastergui/views.py index 784272fd70..e918b052fd 100755 --- a/bitbake/lib/toaster/toastergui/views.py +++ b/bitbake/lib/toaster/toastergui/views.py | |||
@@ -47,10 +47,19 @@ import json | |||
47 | from os.path import dirname | 47 | from os.path import dirname |
48 | import itertools | 48 | import itertools |
49 | 49 | ||
50 | import magic | ||
50 | import logging | 51 | import logging |
51 | 52 | ||
52 | logger = logging.getLogger("toaster") | 53 | logger = logging.getLogger("toaster") |
53 | 54 | ||
55 | class MimeTypeFinder(object): | ||
56 | _magic = magic.Magic(flags = magic.MAGIC_MIME_TYPE) | ||
57 | |||
58 | # returns the mimetype for a file path | ||
59 | @classmethod | ||
60 | def get_mimetype(self, path): | ||
61 | return self._magic.id_filename(path) | ||
62 | |||
54 | # all new sessions should come through the landing page; | 63 | # all new sessions should come through the landing page; |
55 | # determine in which mode we are running in, and redirect appropriately | 64 | # determine in which mode we are running in, and redirect appropriately |
56 | def landing(request): | 65 | def landing(request): |
@@ -68,8 +77,6 @@ def landing(request): | |||
68 | 77 | ||
69 | return render(request, 'landing.html', context) | 78 | return render(request, 'landing.html', context) |
70 | 79 | ||
71 | |||
72 | |||
73 | # returns a list for most recent builds; | 80 | # returns a list for most recent builds; |
74 | def _get_latest_builds(prj=None): | 81 | def _get_latest_builds(prj=None): |
75 | queryset = Build.objects.all() | 82 | queryset = Build.objects.all() |
@@ -2710,40 +2717,11 @@ if True: | |||
2710 | 2717 | ||
2711 | def build_artifact(request, build_id, artifact_type, artifact_id): | 2718 | def build_artifact(request, build_id, artifact_type, artifact_id): |
2712 | if artifact_type in ["cookerlog"]: | 2719 | if artifact_type in ["cookerlog"]: |
2713 | def _mimetype_for_artifact(path): | ||
2714 | try: | ||
2715 | import magic | ||
2716 | |||
2717 | # fair warning: this is a mess; there are multiple competing and incompatible | ||
2718 | # magic modules floating around, so we try some of the most common combinations | ||
2719 | |||
2720 | try: # we try ubuntu's python-magic 5.4 | ||
2721 | m = magic.open(magic.MAGIC_MIME_TYPE) | ||
2722 | m.load() | ||
2723 | return m.file(path) | ||
2724 | except AttributeError: | ||
2725 | pass | ||
2726 | |||
2727 | try: # we try python-magic 0.4.6 | ||
2728 | m = magic.Magic(magic.MAGIC_MIME) | ||
2729 | return m.from_file(path) | ||
2730 | except AttributeError: | ||
2731 | pass | ||
2732 | |||
2733 | try: # we try pip filemagic 1.6 | ||
2734 | m = magic.Magic(flags=magic.MAGIC_MIME_TYPE) | ||
2735 | return m.id_filename(path) | ||
2736 | except AttributeError: | ||
2737 | pass | ||
2738 | |||
2739 | return "binary/octet-stream" | ||
2740 | except ImportError: | ||
2741 | return "binary/octet-stream" | ||
2742 | try: | 2720 | try: |
2743 | build = Build.objects.get(pk = build_id) | 2721 | build = Build.objects.get(pk = build_id) |
2744 | file_name = build.cooker_log_path | 2722 | file_name = build.cooker_log_path |
2745 | fsock = open(file_name, "r") | 2723 | fsock = open(file_name, "r") |
2746 | content_type = _mimetype_for_artifact(file_name) | 2724 | content_type = MimeTypeFinder.get_mimetype(file_name) |
2747 | 2725 | ||
2748 | response = HttpResponse(fsock, content_type = content_type) | 2726 | response = HttpResponse(fsock, content_type = content_type) |
2749 | 2727 | ||
diff --git a/bitbake/toaster-requirements.txt b/bitbake/toaster-requirements.txt index 19b5293722..1d92d5e3a7 100644 --- a/bitbake/toaster-requirements.txt +++ b/bitbake/toaster-requirements.txt | |||
@@ -2,3 +2,4 @@ Django==1.6 | |||
2 | South==0.8.4 | 2 | South==0.8.4 |
3 | argparse==1.2.1 | 3 | argparse==1.2.1 |
4 | wsgiref==0.1.2 | 4 | wsgiref==0.1.2 |
5 | filemagic==1.6 | ||