def cleanup_incomplete_downloads(): download_dir = os.path.join(app.config.get(prefs.MOVIES_DIRECTORY), 'Incomplete Downloads') if not fileutil.exists(download_dir): return files_in_use = set() for downloader in RemoteDownloader.make_view(): if downloader.get_state() in ('downloading', 'paused', 'offline', 'uploading', 'finished', 'uploading-paused'): filename = downloader.get_filename() if filename: if not fileutil.isabs(filename): filename = os.path.join(download_dir, filename) files_in_use.add(filename) try: entries = fileutil.listdir(download_dir) except OSError: entries = [] for f in entries: f = os.path.join(download_dir, f) if f not in files_in_use: try: if fileutil.isfile(f): fileutil.remove(f) elif fileutil.isdir(f): fileutil.rmtree(f) except OSError: # FIXME - maybe a permissions error? pass
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 stop(self, delete): self.state = "stopped" self._shutdown_torrent() self.update_client() if delete: try: if fileutil.isdir(self.filename): fileutil.rmtree(self.filename) else: fileutil.remove(self.filename) except OSError: pass
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)
def stop(self, delete): """Stops the download and removes the partially downloaded file. """ if self.state == 'finished': if delete: try: if fileutil.isdir(self.filename): fileutil.rmtree(self.filename) else: fileutil.remove(self.filename) except OSError: pass else: # Cancel the request, don't keep around partially # downloaded data self.cancel_request(remove_file=True) self.currentSize = 0 self.state = "stopped" self.update_client()