[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:
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"""

View File

@ -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: