Help controlling CDROM from python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ognjen Bezanov

    #1

    Help controlling CDROM from python

    Hello,

    I am trying to control a CD-ROM drive using python. The code I use is
    shown below.
    import CDROM
    from fcntl import ioctl
    import os
    >
    >
    class Device:
    >
    CDdevice=""
    CDfd = None
    >
    def __init__(self,n ame):
    self.CDdevice = name #we get a device name when module loaded
    >
    if self.CDdevice == "":
    print "No device specified"
    sys.exit(-1)
    self.openCD()
    >
    def openCD(self):
    >
    try:
    self.CDfd = open(self.CDdev ice, 'r') #open the device and return filedescriptor
    >
    except(OSError, IOError): #if there is an OS or IO Error (usually indicates nodisk)
    print "Device Error, Halting.... (usually means drive or disk not found)"
    sys.exit(-1)
    > def unlockCD(self):
    return self.sendCDcomm and(CDROM.CDROM _LOCKDOOR,0)
    def ejectCD(self):
    self.unlockCD() #we need to unlock the CD tray before we try to eject, otherwise we get an IO Error (#5)
    return self.sendCDcomm and(CDROM.CDROM EJECT)
    >
    def sendCDcommand(s elf,command,arg ument=''):
    return ioctl(self.CDfd ,command,argume nt)
    >


    The code that calls the class is a follows:
    import CD_Bindings
    >
    CD = CD_Bindings.Dev ice("/dev/cdrom")
    >
    print CD.ejectCD()
    This works great, but only when there is a disk inside, otherwise we get
    an error.

    My issue is that I need to be able to eject the CDROM tray even if there
    is no disk inside.

    This is possible because other programs (like the linux "eject" command)
    can do it. Its just a question of how it is done in python. So I'm
    posting here in the hope someone can tell me.

    Thanks,

    Ognjen


  • MonkeeSage

    #2
    Re: Help controlling CDROM from python

    On Mar 10, 8:27 am, Ognjen Bezanov <Ogn...@mailsha ck.comwrote:
    My issue is that I need to be able to eject the CDROM tray even if there
    is no disk inside.
    Here's a Q&D version (haven't tested the windows part, it's from an
    old mailing list post, but it looks correct):


    import os, sys
    if 'win' in sys.platform:
    # untested: found in old mailing list post:
    # http://mail.python.org/pipermail/pyt...er/000593.html
    import win32file
    from win32con import *
    drive = 'D:'
    win32file.Creat eFile(r'\\.\\' + drive, GENERIC_READ,
    FILE_SHARE_READ ,
    None, OPEN_EXISTING, 0, 0)
    # IOCTL_STORAGE_E JECT_MEDIA = 0x002d4808 from <winioctl.h>
    win32file.Devic eIoControl(h, 0x002d4808, "", 0)
    win32file.Close Handle(h)
    else: # does this work on OSX and BSD?
    import fcntl
    cd_device = '/dev/cdrom'
    if os.path.islink( cd_device):
    base_path = os.path.dirname (cd_device)
    cd_device = os.readlink(cd_ device)
    if not cd_device[0] == '/':
    cd_device = os.path.join(ba se_path, cd_device)
    cdrom = os.open(cd_devi ce, os.O_RDONLY | os.O_NONBLOCK)
    # CDROMEJECT = 0x5309 from <linux/cdrom.hon linux 2.6
    fcntl.ioctl(cdr om, 0x5309, 0)
    os.close(cdrom)


    NB: If you're using pygame, you can also just use it's builtin
    commands:

    import pygame.cdrom as cdrom
    cdrom.init()
    cd = cdrom.CD(0) # 0 = first cdrom device
    cd.init()
    cd.eject()
    cd.quit()
    cdrom.quit()

    Regards,
    Jordan

    Comment

    • MonkeeSage

      #3
      Re: Help controlling CDROM from python

      On Mar 10, 4:11 pm, "MonkeeSage " <MonkeeS...@gma il.comwrote:
      win32file.Creat eFile(r'\\.\\' + drive, GENERIC_READ,
      FILE_SHARE_READ ,
      None, OPEN_EXISTING, 0, 0)
      Oops! That should have been:

      h = win32file.Creat eFile(r'\\.\\' + drive, GENERIC_READ,
      FILE_SHARE_READ , None,
      OPEN_EXISTING, 0, 0)

      Comment

      • R. Bernstein

        #4
        Re: Help controlling CDROM from python

        If you want an OS neutral way one may be able to use pycdio from the
        Cheese shop.

        It requires libcdio to be installed and that sometimes the case if you
        have a free media player (like vlc or xine, or mplayer) installed.

        I don't really use it all that often so I can't vouch for how good it
        is. (Although there *are* regression tests).

        Ognjen Bezanov <Ognjen@mailsha ck.comwrites:
        Hello,
        >
        I am trying to control a CD-ROM drive using python. The code I use is
        shown below.
        >
        import CDROM
        from fcntl import ioctl
        import os


        class Device:

        CDdevice=""
        CDfd = None

        def __init__(self,n ame):
        self.CDdevice = name #we get a device name when module loaded

        if self.CDdevice == "":
        print "No device specified"
        sys.exit(-1)
        self.openCD()

        def openCD(self):

        try:
        self.CDfd = open(self.CDdev ice, 'r') #open the device and return filedescriptor

        except(OSError, IOError): #if there is an OS or IO Error (usually indicates nodisk)
        print "Device Error, Halting.... (usually means drive or disk not found)"
        sys.exit(-1)
        >
        def unlockCD(self):
        return self.sendCDcomm and(CDROM.CDROM _LOCKDOOR,0)
        >
        def ejectCD(self):
        self.unlockCD() #we need to unlock the CD tray before we try to eject, otherwise we get an IO Error (#5)
        return self.sendCDcomm and(CDROM.CDROM EJECT)
        >
        >
        def sendCDcommand(s elf,command,arg ument=''):
        return ioctl(self.CDfd ,command,argume nt)
        >
        >
        >
        The code that calls the class is a follows:
        >
        import CD_Bindings

        CD = CD_Bindings.Dev ice("/dev/cdrom")

        print CD.ejectCD()
        >
        This works great, but only when there is a disk inside, otherwise we get
        an error.
        >
        My issue is that I need to be able to eject the CDROM tray even if there
        is no disk inside.
        >
        This is possible because other programs (like the linux "eject" command)
        can do it. Its just a question of how it is done in python. So I'm
        posting here in the hope someone can tell me.
        >
        Thanks,
        >
        Ognjen

        Comment

        Working...