22# Copyright (c) nexB Inc. and others.
33# Redistribution and use in source and binary forms, with or without modification,
44# are permitted provided that the following conditions are met:
5- #
5+ #
66# Redistributions of source code must retain the above copyright notice, this list
77# of conditions and the following disclaimer.
8- #
8+ #
99# Redistributions in binary form must reproduce the above copyright notice, this
1010# list of conditions and the following disclaimer in the documentation and/or
1111# other materials provided with the distribution.
12- #
12+ #
1313# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
1414# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
1515# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
2323
2424
2525import platform
26- from os import environ
2726from os import path
2827
2928from plugincode .location_provider import LocationProviderPlugin
@@ -36,38 +35,54 @@ def get_locations(self):
3635 locations of the libmagic shared library as installed on various Linux
3736 distros or on FreeBSD.
3837 """
39-
40- system_arch = platform .machine ()
4138 mainstream_system = platform .system ().lower ()
4239 if mainstream_system == 'linux' :
40+ system_arch = platform .machine ()
4341 distribution = platform .linux_distribution ()[0 ].lower ()
4442 debian_based_distro = ['ubuntu' , 'mint' , 'debian' ]
4543 rpm_based_distro = ['fedora' , 'redhat' ]
4644
4745 if distribution in debian_based_distro :
4846 db_dir = '/usr/lib/file'
4947 lib_dir = '/usr/lib/' + system_arch + '-linux-gnu'
50-
5148 elif distribution in rpm_based_distro :
5249 db_dir = '/usr/share/misc'
5350 lib_dir = '/usr/lib64'
54-
5551 else :
5652 raise Exception ('Unsupported system: {}' .format (distribution ))
5753
58- dll_loc = path .join (lib_dir , 'libmagic.so' )
59-
54+ dll_loc = path .join (lib_dir , 'libmagic.so.1' )
6055 elif mainstream_system == 'freebsd' :
61- if path .isdir ('/usr/local/' ):
62- lib_dir = '/usr/local'
63- else :
64- lib_dir = '/usr'
65-
66- dll_loc = path .join (lib_dir , 'lib/libmagic.so' )
67- db_dir = path .join (lib_dir ,'share/file' )
56+ dll_loc = ''
57+ db_dir = ''
58+ for lib_dir in ('/usr/local/' , '/usr' ):
59+ possible_dll_loc = path .join (lib_dir , 'lib/libmagic.so' )
60+ possible_db_loc = path .join (lib_dir , 'share/misc/magic.mgc' )
61+ if path .exists (possible_dll_loc ) and path .exists (possible_db_loc ):
62+ dll_loc = possible_dll_loc
63+ db_dir = path .dirname (possible_db_loc )
64+ break
65+ elif mainstream_system == 'darwin' :
66+ # This assumes that libmagic was installed using Homebrew
67+ lib_dir = '/opt/homebrew'
68+ dll_loc = path .join (lib_dir , 'lib/libmagic.dylib' )
69+ db_dir = path .join (lib_dir , 'share/misc' )
6870
6971 magicdb_loc = path .join (db_dir , 'magic.mgc' )
7072
73+ # Check that paths exist
74+ if not path .exists (dll_loc ):
75+ raise Exception (
76+ 'libmagic not found. Please refer to the scancode-toolkit '
77+ 'documentation on how to install libmagic for your operating system.'
78+ )
79+
80+ if not path .exists (magicdb_loc ):
81+ raise Exception (
82+ 'magic.mgc not found. Please refer to the scancode-toolkit '
83+ 'documentation on how to install libmagic for your system.'
84+ )
85+
7186 locations = {
7287 # typecode.libmagic.libdir is not used anymore and deprecated
7388 # but we are keeping it around for now for backward compatibility
0 commit comments