]>
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 ( | |
8a0b9322 | 7 | decodeFilename, |
0202b52a | 8 | encodeFilename, |
9 | make_dir, | |
10 | PostProcessingError, | |
11 | ) | |
0202b52a | 12 | |
13 | ||
14 | class MoveFilesAfterDownloadPP(PostProcessor): | |
15 | ||
56d868db | 16 | def __init__(self, downloader=None, downloaded=True): |
17 | PostProcessor.__init__(self, downloader) | |
18 | self._downloaded = downloaded | |
19 | ||
0202b52a | 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) | |
56d868db | 28 | if self._downloaded: |
29 | info['__files_to_move'][info['filepath']] = decodeFilename(finalpath) | |
0202b52a | 30 | |
8a0b9322 | 31 | make_newfilename = lambda old: decodeFilename(os.path.join(finaldir, os.path.basename(encodeFilename(old)))) |
dcf64d43 | 32 | for oldfile, newfile in info['__files_to_move'].items(): |
0202b52a | 33 | if not newfile: |
8a0b9322 | 34 | newfile = make_newfilename(oldfile) |
0202b52a | 35 | if os.path.abspath(encodeFilename(oldfile)) == os.path.abspath(encodeFilename(newfile)): |
36 | continue | |
98820640 | 37 | if not os.path.exists(encodeFilename(oldfile)): |
38 | self.report_warning('File "%s" cannot be found' % oldfile) | |
39 | continue | |
0202b52a | 40 | if os.path.exists(encodeFilename(newfile)): |
41 | if self.get_param('overwrites', True): | |
42 | self.report_warning('Replacing existing file "%s"' % newfile) | |
de6000d9 | 43 | os.remove(encodeFilename(newfile)) |
0202b52a | 44 | else: |
45 | self.report_warning( | |
46 | 'Cannot move file "%s" out of temporary directory since "%s" already exists. ' | |
47 | % (oldfile, newfile)) | |
48 | continue | |
49 | make_dir(newfile, PostProcessingError) | |
50 | self.to_screen('Moving file "%s" to "%s"' % (oldfile, newfile)) | |
51 | shutil.move(oldfile, newfile) # os.rename cannot move between volumes | |
52 | ||
8a0b9322 | 53 | info['filepath'] = finalpath |
0202b52a | 54 | return [], info |