moved extractor code

into /extractor
included 7zip
included a which() command which check for available commands on unix

had a chance to test the code 2 times, but needs more testing
This commit is contained in:
Joel Kåberg 2013-02-26 10:03:15 +01:00
commit d0d3437ef9
10 changed files with 143 additions and 82 deletions

View file

@ -13,6 +13,7 @@ from subprocess import call
# Custom imports
import linktastic.linktastic as linktastic
import extractor.extractor as extractor
import autoProcessMovie
import autoProcessTV
from nzbToMediaEnv import *
@ -28,15 +29,15 @@ def category_search_recurs(inputDirectory, inputName, root, categories):
def category_search(inputDirectory, inputName, inputCategory, root, categories):
categorySearch = [os.path.normpath(inputDirectory),""] #initializie
notfound = 0
for x in range(10): # loop up through 10 directories looking for category.
try:
categorySearch2 = os.path.split(os.path.normpath(categorySearch[0]))
except: # this might happen when we can't go higher.
if inputCategory and inputName: # if these exists, we are ok to proceed, but assume we are in a root/common directory.
Logger.info("SEARCH: Could not find a Torrent Name or category in the directory structure")
Logger.info("SEARCH: We assume the directory passed is the root directory for your downlaoder")
categorySearch = [os.path.normpath(inputDirectory),""] #initializie
notfound = 0
for x in range(10): # loop up through 10 directories looking for category.
try:
categorySearch2 = os.path.split(os.path.normpath(categorySearch[0]))
except: # this might happen when we can't go higher.
if inputCategory and inputName: # if these exists, we are ok to proceed, but assume we are in a root/common directory.
Logger.info("SEARCH: Could not find a Torrent Name or category in the directory structure")
Logger.info("SEARCH: We assume the directory passed is the root directory for your downlaoder")
Logger.warn("SEARCH: You should change settings to download torrents to their own directory if possible")
Logger.info("SEARCH: We will try and determine which files to process, individually")
root = 1
@ -158,76 +159,6 @@ def copy_link(source, target, useLink, outputDestination):
shutil.copy(source, target)
return True
def unpack(dirpath, file, destination):
# Using Windows
if os.name == 'nt':
Logger.info("EXTRACTOR: We are using Windows")
cmd_7zip = [extractionTool, 'x -y']
ext_7zip = [".rar",".zip",".tar.gz","tgz",".tar.bz2",".tbz",".tar.lzma",".tlz",".7z",".xz"]
EXTRACT_COMMANDS = dict.fromkeys(ext_7zip, cmd_7zip)
# Using linux
elif os.name == 'posix':
Logger.info("EXTRACTOR: We are using *nix")
EXTRACT_COMMANDS = {".rar": ["unrar", "e"], ".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"], ".txz": ["tar", "--xz xf"], ".7z": ["7zr", "x"],}
# Need to add a check for which commands that can be utilized in *nix systems..
else:
Logger.error("EXTRACTOR: Unknown OS, exiting")
ext = os.path.splitext(file)
fp = os.path.join(dirpath, file)
if ext[1] in (".gz", ".bz2", ".lzma"):
# Check if this is a tar
if os.path.splitext(ext[0])[1] == ".tar":
cmd = EXTRACT_COMMANDS[".tar" + ext[1]]
else:
if ext[1] in EXTRACT_COMMANDS:
cmd = EXTRACT_COMMANDS[ext[1]]
else:
Logger.debug("EXTRACTOR: Unknown file type: %s", ext[1])
return False
# Create destination folder
if not os.path.exists(destination):
try:
Logger.debug("EXTRACTOR: Creating destination folder: %s", destination)
os.makedirs(destination)
except Exception, e:
Logger.error("EXTRACTOR: Not possible to create destination folder: %s", e)
return False
Logger.info("Extracting %s to %s", fp, destination)
# Running
Logger.debug("Extracting %s %s %s %s", cmd[0], cmd[1], fp, destination)
pwd = os.getcwd() # Get our Present Working Directory
os.chdir(destination) # Not all unpack commands accept full paths, so just extract into this directory
if os.name == 'nt': # Windows needs quotes around directory structure
try:
run = "\"" + cmd[0] + "\" " + cmd[1] + " \"" + fp + "\"" # Windows needs quotes around directories
res = call(run)
if res == 0:
Logger.info("EXTRACTOR: Extraction was successful for %s to %s", fp, destination)
else:
Logger.info("EXTRACTOR: Extraction failed for %s. 7zip result was %s", fp, res)
except:
Logger.error("EXTRACTOR: Extraction failed for %s. Could not call command %s %s", fp, run)
else:
try:
if cmd[1] == "": # If calling unzip, we dont want to pass the ""
res = call([cmd[0], fp])
else:
res = call([cmd[0], cmd[1], fp])
if res == 0:
Logger.info("EXTRACTOR: Extraction was successful for %s to %s", fp, destination)
else:
Logger.error("EXTRACTOR: Extraction failed for %s. 7zip result was %s", fp, res)
except:
Logger.error("EXTRACTOR: Extraction failed for %s. Could not call command %s %s %s %s", fp, cmd[0], cmd[1], fp)
os.chdir(pwd) # Go back to our Original Working Directory
return True
def flatten(outputDestination):
Logger.info("FLATTEN: Flattening directory: %s", outputDestination)
for dirpath, dirnames, filenames in os.walk(outputDestination): # Flatten out the directory to make postprocessing easier
@ -294,7 +225,6 @@ movieCategory = config.get("CouchPotato", "category")
movieDestination = os.path.normpath(config.get("CouchPotato", "outputDirectory"))
# Torrent specific
useLink = int(config.get("Torrent", "useLink"))
extractionTool = os.path.normpath(config.get("Torrent", "extractionTool"))
uTorrentWEBui = config.get("Torrent", "uTorrentWEBui")
uTorrentUSR = config.get("Torrent", "uTorrentUSR")
uTorrentPWD = config.get("Torrent", "uTorrentPWD")
@ -374,7 +304,7 @@ for dirpath, dirnames, filenames in os.walk(inputDirectory):
Logger.info("MAIN: Found compressed archive %s for file %s", fileExtention, filePath)
source = filePath
target = os.path.join(outputDestination, file)
state = unpack(dirpath, file, outputDestination)
state = extractor.extract(dirpath, file, outputDestination)
if state == False:
Logger.info("MAIN: Failed to unpack file %s", file)
failed_extract = 1

View file

@ -32,7 +32,6 @@ failed_fork=0
# Supported values: utorrent, transmission, deluge, other
clientAgent = other
useLink = 0
extractionTool = C:\Program Files\7-Zip\7z.exe
categories = music,music_videos,pictures,software
###### uTorrent Hardlink solution (You must edit this if your using TorrentToMedia.py with uTorrent)
uTorrentWEBui = http://localhost:8090/gui/

0
extractor/__init__.py Normal file
View file

BIN
extractor/bin/7z.dll Normal file

Binary file not shown.

BIN
extractor/bin/7z.exe Normal file

Binary file not shown.

115
extractor/extractor.py Normal file
View file

@ -0,0 +1,115 @@
import os
import sys
import logging
Logger = logging.getLogger(__name__)
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
def extract(dirpath, file, outputDestination):
# Using Windows
if os.name == 'nt':
binLocation = 'extractor/bin/7z.exe' # Only 32bit for now
sevenzipLocation = os.path.normpath(os.path.join(os.path.dirname(sys.argv[0]), binLocation))
if not os.path.exists(sevenzipLocation):
Logger.error("EXTRACTOR: Couldnt find 7-zip, Exiting")
sys.exit(-1)
else:
cmd_7zip = [sevenzipLocation, 'x -y']
ext_7zip = [".rar",".zip",".tar.gz","tgz",".tar.bz2",".tbz",".tar.lzma",".tlz",".7z",".xz"]
EXTRACT_COMMANDS = dict.fromkeys(ext_7zip, cmd_7zip)
# Using unix
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")
ext = os.path.splitext(file)
filePath = os.path.join(dirpath, file)
if ext[1] in (".gz", ".bz2", ".lzma"):
# Check if this is a tar
if os.path.splitext(ext[0])[1] == ".tar":
cmd = EXTRACT_COMMANDS[".tar" + ext[1]]
else:
if ext[1] in EXTRACT_COMMANDS:
cmd = EXTRACT_COMMANDS[ext[1]]
else:
Logger.debug("EXTRACTOR: Unknown file type: %s", ext[1])
return False
# Create outputDestination folder
if not os.path.exists(outputDestination):
try:
Logger.debug("EXTRACTOR: Creating destination folder: %s", outputDestination)
os.makedirs(outputDestination)
except Exception, e:
Logger.error("EXTRACTOR: Not possible to create destination folder: %s", e)
return False
Logger.info("Extracting %s to %s", filePath, outputDestination)
Logger.debug("Extracting %s %s %s %s", cmd[0], cmd[1], filePath, outputDestination)
pwd = os.getcwd() # Get our Present Working Directory
os.chdir(outputDestination) # Not all unpack commands accept full paths, so just extract into this directory
if os.name == 'nt': # Windows needs quotes around directory structure
try:
run = "\"" + cmd[0] + "\" " + cmd[1] + " \"" + filePath + "\"" # Windows needs quotes around directories
res = call(run)
if res == 0:
Logger.info("EXTRACTOR: Extraction was successful for %s to %s", filePath, outputDestination)
else:
Logger.info("EXTRACTOR: Extraction failed for %s. 7zip result was %s", filePath, res)
except:
Logger.error("EXTRACTOR: Extraction failed for %s. Could not call command %s %s", filePath, run)
else:
try:
if cmd[1] == "": # If calling unzip, we dont want to pass the ""
res = call([cmd[0], filePath])
else:
res = call([cmd[0], cmd[1], filePath])
if res == 0:
Logger.info("EXTRACTOR: Extraction was successful for %s to %s", filePath, outputDestination)
else:
Logger.error("EXTRACTOR: Extraction failed for %s. 7zip result was %s", filePath, res)
except:
Logger.error("EXTRACTOR: Extraction failed for %s. Could not call command %s %s %s %s", filePath, cmd[0], cmd[1], filePath)
os.chdir(pwd) # Go back to our Original Working Directory
return True

17
extractor/which.py Normal file
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

BIN
utorrent/__init__.pyc Normal file

Binary file not shown.

BIN
utorrent/client.pyc Normal file

Binary file not shown.

BIN
utorrent/upload.pyc Normal file

Binary file not shown.