diff --git a/src/tivomirror/tivomirror b/src/tivomirror/tivomirror index 8e066cc..98469b2 100755 --- a/src/tivomirror/tivomirror +++ b/src/tivomirror/tivomirror @@ -1,6 +1,6 @@ #!/usr/bin/python -# $Schlepperbande: src/tivomirror/tivomirror,v 1.34 2010/08/07 13:40:31 stb Exp $ +# $Schlepperbande: src/tivomirror/tivomirror,v 1.35 2010/08/07 13:42:24 stb Exp $ # # Stefans Script, um die Sendungen vom Tivo runterzuladen und in MPEG4 # zu transkodieren. @@ -99,6 +99,34 @@ def getAvail(dir): s = os.statvfs(dir) return s.f_bsize * s.f_bavail + +def quit_process(pid): + try: + os.kill(pid, signal.SIGQUIT) + except OSError: + pass + + +def waitForProcs(pids): + success = True + while len(pids) > 0: + (spid, sse) = os.wait() + pids.remove(spid) + if os.WIFSIGNALED(sse) \ + or (os.WIFEXITED(sse) and os.WEXITSTATUS(sse) != 0): + if os.WIFSIGNALED(sse): + print "--- process %d was terminated by signal %d" % (spid, os.WTERMSIG(sse)) + elif os.WIFEXITED(sse): + print "--- process %d exited with code %d" % (spid, os.WEXITSTATUS(sse)) + else: + print "--- process %d terminated unsuccessfully" % spid + success = False + for pid in pids: + quit_process(pid) + if not success: + raise TivoException("error executing processes") + + def transcode(file, src, passno, ar): print "--- transcoding pass %d" % passno try: @@ -144,30 +172,6 @@ def transcode(file, src, passno, ar): subprocess.check_call(transcode_opts) -def checkProcessError(curl, decode): - (spid, sse) = os.wait() - if not os.WIFSIGNALED(sse) \ - and os.WIFEXITED(sse) and os.WEXITSTATUS(sse) == 0: - return False - if spid == curl.pid: - proc = "curl" - elif spid == decode.pid: - proc = "tivodecode" - if os.WIFSIGNALED(sse): - cause = "exited with signal %d" % os.WTERMSIG(sse) - else: - cause = "exited with code %d" % os.WEXITSTATUS(sse) - proc = "another" - print "--- %s %s" % (proc, cause) - return True - - -def quit_process(pid): - try: - os.kill(pid, signal.SIGQUIT) - except OSError: - pass - def download(file, url, mak, target): print "--- dowloading \"%s\"" % (url) p_curl = subprocess.Popen(["curl", "--anyauth", "--fail", \ @@ -177,16 +181,20 @@ def download(file, url, mak, target): stdout=subprocess.PIPE) 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) - raise TivoException("error downloading/decoding file") + try: + waitForProcs([p_curl.pid, p_decode.pid]) + except Exception, e: + try: + os.remove(target) + except OSError: + pass + raise e if os.path.getsize(target) < 1024: print "error transcoding file: too small" os.remove(target) raise TivoException("downloaded file is too small") + def download_decode(file, url, mak, ar): #target = tmpmp2 target = "%s.mpg" % file @@ -206,11 +214,13 @@ def download_decode(file, url, mak, ar): os.remove(tmpmp2) os.remove(tmpmp4) + def savetoc(toc): fd=open("toc.xml", "w") fd.write(toc) fd.close() + curdir = os.getcwd() os.chdir(tmp) avail = getAvail(targetdir) @@ -218,6 +228,7 @@ if avail < minfree: print "%s: %.1fG available, at least %.1fG needed, stopping" % \ (targetdir, avail / gig, minfree / gig) sys.exit(1) + downloaddb = anydbm.open(curdir + "/tivo/.downloads", "c") print "*** Getting listing" toc = gettoc()