]>
Commit | Line | Data |
---|---|---|
0202b52a | 1 | import os |
0202b52a | 2 | |
3 | from .common import PostProcessor | |
fbb0ee77 | 4 | from ..compat import shutil |
0202b52a | 5 | from ..utils import ( |
f8271158 | 6 | PostProcessingError, |
8a0b9322 | 7 | decodeFilename, |
0202b52a | 8 | encodeFilename, |
9 | make_dir, | |
0202b52a | 10 | ) |
0202b52a | 11 | |
12 | ||
13 | class MoveFilesAfterDownloadPP(PostProcessor): | |
14 | ||
56d868db | 15 | def __init__(self, downloader=None, downloaded=True): |
16 | PostProcessor.__init__(self, downloader) | |
17 | self._downloaded = downloaded | |
18 | ||
0202b52a | 19 | @classmethod |
20 | def pp_key(cls): | |
21 | return 'MoveFiles' | |
22 | ||
23 | def run(self, info): | |
c571435f | 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) | |
56d868db | 27 | if self._downloaded: |
28 | info['__files_to_move'][info['filepath']] = decodeFilename(finalpath) | |
0202b52a | 29 | |
8a0b9322 | 30 | make_newfilename = lambda old: decodeFilename(os.path.join(finaldir, os.path.basename(encodeFilename(old)))) |
dcf64d43 | 31 | for oldfile, newfile in info['__files_to_move'].items(): |
0202b52a | 32 | if not newfile: |
8a0b9322 | 33 | newfile = make_newfilename(oldfile) |
0202b52a | 34 | if os.path.abspath(encodeFilename(oldfile)) == os.path.abspath(encodeFilename(newfile)): |
35 | continue | |
98820640 | 36 | if not os.path.exists(encodeFilename(oldfile)): |
37 | self.report_warning('File "%s" cannot be found' % oldfile) | |
38 | continue | |
0202b52a | 39 | if os.path.exists(encodeFilename(newfile)): |
40 | if self.get_param('overwrites', True): | |
41 | self.report_warning('Replacing existing file "%s"' % newfile) | |
de6000d9 | 42 | os.remove(encodeFilename(newfile)) |
0202b52a | 43 | else: |
44 | self.report_warning( | |
45 | 'Cannot move file "%s" out of temporary directory since "%s" already exists. ' | |
46 | % (oldfile, newfile)) | |
47 | continue | |
48 | make_dir(newfile, PostProcessingError) | |
86e5f3ed | 49 | self.to_screen(f'Moving file "{oldfile}" to "{newfile}"') |
0202b52a | 50 | shutil.move(oldfile, newfile) # os.rename cannot move between volumes |
51 | ||
8a0b9322 | 52 | info['filepath'] = finalpath |
0202b52a | 53 | return [], info |