#!/usr/bin/python2.4 # Returns a CD mounting Point # Author: Paulo Roma # Date: 15 07 2006 # import sys, os, time, re, string, pwd import subprocess # Search in /etc/mtab for a mount point def GetCDMountPoint(): mtab = "/etc/mtab" # returns the suffix after the last "/" getName = lambda x: x.split("/")[-1] if not os.path.exists(mtab): Log("Wasn't able to locate mtab") cd = GetCD() return [(cd, getName(cd))] list = [] for line in file("/etc/mtab"): mpoint, fsys = line.split()[1:3] if fsys == "iso9660": #TODO: Some systems may call it cd9660? Log(mpoint) # if Found, return mount point return mpoint #TODO: Should we return a list if somebody has multiple drives? list.append((mpoint, mpoint.split("/")[-1])) if not list: # Not Found, then search in /etc/fstab cd = GetCD() # return [(cd, getName(cd))] return cd return list def Log(s): # print "Detected CD drive:", s return # Search in /etc/fstab for a mount point def GetCD(): cdpath = "None" # Lifted almost completely out of freevo (thanks dischi ;) if not os.path.isfile( '/etc/fstab' ): return cdpath re_cd = re.compile( '^(/dev/cdrom[0-9]*|/dev/[am]?cd[0-9]+[a-z]?)[ \t]+([^ \t]+)[ \t]+', re.I ) re_cdrec = re.compile( '^(/dev/cdrecorder[0-9]*)[ \t]+([^ \t]+)[ \t]+', re.I ) re_dvd = re.compile( '^(/dev/dvd[0-9]*)[ \t]+([^ \t]+)[ \t]+', re.I ) re_iso = re.compile( '^([^ \t]+)[ \t]+([^ \t]+)[ \t]+(iso|cd)9660', re.I ) re_automount = re.compile( '^none[ \t]+([^ \t]+).*supermount.*dev=([^,]+).*', re.I ) re_bymountcd = re.compile( '^(/dev/[^ \t]+)[ \t]+([^ ]*cdrom[0-9]*)[ \t]+', re.I ) re_bymountdvd= re.compile( '^(/dev/[^ \t]+)[ \t]+([^ ]*dvd[0-9]*)[ \t]+', re.I ) fd_fstab = open( '/etc/fstab' ) for line in fd_fstab: # Match on the devices /dev/cdrom, /dev/dvd, and fstype iso9660 match_cd = re_cd.match( line ) match_cdrec = re_cdrec.match( line ) match_dvd = re_dvd.match( line ) match_iso = re_iso.match( line ) match_automount = re_automount.match( line ) match_bymountcd = re_bymountcd.match( line ) match_bymountdvd= re_bymountdvd.match( line ) mntdir = '' if match_cd or match_bymountcd: m = match_cd or match_bymountcd mntdir = m.group( 2 ) elif match_cdrec: mntdir = match_cdrec.group( 2 ) elif match_dvd or match_bymountdvd: m = match_dvd or match_bymountdvd mntdir = m.group( 2 ) elif match_iso: mntdir = match_iso.group( 2 ) elif match_automount: mntdir = match_automount.group( 1 ) if mntdir: cdpath = mntdir fd_fstab.close() if cdpath: Log(cdpath) return cdpath # Main program. # def main(argv=None): if argv is None: argv = sys.argv return GetCDMountPoint() # When main() calls sys.exit(), your interactive Python interpreter # will exit! The remedy is to let main()'s return value specify # the exit status. # if __name__=='__main__': sys.exit(main())