def migrate(self, directory): if app.download_state_manager.get_download(self.dlid): c = command.MigrateDownloadCommand(RemoteDownloader.dldaemon, self.dlid, directory) c.send() else: # downloader doesn't have our dlid. Move the file ourself. short_filename = self.status.get("shortFilename") if not short_filename: logging.warning( "can't migrate download; no shortfilename! URL was %s", self.url) return filename = self.status.get("filename") if not filename: logging.warning( "can't migrate download; no filename! URL was %s", self.url) return if fileutil.exists(filename): if self.status.get('channelName', None) is not None: channelName = filter_directory_name( self.status['channelName']) directory = os.path.join(directory, channelName) if not os.path.exists(directory): try: fileutil.makedirs(directory) except OSError: # FIXME - what about permission issues? pass newfilename = os.path.join(directory, short_filename) if newfilename == filename: return # create a file or directory to serve as a placeholder before # we start to migrate. This helps ensure that the destination # we're migrating too is not already taken. try: is_dir = fileutil.isdir(filename) if is_dir: newfilename = next_free_directory(newfilename) fp = None else: newfilename, fp = next_free_filename(newfilename) fp.close() except ValueError: func = ('next_free_directory' if is_dir else 'next_free_filename') logging.warn('migrate: %s failed. candidate = %r', func, newfilename) else: def callback(): self.status['filename'] = newfilename self.signal_change(needs_signal_item=False) self._file_migrated(filename) fileutil.migrate_file(filename, newfilename, callback) for i in self.item_list: i.migrate_children(directory)
def migrate(self, directory): if app.download_state_manager.get_download(self.dlid): c = command.MigrateDownloadCommand(RemoteDownloader.dldaemon, self.dlid, directory) c.send() else: # downloader doesn't have our dlid. Move the file ourself. short_filename = self.short_filename if not short_filename: logging.warning( "can't migrate download; no shortfilename! URL was %s", self.url) return filename = self.filename if not filename: logging.warning( "can't migrate download; no filename! URL was %s", self.url) return if fileutil.exists(filename): if self.channel_name is not None: channel_name = filter_directory_name(self.channel_name) directory = os.path.join(directory, channel_name) if not os.path.exists(directory): try: fileutil.makedirs(directory) except OSError: # FIXME - what about permission issues? pass newfilename = os.path.join(directory, short_filename) if newfilename == filename: return # create a file or directory to serve as a placeholder before # we start to migrate. This helps ensure that the destination # we're migrating too is not already taken. try: is_dir = fileutil.isdir(filename) if is_dir: newfilename = next_free_directory(newfilename) fp = None else: newfilename, fp = next_free_filename(newfilename) fp.close() except ValueError: func = ('next_free_directory' if is_dir else 'next_free_filename') logging.warn('migrate: %s failed. candidate = %r', func, newfilename) else: def callback(): self.filename = newfilename self.signal_change(needs_signal_item=False) self._file_migrated(filename) fileutil.migrate_file(filename, newfilename, callback) for i in self.item_list: i.migrate_children(directory)
def test_next_free_directory(self): # make a bunch of directories that we should skip over for name in ('foo', 'foo.1', 'foo.2'): path = os.path.join(self.tempdir, name) os.mkdir(path) path = os.path.join(self.tempdir, 'foo') # test we find the a nonexistent file returned_path = download_utils.next_free_directory(path) self.assertEquals(returned_path, os.path.join(self.tempdir, 'foo.3')) # test that we don't create the directory self.assert_(not os.path.exists(returned_path))
def migrate(self, directory): if _downloads.has_key(self.dlid): c = command.MigrateDownloadCommand(RemoteDownloader.dldaemon, self.dlid, directory) c.send() else: # downloader doesn't have our dlid. Move the file ourself. short_filename = self.status.get("shortFilename") if not short_filename: logging.warning( "can't migrate download; no shortfilename! URL was %s", self.url) return filename = self.status.get("filename") if not filename: logging.warning( "can't migrate download; no filename! URL was %s", self.url) return if fileutil.exists(filename): if self.status.get('channelName', None) is not None: channelName = filter_directory_name(self.status['channelName']) directory = os.path.join(directory, channelName) if not os.path.exists(directory): try: fileutil.makedirs(directory) except OSError: # FIXME - what about permission issues? pass newfilename = os.path.join(directory, short_filename) if newfilename == filename: return # create a file or directory to serve as a placeholder before # we start to migrate. This helps ensure that the destination # we're migrating too is not already taken. if fileutil.isdir(filename): newfilename = next_free_directory(newfilename) fp = None else: newfilename, fp = next_free_filename(newfilename) def callback(): self.status['filename'] = newfilename self.signal_change(needs_signal_item=False) self._file_migrated(filename) fileutil.migrate_file(filename, newfilename, callback) if fp is not None: fp.close() # clean up if we called next_free_filename() for i in self.item_list: i.migrate_children(directory)