#!/usr/bin/python
#
# pythonlized partman-base/parted_devices.c
#

import parted
import os,fcntl,array
import re

'''is cdrom return 0'''
def is_cdrom(path):
    CDROM_GET_CAPABILITY = 0x5331
    ret = 1
	
    '''h: array type code, mean signed short '''
    buf= array.array('h', [0])
    fd = os.open(path, os.O_RDONLY | os.O_NONBLOCK)
    try:
        ret = fcntl.ioctl(fd, CDROM_GET_CAPABILITY, buf)
        ret = buf[0]
    except IOError:
        pass
                     
    return ret

'''is floppy return true'''
def is_floppy(path):
    return path == "/dev/floppy" or path ==  "/dev/fd";

'''is device mapper return true,
filter /dev/mapper, we don't use /dev/mapper in installer -- bqin'''
def is_devicemapper(path):
    return (path.startswith("/dev/mapper"))

def process_device(dev):
    if dev.readOnly == 1:
        return
    if not is_cdrom(dev.path) or is_floppy(dev.path) or is_devicemapper(dev.path):
        return
    if dev.path == "/dev/ramzswap" or dev.path == "/dev/zram": 
        return
    return dev.path


def get_disks():
    disk_list = []

    device_list = parted.getAllDevices()

    for i in range(len(device_list)):
        item = process_device(device_list[i])
        if item:
            disk_list.append(item)

    return disk_list


if __name__ == "__main__":
    for i in get_disks():
        print i
