#!@PYTHON@ # # Check the free space available on a writable DVD # Should always exit with 0 status, otherwise it indicates a serious error. # (wrong number of arguments, Python exception...) # # called: dvd-freespace # # # returns: # Prints on the first output line the free space available in bytes. # If an error occurs, prints a negative number (-errno), followed, # on the second line, by an error message. # # $Id$ # Configurable values: df="@DF@" dvdrwmediainfo="@DVDRWMEDIAINFO@" margin=10485760 # 10 mb security margin # end of configurable values import popen2 import os import errno import sys if len(sys.argv) < 2: print "Wrong number of arguments." sys.exit(1) device=sys.argv[1] def gettotalsize(): cmd=dvdrwmediainfo + " " + device processi = popen2.Popen4(cmd) status = processi.wait() if not os.WIFEXITED(status): print -errno.EPIPE print dvdrwmediainfo + " process did not exit correctly." sys.exit(0) if os.WEXITSTATUS(status) != 0: print -errno.EPIPE print "Cannot get media info from " + dvdrwmediainfo sys.exit(0) while 1: result = processi.fromchild.readline() if result.find("Track Size:") > -1: index = result.find("*2KB") if index > 0: return long(result[12:index]) * 2048 else: print -errno.EPIPE print "Invalid format in media info lead-out line from " + dvdrwmediainfo sys.exit(0) if result.find("Legacy lead-out at:") > -1: index = result.find("=") if index > -1: res = result[index+1:-1] if not res.isdigit(): print -errno.EPIPE print "Invalid format in media info lead-out line from " + dvdrwmediainfo sys.exit(0) return long(res) else: print -errno.EPIPE print "Invalid format in media info lead-out line from " + dvdrwmediainfo sys.exit(0) if not result: print -errno.EPIPE print "Cannot get media lead-out index from " + dvdrwmediainfo sys.exit(0) cmd=df + " " + device process = popen2.Popen4(cmd) status = process.wait() if not os.WIFEXITED(status): print -errno.EPIPE print df + " process did not not exit correctly." sys.exit(0) exitstat = os.WEXITSTATUS(status) & ~0x80 if exitstat == errno.ENOSPC: print "0" print os.strerror(exitstat) sys.exit(0) if exitstat != 0: print -exitstat print os.strerror(exitstat) sys.exit(0) size = 0 while 1: result = process.fromchild.readline() if not result: print -errno.EPIPE print "Cannot get output from " + df sys.exit(0) index = result.find(device) if index > -1: while result[index] != ' ': index += 1 while result[index] == ' ': index += 1 eindex = index; while result[eindex] != ' ': eindex += 1 res = result[index:eindex] if not res.isdigit(): print -errno.EPIPE; print "Could not find size in df output" sys.exit(0) size = long(res)*1024 break size = gettotalsize()-(size+margin) if size < 0: size = 0 print size print "No error occurred" sys.exit(0)