mirror of
https://git.deluge-torrent.org/deluge
synced 2025-08-09 18:08:39 +00:00
Add support for saving .fastresume files. They are not loaded on add
yet.
This commit is contained in:
parent
0e73700f15
commit
78244649b8
3 changed files with 76 additions and 19 deletions
|
@ -110,6 +110,12 @@ class Core(dbus.service.Object):
|
||||||
# Start the AlertManager
|
# Start the AlertManager
|
||||||
self.alerts = AlertManager(self.session)
|
self.alerts = AlertManager(self.session)
|
||||||
|
|
||||||
|
# Register alert functions
|
||||||
|
self.alerts.register_handler("torrent_finished_alert",
|
||||||
|
self.on_alert_torrent_finished)
|
||||||
|
self.alerts.register_handler("torrent_paused_alert",
|
||||||
|
self.on_alert_torrent_paused)
|
||||||
|
|
||||||
# Register set functions in the Config
|
# Register set functions in the Config
|
||||||
self.config.register_set_function("listen_ports",
|
self.config.register_set_function("listen_ports",
|
||||||
self.on_set_listen_ports)
|
self.on_set_listen_ports)
|
||||||
|
@ -230,15 +236,14 @@ class Core(dbus.service.Object):
|
||||||
in_signature="s", out_signature="")
|
in_signature="s", out_signature="")
|
||||||
def pause_torrent(self, torrent_id):
|
def pause_torrent(self, torrent_id):
|
||||||
log.debug("Pausing torrent %s", torrent_id)
|
log.debug("Pausing torrent %s", torrent_id)
|
||||||
if self.torrents.pause(torrent_id):
|
if not self.torrents.pause(torrent_id):
|
||||||
self.torrent_paused(torrent_id)
|
log.warning("Error pausing torrent %s", torrent_id)
|
||||||
|
|
||||||
@dbus.service.method(dbus_interface="org.deluge_torrent.Deluge")
|
@dbus.service.method(dbus_interface="org.deluge_torrent.Deluge")
|
||||||
def pause_all_torrents(self):
|
def pause_all_torrents(self):
|
||||||
"""Pause all torrents in the session"""
|
"""Pause all torrents in the session"""
|
||||||
if self.torrents.pause_all():
|
if not self.torrents.pause_all():
|
||||||
# Emit 'torrent_all_paused' signal
|
log.warning("Error pausing all torrents..")
|
||||||
self.torrent_all_paused()
|
|
||||||
|
|
||||||
@dbus.service.method(dbus_interface="org.deluge_torrent.Deluge")
|
@dbus.service.method(dbus_interface="org.deluge_torrent.Deluge")
|
||||||
def resume_all_torrents(self):
|
def resume_all_torrents(self):
|
||||||
|
@ -481,3 +486,21 @@ class Core(dbus.service.Object):
|
||||||
def on_set_max_upload_slots_per_torrent(self, key, value):
|
def on_set_max_upload_slots_per_torrent(self, key, value):
|
||||||
log.debug("max_upload_slots_per_torrent set to %s..", value)
|
log.debug("max_upload_slots_per_torrent set to %s..", value)
|
||||||
self.torrents.set_max_uploads(value)
|
self.torrents.set_max_uploads(value)
|
||||||
|
|
||||||
|
## Alert handlers ##
|
||||||
|
def on_alert_torrent_finished(self, alert):
|
||||||
|
log.debug("on_alert_torrent_finished")
|
||||||
|
# Get the torrent_id
|
||||||
|
torrent_id = str(alert.handle.info_hash())
|
||||||
|
log.debug("%s is finished..", torrent_id)
|
||||||
|
# Write the fastresume file
|
||||||
|
self.torrents.write_fastresume(torrent_id)
|
||||||
|
|
||||||
|
def on_alert_torrent_paused(self, alert):
|
||||||
|
log.debug("on_alert_torrent_paused")
|
||||||
|
# Get the torrent_id
|
||||||
|
torrent_id = str(alert.handle.info_hash())
|
||||||
|
# Write the fastresume file
|
||||||
|
self.torrents.write_fastresume(torrent_id)
|
||||||
|
# Emit torrent_paused signal
|
||||||
|
self.torrent_paused(torrent_id)
|
||||||
|
|
|
@ -63,6 +63,8 @@ class TorrentManager:
|
||||||
log.debug("TorrentManager init..")
|
log.debug("TorrentManager init..")
|
||||||
# Set the libtorrent session
|
# Set the libtorrent session
|
||||||
self.session = session
|
self.session = session
|
||||||
|
# Get the core config
|
||||||
|
self.config = ConfigManager("core.conf")
|
||||||
# Per torrent connection limit and upload slot limit
|
# Per torrent connection limit and upload slot limit
|
||||||
self.max_connections = -1
|
self.max_connections = -1
|
||||||
self.max_uploads = -1
|
self.max_uploads = -1
|
||||||
|
@ -87,8 +89,6 @@ class TorrentManager:
|
||||||
def add(self, filename, filedump=None, compact=None):
|
def add(self, filename, filedump=None, compact=None):
|
||||||
"""Add a torrent to the manager and returns it's torrent_id"""
|
"""Add a torrent to the manager and returns it's torrent_id"""
|
||||||
log.info("Adding torrent: %s", filename)
|
log.info("Adding torrent: %s", filename)
|
||||||
# Get the core config
|
|
||||||
config = ConfigManager("core.conf")
|
|
||||||
|
|
||||||
# Make sure 'filename' is a python string
|
# Make sure 'filename' is a python string
|
||||||
filename = str(filename)
|
filename = str(filename)
|
||||||
|
@ -103,7 +103,8 @@ class TorrentManager:
|
||||||
# Get the data from the file
|
# Get the data from the file
|
||||||
try:
|
try:
|
||||||
log.debug("Attempting to open %s for add.", filename)
|
log.debug("Attempting to open %s for add.", filename)
|
||||||
filedump = open(os.path.join(config["torrentfiles_location"],
|
filedump = open(
|
||||||
|
os.path.join(self.config["torrentfiles_location"],
|
||||||
filename), "rb").read()
|
filename), "rb").read()
|
||||||
except IOError:
|
except IOError:
|
||||||
log.warning("Unable to open %s", filename)
|
log.warning("Unable to open %s", filename)
|
||||||
|
@ -115,12 +116,12 @@ class TorrentManager:
|
||||||
|
|
||||||
# Make sure we are adding it with the correct allocation method.
|
# Make sure we are adding it with the correct allocation method.
|
||||||
if compact is None:
|
if compact is None:
|
||||||
compact = config["compact_allocation"]
|
compact = self.config["compact_allocation"]
|
||||||
|
|
||||||
try:
|
try:
|
||||||
handle = self.session.add_torrent(
|
handle = self.session.add_torrent(
|
||||||
lt.torrent_info(torrent_filedump),
|
lt.torrent_info(torrent_filedump),
|
||||||
config["download_location"],
|
self.config["download_location"],
|
||||||
compact)
|
compact)
|
||||||
except RuntimeError:
|
except RuntimeError:
|
||||||
log.warning("Error adding torrent")
|
log.warning("Error adding torrent")
|
||||||
|
@ -135,17 +136,18 @@ class TorrentManager:
|
||||||
|
|
||||||
log.debug("Attemping to save torrent file: %s", filename)
|
log.debug("Attemping to save torrent file: %s", filename)
|
||||||
# Test if the torrentfiles_location is accessible
|
# Test if the torrentfiles_location is accessible
|
||||||
if os.access(os.path.join(config["torrentfiles_location"]), os.F_OK) \
|
if os.access(
|
||||||
|
os.path.join(self.config["torrentfiles_location"]), os.F_OK) \
|
||||||
is False:
|
is False:
|
||||||
# The directory probably doesn't exist, so lets create it
|
# The directory probably doesn't exist, so lets create it
|
||||||
try:
|
try:
|
||||||
os.makedirs(os.path.join(config["torrentfiles_location"]))
|
os.makedirs(os.path.join(self.config["torrentfiles_location"]))
|
||||||
except IOError:
|
except IOError:
|
||||||
log.warning("Unable to create torrent files directory..")
|
log.warning("Unable to create torrent files directory..")
|
||||||
|
|
||||||
# Write the .torrent file to the torrent directory
|
# Write the .torrent file to the torrent directory
|
||||||
try:
|
try:
|
||||||
save_file = open(os.path.join(config["torrentfiles_location"],
|
save_file = open(os.path.join(self.config["torrentfiles_location"],
|
||||||
filename),
|
filename),
|
||||||
"wb")
|
"wb")
|
||||||
save_file.write(filedump)
|
save_file.write(filedump)
|
||||||
|
@ -207,16 +209,21 @@ class TorrentManager:
|
||||||
except:
|
except:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
status = self.torrents[torrent_id].get_status(
|
||||||
|
["total_done", "total_wanted"])
|
||||||
|
|
||||||
|
# Only delete the .fastresume file if we're still downloading stuff
|
||||||
|
if status["total_done"] < status["total_wanted"]:
|
||||||
|
self.delete_fastresume(torrent_id)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def resume_all(self):
|
def resume_all(self):
|
||||||
"""Resumes all torrents.. Returns a list of torrents resumed"""
|
"""Resumes all torrents.. Returns a list of torrents resumed"""
|
||||||
torrent_was_resumed = False
|
torrent_was_resumed = False
|
||||||
for key in self.torrents.keys():
|
for key in self.torrents.keys():
|
||||||
try:
|
if self.resume(key):
|
||||||
self.torrents[key].handle.resume()
|
|
||||||
torrent_was_resumed = True
|
torrent_was_resumed = True
|
||||||
except:
|
else:
|
||||||
log.warning("Unable to resume torrent %s", key)
|
log.warning("Unable to resume torrent %s", key)
|
||||||
|
|
||||||
return torrent_was_resumed
|
return torrent_was_resumed
|
||||||
|
@ -265,6 +272,33 @@ class TorrentManager:
|
||||||
except IOError:
|
except IOError:
|
||||||
log.warning("Unable to save state file.")
|
log.warning("Unable to save state file.")
|
||||||
|
|
||||||
|
def delete_fastresume(self, torrent_id):
|
||||||
|
"""Deletes the .fastresume file"""
|
||||||
|
torrent = self.torrents[torrent_id]
|
||||||
|
path = "%s/%s.fastresume" % (
|
||||||
|
self.config["torrentfiles_location"],
|
||||||
|
torrent.filename)
|
||||||
|
log.debug("Deleting fastresume file: %s", path)
|
||||||
|
try:
|
||||||
|
os.remove(path)
|
||||||
|
except IOError:
|
||||||
|
log.warning("Unable to delete the fastresume file: %s", path)
|
||||||
|
|
||||||
|
def write_fastresume(self, torrent_id):
|
||||||
|
"""Writes the .fastresume file for the torrent"""
|
||||||
|
torrent = self.torrents[torrent_id]
|
||||||
|
resume_data = lt.bencode(torrent.handle.write_resume_data())
|
||||||
|
path = "%s/%s.fastresume" % (
|
||||||
|
self.config["torrentfiles_location"],
|
||||||
|
torrent.filename)
|
||||||
|
log.debug("Saving fastresume file: %s", path)
|
||||||
|
try:
|
||||||
|
fastresume = open(path,"wb")
|
||||||
|
fastresume.write(resume_data)
|
||||||
|
fastresume.close()
|
||||||
|
except IOError:
|
||||||
|
log.warning("Error trying to save fastresume file")
|
||||||
|
|
||||||
def set_max_connections(self, value):
|
def set_max_connections(self, value):
|
||||||
"""Sets the per-torrent connection limit"""
|
"""Sets the per-torrent connection limit"""
|
||||||
self.max_connections = value
|
self.max_connections = value
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue