How to detect whether there is a CD-ROM in the drive?

You can get information about any block device using the command blkid.

[root@arch32-vm ~]# blkid /dev/sr0
/dev/sr0: UUID="2013-05-31-23-04-19-00" LABEL="ARCH_201306" TYPE="iso9660" PTTYPE="dos"
[root@arch32-vm ~]# echo $?
0

If I remove the disk, I don't get any output and exit value is 2. (0 means success. A non-zero value will typically mean something abnormal happen or an error occurred)

[root@arch32-vm ~]# blkid /dev/sr0
[root@arch32-vm ~]# echo $?
2

setcd -i (in the setcd package, at least on Debian) can tell you the state of the drive. Unlike some of the other approaches (mount at least, probably blkid too), this will not attempt to close the tray, even on drives capable of that. (Which is really annoying if it tries to close on you while you're putting a disc in).

With the tray open:

$ setcd -i /dev/sr0
/dev/sr0:
  CD tray is open

Right after closing the tray:

$ setcd -i /dev/sr0
/dev/sr0:
  Drive is not ready

After it's ready:

$ setcd -i /dev/sr0
/dev/sr0:
  Disc found in drive: data disc type 1
    Volume name: «name»
    Publisher:                                                                                                                                                                                                                                                                 MKISOFS ISO 9660/HFS FILESYSTEM BUILDER & CDRECORD CD-R/DVD CREATOR (C) 1993 E.YOUNGDALE (C) 1997 J.PEARSON/J.SCHILLING                                                                                                                        2005030913034700�2005030913034700�0000000000000000
    Data preparer:                                                                                                                                 MKISOFS ISO 9660/HFS FILESYSTEM BUILDER & CDRECORD CD-R/DVD CREATOR (C) 1993 E.YOUNGDALE (C) 1997 J.PEARSON/J.SCHILLING                                                                                                                        2005030913034700�2005030913034700�0000000000000000

Closed, but no disc:

$ setcd -i /dev/sr0
/dev/sr0:
  No disc is inserted

You can fairly easily script it:

while true; do
    cdinfo=$(setcd -i "$dev")
    case "$cdinfo" in
        *'Disc found'*)
            break;
            ;;
        *'not ready'*)
            echo '(waiting for drive to be ready)' >&2;
            sleep 3;
            ;;
        *'is open'*)
            echo '(drive is open)' >&2;
            sleep 5;
            ;;
        *)
            printf 'Confused by setcd -i, bailing out:\n%s\n' "$cdinfo" &2
            exit 1
    esac
done

You can do the following with Python3 and the standard library:

import fcntl
import os

CDROM_DRIVE = '/dev/sr0'

def detect_tray(CDROM_DRIVE):
    """detect_tray reads status of the CDROM_DRIVE.
    Statuses:
    1 = no disk in tray
    2 = tray open
    3 = reading tray
    4 = disk in tray
    """
    fd = os.open(CDROM_DRIVE, os.O_RDONLY | os.O_NONBLOCK)
    rv = fcntl.ioctl(fd, 0x5326)
    os.close(fd)
    print(rv)