From 45e4b359a0072d3c87bbc71ad7c240f1376f3356 Mon Sep 17 00:00:00 2001 From: Gilles DAVID Date: Fri, 22 Sep 2017 18:20:25 +0200 Subject: [PATCH] Try to use libmagic library to detect file format --- copyright/insert_copyright | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/copyright/insert_copyright b/copyright/insert_copyright index 6f70a36..b08a807 100755 --- a/copyright/insert_copyright +++ b/copyright/insert_copyright @@ -240,14 +240,31 @@ def autodetect_format(filename): 'shell': ('sh', 'bash', 'csh', 'cmake', 'txt', 'cfg', 'ini', 'm4'), 'python': ('py',), } + rev_extensions = {e: k for k, exts in extensions.items() for e in exts} if filename and os.path.isfile(filename): extension = os.path.splitext(filename)[1][1:].lower() if extension in ('in',): name = os.path.splitext(filename)[0] extension = os.path.splitext(name)[1][1:].lower() - for file_format in extensions: - if extension in extensions[file_format]: - return format + if extension in rev_extensions: + return rev_extensions[extension] + + try: + import magic + m = magic.open(magic.MAGIC_MIME_TYPE) + m.load() + file_formats = { + 'cpp': ('text/x-c', 'text/x-c++'), + 'shell': ('text/x-shellscript',), + 'python': ('text/x-python',), + } + rev_file_formats = {f: k for k, ff in file_formats.items() for f in ff} + file_format = m.file(filename) + if file_format in rev_file_formats: + return rev_file_formats[file_format] + except ImportError: + pass + return None -- 2.39.2