Skip to content

Commit 25697eb

Browse files
Fix removing old backups (#622)
1 parent d8056bb commit 25697eb

File tree

4 files changed

+21
-14
lines changed

4 files changed

+21
-14
lines changed

lib/inputstreamhelper/kodiutils.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -363,22 +363,25 @@ def open_file(path, flags='r'):
363363
def copy(src, dest):
364364
"""Copy a file (using xbmcvfs)"""
365365
from xbmcvfs import copy as vfscopy
366-
log(2, "Copy file '{src}' to '{dest}'.", src=src, dest=dest)
366+
log(0, "Copy file '{src}' to '{dest}'.", src=src, dest=dest)
367367
return vfscopy(from_unicode(src), from_unicode(dest))
368368

369369

370370
def delete(path):
371371
"""Remove a file (using xbmcvfs)"""
372372
from xbmcvfs import delete as vfsdelete
373-
log(2, "Delete file '{path}'.", path=path)
373+
log(0, "Delete file '{path}'.", path=path)
374374
return vfsdelete(from_unicode(path))
375375

376376

377377
def exists(path):
378378
"""Whether the path exists (using xbmcvfs)"""
379379
# File or folder (folder must end with slash or backslash)
380380
from xbmcvfs import exists as vfsexists
381-
return vfsexists(from_unicode(path))
381+
if vfsexists(from_unicode(path)):
382+
log(0, "Path '{path}' exists.", path=path)
383+
return True
384+
return False
382385

383386

384387
def listdir(path):
@@ -391,14 +394,14 @@ def listdir(path):
391394
def mkdir(path):
392395
"""Create a directory (using xbmcvfs)"""
393396
from xbmcvfs import mkdir as vfsmkdir
394-
log(2, "Create directory '{path}'.", path=path)
397+
log(0, "Create directory '{path}'.", path=path)
395398
return vfsmkdir(from_unicode(path))
396399

397400

398401
def mkdirs(path):
399402
"""Create directory including parents (using xbmcvfs)"""
400403
from xbmcvfs import mkdirs as vfsmkdirs
401-
log(2, "Recursively create directory '{path}'.", path=path)
404+
log(0, "Recursively create directory '{path}'.", path=path)
402405
return vfsmkdirs(from_unicode(path))
403406

404407

lib/inputstreamhelper/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def __eq__(self, other):
4949

5050
def temp_path():
5151
"""Return temporary path, usually ~/.kodi/userdata/addon_data/script.module.inputstreamhelper/temp/"""
52-
tmp_path = translate_path(os.path.join(get_setting('temp_path', 'special://masterprofile/addon_data/script.module.inputstreamhelper'), 'temp'))
52+
tmp_path = translate_path(os.path.join(get_setting('temp_path', 'special://masterprofile/addon_data/script.module.inputstreamhelper'), 'temp', ''))
5353
if not exists(tmp_path):
5454
mkdirs(tmp_path)
5555

@@ -388,7 +388,7 @@ def hardlink(src, dest):
388388
link(compat_path(src), compat_path(dest))
389389
except (AttributeError, OSError, ImportError):
390390
return copy(src, dest)
391-
log(2, "Hardlink file '{src}' to '{dest}'.", src=src, dest=dest)
391+
log(0, "Hardlink file '{src}' to '{dest}'.", src=src, dest=dest)
392392
return True
393393

394394

lib/inputstreamhelper/widevine/widevine.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ def has_widevinecdm():
9797
return True
9898

9999
widevinecdm = widevinecdm_path()
100-
log(3, widevinecdm)
101100
if widevinecdm is None:
102101
return False
103102
if not exists(widevinecdm):
@@ -178,6 +177,7 @@ def latest_widevine_version():
178177
def remove_old_backups(bpath):
179178
"""Removes old Widevine backups, if number of allowed backups is exceeded"""
180179
max_backups = get_setting_int('backups', 4)
180+
to_remove = []
181181
versions = sorted([parse_version(version) for version in listdir(bpath)])
182182

183183
if len(versions) < 2:
@@ -186,13 +186,16 @@ def remove_old_backups(bpath):
186186
try:
187187
installed_version = load_widevine_config()['version']
188188
except TypeError:
189-
log(2, "could not determine installed version. Aborting cleanup of old versions.")
189+
log(2, "Could not determine installed version. Aborting cleanup of old versions.")
190190
return
191191

192-
while len(versions) > max_backups + 1:
193-
remove_version = str(versions[1] if versions[0] == parse_version(installed_version) else versions[0])
194-
log(0, 'Removing oldest backup which is not installed: {version}', version=remove_version)
195-
remove_tree(os.path.join(bpath, remove_version))
196-
versions = sorted([parse_version(version) for version in listdir(bpath)])
192+
filtered = [v for v in versions if v != parse_version(installed_version)]
197193

194+
if len(filtered) > max_backups:
195+
to_remove = filtered[: len(filtered) - max_backups]
196+
197+
for v in to_remove:
198+
remove_version = str(v)
199+
log(2, 'Removing old backup: {version}', version=remove_version)
200+
remove_tree(os.path.join(bpath, remove_version, '')) # ensure trailing separator
198201
return

tests/userdata/addon_settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"disabled": false,
1111
"last_modified": "88888",
1212
"last_check": "",
13+
"backups": 4,
1314
"temp_path": "special://masterprofile/addon_data/script.module.inputstreamhelper",
1415
"update_frequency": "31",
1516
"version": "0.7.0"

0 commit comments

Comments
 (0)