1
0
mirror of https://gitlab.com/fdroid/fdroidserver.git synced 2024-10-05 18:50:09 +02:00

Only do the magic import/except dance once

No need to do it for every file, once per app is enough
This commit is contained in:
Daniel Martí 2015-08-18 23:46:35 -07:00
parent 41fd1e86af
commit 46b1f09de3

View File

@ -1440,7 +1440,7 @@ def getpaths(build_dir, build, field):
return paths return paths
def get_mime_type(path): def init_mime_type():
''' '''
There are two incompatible versions of the 'magic' module, one There are two incompatible versions of the 'magic' module, one
that comes as part of libmagic, which is what Debian includes as that comes as part of libmagic, which is what Debian includes as
@ -1452,25 +1452,49 @@ def get_mime_type(path):
libmagic. Hence this function with the following hacks: libmagic. Hence this function with the following hacks:
''' '''
init_path = ''
method = ''
ms = None ms = None
def mime_from_file(path):
try:
return magic.from_file(path, mime=True)
except UnicodeError:
return None
def mime_file(path):
try:
return ms.file(path)
except UnicodeError:
return None
def mime_guess_type(path):
return mimetypes.guess_type(path, strict=False)
try: try:
import magic import magic
try: try:
ms = magic.open(magic.MIME_TYPE) ms = magic.open(magic.MIME_TYPE)
ms.load() ms.load()
result = magic.from_file(path, mime=True) magic.from_file(init_path, mime=True)
method = 'from_file'
except AttributeError: except AttributeError:
result = ms.file(path) ms.file(init_path)
except UnicodeError: method = 'file'
logging.warn('Found malformed magic number at %s' % path)
result = None
except ImportError: except ImportError:
import mimetypes import mimetypes
mimetypes.init() mimetypes.init()
result = mimetypes.guess_type(path, strict=False) method = 'guess_type'
if ms is not None:
ms.close() logging.info("Using magic method " + method)
return result if method == 'from_file':
return mime_from_file
if method == 'file':
return mime_file
if method == 'guess_type':
return mime_guess_type
logging.critical("unknown magic method!")
# Scan the source code in the given directory (and all subdirectories) # Scan the source code in the given directory (and all subdirectories)
@ -1538,6 +1562,8 @@ def scan_source(build_dir, root_dir, thisbuild):
logging.error('Found %s at %s' % (what, fd)) logging.error('Found %s at %s' % (what, fd))
return 1 return 1
get_mime_type = init_mime_type()
# Iterate through all files in the source code # Iterate through all files in the source code
for r, d, f in os.walk(build_dir, topdown=True): for r, d, f in os.walk(build_dir, topdown=True):