#!/usr/bin/env ensim-python

# $Author: naris $
# $Id: quota_report.py,v 1.2 2003/06/12 02:08:20 naris Exp $

import sys,quota
from getopt import getopt,GetoptError

def usage(errstr =None):
    if errstr:
        sys.stderr.write("%s\n" % errstr)
    sys.stderr.write(\
"""
Usage : quota_report -d <directory> [-q|-u <uid>|-g <gid>]

 -d directory    the directory, whose containing filesystem's quotas to report
 -q              just print out <user_quota_enabled>:<group_quota_enabled>
 -g gid          the group ID whose quota is to be displayed
 -u uid          the user ID whose quota is to be displayed,ignored if -g is set

Output format :
Unless the -q option is specified, the output is structured as follows:

 user_quota_enabled:group_quota_enabled (boolean, so this can be 0:0, 1:1, etc)
 current space occupied (in 1024-byte blocks)
 preferred limit on disk space (in blocks)
 absolute limit on disk space (in blocks)
 time limit for excessive disk use
 current # allocated inodes
 preferred inode limit
 absolute limit on allocated inodes
 time limit for excessive inode use

""")
    sys.exit(1)

def parseopts():
    try:
        opts,args =getopt(sys.argv[1:],"qu:g:d:")
    except GetoptError, e:
        usage(str(e))

    optdict ={}
    for switch,val in opts:
        optdict[switch[1:]] =val

    return optdict,args

def user_quota_enabled(dev):
    try:
        stats ={}
        # use quotactl to find out whether group quota is enabled or not
        quota.quotactl(quota.qcmd(quota.Q_GETQUOTA,quota.USRQUOTA),dev,0,stats)
    except OSError:
        return 0
    else:
        return 1

def group_quota_enabled(dev):
    try:
        stats ={}
        # use quotactl to find out whether group quota is enabled or not
        quota.quotactl(quota.qcmd(quota.Q_GETQUOTA,quota.GRPQUOTA),dev,0,stats)
    except OSError:
        return 0
    else:
        return 1

if __name__ == "__main__":
    opts,args =parseopts()

    dev =None
    # directory must be defined (-d)
    if not opts.get("d",None):
        usage()

    try:
        dev =quota.getqcarg(opts["d"])
    except OSError,o:
        sys.stdout.write("0:0\n")
        sys.stderr.write("Device not found\n")
        sys.exit(1)

    usrquota =user_quota_enabled(dev)
    grpquota =group_quota_enabled(dev)

    _bc =0
    _bs =0
    _bh =0
    _bt =0
    _ic =0
    _is =0
    _ih =0
    _it =0

    # stats holds the return value from quota_linux::quotactl
    stats ={}

    if "q" in opts:
        # only print out whether quotas are enabled or not
        sys.stdout.write("%d:%d\n" %(usrquota, grpquota))
        sys.exit(0)
    elif opts.get("g",None):
        # print out group quota information for the GID specified
        try:
            quota.quotactl(quota.qcmd(quota.Q_GETQUOTA,quota.GRPQUOTA),dev,\
                int(opts["g"]),stats)
        except OSError:
            sys.stdout.write("%d:%d\n" %(usrquota, grpquota))
            sys.stderr.write("Group quota not supported\n")
            sys.exit(1)
        else:
            grpquota =1 # prolly unnecessary
    elif opts.get("u",None):
        # print out the user quota information for the UID specified
        try:
            quota.quotactl(quota.qcmd(quota.Q_GETQUOTA,quota.USRQUOTA),dev,\
                int(opts["u"]),stats)
        except OSError:
            sys.stdout.write("%d:%d\n" %(usrquota, grpquota))
            sys.stderr.write("User quota not supported\n")
            sys.exit(1)
        else:
            usrquota =1 # prolly unnecessary

    _bc =stats.get("curspace",0) / 1024 # quota_linux reports in bytes
    _bs =stats.get("bsoftlimit",0)
    _bh =stats.get("bhardlimit",0)
    _bt =stats.get("btime",0)
    _ic =stats.get("curinodes",0)
    _is =stats.get("ihardlimit",0)
    _ih =stats.get("isoftlimit",0)
    _it =stats.get("itime",0)

    sys.stdout.write("%d:%d\n" %(usrquota,grpquota))
    print _bc
    print _bs
    print _bh
    print _bt
    print _ic
    print _is
    print _ih
    print _it

    sys.exit(0)

