Skip to content

Commit 67b979c

Browse files
committed
Media should not contain text #1347 #1348
Treat all media and .ppm and .pgm in particualr as not containing text (and therefore not scanned for copyrights and licenses) Reported-by: Armijn Hemel @armijnhemel Signed-off-by: Philippe Ombredanne <pombredanne@nexb.com>
1 parent c6c5d64 commit 67b979c

4 files changed

Lines changed: 1552 additions & 35 deletions

File tree

src/typecode/contenttype.py

Lines changed: 57 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
import mimetypes as mimetype_python
3232

3333
import attr
34-
import binaryornot.check
34+
from binaryornot.helpers import get_starting_chunk
35+
from binaryornot.helpers import is_binary_string
3536

3637
from pdfminer.pdfparser import PDFParser
3738
from pdfminer.pdfdocument import PDFDocument
@@ -299,7 +300,7 @@ def is_binary(self):
299300
if self._is_binary is None:
300301
self._is_binary = False
301302
if self.is_file is True:
302-
self._is_binary = binaryornot.check.is_binary(self.location)
303+
self._is_binary = is_binary(self.location)
303304
return self._is_binary
304305

305306
@property
@@ -379,12 +380,20 @@ def is_archive(self):
379380
can_extract = bool(archive.can_extract(self.location))
380381
docx_ext = 'x' if on_linux else u'x'
381382

382-
if (not self.is_text and (
383-
self.is_compressed or 'archive' in ft or can_extract
384-
or self.is_package or self.is_filesystem
383+
if self.is_text:
384+
self._is_archive = False
385+
386+
elif self.filetype_file.lower().startswith('gem image data'):
387+
self._is_archive = False
388+
389+
elif (self.is_compressed
390+
or 'archive' in ft
391+
or can_extract
392+
or self.is_package
393+
or self.is_filesystem
385394
or (self.is_office_doc and self.location.endswith(docx_ext))
386395
# FIXME: is this really correct???
387-
or '(zip)' in ft)):
396+
or '(zip)' in ft):
388397
self._is_archive = True
389398

390399
return self._is_archive
@@ -464,7 +473,7 @@ def is_media(self):
464473
'png image', 'interleaved image', 'microsoft asf', 'image text',
465474
'photoshop image', 'shop pro image', 'ogg data', 'vorbis', 'mpeg',
466475
'theora', 'bitmap', 'audio', 'video', 'sound', 'riff', 'icon',
467-
'pc bitmap', 'image data',
476+
'pc bitmap', 'image data', 'netpbm'
468477
)
469478

470479
if any(m in mt for m in mimes) or any(t in ft for t in types):
@@ -478,10 +487,12 @@ def is_media_with_meta(self):
478487
Return True if the file is a media file that may contain text metadata.
479488
"""
480489
# For now we only exclude PNGs, JEPG and Gifs, though there are likely
481-
# several other
482-
# mp(1,2,3,4), jpeg, gif all have support for metadata but we exclude some
490+
# several other. mp(1,2,3,4), jpeg, gif all have support for metadata
491+
# but we exclude some.
492+
493+
# FIXME: only include types that are known to have metadata
483494
if (self.is_media and self.filetype_file.lower().startswith(
484-
('gif image', 'png image', 'jpeg image'))):
495+
('gif image', 'png image', 'jpeg image', 'netpbm', 'mpeg'))):
485496
return False
486497
else:
487498
return True
@@ -524,18 +535,27 @@ def contains_text(self):
524535
if self._contains_text is None:
525536
if not self.is_file:
526537
self._contains_text = False
538+
539+
elif self.is_media and not self.location.lower().endswith(('.svg')):
540+
# and not self.is_media_with_meta:
541+
self._contains_text = False
542+
527543
elif self.is_text:
528544
self._contains_text = True
545+
529546
elif self.is_pdf and not self.is_pdf_with_text:
530547
self._contains_text = False
548+
531549
elif self.is_compressed:
532550
self._contains_text = False
533-
elif self.is_archive and self.is_compressed:
534-
self._contains_text = False
551+
535552
elif self.is_archive and not self.is_compressed:
536553
self._contains_text = True
537-
elif self.is_media and not self.is_media_with_meta:
538-
self._contains_text = False
554+
555+
# TODO: exclude all binaries??
556+
# elif self.is_binary:
557+
# self._contains_text = False
558+
539559
else:
540560
self._contains_text = True
541561
return self._contains_text
@@ -557,7 +577,7 @@ def is_data(self):
557577
size = self.size
558578
max_entropy = 1.3
559579

560-
if (ft=='data'
580+
if (ft == 'data'
561581
or is_data(self.location)
562582
or ('data' in ft and size > large_file)
563583
or (self.is_text and size > large_text_file)
@@ -690,19 +710,19 @@ def is_java_class(self):
690710

691711
@attr.attributes
692712
class TypeDefinition(object):
693-
name=String(repr=True)
694-
filetypes=List(repr=True)
695-
mimetypes=List(repr=True)
696-
extensions=List(repr=True)
697-
strict=Boolean(repr=True,
713+
name = String(repr=True)
714+
filetypes = List(repr=True)
715+
mimetypes = List(repr=True)
716+
extensions = List(repr=True)
717+
strict = Boolean(repr=True,
698718
help=' if True, all criteria must be matched to select this detector.')
699719

700720

701-
DATA_TYPE_DEFINITIONS =tuple([
721+
DATA_TYPE_DEFINITIONS = tuple([
702722
TypeDefinition(
703723
name='MySQL ARCHIVE Storage Engine data files',
704724
filetypes=('mysql table definition file',),
705-
extensions=('.arm','.arz', '.arn',),
725+
extensions=('.arm', '.arz', '.arn',),
706726
),
707727
])
708728

@@ -756,7 +776,7 @@ def get_pygments_lexer(location):
756776
if T.is_binary:
757777
return
758778
except KeyError:
759-
if binaryornot.check.is_binary(location):
779+
if is_binary(location):
760780
return
761781
try:
762782
# FIXME: Latest Pygments versions should work fine
@@ -793,10 +813,22 @@ def get_filetype(location):
793813

794814
def is_standard_include(location):
795815
"""
796-
Return True if a file path refers to something that looks like a
797-
standard include.
816+
Return True if the `location` file path refers to something that looks like
817+
a standard C/C++ include.
798818
"""
799819
if (location.startswith(STD_INCLUDES) or location.endswith(STD_INCLUDES)):
800820
return True
801821
else:
802822
return False
823+
824+
825+
def is_binary(location):
826+
"""
827+
Retrun True if the file at `location` is a binary file.
828+
"""
829+
known_extensions = (
830+
'.pyc', '.pgm', '.mp3', '.mp4', '.mpeg', '.mpg', '.emf',
831+
'.pgm', '.pbm', '.ppm')
832+
if location.endswith(known_extensions):
833+
return True
834+
return is_binary_string(get_starting_chunk(location))

0 commit comments

Comments
 (0)