Skip to content

Commit 54790d1

Browse files
libmagic-sys-provided: let user override paths
let the user override assumed paths via environment. TYPECODE_LIBMAGIC_PATH environment variable can be used to explicitly set the path to libmagic.so. If not specified it falls back to previously used calculation from distro data. Location of the magic database can be specified via TYPECODE_LIBMAGIC_DB_PATH environment variable. If not found it falls back to the previous bevahior. With this patch it possible to run scancode is buildsystems like YOCTO, which heaviily rely on overriding paths via environment to pick the correct implementation from the build workspace Signed-off-by: Konrad Weihmann <kweihmann@outlook.com>
1 parent 0badaa8 commit 54790d1

2 files changed

Lines changed: 41 additions & 25 deletions

File tree

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
1-
A ScanCode Toolkit plugin to use pre-installed system binary libraries and utilities.
1+
A ScanCode Toolkit plugin to use pre-installed libmagic library and data file
2+
=============================================================================
3+
4+
the path of libmagic is either determined by distro data or explicitly
5+
taken from ``TYPECODE_LIBMAGIC_PATH`` environment variable. additionally
6+
the path to ``magic.mgc`` file can be explicitly set by using
7+
``TYPECODE_LIBMAGIC_DB_PATH`` environment variable, otherwise it is
8+
calculated from distro data

builtins/typecode_libmagic_system_provided/src/typecode_libmagic/__init__.py

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@
2222
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2323

2424

25-
from os import path
2625
import platform
26+
from os import environ
27+
from os import path
2728

2829
from plugincode.location_provider import LocationProviderPlugin
2930

@@ -35,38 +36,46 @@ def get_locations(self):
3536
locations of the libmagic shared library as installed on various Linux
3637
distros or on FreeBSD.
3738
"""
38-
system_arch = platform.machine()
39-
mainstream_system = platform.system().lower()
40-
if mainstream_system == 'linux':
41-
distribution = platform.linux_distribution()[0].lower()
42-
debian_based_distro = ['ubuntu', 'mint', 'debian']
43-
rpm_based_distro = ['fedora', 'redhat']
39+
lib_dll = environ.get('TYPECODE_LIBMAGIC_PATH')
40+
if not lib_dll:
41+
system_arch = platform.machine()
42+
mainstream_system = platform.system().lower()
43+
if mainstream_system == 'linux':
44+
distribution = platform.linux_distribution()[0].lower()
45+
debian_based_distro = ['ubuntu', 'mint', 'debian']
46+
rpm_based_distro = ['fedora', 'redhat']
47+
48+
if distribution in debian_based_distro:
49+
data_dir = '/usr/lib/file'
50+
lib_dir = '/usr/lib/'+system_arch+'-linux-gnu'
4451

45-
if distribution in debian_based_distro:
46-
data_dir = '/usr/lib/file'
47-
lib_dir = '/usr/lib/'+system_arch+'-linux-gnu'
52+
elif distribution in rpm_based_distro:
53+
data_dir = '/usr/share/misc'
54+
lib_dir = '/usr/lib64'
4855

49-
elif distribution in rpm_based_distro:
50-
data_dir = '/usr/share/misc'
51-
lib_dir = '/usr/lib64'
56+
else:
57+
raise Exception('Unsupported system: {}'.format(distribution))
5258

53-
else:
54-
raise Exception('Unsupported system: {}'.format(distribution))
59+
lib_dll = path.join(lib_dir, 'libmagic.so')
5560

56-
lib_dll = path.join(lib_dir, 'libmagic.so');
61+
elif mainstream_system == 'freebsd':
62+
if path.isdir('/usr/local/'):
63+
lib_dir = '/usr/local'
64+
else:
65+
lib_dir = '/usr'
5766

58-
elif mainstream_system == 'freebsd':
59-
if path.isdir('/usr/local/'):
60-
lib_dir = '/usr/local'
61-
else:
62-
lib_dir = '/usr'
67+
lib_dll = path.join(lib_dir, 'lib/libmagic.so')
68+
data_dir = path.join(lib_dir,'share/file')
69+
else:
70+
lib_dir = path.dirname(lib_dll)
6371

64-
lib_dll = path.join(lib_dir, 'lib/libmagic.so')
65-
data_dir = path.join(lib_dir,'share/file')
72+
file_magic_data = environ.get('TYPECODE_LIBMAGIC_DB_PATH')
73+
if not file_magic_data:
74+
file_magic_data = path.join(data_dir, 'magic.mgc')
6675

6776
locations = {
6877
'typecode.libmagic.libdir': lib_dir,
6978
'typecode.libmagic.dll': lib_dll,
70-
'typecode.libmagic.db': path.join(data_dir, 'magic.mgc'),
79+
'typecode.libmagic.db': file_magic_data,
7180
}
7281
return locations

0 commit comments

Comments
 (0)