#!/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):
        return
    if dev.path == "/dev/ramzswap" or dev.path == "/dev/zram": 
        return
    return dev.path


def get_disks():
    disk_list = []

    disk_remove_list = []

    device_list = parted.getAllDevices()

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

    for j in range(len(disk_list)):
        for k in range(len(disk_list)) :
		if k == j:
			continue
		if re.match(disk_list[k],disk_list[j]):
			disk_remove_list.append(disk_list[j])

    for k in range(len(disk_remove_list)):
        disk_list.remove(disk_remove_list[k])

    return disk_list


if __name__ == "__main__":
    print get_disks()
