Skip to content

Commit

Permalink
playlist: Work around Windows' Mandatory File Locking on playlist upd…
Browse files Browse the repository at this point in the history
…ates
  • Loading branch information
Holzhaus committed Feb 17, 2019
1 parent d991e2a commit 14149f6
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions beetsplug/playlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ def update_playlist(self, filename, base_dir):
changes = 0
deletions = 0

with tempfile.NamedTemporaryFile(mode='w+b') as tempfp:
with tempfile.NamedTemporaryFile(mode='w+b', delete=False) as tempfp:
new_playlist = tempfp.name
with open(filename, mode='rb') as fp:
for line in fp:
original_path = line.rstrip(b'\r\n')
Expand All @@ -176,9 +177,11 @@ def update_playlist(self, filename, base_dir):
new_path = os.path.relpath(new_path, base_dir)

tempfp.write(line.replace(original_path, new_path))
if changes or deletions:
self._log.info(
'Updated playlist {0} ({1} changes, {2} deletions)'.format(
filename, changes, deletions))
tempfp.flush()
beets.util.copy(tempfp.name, filename, replace=True)

if changes or deletions:
self._log.info(
'Updated playlist {0} ({1} changes, {2} deletions)'.format(
filename, changes, deletions))
tempfp.flush()
beets.util.copy(new_playlist, filename, replace=True)
beets.util.remove(new_playlist)

0 comments on commit 14149f6

Please sign in to comment.