Skip to content

Commit 622c3b5

Browse files
committed
Implement failover with libraries found in the system
This allows scancode to install and run in systems not supported by extractcode-libarchive and extractcode-7zip Signed-off-by: Pierre Tardy <pierre.tardy@renault.com>
1 parent 49af85b commit 622c3b5

2 files changed

Lines changed: 25 additions & 10 deletions

File tree

src/extractcode/libarchive2.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import mmap
2525
import os
2626

27+
import ctypes
2728
from ctypes import c_char_p, c_wchar_p
2829
from ctypes import c_int, c_longlong
2930
from ctypes import c_size_t, c_ssize_t
@@ -85,6 +86,10 @@
8586
# keys for plugin-provided locations
8687
EXTRACTCODE_LIBARCHIVE_LIBDIR = 'extractcode.libarchive.libdir'
8788
EXTRACTCODE_LIBARCHIVE_DLL = 'extractcode.libarchive.dll'
89+
_LIBRARY_NAME = "libarchive"
90+
91+
def load_lib_failover():
92+
pass
8893

8994

9095
def load_lib():
@@ -96,11 +101,16 @@ def load_lib():
96101
dll = get_location(EXTRACTCODE_LIBARCHIVE_DLL)
97102
libdir = get_location(EXTRACTCODE_LIBARCHIVE_LIBDIR)
98103
if not (dll and libdir) or not os.path.isfile(dll) or not os.path.isdir(libdir):
99-
raise Exception(
100-
'CRITICAL: libarchive DLL is not installed. '
101-
'Unable to continue: you need to install a valid extractcode-libarchive '
102-
'plugin with a valid libarchive DLL available.'
103-
)
104+
filepath = ctypes.util.find_library(_LIBRARY_NAME)
105+
libarchive = ctypes.cdll.LoadLibrary(filepath)
106+
if libarchive is None:
107+
raise ImportError(
108+
'CRITICAL: libarchive DLL is not installed. '
109+
'Unable to continue: you need to install a valid extractcode-libarchive '
110+
'plugin with a valid libarchive DLL available '
111+
'or to have "libarchive" library installed in your system.'
112+
)
113+
return libarchive
104114
return command.load_shared_library(dll, libdir)
105115

106116

src/extractcode/sevenzip.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import os
2525
import pprint
2626
import re
27+
from shutil import which
2728

2829
import attr
2930

@@ -80,11 +81,15 @@ def get_bin_locations():
8081
cmd_loc = get_location(EXTRACTCODE_7ZIP_EXE)
8182
libdir = get_location(EXTRACTCODE_7ZIP_LIBDIR)
8283
if not (cmd_loc and libdir) or not os.path.isfile(cmd_loc) or not os.path.isdir(libdir):
83-
raise Exception(
84-
'CRITICAL: 7zip executable is not installed. '
85-
'Unable to continue: you need to install a valid extractcode-7z '
86-
'plugin with a valid executable available.'
87-
)
84+
sevenzip = which("7z")
85+
if sevenzip is None:
86+
raise ImportError(
87+
'CRITICAL: 7zip executable is not installed. '
88+
'Unable to continue: you need to install a valid extractcode-7z '
89+
'plugin with a valid executable available '
90+
'or install p7zip in your system, so that a "7z" program is available on your PATH'
91+
)
92+
return "", sevenzip
8893

8994
return libdir, cmd_loc
9095

0 commit comments

Comments
 (0)