From d34705860af0e4e9f1902cf43a5fe7a5838ad096 Mon Sep 17 00:00:00 2001 From: Calum Lind Date: Thu, 1 Oct 2015 15:14:08 +0100 Subject: [PATCH] [Core] Updates to writing and deleting torrentfile * Reduces the complexity in tm.remove --- deluge/core/torrent.py | 33 ++++++++++++++++----------------- deluge/core/torrentmanager.py | 20 ++++++-------------- 2 files changed, 22 insertions(+), 31 deletions(-) diff --git a/deluge/core/torrent.py b/deluge/core/torrent.py index 8d28e3c4b..32bf03197 100644 --- a/deluge/core/torrent.py +++ b/deluge/core/torrent.py @@ -243,11 +243,7 @@ class Torrent(object): else: self.set_trackers() self.is_finished = False - # Use infohash as fallback. - if not filename: - self.filename = self.torrent_id - else: - self.filename = filename + self.filename = filename self.error_statusmsg = None self.statusmsg = "OK" @@ -1116,11 +1112,10 @@ class Torrent(object): flags = lt.save_resume_flags_t.flush_disk_cache if flush_disk_cache else 0 self.handle.save_resume_data(flags) - def write_torrentfile(self, filename=None, filedump=None): + def write_torrentfile(self, filedump=None): """Writes the torrent file to the state dir and optional 'copy of' dir. Args: - filename (str, optional): The filename of the torrent file. filedump (str, optional): bencoded filedump of a torrent file. """ @@ -1144,19 +1139,23 @@ class Torrent(object): # If the user has requested a copy of the torrent be saved elsewhere we need to do that. if self.config["copy_torrent_file"]: - if filename is None: - filename = self.get_name() + ".torrent" - filepath = os.path.join(self.config["torrentfiles_location"], filename) + if not self.filename: + self.filename = self.get_name() + ".torrent" + filepath = os.path.join(self.config["torrentfiles_location"], self.filename) write_file(filepath, filedump) - def delete_torrentfile(self): + def delete_torrentfile(self, delete_copies=False): """Deletes the .torrent file in the state directory in config""" - path = os.path.join(get_config_dir(), "state", self.torrent_id + ".torrent") - log.debug("Deleting torrent file: %s", path) - try: - os.remove(path) - except OSError as ex: - log.warning("Unable to delete the torrent file: %s", ex) + torrent_files = [os.path.join(get_config_dir(), "state", self.torrent_id + ".torrent")] + if delete_copies: + torrent_files.append(os.path.join(self.config["torrentfiles_location"], self.filename)) + + for torrent_file in torrent_files: + log.debug("Deleting torrent file: %s", torrent_file) + try: + os.remove(torrent_file) + except OSError as ex: + log.warning("Unable to delete the torrent file: %s", ex) def force_reannounce(self): """Force a tracker reannounce""" diff --git a/deluge/core/torrentmanager.py b/deluge/core/torrentmanager.py index f6bdcb4c2..433d165d3 100644 --- a/deluge/core/torrentmanager.py +++ b/deluge/core/torrentmanager.py @@ -437,7 +437,7 @@ class TorrentManager(component.Component): # Write the .torrent file to the state directory. if filedump: - torrent.write_torrentfile(filename, filedump) + torrent.write_torrentfile(filedump) # Save the session state. if save_state: @@ -465,6 +465,8 @@ class TorrentManager(component.Component): except KeyError: raise InvalidTorrentError("torrent_id '%s' not in session." % torrent_id) + torrent_name = torrent.get_status(["name"])["name"] + # Emit the signal to the clients component.get("EventManager").emit(PreTorrentRemovedEvent(torrent_id)) @@ -477,19 +479,9 @@ class TorrentManager(component.Component): # Remove fastresume data if it is exists self.resume_data.pop(torrent_id, None) - # Remove the .torrent file in the state - torrent.delete_torrentfile() - - # Remove the torrent file from the user specified directory - torrent_name = torrent.get_status(["name"])["name"] - filename = torrent.filename - if self.config["copy_torrent_file"] and self.config["del_copy_torrent_file"] and filename: - users_torrent_file = os.path.join(self.config["torrentfiles_location"], filename) - log.info("Delete user's torrent file: %s", users_torrent_file) - try: - os.remove(users_torrent_file) - except OSError as ex: - log.warning("Unable to remove copy torrent file: %s", ex) + # Remove the .torrent file in the state and copy location, if user requested. + delete_copies = self.config["copy_torrent_file"] and self.config["del_copy_torrent_file"] + torrent.delete_torrentfile(delete_copies) # Remove from set if it wasn't finished if not torrent.is_finished: