2021-01-23 13:18:12 +01:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
|
|
|
|
from .common import PostProcessor
|
|
|
|
from ..utils import (
|
|
|
|
encodeFilename,
|
|
|
|
make_dir,
|
|
|
|
PostProcessingError,
|
|
|
|
)
|
|
|
|
from ..compat import compat_str
|
|
|
|
|
|
|
|
|
|
|
|
class MoveFilesAfterDownloadPP(PostProcessor):
|
|
|
|
|
|
|
|
def __init__(self, downloader, files_to_move):
|
|
|
|
PostProcessor.__init__(self, downloader)
|
|
|
|
self.files_to_move = files_to_move
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def pp_key(cls):
|
|
|
|
return 'MoveFiles'
|
|
|
|
|
|
|
|
def run(self, info):
|
2021-01-23 16:25:45 +01:00
|
|
|
dl_path, dl_name = os.path.split(encodeFilename(info['filepath']))
|
|
|
|
finaldir = info.get('__finaldir', dl_path)
|
|
|
|
finalpath = os.path.join(finaldir, dl_name)
|
2021-02-03 14:36:09 +01:00
|
|
|
self.files_to_move.update(info['__files_to_move'])
|
2021-01-23 16:25:45 +01:00
|
|
|
self.files_to_move[info['filepath']] = finalpath
|
2021-01-23 13:18:12 +01:00
|
|
|
|
|
|
|
for oldfile, newfile in self.files_to_move.items():
|
|
|
|
if not newfile:
|
2021-01-23 16:25:45 +01:00
|
|
|
newfile = os.path.join(finaldir, os.path.basename(encodeFilename(oldfile)))
|
|
|
|
oldfile, newfile = compat_str(oldfile), compat_str(newfile)
|
2021-01-23 13:18:12 +01:00
|
|
|
if os.path.abspath(encodeFilename(oldfile)) == os.path.abspath(encodeFilename(newfile)):
|
|
|
|
continue
|
2021-01-26 11:23:07 +01:00
|
|
|
if not os.path.exists(encodeFilename(oldfile)):
|
|
|
|
self.report_warning('File "%s" cannot be found' % oldfile)
|
|
|
|
continue
|
2021-01-23 13:18:12 +01:00
|
|
|
if os.path.exists(encodeFilename(newfile)):
|
|
|
|
if self.get_param('overwrites', True):
|
|
|
|
self.report_warning('Replacing existing file "%s"' % newfile)
|
2021-02-03 14:36:09 +01:00
|
|
|
os.remove(encodeFilename(newfile))
|
2021-01-23 13:18:12 +01:00
|
|
|
else:
|
|
|
|
self.report_warning(
|
|
|
|
'Cannot move file "%s" out of temporary directory since "%s" already exists. '
|
|
|
|
% (oldfile, newfile))
|
|
|
|
continue
|
|
|
|
make_dir(newfile, PostProcessingError)
|
|
|
|
self.to_screen('Moving file "%s" to "%s"' % (oldfile, newfile))
|
|
|
|
shutil.move(oldfile, newfile) # os.rename cannot move between volumes
|
|
|
|
|
2021-01-23 16:25:45 +01:00
|
|
|
info['filepath'] = compat_str(finalpath)
|
2021-01-23 13:18:12 +01:00
|
|
|
return [], info
|