Return-Path: Received: by yocto-www.yoctoproject.org (Postfix, from userid 118) id 10F45E00546; Wed, 2 Dec 2015 02:47:35 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on yocto-www.yoctoproject.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 X-Spam-HAM-Report: * -5.0 RCVD_IN_DNSWL_HI RBL: Sender listed at http://www.dnswl.org/, high * trust * [192.55.52.88 listed in list.dnswl.org] * -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% * [score: 0.0000] Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by yocto-www.yoctoproject.org (Postfix) with ESMTP id 258DAE00406 for ; Wed, 2 Dec 2015 02:47:33 -0800 (PST) Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmsmga101.fm.intel.com with ESMTP; 02 Dec 2015 02:47:33 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.20,372,1444719600"; d="scan'208";a="6092972" Received: from tpalli-mobl2.ger.corp.intel.com (HELO lp.ger.corp.intel.com) ([10.252.26.112]) by fmsmga004.fm.intel.com with ESMTP; 02 Dec 2015 02:47:31 -0800 From: Elliot Smith To: toaster@yoctoproject.org Date: Wed, 2 Dec 2015 10:47:26 +0000 Message-Id: <1449053246-1075-1-git-send-email-elliot.smith@intel.com> X-Mailer: git-send-email 1.9.3 Subject: [review-request][PATCH][1.8] toaster: Rework mimetype guessing to fix artifact downloads X-BeenThere: toaster@yoctoproject.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: Web based interface for BitBake List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 02 Dec 2015 10:47:35 -0000 Artifact download links were broken because the function to get the mimetype for the artifact was incorrectly using the underlying mimetype library. The function was also attached to the build environment controller, which was unnecessary, as we only support local controllers anyway. Remove the mimetype getter on the build environment and use the one in the view code instead. This prevents the download error from occurring. (Backport of dd957fe0f261db6481882fee0413f459425000c2 and dd957fe0f261db6481882fee0413f459425000c2 from master to Yocto 1.8) [YOCTO #8472] Signed-off-by: Elliot Smith --- bitbake/lib/toaster/bldcontrol/models.py | 34 -------------------------------- bitbake/lib/toaster/toastergui/views.py | 18 ++++++++++++++++- 2 files changed, 17 insertions(+), 35 deletions(-) diff --git a/bitbake/lib/toaster/bldcontrol/models.py b/bitbake/lib/toaster/bldcontrol/models.py index 02cfaf7..770ce40 100644 --- a/bitbake/lib/toaster/bldcontrol/models.py +++ b/bitbake/lib/toaster/bldcontrol/models.py @@ -39,40 +39,6 @@ class BuildEnvironment(models.Model): created = models.DateTimeField(auto_now_add = True) updated = models.DateTimeField(auto_now = True) - - def get_artifact_type(self, path): - if self.betype == BuildEnvironment.TYPE_LOCAL: - try: - import magic - - # fair warning: this is a mess; there are multiple competeing 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" - raise Exception("FIXME: artifact type not implemented for build environment type %s" % be.get_betype_display()) - - def get_artifact(self, path): if self.betype == BuildEnvironment.TYPE_LOCAL: return open(path, "r") diff --git a/bitbake/lib/toaster/toastergui/views.py b/bitbake/lib/toaster/toastergui/views.py index be59c83..98b040e 100755 --- a/bitbake/lib/toaster/toastergui/views.py +++ b/bitbake/lib/toaster/toastergui/views.py @@ -39,6 +39,22 @@ from datetime import timedelta, datetime, date from django.utils import formats from toastergui.templatetags.projecttags import json as jsonfilter import json +import mimetypes + +class MimeTypeFinder(object): + # setting this to False enables additional non-standard mimetypes + # to be included in the guess + _strict = False + + # returns the mimetype for a file path as a string, + # or 'application/octet-stream' if the type couldn't be guessed + @classmethod + def get_mimetype(self, path): + guess = mimetypes.guess_type(path, self._strict) + guessed_type = guess[0] + if guessed_type == None: + guessed_type = 'application/octet-stream' + return guessed_type # all new sessions should come through the landing page; # determine in which mode we are running in, and redirect appropriately @@ -3209,7 +3225,7 @@ if toastermain.settings.MANAGED: if file_name is None: raise Exception("Could not handle artifact %s id %s" % (artifact_type, artifact_id)) else: - content_type = b.buildrequest.environment.get_artifact_type(file_name) + content_type = MimeTypeFinder.get_mimetype(file_name) fsock = b.buildrequest.environment.get_artifact(file_name) file_name = os.path.basename(file_name) # we assume that the build environment system has the same path conventions as host -- Elliot Smith Software Engineer Intel OTC --------------------------------------------------------------------- Intel Corporation (UK) Limited Registered No. 1134945 (England) Registered Office: Pipers Way, Swindon SN3 1RJ VAT No: 860 2173 47 This e-mail and any attachments may contain confidential material for the sole use of the intended recipient(s). Any review or distribution by others is strictly prohibited. If you are not the intended recipient, please contact the sender and delete all copies.