Mit Exit-Status richtig umgehen, nur bekannte Fehler uebergehen

This commit is contained in:
Stefan Bethke 2010-08-07 12:23:17 +00:00
parent f004138cb1
commit 4a5bcc4f81

View file

@ -1,6 +1,6 @@
#!/usr/bin/python
# $Schlepperbande: src/tivomirror/tivomirror,v 1.31 2010/08/06 08:41:55 stb Exp $
# $Schlepperbande: src/tivomirror/tivomirror,v 1.32 2010/08/07 09:04:16 stb Exp $
#
# Stefans Script, um die Sendungen vom Tivo runterzuladen und in MPEG4
# zu transkodieren.
@ -11,12 +11,13 @@ import anydbm
import cookielib
import datetime
import os
import re
import signal
import shutil
import subprocess
import sys
import urllib2
import xml.dom.minidom
import re
host = "tivo.lassitu.de"
mak = "7194378159"
@ -65,6 +66,12 @@ tmp = "/tmp"
tmpmp2 = "/home/stb/tmp.mpg"
tmpmp4 = "/home/stb/tmp.mp4"
class TivoException(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)
def gettoc():
url = "https://" + host + "/TiVoConnect?Command=QueryContainer&Container=%2FNowPlaying&Recurse=Yes"
pwmgr.add_password(None, url, "tivo", mak)
@ -136,19 +143,31 @@ def transcode(file, src, passno, ar):
print " %s" % " ".join(transcode_opts)
subprocess.check_call(transcode_opts)
def checkProcessError(curl, decode):
(spid, sse) = os.wait()
if (sse >> 8) == 0:
if not os.WIFSIGNALED(sse) \
and os.WIFEXITED(sse) and os.WEXITSTATUS(sse) == 0:
return False
if spid == curl.pid:
print "error downloading file: %d" % (sse >> 8)
proc = "curl"
elif spid == decode.pid:
print "error decoding file: %d" % (sse >> 8)
proc = "tivodecode"
if os.WIFSIGNALED(sse):
cause = "exited with signal %d" % os.WTERMSIG(sse)
else:
print "another process(?!) finished with an error"
cause = "exited with code %d" % os.WEXITSTATUS(sse)
proc = "another"
print "--- %s %s" % (proc, cause)
return True
def quit_process(pid):
try:
os.kill(p_curl.pid, signal.SIGQUIT)
except OSError:
pass
def download(file, url, mak, target):
print "--- dowloading \"%s\"" % (url)
p_curl = subprocess.Popen(["curl", "--anyauth", "--fail", \
@ -159,14 +178,14 @@ def download(file, url, mak, target):
p_decode = subprocess.Popen(["tivodecode", "--mak", mak, \
"--out", target, "-"], stdin=p_curl.stdout)
if checkProcessError(p_curl, p_decode) or checkProcessError(p_curl, p_decode):
quit_process(p_curl.pid)
quit_process(p_decode.pid.pid)
os.remove(target)
os.kill(p_curl.pid, signal.SIGQUIT)
os.kill(p_decode.pid, signal.SIGQUIT)
raise Exception
raise TivoException("error downloading/decoding file")
if os.path.getsize(target) < 1024:
print "error transcoding file: too small"
os.remove(target)
raise Exception
raise TivoException("downloaded file is too small")
def download_decode(file, url, mak, ar):
#target = tmpmp2
@ -251,7 +270,7 @@ for i in items:
downloaddb.sync()
# stop after the fist successful download since the tivo hangs easily
break
except Exception, e:
except TivoException, e:
print "Error processing \"%s\": %s" % (name, e)
print "*** Completed"