3 from .common
import PostProcessor
4 from ..compat
import shutil
13 class MoveFilesAfterDownloadPP(PostProcessor
):
15 def __init__(self
, downloader
=None, downloaded
=True):
16 PostProcessor
.__init
__(self
, downloader
)
17 self
._downloaded
= downloaded
24 dl_path
, dl_name
= os
.path
.split(encodeFilename(info
['filepath']))
25 finaldir
= info
.get('__finaldir', dl_path
)
26 finalpath
= os
.path
.join(finaldir
, dl_name
)
28 info
['__files_to_move'][info
['filepath']] = decodeFilename(finalpath
)
30 make_newfilename
= lambda old
: decodeFilename(os
.path
.join(finaldir
, os
.path
.basename(encodeFilename(old
))))
31 for oldfile
, newfile
in info
['__files_to_move'].items():
33 newfile
= make_newfilename(oldfile
)
34 if os
.path
.abspath(encodeFilename(oldfile
)) == os
.path
.abspath(encodeFilename(newfile
)):
36 if not os
.path
.exists(encodeFilename(oldfile
)):
37 self
.report_warning(f
'File "{oldfile}" cannot be found')
39 if os
.path
.exists(encodeFilename(newfile
)):
40 if self
.get_param('overwrites', True):
41 self
.report_warning(f
'Replacing existing file "{newfile}"')
42 os
.remove(encodeFilename(newfile
))
45 f
'Cannot move file "{oldfile}" out of temporary directory since "{newfile}" already exists. ')
47 make_dir(newfile
, PostProcessingError
)
48 self
.to_screen(f
'Moving file "{oldfile}" to "{newfile}"')
49 shutil
.move(oldfile
, newfile
) # os.rename cannot move between volumes
51 info
['filepath'] = finalpath