Skip to content

Commit 78994e6

Browse files
authored
Merge pull request #18 from tardyp/failover
Implement failover with libraries found in the system
2 parents 49af85b + ccb3469 commit 78994e6

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

src/extractcode/libarchive2.py

Lines changed: 13 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.util
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,7 @@
8586
# keys for plugin-provided locations
8687
EXTRACTCODE_LIBARCHIVE_LIBDIR = 'extractcode.libarchive.libdir'
8788
EXTRACTCODE_LIBARCHIVE_DLL = 'extractcode.libarchive.dll'
89+
_LIBRARY_NAME = 'libarchive'
8890

8991

9092
def load_lib():
@@ -96,11 +98,17 @@ def load_lib():
9698
dll = get_location(EXTRACTCODE_LIBARCHIVE_DLL)
9799
libdir = get_location(EXTRACTCODE_LIBARCHIVE_LIBDIR)
98100
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-
)
101+
filepath = ctypes.util.find_library(_LIBRARY_NAME)
102+
libarchive = ctypes.cdll.LoadLibrary(filepath)
103+
if not libarchive:
104+
raise ImportError(
105+
'CRITICAL: libarchive DLL is not installed. '
106+
'Unable to continue: you need to install a valid extractcode-libarchive '
107+
'plugin with a valid libarchive DLL available '
108+
'or to have "libarchive" library installed in your system.'
109+
)
110+
logger.warning('Cannot to use plugin for libarchive, defaulting to system library at ' + filepath)
111+
return libarchive
104112
return command.load_shared_library(dll, libdir)
105113

106114

src/extractcode/sevenzip.py

Lines changed: 11 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,16 @@ 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 not sevenzip:
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+
logger.warning('Cannot to use plugin for libarchive, defaulting to system binary at ' + sevenzip)
93+
return '', sevenzip
8894

8995
return libdir, cmd_loc
9096

0 commit comments

Comments
 (0)