[Core] Updates to writing and deleting torrentfile

* Reduces the complexity in tm.remove
This commit is contained in:
Calum Lind 2015-10-01 15:14:08 +01:00
parent fb95d0ef58
commit d34705860a
2 changed files with 22 additions and 31 deletions

View File

@ -243,11 +243,7 @@ class Torrent(object):
else: else:
self.set_trackers() self.set_trackers()
self.is_finished = False self.is_finished = False
# Use infohash as fallback. self.filename = filename
if not filename:
self.filename = self.torrent_id
else:
self.filename = filename
self.error_statusmsg = None self.error_statusmsg = None
self.statusmsg = "OK" 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 flags = lt.save_resume_flags_t.flush_disk_cache if flush_disk_cache else 0
self.handle.save_resume_data(flags) 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. """Writes the torrent file to the state dir and optional 'copy of' dir.
Args: Args:
filename (str, optional): The filename of the torrent file.
filedump (str, optional): bencoded filedump of a 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 the user has requested a copy of the torrent be saved elsewhere we need to do that.
if self.config["copy_torrent_file"]: if self.config["copy_torrent_file"]:
if filename is None: if not self.filename:
filename = self.get_name() + ".torrent" self.filename = self.get_name() + ".torrent"
filepath = os.path.join(self.config["torrentfiles_location"], filename) filepath = os.path.join(self.config["torrentfiles_location"], self.filename)
write_file(filepath, filedump) 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""" """Deletes the .torrent file in the state directory in config"""
path = os.path.join(get_config_dir(), "state", self.torrent_id + ".torrent") torrent_files = [os.path.join(get_config_dir(), "state", self.torrent_id + ".torrent")]
log.debug("Deleting torrent file: %s", path) if delete_copies:
try: torrent_files.append(os.path.join(self.config["torrentfiles_location"], self.filename))
os.remove(path)
except OSError as ex: for torrent_file in torrent_files:
log.warning("Unable to delete the torrent file: %s", ex) 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): def force_reannounce(self):
"""Force a tracker reannounce""" """Force a tracker reannounce"""

View File

@ -437,7 +437,7 @@ class TorrentManager(component.Component):
# Write the .torrent file to the state directory. # Write the .torrent file to the state directory.
if filedump: if filedump:
torrent.write_torrentfile(filename, filedump) torrent.write_torrentfile(filedump)
# Save the session state. # Save the session state.
if save_state: if save_state:
@ -465,6 +465,8 @@ class TorrentManager(component.Component):
except KeyError: except KeyError:
raise InvalidTorrentError("torrent_id '%s' not in session." % torrent_id) raise InvalidTorrentError("torrent_id '%s' not in session." % torrent_id)
torrent_name = torrent.get_status(["name"])["name"]
# Emit the signal to the clients # Emit the signal to the clients
component.get("EventManager").emit(PreTorrentRemovedEvent(torrent_id)) component.get("EventManager").emit(PreTorrentRemovedEvent(torrent_id))
@ -477,19 +479,9 @@ class TorrentManager(component.Component):
# Remove fastresume data if it is exists # Remove fastresume data if it is exists
self.resume_data.pop(torrent_id, None) self.resume_data.pop(torrent_id, None)
# Remove the .torrent file in the state # Remove the .torrent file in the state and copy location, if user requested.
torrent.delete_torrentfile() delete_copies = self.config["copy_torrent_file"] and self.config["del_copy_torrent_file"]
torrent.delete_torrentfile(delete_copies)
# 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 from set if it wasn't finished # Remove from set if it wasn't finished
if not torrent.is_finished: if not torrent.is_finished: