import os
import sys
import regex
from regex_syntax import RE_SYNTAX_AWK

# returns a list of (devicename, blocks, id, type) tuples

def readPartitionTable(fdisk = "/sbin/fdisk"):
    output = os.popen("/sbin/fdisk -l", "r", 1024)
    fdisklines = output.readlines()
    output.close()

    regex.set_syntax(RE_SYNTAX_AWK)

    infoline = regex.compile("^(/dev/[hs]d.[1-9]+)[ \t]*[*]?[ \t]*" +
			     "[0-9]+[ \t]*[0-9]+[ \t]*[0-9]+[ \t]*" +
			     "([0-9]+)\+?[ \t]*([a-f0-9]+)[ \t]*([a-zA-Z].*)$")

    # /dev/hdb1   *       1       1     199  199000+  83  Linux native

    list = []
    for line in fdisklines:
	if (infoline.match(line) != -1):
	    list.append(infoline.group(1, 2, 3, 4))

    return list
