]>
Commit | Line | Data |
---|---|---|
0202b52a | 1 | from __future__ import unicode_literals |
2 | import os | |
3 | import shutil | |
4 | ||
5 | from .common import PostProcessor | |
6 | from ..utils import ( | |
7 | encodeFilename, | |
8 | make_dir, | |
9 | PostProcessingError, | |
10 | ) | |
11 | from ..compat import compat_str | |
12 | ||
13 | ||
14 | class MoveFilesAfterDownloadPP(PostProcessor): | |
15 | ||
16 | def __init__(self, downloader, files_to_move): | |
17 | PostProcessor.__init__(self, downloader) | |
18 | self.files_to_move = files_to_move | |
19 | ||
20 | @classmethod | |
21 | def pp_key(cls): | |
22 | return 'MoveFiles' | |
23 | ||
24 | def run(self, info): | |
c571435f | 25 | dl_path, dl_name = os.path.split(encodeFilename(info['filepath'])) |
26 | finaldir = info.get('__finaldir', dl_path) | |
27 | finalpath = os.path.join(finaldir, dl_name) | |
28 | self.files_to_move[info['filepath']] = finalpath | |
0202b52a | 29 | |
30 | for oldfile, newfile in self.files_to_move.items(): | |
31 | if not os.path.exists(encodeFilename(oldfile)): | |
32 | self.report_warning('File "%s" cannot be found' % oldfile) | |
33 | continue | |
34 | if not newfile: | |
c571435f | 35 | newfile = os.path.join(finaldir, os.path.basename(encodeFilename(oldfile))) |
36 | oldfile, newfile = compat_str(oldfile), compat_str(newfile) | |
0202b52a | 37 | if os.path.abspath(encodeFilename(oldfile)) == os.path.abspath(encodeFilename(newfile)): |
38 | continue | |
39 | if os.path.exists(encodeFilename(newfile)): | |
40 | if self.get_param('overwrites', True): | |
41 | self.report_warning('Replacing existing file "%s"' % newfile) | |
42 | os.path.remove(encodeFilename(newfile)) | |
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) | |
49 | self.to_screen('Moving file "%s" to "%s"' % (oldfile, newfile)) | |
50 | shutil.move(oldfile, newfile) # os.rename cannot move between volumes | |
51 | ||
c571435f | 52 | info['filepath'] = compat_str(finalpath) |
0202b52a | 53 | return [], info |