Update Extractor with Win32 and extra archives support

Also disabled extracting on move_completed enabled torrents and fixed
some other small issues.
This commit is contained in:
Calum Lind 2013-02-21 21:58:18 +00:00
parent 61c125420b
commit e2e37a83de
3 changed files with 105 additions and 41 deletions

View File

@ -46,27 +46,70 @@ from deluge.plugins.pluginbase import CorePluginBase
import deluge.component as component import deluge.component as component
import deluge.configmanager import deluge.configmanager
from deluge.core.rpcserver import export from deluge.core.rpcserver import export
from deluge.common import windows_check
from extractor.which import which
DEFAULT_PREFS = { DEFAULT_PREFS = {
"extract_path": "", "extract_path": "",
"use_name_folder": True "use_name_folder": True
} }
# The first format is the source file, the second is the dest path if windows_check():
EXTRACT_COMMANDS = { win_7z_exes = [
".rar": ["unrar", "x -o+ -y"], '7z.exe',
".zip": ["unzip", ""], 'C:\\Program Files\\7-Zip\\7z.exe',
".tar.gz": ["tar", "xvzf"], 'C:\\Program Files (x86)\\7-Zip\\7z.exe',
".tar.bz2": ["tar", "xvjf"], ]
".tar.lzma": ["tar", "--lzma xvf"] switch_7z = "x -y"
} ## Future suport:
## 7-zip cannot extract tar.* with single command.
# ".tar.gz", ".tgz",
# ".tar.bz2", ".tbz",
# ".tar.lzma", ".tlz",
# ".tar.xz", ".txz",
exts_7z = [
".rar", ".zip", ".tar",
".7z", ".xz", ".lzma",
]
for win_7z_exe in win_7z_exes:
if which(win_7z_exe):
EXTRACT_COMMANDS = dict.fromkeys(exts_7z, [win_7z_exe, switch_7z])
break
else:
required_cmds=["unrar", "unzip", "tar", "unxz", "unlzma", "7zr", "bunzip2"]
## Possible future suport:
# gunzip: gz (cmd will delete original archive)
## the following do not extract to dest dir
# ".xz": ["xz", "-d --keep"],
# ".lzma": ["xz", "-d --format=lzma --keep"],
# ".bz2": ["bzip2", "-d --keep"],
EXTRACT_COMMANDS = {
".rar": ["unrar", "x -o+ -y"],
".tar": ["tar", "-xf"],
".zip": ["unzip", ""],
".tar.gz": ["tar", "-xzf"], ".tgz": ["tar", "-xzf"],
".tar.bz2": ["tar", "-xjf"], ".tbz": ["tar", "-xjf"],
".tar.lzma": ["tar", "--lzma -xf"], ".tlz": ["tar", "--lzma -xf"],
".tar.xz": ["tar", "--xz -xf"], ".txz": ["tar", "--xz -xf"],
".7z": ["7zr", "x"],
}
# Test command exists and if not, remove.
for cmd in required_cmds:
if not which(cmd):
for k,v in EXTRACT_COMMANDS.items():
if cmd in v[0]:
log.error("EXTRACTOR: %s not found, disabling support for %s", cmd, k)
del EXTRACT_COMMANDS[k]
if not EXTRACT_COMMANDS:
raise Exception("EXTRACTOR: No archive extracting programs found, plugin will be disabled")
class Core(CorePluginBase): class Core(CorePluginBase):
def enable(self): def enable(self):
self.config = deluge.configmanager.ConfigManager("extractor.conf", DEFAULT_PREFS) self.config = deluge.configmanager.ConfigManager("extractor.conf", DEFAULT_PREFS)
if not self.config["extract_path"]: if not self.config["extract_path"]:
self.config["extract_path"] = deluge.configmanager.ConfigManager("core.conf")["download_location"] self.config["extract_path"] = deluge.configmanager.ConfigManager("core.conf")["download_location"]
component.get("EventManager").register_event_handler("TorrentFinishedEvent", self._on_torrent_finished) component.get("EventManager").register_event_handler("TorrentFinishedEvent", self._on_torrent_finished)
def disable(self): def disable(self):
@ -77,55 +120,59 @@ class Core(CorePluginBase):
def _on_torrent_finished(self, torrent_id): def _on_torrent_finished(self, torrent_id):
""" """
This is called when a torrent finishes. We need to check to see if there This is called when a torrent finishes and checks if any files to extract.
are any files to extract.
""" """
# Get the save path tid = component.get("TorrentManager").torrents[torrent_id]
save_path = component.get("TorrentManager")[torrent_id].get_status(["save_path"])["save_path"] tid_status = tid.get_status(["save_path", "move_completed", "name"])
files = component.get("TorrentManager")[torrent_id].get_files()
if tid_status["move_completed"]:
log.error("EXTRACTOR: Cannot extract torrents with 'Move Completed' enabled")
return
files = tid.get_files()
for f in files: for f in files:
ext = os.path.splitext(f["path"]) cmd = ''
if ext[1] in (".gz", ".bz2", ".lzma"): file_ext = os.path.splitext(f["path"])[1]
# We need to check if this is a tar file_ext_sec = os.path.splitext(os.path.splitext(f["path"])[0])[1]
if os.path.splitext(ext[0]) == ".tar": if file_ext in (".gz", ".bz2", ".lzma", ".xz") and file_ext_sec == ".tar":
cmd = EXTRACT_COMMANDS[".tar" + ext[1]] cmd = EXTRACT_COMMANDS[".tar" + file_ext]
elif file_ext in EXTRACT_COMMANDS:
cmd = EXTRACT_COMMANDS[file_ext]
else: else:
if ext[1] in EXTRACT_COMMANDS: log.error("EXTRACTOR: Can't extract unknown file type: %s", file_ext)
cmd = EXTRACT_COMMANDS[ext[1]] continue
else:
log.debug("Can't extract unknown file type: %s", ext[1])
continue
# Now that we have the cmd, lets run it to extract the files # Now that we have the cmd, lets run it to extract the files
fp = os.path.join(save_path, f["path"]) fpath = os.path.join(tid_status["save_path"], os.path.normpath(f["path"]))
# Get the destination path # Get the destination path
dest = self.config["extract_path"] dest = os.path.normpath(self.config["extract_path"])
if self.config["use_name_folder"]: if self.config["use_name_folder"]:
name = component.get("TorrentManager")[torrent_id].get_status(["name"])["name"] name = tid_status["name"]
dest = os.path.join(dest, name) dest = os.path.join(dest, name)
# Create the destination folder if it doesn't exist # Create the destination folder if it doesn't exist
if not os.path.exists(dest): if not os.path.exists(dest):
try: try:
os.makedirs(dest) os.makedirs(dest)
except Exception, e: except Exception, e:
log.error("Error creating destination folder: %s", e) log.error("EXTRACTOR: Error creating destination folder: %s", e)
return return
log.debug("Extracting to %s", dest)
def on_extract_success(result, torrent_id):
# XXX: Emit an event
log.debug("Extract was successful for %s", torrent_id)
def on_extract_failed(result, torrent_id): def on_extract_success(result, torrent_id, fpath):
# XXX: Emit an event # XXX: Emit an event
log.debug("Extract failed for %s", torrent_id) log.info("EXTRACTOR: Extract successful: %s (%s)", fpath, torrent_id)
def on_extract_failed(result, torrent_id, fpath):
# XXX: Emit an event
log.error("EXTRACTOR: Extract failed: %s (%s)", fpath, torrent_id)
# Run the command and add some callbacks # Run the command and add some callbacks
d = getProcessValue(cmd[0], cmd[1].split() + [str(fp)], {}, str(dest)) if cmd:
d.addCallback(on_extract_success, torrent_id) log.debug("EXTRACTOR: Extracting %s with %s %s to %s", fpath, cmd[0], cmd[1], dest)
d.addErrback(on_extract_failed, torrent_id) d = getProcessValue(cmd[0], cmd[1].split() + [str(fpath)], {}, str(dest))
d.addCallback(on_extract_success, torrent_id, fpath)
d.addErrback(on_extract_failed, torrent_id, fpath)
@export @export
def set_config(self, config): def set_config(self, config):

View File

@ -0,0 +1,17 @@
def which(program):
# Author Credit: Jay @ http://stackoverflow.com/a/377028
import os
def is_exe(fpath):
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
fpath, fname = os.path.split(program)
if fpath:
if is_exe(program):
return program
else:
for path in os.environ["PATH"].split(os.pathsep):
exe_file = os.path.join(path, program)
if is_exe(exe_file):
return exe_file
return None

View File

@ -42,7 +42,7 @@ from setuptools import setup
__plugin_name__ = "Extractor" __plugin_name__ = "Extractor"
__author__ = "Andrew Resch" __author__ = "Andrew Resch"
__author_email__ = "andrewresch@gmail.com" __author_email__ = "andrewresch@gmail.com"
__version__ = "0.1" __version__ = "0.2"
__url__ = "http://deluge-torrent.org" __url__ = "http://deluge-torrent.org"
__license__ = "GPLv3" __license__ = "GPLv3"
__description__ = "Extract files upon completion" __description__ = "Extract files upon completion"