2021-01-23 12:18:12 +00:00
|
|
|
import os
|
|
|
|
|
|
|
|
from .common import PostProcessor
|
2022-11-07 15:24:30 +00:00
|
|
|
from ..compat import shutil
|
2021-01-23 12:18:12 +00:00
|
|
|
from ..utils import (
|
2022-04-11 22:32:57 +00:00
|
|
|
PostProcessingError,
|
2021-02-07 11:17:48 +00:00
|
|
|
decodeFilename,
|
2021-01-23 12:18:12 +00:00
|
|
|
encodeFilename,
|
|
|
|
make_dir,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class MoveFilesAfterDownloadPP(PostProcessor):
|
|
|
|
|
2021-04-10 22:18:07 +00:00
|
|
|
def __init__(self, downloader=None, downloaded=True):
|
|
|
|
PostProcessor.__init__(self, downloader)
|
|
|
|
self._downloaded = downloaded
|
|
|
|
|
2021-01-23 12:18:12 +00:00
|
|
|
@classmethod
|
|
|
|
def pp_key(cls):
|
|
|
|
return 'MoveFiles'
|
|
|
|
|
|
|
|
def run(self, info):
|
2021-01-23 15:25:45 +00: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-04-10 22:18:07 +00:00
|
|
|
if self._downloaded:
|
|
|
|
info['__files_to_move'][info['filepath']] = decodeFilename(finalpath)
|
2021-01-23 12:18:12 +00:00
|
|
|
|
2021-02-07 11:17:48 +00:00
|
|
|
make_newfilename = lambda old: decodeFilename(os.path.join(finaldir, os.path.basename(encodeFilename(old))))
|
2021-03-18 15:24:53 +00:00
|
|
|
for oldfile, newfile in info['__files_to_move'].items():
|
2021-01-23 12:18:12 +00:00
|
|
|
if not newfile:
|
2021-02-07 11:17:48 +00:00
|
|
|
newfile = make_newfilename(oldfile)
|
2021-01-23 12:18:12 +00:00
|
|
|
if os.path.abspath(encodeFilename(oldfile)) == os.path.abspath(encodeFilename(newfile)):
|
|
|
|
continue
|
2021-01-26 10:23:07 +00:00
|
|
|
if not os.path.exists(encodeFilename(oldfile)):
|
|
|
|
self.report_warning('File "%s" cannot be found' % oldfile)
|
|
|
|
continue
|
2021-01-23 12:18:12 +00:00
|
|
|
if os.path.exists(encodeFilename(newfile)):
|
|
|
|
if self.get_param('overwrites', True):
|
|
|
|
self.report_warning('Replacing existing file "%s"' % newfile)
|
2021-02-03 13:36:09 +00:00
|
|
|
os.remove(encodeFilename(newfile))
|
2021-01-23 12:18:12 +00:00
|
|
|
else:
|
|
|
|
self.report_warning(
|
|
|
|
'Cannot move file "%s" out of temporary directory since "%s" already exists. '
|
|
|
|
% (oldfile, newfile))
|
|
|
|
continue
|
|
|
|
make_dir(newfile, PostProcessingError)
|
2022-04-11 15:10:28 +00:00
|
|
|
self.to_screen(f'Moving file "{oldfile}" to "{newfile}"')
|
2021-01-23 12:18:12 +00:00
|
|
|
shutil.move(oldfile, newfile) # os.rename cannot move between volumes
|
|
|
|
|
2021-02-07 11:17:48 +00:00
|
|
|
info['filepath'] = finalpath
|
2021-01-23 12:18:12 +00:00
|
|
|
return [], info
|