Skip to content

Commit cbdd2e8

Browse files
committed
Improve get_lib_version, use as check
1 parent e77df91 commit cbdd2e8

File tree

2 files changed

+37
-21
lines changed

2 files changed

+37
-21
lines changed

lib/inputstreamhelper/__init__.py

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
set_setting, set_setting_bool, textviewer, translate_path, yesno_dialog)
1212
from .utils import arch, http_download, remove_tree, run_cmd, store, system_os, temp_path, unzip
1313
from .widevine.arm import install_widevine_arm, unmount
14-
from .widevine.widevine import (backup_path, has_widevinecdm, ia_cdm_path, install_cdm_from_backup, latest_widevine_version,
14+
from .widevine.widevine import (backup_path, has_widevinecdm, get_lib_version, ia_cdm_path, install_cdm_from_backup, latest_widevine_version,
1515
load_widevine_config, missing_widevine_libs, widevine_config_path, widevine_eula, widevinecdm_path)
1616
from .unicodes import compat_path
1717

@@ -96,18 +96,6 @@ def _inputstream_version(self):
9696
from .unicodes import to_unicode
9797
return to_unicode(addon.getAddonInfo('version'))
9898

99-
@staticmethod
100-
def _get_lib_version(path):
101-
if not path or not exists(path):
102-
return '(Not found)'
103-
import re
104-
with open(compat_path(path), 'rb') as library:
105-
match = re.search(br'[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+', library.read())
106-
if not match:
107-
return '(Undetected)'
108-
from .unicodes import to_unicode
109-
return to_unicode(match.group(0))
110-
11199
def _has_inputstream(self):
112100
"""Checks if selected InputStream add-on is installed."""
113101
data = jsonrpc(method='Addons.GetAddonDetails', params=dict(addonid=self.inputstream_addon))
@@ -195,7 +183,9 @@ def install_and_finish(self, progress, version):
195183
"""Installs the cdm from backup and runs checks"""
196184

197185
progress.update(97, message=localize(30049)) # Installing Widevine CDM
198-
install_cdm_from_backup(version)
186+
if not install_cdm_from_backup(version):
187+
progress.close()
188+
return False
199189

200190
progress.update(98, message=localize(30050)) # Finishing
201191
if has_widevinecdm():
@@ -432,7 +422,7 @@ def info_dialog(self):
432422
wv_updated = strftime('%Y-%m-%d %H:%M', localtime(get_setting_float('last_modified', 0.0)))
433423
else:
434424
wv_updated = 'Never'
435-
text += localize(30821, version=self._get_lib_version(widevinecdm_path()), date=wv_updated) + '\n'
425+
text += localize(30821, version=get_lib_version(widevinecdm_path()), date=wv_updated) + '\n'
436426
if arch() in ('arm', 'arm64'): # Chrome OS version
437427
wv_cfg = load_widevine_config()
438428
if wv_cfg:
@@ -451,7 +441,8 @@ def info_dialog(self):
451441
log(2, '\n{info}'.format(info=kodi_to_ascii(text)))
452442
textviewer(localize(30901), text)
453443

454-
def rollback_libwv(self):
444+
@staticmethod
445+
def rollback_libwv():
455446
"""Rollback lib to a version specified by the user"""
456447
bpath = backup_path()
457448
versions = listdir(bpath)
@@ -470,7 +461,7 @@ def rollback_libwv(self):
470461
show_versions = []
471462

472463
for version in versions:
473-
lib_version = self._get_lib_version(os.path.join(bpath, version, config.WIDEVINE_CDM_FILENAME[system_os()]))
464+
lib_version = get_lib_version(os.path.join(bpath, version, config.WIDEVINE_CDM_FILENAME[system_os()]))
474465
show_versions.append('{} ({})'.format(lib_version, version))
475466

476467
if not show_versions:
@@ -480,7 +471,7 @@ def rollback_libwv(self):
480471
version = select_dialog(localize(30057), show_versions)
481472
if version != -1:
482473
log(0, 'Rollback to version {version}', version=versions[version])
483-
install_cdm_from_backup(versions[version])
484-
notification(localize(30037), localize(30051)) # Success! Widevine successfully installed.
474+
if install_cdm_from_backup(versions[version]):
475+
notification(localize(30037), localize(30051)) # Success! Widevine successfully installed.
485476

486477
return

lib/inputstreamhelper/widevine/widevine.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,23 @@
1414

1515
def install_cdm_from_backup(version):
1616
"""Copies files from specified backup version to cdm dir"""
17-
filenames = listdir(os.path.join(backup_path(), version))
17+
backup_dir = os.path.join(backup_path(), version)
18+
19+
if not get_lib_version(os.path.join(backup_dir, config.WIDEVINE_CDM_FILENAME[system_os()])):
20+
log(4, 'lib version check failed! Aborting installation.')
21+
ok_dialog(localize(30004), localize(30005)) # Error during install, please report
22+
return False
23+
filenames = listdir(backup_dir)
1824

1925
for filename in filenames:
20-
backup_fpath = os.path.join(backup_path(), version, filename)
26+
backup_fpath = os.path.join(backup_dir, filename)
2127
install_fpath = os.path.join(ia_cdm_path(), filename)
2228
hardlink(backup_fpath, install_fpath)
2329

2430
log(0, 'Installed CDM version {version} from backup', version=version)
2531
set_setting('last_modified', time())
2632
remove_old_backups(backup_path())
33+
return True
2734

2835

2936
def widevine_eula():
@@ -118,6 +125,24 @@ def ia_cdm_path():
118125
return cdm_path
119126

120127

128+
def get_lib_version(path):
129+
"""
130+
Determines version of the Widevine library.
131+
Returns empty string if not possible, which might indicate a problematic file/arch mismatch, so this can be used as a check.
132+
"""
133+
from ctypes import CDLL, c_char_p
134+
135+
lib_version = ''
136+
try:
137+
lib = CDLL(compat_path(path))
138+
lib.GetCdmVersion.restype = c_char_p
139+
lib_version = to_unicode(lib.GetCdmVersion())
140+
except (OSError, AttributeError) as exc:
141+
log(4, 'Failed to determine lib version: ' + str(exc))
142+
143+
return lib_version
144+
145+
121146
def missing_widevine_libs():
122147
"""Parses ldd output of libwidevinecdm.so and displays dialog if any depending libraries are missing."""
123148
if system_os() != 'Linux': # this should only be needed for linux

0 commit comments

Comments
 (0)