2021-01-23 07:18:12 -05:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
|
|
|
|
from .common import PostProcessor
|
|
|
|
from ..utils import (
|
2021-02-07 06:17:48 -05:00
|
|
|
decodeFilename,
|
2021-01-23 07:18:12 -05:00
|
|
|
encodeFilename,
|
|
|
|
make_dir,
|
|
|
|
PostProcessingError,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class MoveFilesAfterDownloadPP(PostProcessor):
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def pp_key(cls):
|
|
|
|
return 'MoveFiles'
|
|
|
|
|
|
|
|
def run(self, info):
|
2021-01-23 10:25:45 -05: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-03-18 11:24:53 -04:00
|
|
|
info['__files_to_move'][info['filepath']] = decodeFilename(finalpath)
|
2021-01-23 07:18:12 -05:00
|
|
|
|
2021-02-07 06:17:48 -05:00
|
|
|
make_newfilename = lambda old: decodeFilename(os.path.join(finaldir, os.path.basename(encodeFilename(old))))
|
2021-03-18 11:24:53 -04:00
|
|
|
for oldfile, newfile in info['__files_to_move'].items():
|
2021-01-23 07:18:12 -05:00
|
|
|
if not newfile:
|
2021-02-07 06:17:48 -05:00
|
|
|
newfile = make_newfilename(oldfile)
|
2021-01-23 07:18:12 -05:00
|
|
|
if os.path.abspath(encodeFilename(oldfile)) == os.path.abspath(encodeFilename(newfile)):
|
|
|
|
continue
|
2021-01-26 05:23:07 -05:00
|
|
|
if not os.path.exists(encodeFilename(oldfile)):
|
|
|
|
self.report_warning('File "%s" cannot be found' % oldfile)
|
|
|
|
continue
|
2021-01-23 07:18:12 -05:00
|
|
|
if os.path.exists(encodeFilename(newfile)):
|
|
|
|
if self.get_param('overwrites', True):
|
|
|
|
self.report_warning('Replacing existing file "%s"' % newfile)
|
2021-02-03 08:36:09 -05:00
|
|
|
os.remove(encodeFilename(newfile))
|
2021-01-23 07:18:12 -05: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-02-07 06:17:48 -05:00
|
|
|
info['filepath'] = finalpath
|
2021-01-23 07:18:12 -05:00
|
|
|
return [], info
|